-
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 (#30606) * fix: ensure that each js file served up by vite dev server has an inline sourcemap * refactor * refactor with more understanding * add changelog * PR comments and other tweaks * add comments * add comments * Apply suggestions from code review
- Loading branch information
1 parent
50db03b
commit 4bf8e58
Showing
3 changed files
with
130 additions
and
14 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
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,107 @@ | ||
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' | ||
import sinon from 'sinon' | ||
|
||
Chai.use(SinonChai) | ||
|
||
describe('sourcemap plugin', () => { | ||
['js', 'jsx', 'ts', 'tsx', 'vue', 'svelte', 'mjs', 'cjs'].forEach((ext) => { | ||
it(`should append sourcemap to the code if sourceMappingURL is not present for files with extension ${ext}`, () => { | ||
const code = 'console.log("hello world")' | ||
const id = `test.js` | ||
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.eq('console.log("hello world")\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozfQ==') | ||
} else { | ||
throw new Error('transform is not a function') | ||
} | ||
}) | ||
|
||
it(`should replace sourceMappingURL with sourcemap and handle query parameters for files with extension ${ext}`, () => { | ||
const code = 'console.log("hello world")\n//# sourceMappingURL=old-url' | ||
const id = `test.js?v=12345` | ||
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.eq('console.log("hello world")\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozfQ==') | ||
} else { | ||
throw new Error('transform is not a function') | ||
} | ||
}) | ||
}) | ||
|
||
it('should not append sourcemap to the code if sourceMappingURL is already present', () => { | ||
const code = 'console.log("hello world")\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozfQ==' | ||
const id = `test.js` | ||
const options = {} as ViteDevServerConfig | ||
const vite = {} as Vite | ||
const plugin = CypressSourcemap(options, vite) as Plugin & { getCombinedSourcemap: () => { toUrl: () => string } } | ||
|
||
plugin.getCombinedSourcemap = sinon.stub() | ||
|
||
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).to.be.undefined | ||
expect(plugin.getCombinedSourcemap).not.to.have.been.called | ||
} else { | ||
throw new Error('transform is not a function') | ||
} | ||
}) | ||
|
||
it('should not append sourcemap to the code if the file is not a js, jsx, ts, tsx, vue, mjs, or cjs file', () => { | ||
const code = 'console.log("hello world")' | ||
const id = `test.css` | ||
const options = {} as ViteDevServerConfig | ||
const vite = {} as Vite | ||
const plugin = CypressSourcemap(options, vite) as Plugin & { getCombinedSourcemap: () => { toUrl: () => string } } | ||
|
||
plugin.getCombinedSourcemap = sinon.stub() | ||
|
||
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).to.be.undefined | ||
expect(plugin.getCombinedSourcemap).not.to.have.been.called | ||
} else { | ||
throw new Error('transform is not a function') | ||
} | ||
}) | ||
}) |
4bf8e58
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circle has built the
linux x64
version of the Test Runner.Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version
Run this command to install the pre-release locally:
4bf8e58
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circle has built the
linux arm64
version of the Test Runner.Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version
Run this command to install the pre-release locally:
4bf8e58
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circle has built the
darwin arm64
version of the Test Runner.Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version
Run this command to install the pre-release locally:
4bf8e58
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circle has built the
linux arm64
version of the Test Runner.Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version
Run this command to install the pre-release locally:
4bf8e58
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circle has built the
linux x64
version of the Test Runner.Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version
Run this command to install the pre-release locally:
4bf8e58
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circle has built the
win32 x64
version of the Test Runner.Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version
Run this command to install the pre-release locally:
4bf8e58
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circle has built the
darwin x64
version of the Test Runner.Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version
Run this command to install the pre-release locally:
4bf8e58
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circle has built the
darwin arm64
version of the Test Runner.Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version
Run this command to install the pre-release locally:
4bf8e58
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circle has built the
darwin x64
version of the Test Runner.Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version
Run this command to install the pre-release locally: