Skip to content

Commit 890538a

Browse files
authoredMar 26, 2024··
fix: skip injecting __vite__mapDeps when it's not used (#16271)
1 parent cdc664d commit 890538a

File tree

6 files changed

+39
-14
lines changed

6 files changed

+39
-14
lines changed
 

‎packages/vite/src/node/plugins/importAnalysisBuild.ts

+16-12
Original file line numberDiff line numberDiff line change
@@ -498,32 +498,36 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
498498
s.update(
499499
markerStartPos,
500500
markerStartPos + preloadMarker.length + 2,
501-
`__vite__mapDeps([${renderedDeps.join(',')}])`,
501+
renderedDeps.length > 0
502+
? `__vite__mapDeps([${renderedDeps.join(',')}])`
503+
: `[]`,
502504
)
503505
rewroteMarkerStartPos.add(markerStartPos)
504506
}
505507
}
506508
}
507509

508-
const fileDepsCode = `[${fileDeps
509-
.map((fileDep) =>
510-
fileDep.runtime ? fileDep.url : JSON.stringify(fileDep.url),
511-
)
512-
.join(',')}]`
510+
if (fileDeps.length > 0) {
511+
const fileDepsCode = `[${fileDeps
512+
.map((fileDep) =>
513+
fileDep.runtime ? fileDep.url : JSON.stringify(fileDep.url),
514+
)
515+
.join(',')}]`
513516

514-
const mapDepsCode = `\
517+
const mapDepsCode = `\
515518
function __vite__mapDeps(indexes) {
516519
if (!__vite__mapDeps.viteFileDeps) {
517520
__vite__mapDeps.viteFileDeps = ${fileDepsCode}
518521
}
519522
return indexes.map((i) => __vite__mapDeps.viteFileDeps[i])
520523
}\n`
521524

522-
// inject extra code at the top or next line of hashbang
523-
if (code.startsWith('#!')) {
524-
s.prependLeft(code.indexOf('\n') + 1, mapDepsCode)
525-
} else {
526-
s.prepend(mapDepsCode)
525+
// inject extra code at the top or next line of hashbang
526+
if (code.startsWith('#!')) {
527+
s.prependLeft(code.indexOf('\n') + 1, mapDepsCode)
528+
} else {
529+
s.prepend(mapDepsCode)
530+
}
527531
}
528532

529533
// there may still be markers due to inlined dynamic imports, remove

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

+15-2
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,22 @@ describe.runIf(isBuild)('build tests', () => {
152152
}
153153
`)
154154
// verify sourcemap comment is preserved at the last line
155-
const js = findAssetFile(/after-preload-dynamic.*\.js$/)
155+
const js = findAssetFile(/after-preload-dynamic-[-\w]{8}\.js$/)
156156
expect(js).toMatch(
157-
/\n\/\/# sourceMappingURL=after-preload-dynamic.*\.js\.map\n$/,
157+
/\n\/\/# sourceMappingURL=after-preload-dynamic-[-\w]{8}\.js\.map\n$/,
158158
)
159159
})
160+
161+
test('__vite__mapDeps injected after banner', async () => {
162+
const js = findAssetFile(/after-preload-dynamic-hashbang-[-\w]{8}\.js$/)
163+
expect(js.split('\n').slice(0, 2)).toEqual([
164+
'#!/usr/bin/env node',
165+
'function __vite__mapDeps(indexes) {',
166+
])
167+
})
168+
169+
test('no unused __vite__mapDeps', async () => {
170+
const js = findAssetFile(/after-preload-dynamic-no-dep-[-\w]{8}\.js$/)
171+
expect(js).not.toMatch(/__vite__mapDeps/)
172+
})
160173
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import('./dynamic/dynamic-no-dep')
2+
3+
console.log('after preload dynamic no dep')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('dynamic/dynamic-no-dep')

‎playground/js-sourcemap/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ <h1>JS Sourcemap</h1>
77
<script type="module" src="./bar.ts"></script>
88
<script type="module" src="./after-preload-dynamic.js"></script>
99
<script type="module" src="./after-preload-dynamic-hashbang.js"></script>
10+
<script type="module" src="./after-preload-dynamic-no-dep.js"></script>
1011
<script type="module" src="./with-multiline-import.ts"></script>
1112
<script type="module" src="./zoo.js"></script>

‎playground/js-sourcemap/vite.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ export default defineConfig({
1818
if (name.endsWith('after-preload-dynamic-hashbang.js')) {
1919
return 'after-preload-dynamic-hashbang'
2020
}
21+
if (name.endsWith('after-preload-dynamic-no-dep.js')) {
22+
return 'after-preload-dynamic-no-dep'
23+
}
2124
},
2225
banner(chunk) {
2326
if (chunk.name.endsWith('after-preload-dynamic-hashbang')) {

0 commit comments

Comments
 (0)
Please sign in to comment.