-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure that each js file served up by vite dev server has an inl…
…ine sourcemap
- Loading branch information
1 parent
55888e8
commit 7746a60
Showing
2 changed files
with
70 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Plugin } from 'vite-5' | ||
import { ViteDevServerConfig } from '../../src/devServer' | ||
import { Vite } from '../../src/getVite' | ||
import { CypressSourcemap } from '../../src/plugins' | ||
import Chai, { expect } from 'chai' | ||
import SinonChai from 'sinon-chai' | ||
|
||
Chai.use(SinonChai) | ||
|
||
describe('sourcemap plugin', () => { | ||
['js', 'jsx', 'ts', 'tsx', 'vue', 'mjs', 'cjs'].forEach((ext) => { | ||
it('should append sourcemap to the code if sourceMappingURL is not present', () => { | ||
const code = 'console.log("hello world")' | ||
const id = `test.${ext}` | ||
const options = {} as ViteDevServerConfig | ||
const vite = {} as Vite | ||
const plugin = CypressSourcemap(options, vite) as Plugin & { getCombinedSourcemap: () => { toUrl: () => string } } | ||
|
||
plugin.getCombinedSourcemap = () => { | ||
return { | ||
toUrl: () => 'data:application/json;base64,eyJ2ZXJzaW9uIjozfQ==', | ||
} | ||
} | ||
|
||
expect(plugin.name).to.equal('cypress:sourcemap') | ||
expect(plugin.enforce).to.equal('post') | ||
|
||
if (plugin.transform instanceof Function) { | ||
const result = plugin.transform.call(plugin, code, id) | ||
|
||
expect(result.code).to.include('//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozfQ==') | ||
} else { | ||
throw new Error('transform is not a function') | ||
} | ||
}) | ||
|
||
it('should replace sourceMappingURL with sourcemap', () => { | ||
const code = 'console.log("hello world")\n//# sourceMappingURL=old-url' | ||
const id = `test.${ext}` | ||
const options = {} as ViteDevServerConfig | ||
const vite = {} as Vite | ||
const plugin = CypressSourcemap(options, vite) as Plugin & { getCombinedSourcemap: () => { toUrl: () => string } } | ||
|
||
plugin.getCombinedSourcemap = () => { | ||
return { | ||
toUrl: () => 'data:application/json;base64,eyJ2ZXJzaW9uIjozfQ==', | ||
} | ||
} | ||
|
||
expect(plugin.name).to.equal('cypress:sourcemap') | ||
expect(plugin.enforce).to.equal('post') | ||
|
||
if (plugin.transform instanceof Function) { | ||
const result = plugin.transform.call(plugin, code, id) | ||
|
||
expect(result.code).to.include('//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozfQ==') | ||
} else { | ||
throw new Error('transform is not a function') | ||
} | ||
}) | ||
}) | ||
}) |