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: skip injecting __vite__mapDeps when it's not used #16271

Merged
merged 2 commits into from
Mar 26, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 16 additions & 12 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,32 +498,36 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
s.update(
markerStartPos,
markerStartPos + preloadMarker.length + 2,
`__vite__mapDeps([${renderedDeps.join(',')}])`,
renderedDeps.length > 0
? `__vite__mapDeps([${renderedDeps.join(',')}])`
: `[]`,
)
rewroteMarkerStartPos.add(markerStartPos)
}
}
}

const fileDepsCode = `[${fileDeps
.map((fileDep) =>
fileDep.runtime ? fileDep.url : JSON.stringify(fileDep.url),
)
.join(',')}]`
if (fileDeps.length > 0) {
const fileDepsCode = `[${fileDeps
.map((fileDep) =>
fileDep.runtime ? fileDep.url : JSON.stringify(fileDep.url),
)
.join(',')}]`

const mapDepsCode = `\
const mapDepsCode = `\
function __vite__mapDeps(indexes) {
if (!__vite__mapDeps.viteFileDeps) {
__vite__mapDeps.viteFileDeps = ${fileDepsCode}
}
return indexes.map((i) => __vite__mapDeps.viteFileDeps[i])
}\n`

// inject extra code at the top or next line of hashbang
if (code.startsWith('#!')) {
s.prependLeft(code.indexOf('\n') + 1, mapDepsCode)
} else {
s.prepend(mapDepsCode)
// inject extra code at the top or next line of hashbang
if (code.startsWith('#!')) {
s.prependLeft(code.indexOf('\n') + 1, mapDepsCode)
} else {
s.prepend(mapDepsCode)
}
}

// there may still be markers due to inlined dynamic imports, remove
Expand Down
17 changes: 15 additions & 2 deletions playground/js-sourcemap/__tests__/js-sourcemap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,22 @@ describe.runIf(isBuild)('build tests', () => {
}
`)
// verify sourcemap comment is preserved at the last line
const js = findAssetFile(/after-preload-dynamic.*\.js$/)
const js = findAssetFile(/after-preload-dynamic-[-\w]{8}\.js$/)
expect(js).toMatch(
/\n\/\/# sourceMappingURL=after-preload-dynamic.*\.js\.map\n$/,
/\n\/\/# sourceMappingURL=after-preload-dynamic-[-\w]{8}\.js\.map\n$/,
)
})

test('__vite__mapDeps injected after banner', async () => {
const js = findAssetFile(/after-preload-dynamic-hashbang-[-\w]{8}\.js$/)
expect(js.split('\n').slice(0, 2)).toEqual([
'#!/usr/bin/env node',
'function __vite__mapDeps(indexes) {',
])
})

test('no unused __vite__mapDeps', async () => {
const js = findAssetFile(/after-preload-dynamic-no-dep-[-\w]{8}\.js$/)
expect(js).not.toMatch(/__vite__mapDeps/)
})
})
3 changes: 3 additions & 0 deletions playground/js-sourcemap/after-preload-dynamic-no-dep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import('./dynamic/dynamic-no-dep')

console.log('after preload dynamic no dep')
1 change: 1 addition & 0 deletions playground/js-sourcemap/dynamic/dynamic-no-dep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('dynamic/dynamic-no-dep')
1 change: 1 addition & 0 deletions playground/js-sourcemap/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ <h1>JS Sourcemap</h1>
<script type="module" src="./bar.ts"></script>
<script type="module" src="./after-preload-dynamic.js"></script>
<script type="module" src="./after-preload-dynamic-hashbang.js"></script>
<script type="module" src="./after-preload-dynamic-no-dep.js"></script>
<script type="module" src="./with-multiline-import.ts"></script>
<script type="module" src="./zoo.js"></script>
3 changes: 3 additions & 0 deletions playground/js-sourcemap/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export default defineConfig({
if (name.endsWith('after-preload-dynamic-hashbang.js')) {
return 'after-preload-dynamic-hashbang'
}
if (name.endsWith('after-preload-dynamic-no-dep.js')) {
return 'after-preload-dynamic-no-dep'
}
},
banner(chunk) {
if (chunk.name.endsWith('after-preload-dynamic-hashbang')) {
Expand Down