perf: reduce size of injected __vite__mapDeps code (#16184)

Co-authored-by: bluwy <bjornlu.dev@gmail.com>
Co-authored-by: 翠 / green <green@sapphi.red>
Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
This commit is contained in:
Matyáš Racek 2024-04-05 04:30:10 +02:00 committed by GitHub
parent b762517afc
commit c0ec6bea69
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 3 additions and 9 deletions

View File

@ -514,13 +514,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
)
.join(',')}]`
const mapDepsCode = `\
function __vite__mapDeps(indexes) {
if (!__vite__mapDeps.viteFileDeps) {
__vite__mapDeps.viteFileDeps = ${fileDepsCode}
}
return indexes.map((i) => __vite__mapDeps.viteFileDeps[i])
}\n`
const mapDepsCode = `const __vite__fileDeps=${fileDepsCode},__vite__mapDeps=i=>i.map(i=>__vite__fileDeps[i]);\n`
// inject extra code at the top or next line of hashbang
if (code.startsWith('#!')) {

View File

@ -140,7 +140,7 @@ describe.runIf(isBuild)('build tests', () => {
expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(`
{
"ignoreList": [],
"mappings": ";;;;;;w+BAAA,OAAO,2BAAuB,EAAC,wBAE/B,QAAQ,IAAI,uBAAuB",
"mappings": ";w+BAAA,OAAO,2BAAuB,EAAC,wBAE/B,QAAQ,IAAI,uBAAuB",
"sources": [
"../../after-preload-dynamic.js",
],
@ -164,7 +164,7 @@ describe.runIf(isBuild)('build tests', () => {
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) {',
expect.stringContaining('const __vite__fileDeps=['),
])
})