From a4e4d394589e832849ae8f28dd12a94166cbdbce Mon Sep 17 00:00:00 2001 From: yoho Date: Sat, 21 May 2022 22:44:52 +0800 Subject: [PATCH] feat: sourcemap for importAnalysis (#8258) --- package.json | 2 +- .../vite/src/node/plugins/importAnalysis.ts | 5 +++- playground/glob-import/dir/index.js | 3 +++ playground/glob-import/vite.config.ts | 3 +++ .../__snapshots__/serve.spec.ts.snap | 25 +++++++++++++++++++ .../vue-sourcemap/__tests__/serve.spec.ts | 24 +----------------- 6 files changed, 37 insertions(+), 25 deletions(-) create mode 100644 playground/vue-sourcemap/__tests__/__snapshots__/serve.spec.ts.snap diff --git a/package.json b/package.json index bcfa5acb5..ab04bb594 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "scripts": { "preinstall": "npx only-allow pnpm", "format": "prettier --write .", - "lint": "eslint packages/*/{src,types}/** playground/**/__tests__/** scripts/**", + "lint": "eslint packages/*/{src,types}/** playground/**/__tests__/**/*.ts scripts/**", "typecheck": "tsc -p scripts --noEmit && tsc -p playground --noEmit", "test": "run-s test-unit test-serve test-build", "test-serve": "vitest run -c vitest.config.e2e.ts", diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index 9d1ff991d..4d312636f 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -645,7 +645,10 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { } if (s) { - return s.toString() + return { + code: s.toString(), + map: config.build.sourcemap ? s.generateMap({ hires: true }) : null + } } else { return source } diff --git a/playground/glob-import/dir/index.js b/playground/glob-import/dir/index.js index ab298abed..63381e412 100644 --- a/playground/glob-import/dir/index.js +++ b/playground/glob-import/dir/index.js @@ -1,4 +1,7 @@ const modules = import.meta.glob('./*.(js|ts)', { eager: true }) const globWithAlias = import.meta.glob('@dir/al*.js', { eager: true }) +// test for sourcemap +console.log('hello') + export { modules, globWithAlias } diff --git a/playground/glob-import/vite.config.ts b/playground/glob-import/vite.config.ts index abc75b516..5d8d58de0 100644 --- a/playground/glob-import/vite.config.ts +++ b/playground/glob-import/vite.config.ts @@ -6,5 +6,8 @@ export default defineConfig({ alias: { '@dir': path.resolve(__dirname, './dir/') } + }, + build: { + sourcemap: true } }) diff --git a/playground/vue-sourcemap/__tests__/__snapshots__/serve.spec.ts.snap b/playground/vue-sourcemap/__tests__/__snapshots__/serve.spec.ts.snap new file mode 100644 index 000000000..8daeb18db --- /dev/null +++ b/playground/vue-sourcemap/__tests__/__snapshots__/serve.spec.ts.snap @@ -0,0 +1,25 @@ +// Vitest Snapshot v1 + +exports[`serve:vue-sourcemap > js 1`] = ` +{ + "mappings": "mIAKA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;;;;;AAGP;AACd,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;;;;;;;;;;;wBARlB,oBAAiB,WAAd,MAAU", + "sources": [ + "/root/Js.vue", + ], + "sourcesContent": [ + " + + + + +", + ], + "version": 3, +} +`; diff --git a/playground/vue-sourcemap/__tests__/serve.spec.ts b/playground/vue-sourcemap/__tests__/serve.spec.ts index 83e63c56f..cc3c9835f 100644 --- a/playground/vue-sourcemap/__tests__/serve.spec.ts +++ b/playground/vue-sourcemap/__tests__/serve.spec.ts @@ -22,29 +22,7 @@ describe.runIf(isServe)('serve:vue-sourcemap', () => { const res = await page.request.get(new URL('./Js.vue', page.url()).href) const js = await res.text() const map = extractSourcemap(js) - expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(` - { - "mappings": "AAKA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;;;;;AAGP;AACd,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;;;;;;;;;;;wBARlB,oBAAiB,WAAd,MAAU", - "sources": [ - "/root/Js.vue", - ], - "sourcesContent": [ - " - - - - - ", - ], - "version": 3, - } - `) + expect(formatSourcemapForSnapshot(map)).toMatchSnapshot() }) test('ts', async () => {