Skip to content

Commit 627159d

Browse files
authored
fix: handle sourcemap correctly when multiple line import exists (#14232)
1 parent a57f388 commit 627159d

File tree

8 files changed

+75
-5
lines changed

8 files changed

+75
-5
lines changed

packages/vite/src/node/plugins/importAnalysis.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -850,16 +850,17 @@ export function interopNamedImports(
850850
se: expEnd,
851851
d: dynamicIndex,
852852
} = importSpecifier
853+
const exp = source.slice(expStart, expEnd)
853854
if (dynamicIndex > -1) {
854855
// rewrite `import('package')` to expose the default directly
855856
str.overwrite(
856857
expStart,
857858
expEnd,
858-
`import('${rewrittenUrl}').then(m => m.default && m.default.__esModule ? m.default : ({ ...m.default, default: m.default }))`,
859+
`import('${rewrittenUrl}').then(m => m.default && m.default.__esModule ? m.default : ({ ...m.default, default: m.default }))` +
860+
getLineBreaks(exp),
859861
{ contentOnly: true },
860862
)
861863
} else {
862-
const exp = source.slice(expStart, expEnd)
863864
const rawUrl = source.slice(start, end)
864865
const rewritten = transformCjsImport(
865866
exp,
@@ -870,14 +871,28 @@ export function interopNamedImports(
870871
config,
871872
)
872873
if (rewritten) {
873-
str.overwrite(expStart, expEnd, rewritten, { contentOnly: true })
874+
str.overwrite(expStart, expEnd, rewritten + getLineBreaks(exp), {
875+
contentOnly: true,
876+
})
874877
} else {
875878
// #1439 export * from '...'
876-
str.overwrite(start, end, rewrittenUrl, { contentOnly: true })
879+
str.overwrite(
880+
start,
881+
end,
882+
rewrittenUrl + getLineBreaks(source.slice(start, end)),
883+
{
884+
contentOnly: true,
885+
},
886+
)
877887
}
878888
}
879889
}
880890

891+
// get line breaks to preserve line count for not breaking source maps
892+
function getLineBreaks(str: string) {
893+
return str.includes('\n') ? '\n'.repeat(str.split('\n').length - 1) : ''
894+
}
895+
881896
type ImportNameSpecifier = { importedName: string; localName: string }
882897

883898
/**

playground/js-sourcemap/__tests__/js-sourcemap.spec.ts

+26
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,32 @@ if (!isBuild) {
4444
`)
4545
})
4646

47+
test('multiline import', async () => {
48+
const res = await page.request.get(
49+
new URL('./with-multiline-import.ts', page.url()).href,
50+
)
51+
const multi = await res.text()
52+
const map = extractSourcemap(multi)
53+
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(`
54+
{
55+
"mappings": "AACA;AAAA,EACE;AAAA,OACK;AAEP,QAAQ,IAAI,yBAAyB,GAAG;",
56+
"sources": [
57+
"with-multiline-import.ts",
58+
],
59+
"sourcesContent": [
60+
"// prettier-ignore
61+
import {
62+
foo
63+
} from '@vitejs/test-importee-pkg'
64+
65+
console.log('with-multiline-import', foo)
66+
",
67+
],
68+
"version": 3,
69+
}
70+
`)
71+
})
72+
4773
test('should not output missing source file warning', () => {
4874
serverLogs.forEach((log) => {
4975
expect(log).not.toMatch(/Sourcemap for .+ points to missing source files/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// eslint-disable-next-line import/no-commonjs
2+
exports.foo = 'foo'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "@vitejs/test-importee-pkg",
3+
"private": true,
4+
"version": "0.0.0",
5+
"main": "./index.js"
6+
}

playground/js-sourcemap/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ <h1>JS Sourcemap</h1>
66
<script type="module" src="./foo.js"></script>
77
<script type="module" src="./bar.ts"></script>
88
<script type="module" src="./after-preload-dynamic.js"></script>
9+
<script type="module" src="./with-multiline-import.ts"></script>

playground/js-sourcemap/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@
88
"build": "vite build",
99
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
1010
"preview": "vite preview"
11+
},
12+
"dependencies": {
13+
"@vitejs/test-importee-pkg": "file:importee-pkg"
1114
}
1215
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// prettier-ignore
2+
import {
3+
foo
4+
} from '@vitejs/test-importee-pkg'
5+
6+
console.log('with-multiline-import', foo)

pnpm-lock.yaml

+12-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)