Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: output combined sourcemap in importAnalysisBuild plugin #12642

Merged
merged 1 commit into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { init, parse as parseImports } from 'es-module-lexer'
import type { OutputChunk, SourceMap } from 'rollup'
import colors from 'picocolors'
import type { RawSourceMap } from '@ampproject/remapping'
import convertSourceMap from 'convert-source-map'
import {
bareImportRE,
cleanUrl,
Expand Down Expand Up @@ -608,6 +609,19 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
) as SourceMap
map.toUrl = () => genSourceMapUrl(map)
chunk.map = map

if (config.build.sourcemap === 'inline') {
chunk.code = chunk.code.replace(
convertSourceMap.mapFileCommentRegex,
'',
)
chunk.code += `\n//# sourceMappingURL=${genSourceMapUrl(map)}`
} else if (config.build.sourcemap) {
const mapAsset = bundle[chunk.fileName + '.map']
if (mapAsset && mapAsset.type === 'asset') {
mapAsset.source = map.toString()
}
}
}
}
}
Expand Down
34 changes: 26 additions & 8 deletions playground/js-sourcemap/__tests__/js-sourcemap.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { URL } from 'node:url'
import { expect, test } from 'vitest'
import { describe, expect, test } from 'vitest'
import {
extractSourcemap,
findAssetFile,
formatSourcemapForSnapshot,
isBuild,
page,
Expand Down Expand Up @@ -40,14 +41,31 @@ if (!isBuild) {
expect(log).not.toMatch(/Sourcemap for .+ points to missing source files/)
})
})
} else {
test('this file only includes test for serve', () => {
expect(true).toBe(true)
})
}

test.runIf(isBuild)('should not output sourcemap warning (#4939)', () => {
serverLogs.forEach((log) => {
expect(log).not.toMatch('Sourcemap is likely to be incorrect')
describe.runIf(isBuild)('build tests', () => {
test('should not output sourcemap warning (#4939)', () => {
serverLogs.forEach((log) => {
expect(log).not.toMatch('Sourcemap is likely to be incorrect')
})
})

test('sourcemap is correct when preload information is injected', async () => {
const map = findAssetFile(/after-preload-dynamic.*\.js\.map/)
expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(`
{
"mappings": "stBAAAA,aAAO,2BAAuB,EAAC,sEAE/B,QAAQ,IAAI,uBAAuB",
"sources": [
"../../after-preload-dynamic.js",
],
"sourcesContent": [
"import('./dynamic/dynamic-foo')

console.log('after preload dynamic')
",
],
"version": 3,
}
`)
})
})
3 changes: 3 additions & 0 deletions playground/js-sourcemap/after-preload-dynamic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import('./dynamic/dynamic-foo')

console.log('after preload dynamic')
3 changes: 3 additions & 0 deletions playground/js-sourcemap/dynamic/dynamic-foo.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.dynamic-foo {
color: red;
}
3 changes: 3 additions & 0 deletions playground/js-sourcemap/dynamic/dynamic-foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './dynamic-foo.css'

console.log('dynamic/dynamic-foo')
2 changes: 2 additions & 0 deletions playground/js-sourcemap/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<div class="wrapper">
<h1>JS Sourcemap</h1>
<div class="dynamic-foo">dynamic</div>
</div>

<script type="module" src="./foo.js"></script>
<script type="module" src="./bar.ts"></script>
<script type="module" src="./after-preload-dynamic.js"></script>
9 changes: 9 additions & 0 deletions playground/js-sourcemap/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,14 @@ import { defineConfig } from 'vite'
export default defineConfig({
build: {
sourcemap: true,
rollupOptions: {
output: {
manualChunks(name) {
if (name.includes('after-preload-dynamic')) {
return 'after-preload-dynamic'
}
},
},
},
},
})