Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sourcemap source point to null #8299

Merged
merged 19 commits into from May 29, 2022
6 changes: 2 additions & 4 deletions packages/vite/src/node/plugins/assetImportMetaUrl.ts
Expand Up @@ -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'

Expand Down Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions packages/vite/src/node/plugins/dynamicImportVars.ts
Expand Up @@ -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'

Expand Down Expand Up @@ -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)
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -37,6 +37,7 @@ import {
prettifyUrl,
removeImportQuery,
timeFrom,
transformResult,
unwrapId
} from '../utils'
import type { ResolvedConfig } from '../config'
Expand Down Expand Up @@ -626,10 +627,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
}
Expand Down
7 changes: 2 additions & 5 deletions packages/vite/src/node/plugins/importMetaGlob.ts
Expand Up @@ -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

Expand Down Expand Up @@ -72,10 +72,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)
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions packages/vite/src/node/plugins/workerImportMetaUrl.ts
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions packages/vite/src/node/utils.ts
Expand Up @@ -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,
Expand All @@ -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, '/')
Expand Down Expand Up @@ -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
}
}
3 changes: 3 additions & 0 deletions playground/dynamic-import/vite.config.js
Expand Up @@ -26,5 +26,8 @@ module.exports = vite.defineConfig({
alias: {
'@': path.resolve(__dirname, 'alias')
}
},
build: {
sourcemap: true
}
})
@@ -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,
}
`;
20 changes: 19 additions & 1 deletion 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
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions 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()
Expand Down Expand Up @@ -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')
11 changes: 2 additions & 9 deletions playground/worker/worker/main-module.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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))
}
}
})
Expand Down Expand Up @@ -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))
})