-
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: Address Vite sourcemap edge cases (#24063)
- Loading branch information
1 parent
3e01474
commit e918fc1
Showing
6 changed files
with
128 additions
and
28 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from './inspect' | ||
|
||
export * from './cypress' | ||
|
||
export * from './sourcemap' |
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,51 @@ | ||
import debugFn from 'debug' | ||
import type { Plugin } from 'vite' | ||
import type { Vite } from '../getVite' | ||
|
||
import type { ViteDevServerConfig } from '../devServer' | ||
|
||
const debug = debugFn('cypress:vite-dev-server:plugins:sourcemap') | ||
|
||
export const CypressSourcemap = ( | ||
options: ViteDevServerConfig, | ||
vite: Vite, | ||
): Plugin => { | ||
return { | ||
name: 'cypress:sourcemap', | ||
enforce: 'post', | ||
transform (code, id, options?) { | ||
try { | ||
if (/\.js$/i.test(id) && !/\/\/# sourceMappingURL=/i.test(code)) { | ||
/* | ||
The Vite dev server and plugins automatically generate sourcemaps for most files, but they are | ||
only included in the served files if any transpilation actually occurred (JSX, TS, etc). This | ||
means that files that are "pure" JS won't include sourcemaps, so JS spec files that don't require | ||
transpilation won't get code frames in the runner. | ||
A sourcemap for these files is generated (just not served) which is in effect an "identity" | ||
sourcemap mapping 1-to-1 to the output file. We can grab this and pass it along as a sourcemap | ||
we want Vite to embed into the output, giving Cypress a sourcemap to use for codeFrame lookups. | ||
@see https://rollupjs.org/guide/en/#thisgetcombinedsourcemap | ||
We utilize a 'post' plugin here and manually append the sourcemap content to the code. We *should* | ||
be able to pass the sourcemap along using the `map` attribute in the return value, but Babel-based | ||
plugins don't work with this which causes sourcemaps to break for files that go through common | ||
plugins like `@vitejs/plugin-react`. By manually appending the sourcemap at this point in time | ||
Babel-transformed files end up with correct sourcemaps. | ||
*/ | ||
|
||
const sourcemap = this.getCombinedSourcemap() | ||
|
||
code += `\n//# sourceMappingURL=${sourcemap.toUrl()}` | ||
|
||
return { | ||
code, | ||
map: { mappings: '' }, | ||
} | ||
} | ||
} catch (_err) { | ||
debug('Failed to propagate sourcemap for %s: %o', id, _err) | ||
} | ||
}, | ||
} | ||
} |
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
e918fc1
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 platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.
Run this command to install the pre-release locally:
e918fc1
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 platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.
Run this command to install the pre-release locally:
e918fc1
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 platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.
Run this command to install the pre-release locally:
e918fc1
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 platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.
Run this command to install the pre-release locally: