Skip to content

Commit

Permalink
fix: handle sourcemap correctly when multiple line import exists (#14232
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sapphi-red authored and bluwy committed Oct 3, 2023
1 parent 76dd2f9 commit 8c35aad
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 5 deletions.
23 changes: 19 additions & 4 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -850,16 +850,17 @@ export function interopNamedImports(
se: expEnd,
d: dynamicIndex,
} = importSpecifier
const exp = source.slice(expStart, expEnd)
if (dynamicIndex > -1) {
// rewrite `import('package')` to expose the default directly
str.overwrite(
expStart,
expEnd,
`import('${rewrittenUrl}').then(m => m.default && m.default.__esModule ? m.default : ({ ...m.default, default: m.default }))`,
`import('${rewrittenUrl}').then(m => m.default && m.default.__esModule ? m.default : ({ ...m.default, default: m.default }))` +
getLineBreaks(exp),
{ contentOnly: true },
)
} else {
const exp = source.slice(expStart, expEnd)
const rawUrl = source.slice(start, end)
const rewritten = transformCjsImport(
exp,
Expand All @@ -870,14 +871,28 @@ export function interopNamedImports(
config,
)
if (rewritten) {
str.overwrite(expStart, expEnd, rewritten, { contentOnly: true })
str.overwrite(expStart, expEnd, rewritten + getLineBreaks(exp), {
contentOnly: true,
})
} else {
// #1439 export * from '...'
str.overwrite(start, end, rewrittenUrl, { contentOnly: true })
str.overwrite(
start,
end,
rewrittenUrl + getLineBreaks(source.slice(start, end)),
{
contentOnly: true,
},
)
}
}
}

// get line breaks to preserve line count for not breaking source maps
function getLineBreaks(str: string) {
return str.includes('\n') ? '\n'.repeat(str.split('\n').length - 1) : ''
}

type ImportNameSpecifier = { importedName: string; localName: string }

/**
Expand Down
26 changes: 26 additions & 0 deletions playground/js-sourcemap/__tests__/js-sourcemap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,32 @@ if (!isBuild) {
`)
})

test('multiline import', async () => {
const res = await page.request.get(
new URL('./with-multiline-import.ts', page.url()).href,
)
const multi = await res.text()
const map = extractSourcemap(multi)
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(`
{
"mappings": "AACA;AAAA,EACE;AAAA,OACK;AAEP,QAAQ,IAAI,yBAAyB,GAAG;",
"sources": [
"with-multiline-import.ts",
],
"sourcesContent": [
"// prettier-ignore
import {
foo
} from '@vitejs/test-importee-pkg'
console.log('with-multiline-import', foo)
",
],
"version": 3,
}
`)
})

test('should not output missing source file warning', () => {
serverLogs.forEach((log) => {
expect(log).not.toMatch(/Sourcemap for .+ points to missing source files/)
Expand Down
2 changes: 2 additions & 0 deletions playground/js-sourcemap/importee-pkg/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line import/no-commonjs
exports.foo = 'foo'
6 changes: 6 additions & 0 deletions playground/js-sourcemap/importee-pkg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@vitejs/test-importee-pkg",
"private": true,
"version": "0.0.0",
"main": "./index.js"
}
1 change: 1 addition & 0 deletions playground/js-sourcemap/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ <h1>JS Sourcemap</h1>
<script type="module" src="./foo.js"></script>
<script type="module" src="./bar.ts"></script>
<script type="module" src="./after-preload-dynamic.js"></script>
<script type="module" src="./with-multiline-import.ts"></script>
3 changes: 3 additions & 0 deletions playground/js-sourcemap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
"build": "vite build",
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview"
},
"dependencies": {
"@vitejs/test-importee-pkg": "file:importee-pkg"
}
}
6 changes: 6 additions & 0 deletions playground/js-sourcemap/with-multiline-import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// prettier-ignore
import {
foo
} from '@vitejs/test-importee-pkg'

console.log('with-multiline-import', foo)
13 changes: 12 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8c35aad

Please sign in to comment.