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
7 changes: 6 additions & 1 deletion packages/vite/src/node/plugins/assetImportMetaUrl.ts
Expand Up @@ -17,6 +17,8 @@ import { preloadHelperId } from './importAnalysisBuild'
* ```
*/
export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
const isBuild = config.command === 'build'

return {
name: 'vite:asset-import-meta-url',
async transform(code, id, options) {
Expand Down Expand Up @@ -82,7 +84,10 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
if (s) {
return {
code: s.toString(),
map: config.build.sourcemap ? s.generateMap({ hires: true }) : null
map:
!isBuild || config.build.sourcemap
? s.generateMap({ hires: true, source: id })
: null
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion packages/vite/src/node/plugins/dynamicImportVars.ts
Expand Up @@ -117,6 +117,7 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
const { include, exclude, warnOnError } =
config.build.dynamicImportVarsOptions
const filter = createFilter(include, exclude)
const isBuild = config.command === 'build'

return {
name: 'vite:dynamic-import-vars',
Expand Down Expand Up @@ -206,7 +207,10 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
}
return {
code: s.toString(),
map: config.build.sourcemap ? s.generateMap({ hires: true }) : null
map:
!isBuild || config.build.sourcemap
? s.generateMap({ hires: true, source: importer })
: null
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -111,7 +111,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
const { root, base } = config
const clientPublicPath = path.posix.join(base, CLIENT_PUBLIC_PATH)
let server: ViteDevServer

const isBuild = config.command === 'build'
return {
name: 'vite:import-analysis',

Expand Down Expand Up @@ -628,7 +628,10 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
if (s) {
return {
code: s.toString(),
map: config.build.sourcemap ? s.generateMap({ hires: true }) : null
map:
!isBuild || config.build.sourcemap
? s.generateMap({ hires: true, source: importer })
: null
}
} else {
return source
Expand Down
6 changes: 5 additions & 1 deletion packages/vite/src/node/plugins/importMetaGlob.ts
Expand Up @@ -47,6 +47,7 @@ export function getAffectedGlobModules(file: string, server: ViteDevServer) {

export function importGlobPlugin(config: ResolvedConfig): Plugin {
let server: ViteDevServer | undefined
const isBuild = config.command === 'build'

return {
name: 'vite:import-glob',
Expand Down Expand Up @@ -74,7 +75,10 @@ export function importGlobPlugin(config: ResolvedConfig): Plugin {
}
return {
code: result.s.toString(),
map: config.build.sourcemap ? result.s.generateMap() : null
map:
!isBuild || config.build.sourcemap
? result.s.generateMap({ hires: true, source: id })
: null
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/vite/src/node/plugins/workerImportMetaUrl.ts
Expand Up @@ -132,7 +132,10 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
if (s) {
return {
code: s.toString(),
map: config.build.sourcemap ? s.generateMap({ hires: true }) : null
map:
!isBuild || config.build.sourcemap
? s.generateMap({ hires: true, source: id })
: null
poyoho marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
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))
})