Skip to content

Commit

Permalink
fix: avoid source map plugin from replacing valid code (#30890)
Browse files Browse the repository at this point in the history
Co-authored-by: Jennifer Shehane <[email protected]>
  • Loading branch information
JonathanWbn and jennifer-shehane authored Jan 23, 2025
1 parent 92fc209 commit 3149a8f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions npm/vite-dev-server/src/plugins/sourcemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ export const CypressSourcemap = (
const sourcemap = this.getCombinedSourcemap()
const sourcemapUrl = sourcemap.toUrl()

if (/\/\/# sourceMappingURL=/i.test(code)) {
if (/\/\/# sourceMappingURL=(?!['"])/i.test(code)) {
// If the code already has a sourceMappingURL, it is not an inlined sourcemap
// and we should replace it with the new sourcemap
code = code.replace(/\/\/# sourceMappingURL=(.*)$/m, `//# sourceMappingURL=${sourcemapUrl}`)
code = code.replace(/\/\/# sourceMappingURL=(?!['"])(.*)$/m, `//# sourceMappingURL=${sourcemapUrl}`)
} else {
// If the code does not have a sourceMappingURL, we should append the new sourcemap
code += `\n//# sourceMappingURL=${sourcemapUrl}`
Expand Down
25 changes: 25 additions & 0 deletions npm/vite-dev-server/test/plugins/sourcemap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,29 @@ describe('sourcemap plugin', () => {
throw new Error('transform is not a function')
}
})

it('should not touch sourceMappingURL if it is part of the code', () => {
const code = 'console.log("\n//# sourceMappingURL=")'
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("\n//# sourceMappingURL=")\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozfQ==')
} else {
throw new Error('transform is not a function')
}
})
})

5 comments on commit 3149a8f

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 3149a8f Jan 23, 2025

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:

npm install https://cdn.cypress.io/beta/npm/14.0.1/linux-x64/develop-3149a8f1841c59e9578958c181b66ec39a27ea37/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 3149a8f Jan 23, 2025

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:

npm install https://cdn.cypress.io/beta/npm/14.0.1/linux-arm64/develop-3149a8f1841c59e9578958c181b66ec39a27ea37/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 3149a8f Jan 23, 2025

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:

npm install https://cdn.cypress.io/beta/npm/14.0.1/darwin-arm64/develop-3149a8f1841c59e9578958c181b66ec39a27ea37/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 3149a8f Jan 23, 2025

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:

npm install https://cdn.cypress.io/beta/npm/14.0.1/win32-x64/develop-3149a8f1841c59e9578958c181b66ec39a27ea37/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 3149a8f Jan 23, 2025

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:

npm install https://cdn.cypress.io/beta/npm/14.0.1/darwin-x64/develop-3149a8f1841c59e9578958c181b66ec39a27ea37/cypress.tgz

Please sign in to comment.