diff --git a/npm/vite-dev-server/src/plugins/sourcemap.ts b/npm/vite-dev-server/src/plugins/sourcemap.ts index 2bda5291bfac..9750bbbec6e5 100644 --- a/npm/vite-dev-server/src/plugins/sourcemap.ts +++ b/npm/vite-dev-server/src/plugins/sourcemap.ts @@ -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}` diff --git a/npm/vite-dev-server/test/plugins/sourcemap.spec.ts b/npm/vite-dev-server/test/plugins/sourcemap.spec.ts index 7270f854ed98..2894e4bcd817 100644 --- a/npm/vite-dev-server/test/plugins/sourcemap.spec.ts +++ b/npm/vite-dev-server/test/plugins/sourcemap.spec.ts @@ -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') + } + }) })