From a9bf43088851e9136cda3b649d2998f9b0254000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Racek?= Date: Fri, 5 Apr 2024 04:30:10 +0200 Subject: [PATCH] perf: reduce size of injected __vite__mapDeps code (#16184) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: bluwy Co-authored-by: 翠 / green Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com> --- packages/vite/src/node/plugins/importAnalysisBuild.ts | 8 +------- playground/js-sourcemap/__tests__/js-sourcemap.spec.ts | 4 ++-- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index d7f6491ceb7306..2c3fdcee0e7e23 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -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('#!')) { diff --git a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts index 1566549293efdb..15d82acd776283 100644 --- a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts +++ b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts @@ -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=['), ]) })