From 356b8969981a452fec8554372c49a672c7a49192 Mon Sep 17 00:00:00 2001 From: yoho Date: Sun, 29 May 2022 23:29:46 +0800 Subject: [PATCH] fix: sourcemap source point to null (#8299) --- .../src/node/plugins/assetImportMetaUrl.ts | 6 ++---- .../src/node/plugins/dynamicImportVars.ts | 12 ++++++----- .../vite/src/node/plugins/importAnalysis.ts | 6 ++---- .../vite/src/node/plugins/importMetaGlob.ts | 7 ++----- .../src/node/plugins/workerImportMetaUrl.ts | 13 +++++++----- packages/vite/src/node/utils.ts | 17 ++++++++++++++++ playground/dynamic-import/vite.config.js | 3 +++ .../sourcemap-worker.spec.ts.snap | 14 +++++++++++++ .../sourcemap/sourcemap-worker.spec.ts | 20 ++++++++++++++++++- playground/worker/worker-nested-worker.js | 12 +++++++++++ playground/worker/worker/main-module.js | 11 ++-------- 11 files changed, 88 insertions(+), 33 deletions(-) create mode 100644 playground/worker/__tests__/sourcemap/__snapshots__/sourcemap-worker.spec.ts.snap diff --git a/packages/vite/src/node/plugins/assetImportMetaUrl.ts b/packages/vite/src/node/plugins/assetImportMetaUrl.ts index 636234736cc32c..1d1401dd8b44a9 100644 --- a/packages/vite/src/node/plugins/assetImportMetaUrl.ts +++ b/packages/vite/src/node/plugins/assetImportMetaUrl.ts @@ -3,6 +3,7 @@ import MagicString from 'magic-string' import { stripLiteral } from 'strip-literal' import type { Plugin } from '../plugin' import type { ResolvedConfig } from '../config' +import { transformResult } from '../utils' import { fileToUrl } from './asset' import { preloadHelperId } from './importAnalysisBuild' @@ -80,10 +81,7 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin { ) } if (s) { - return { - code: s.toString(), - map: config.build.sourcemap ? s.generateMap({ hires: true }) : null - } + return transformResult(s, id, config) } } return null diff --git a/packages/vite/src/node/plugins/dynamicImportVars.ts b/packages/vite/src/node/plugins/dynamicImportVars.ts index a9f0d6fb810333..b6047aa59e138d 100644 --- a/packages/vite/src/node/plugins/dynamicImportVars.ts +++ b/packages/vite/src/node/plugins/dynamicImportVars.ts @@ -7,7 +7,12 @@ import { createFilter } from '@rollup/pluginutils' import { dynamicImportToGlob } from '@rollup/plugin-dynamic-import-vars' import type { Plugin } from '../plugin' import type { ResolvedConfig } from '../config' -import { normalizePath, parseRequest, requestQuerySplitRE } from '../utils' +import { + normalizePath, + parseRequest, + requestQuerySplitRE, + transformResult +} from '../utils' export const dynamicImportHelperId = '/@vite/dynamic-import-helper' @@ -204,10 +209,7 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin { `import __variableDynamicImportRuntimeHelper from "${dynamicImportHelperId}";` ) } - return { - code: s.toString(), - map: config.build.sourcemap ? s.generateMap({ hires: true }) : null - } + return transformResult(s, importer, config) } } } diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index 5d2fce4ad13e02..ba221b4b1d5283 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -37,6 +37,7 @@ import { prettifyUrl, removeImportQuery, timeFrom, + transformResult, unwrapId } from '../utils' import type { ResolvedConfig } from '../config' @@ -630,10 +631,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { } if (s) { - return { - code: s.toString(), - map: config.build.sourcemap ? s.generateMap({ hires: true }) : null - } + return transformResult(s, importer, config) } else { return source } diff --git a/packages/vite/src/node/plugins/importMetaGlob.ts b/packages/vite/src/node/plugins/importMetaGlob.ts index 4a6c97e09150eb..e6499425fc6637 100644 --- a/packages/vite/src/node/plugins/importMetaGlob.ts +++ b/packages/vite/src/node/plugins/importMetaGlob.ts @@ -17,7 +17,7 @@ import type { Plugin } from '../plugin' import type { ViteDevServer } from '../server' import type { ModuleNode } from '../server/moduleGraph' import type { ResolvedConfig } from '../config' -import { normalizePath, slash } from '../utils' +import { normalizePath, slash, transformResult } from '../utils' const { isMatch, scan } = micromatch @@ -75,10 +75,7 @@ export function importGlobPlugin(config: ResolvedConfig): Plugin { server!.watcher.add(dirname(file)) }) } - return { - code: result.s.toString(), - map: config.build.sourcemap ? result.s.generateMap() : null - } + return transformResult(result.s, id, config) } } } diff --git a/packages/vite/src/node/plugins/workerImportMetaUrl.ts b/packages/vite/src/node/plugins/workerImportMetaUrl.ts index ac0c4f0b346aac..cfb10fdb2e8591 100644 --- a/packages/vite/src/node/plugins/workerImportMetaUrl.ts +++ b/packages/vite/src/node/plugins/workerImportMetaUrl.ts @@ -5,7 +5,13 @@ import type { RollupError } from 'rollup' import { stripLiteral } from 'strip-literal' import type { ResolvedConfig } from '../config' import type { Plugin } from '../plugin' -import { cleanUrl, injectQuery, normalizePath, parseRequest } from '../utils' +import { + cleanUrl, + injectQuery, + normalizePath, + parseRequest, + transformResult +} from '../utils' import type { WorkerType } from './worker' import { WORKER_FILE_ID, workerFileToUrl } from './worker' import { fileToUrl } from './asset' @@ -130,10 +136,7 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin { } if (s) { - return { - code: s.toString(), - map: config.build.sourcemap ? s.generateMap({ hires: true }) : null - } + return transformResult(s, id, config) } return null diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index db331894bb78fb..fbc6003d3e9b5f 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -13,6 +13,9 @@ import type { DecodedSourceMap, RawSourceMap } from '@ampproject/remapping' import colors from 'picocolors' import debug from 'debug' import type { Alias, AliasOptions } from 'types/alias' +import type MagicString from 'magic-string' + +import type { TransformResult } from 'rollup' import { CLIENT_ENTRY, CLIENT_PUBLIC_PATH, @@ -21,6 +24,7 @@ import { FS_PREFIX, VALID_ID_PREFIX } from './constants' +import type { ResolvedConfig } from '.' export function slash(p: string): string { return p.replace(/\\/g, '/') @@ -971,3 +975,16 @@ function normalizeSingleAlias({ } return alias } + +export function transformResult( + s: MagicString, + id: string, + config: ResolvedConfig +): TransformResult { + const isBuild = config.command === 'build' + const needSourceMap = !isBuild || config.build.sourcemap + return { + code: s.toString(), + map: needSourceMap ? s.generateMap({ hires: true, source: id }) : null + } +} diff --git a/playground/dynamic-import/vite.config.js b/playground/dynamic-import/vite.config.js index 50b90639fddd7f..b12d8347287a2f 100644 --- a/playground/dynamic-import/vite.config.js +++ b/playground/dynamic-import/vite.config.js @@ -26,5 +26,8 @@ module.exports = vite.defineConfig({ alias: { '@': path.resolve(__dirname, 'alias') } + }, + build: { + sourcemap: true } }) diff --git a/playground/worker/__tests__/sourcemap/__snapshots__/sourcemap-worker.spec.ts.snap b/playground/worker/__tests__/sourcemap/__snapshots__/sourcemap-worker.spec.ts.snap new file mode 100644 index 00000000000000..71ea7527a69d51 --- /dev/null +++ b/playground/worker/__tests__/sourcemap/__snapshots__/sourcemap-worker.spec.ts.snap @@ -0,0 +1,14 @@ +// Vitest Snapshot v1 + +exports[`serve:worker-sourcemap > nested worker 1`] = ` +{ + "mappings": "AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,uCAAsB,CAAC;AAClD;AACA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACzB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC1B,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACnC,CAAC,CAAC,CAAC;AACH,CAAC;AACD;AACA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAChB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;", + "sources": [ + "/root/possible-ts-output-worker.mjs?worker_file", + ], + "sourcesContent": [ + null, + ], + "version": 3, +} +`; diff --git a/playground/worker/__tests__/sourcemap/sourcemap-worker.spec.ts b/playground/worker/__tests__/sourcemap/sourcemap-worker.spec.ts index af4a074549b167..1081a8877cf997 100644 --- a/playground/worker/__tests__/sourcemap/sourcemap-worker.spec.ts +++ b/playground/worker/__tests__/sourcemap/sourcemap-worker.spec.ts @@ -1,6 +1,14 @@ +/* eslint-disable node/no-unsupported-features/node-builtins */ import fs from 'fs' import path from 'path' -import { isBuild, testDir } from '~utils' +import { + extractSourcemap, + formatSourcemapForSnapshot, + isBuild, + isServe, + page, + testDir +} from '~utils' describe.runIf(isBuild)('build', () => { // assert correct files @@ -113,6 +121,16 @@ describe.runIf(isBuild)('build', () => { }) }) +describe.runIf(isServe)('serve:worker-sourcemap', () => { + test('nested worker', async () => { + const res = await page.request.get( + new URL('./possible-ts-output-worker.mjs?worker_file', page.url()).href + ) + const map = extractSourcemap(await res.text()) + expect(formatSourcemapForSnapshot(map)).toMatchSnapshot() + }) +}) + function getSourceMapUrl(code: string): string { const regex = /\/\/[#@]\s(?:source(?:Mapping)?URL)=\s*(\S+)/g const results = regex.exec(code) diff --git a/playground/worker/worker-nested-worker.js b/playground/worker/worker-nested-worker.js index e74d1db760409b..f49c020754c957 100644 --- a/playground/worker/worker-nested-worker.js +++ b/playground/worker/worker-nested-worker.js @@ -1,3 +1,4 @@ +import ImportMetaGlobEagerWorker from './importMetaGlobEager.worker?worker' import SubWorker from './sub-worker?worker' const subWorker = new SubWorker() @@ -27,5 +28,16 @@ classicWorker.addEventListener('message', (ev) => { }) }) +const importMetaGlobEagerWorker = new ImportMetaGlobEagerWorker() + +importMetaGlobEagerWorker.postMessage('1') + +importMetaGlobEagerWorker.addEventListener('message', (ev) => { + self.postMessage({ + type: 'importMetaGlobEager', + data: ev.data + }) +}) + // for sourcemap console.log('worker-nested-worker.js') diff --git a/playground/worker/worker/main-module.js b/playground/worker/worker/main-module.js index 6284ca63686e99..81e639341d9d5b 100644 --- a/playground/worker/worker/main-module.js +++ b/playground/worker/worker/main-module.js @@ -3,7 +3,6 @@ import InlineWorker from '../my-worker?worker&inline' import mySharedWorker from '../my-shared-worker?sharedworker&name=shared' import TSOutputWorker from '../possible-ts-output-worker?worker' import NestedWorker from '../worker-nested-worker?worker' -import ImportMetaGlobEagerWorker from '../importMetaGlobEager.worker?worker' import { mode } from '../modules/workerImport' function text(el, text) { @@ -63,6 +62,8 @@ nestedWorker.addEventListener('message', (ev) => { text('.nested-worker-module', JSON.stringify(ev.data)) } else if (data.type === 'constructor') { text('.nested-worker-constructor', JSON.stringify(ev.data)) + } else if (data.type === 'importMetaGlobEager') { + text('.importMetaGlobEager-worker', JSON.stringify(ev.data)) } } }) @@ -91,11 +92,3 @@ w2.port.addEventListener('message', (ev) => { text('.shared-worker-import-meta-url', JSON.stringify(ev.data)) }) w2.port.start() - -const importMetaGlobEagerWorker = new ImportMetaGlobEagerWorker() - -importMetaGlobEagerWorker.postMessage('1') - -importMetaGlobEagerWorker.addEventListener('message', (e) => { - text('.importMetaGlobEager-worker', JSON.stringify(e.data)) -})