Skip to content

Commit be2ae2a

Browse files
committed
perf: skip worker renderChunk in dev
1 parent 7b40cb6 commit be2ae2a

File tree

1 file changed

+55
-46
lines changed

1 file changed

+55
-46
lines changed

packages/vite/src/node/plugins/worker.ts

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -463,53 +463,62 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
463463
},
464464
},
465465

466-
renderChunk(code, chunk, outputOptions) {
467-
let s: MagicString
468-
const result = () => {
469-
return (
470-
s && {
471-
code: s.toString(),
472-
map: this.environment.config.build.sourcemap
473-
? s.generateMap({ hires: 'boundary' })
474-
: null,
475-
}
476-
)
477-
}
478-
workerAssetUrlRE.lastIndex = 0
479-
if (workerAssetUrlRE.test(code)) {
480-
const toRelativeRuntime = createToImportMetaURLBasedRelativeRuntime(
481-
outputOptions.format,
482-
this.environment.config.isWorker,
483-
)
484-
485-
let match: RegExpExecArray | null
486-
s = new MagicString(code)
487-
workerAssetUrlRE.lastIndex = 0
488-
489-
// Replace "__VITE_WORKER_ASSET__5aa0ddc0__" using relative paths
490-
const workerMap = workerCache.get(config.mainConfig || config)!
491-
const { fileNameHash } = workerMap
492-
493-
while ((match = workerAssetUrlRE.exec(code))) {
494-
const [full, hash] = match
495-
const filename = fileNameHash.get(hash)!
496-
const replacement = toOutputFilePathInJS(
497-
this.environment,
498-
filename,
499-
'asset',
500-
chunk.fileName,
501-
'js',
502-
toRelativeRuntime,
503-
)
504-
const replacementString =
505-
typeof replacement === 'string'
506-
? JSON.stringify(encodeURIPath(replacement)).slice(1, -1)
507-
: `"+${replacement.runtime}+"`
508-
s.update(match.index, match.index + full.length, replacementString)
466+
...(isBuild
467+
? {
468+
renderChunk(code, chunk, outputOptions) {
469+
let s: MagicString
470+
const result = () => {
471+
return (
472+
s && {
473+
code: s.toString(),
474+
map: this.environment.config.build.sourcemap
475+
? s.generateMap({ hires: 'boundary' })
476+
: null,
477+
}
478+
)
479+
}
480+
workerAssetUrlRE.lastIndex = 0
481+
if (workerAssetUrlRE.test(code)) {
482+
const toRelativeRuntime =
483+
createToImportMetaURLBasedRelativeRuntime(
484+
outputOptions.format,
485+
this.environment.config.isWorker,
486+
)
487+
488+
let match: RegExpExecArray | null
489+
s = new MagicString(code)
490+
workerAssetUrlRE.lastIndex = 0
491+
492+
// Replace "__VITE_WORKER_ASSET__5aa0ddc0__" using relative paths
493+
const workerMap = workerCache.get(config.mainConfig || config)!
494+
const { fileNameHash } = workerMap
495+
496+
while ((match = workerAssetUrlRE.exec(code))) {
497+
const [full, hash] = match
498+
const filename = fileNameHash.get(hash)!
499+
const replacement = toOutputFilePathInJS(
500+
this.environment,
501+
filename,
502+
'asset',
503+
chunk.fileName,
504+
'js',
505+
toRelativeRuntime,
506+
)
507+
const replacementString =
508+
typeof replacement === 'string'
509+
? JSON.stringify(encodeURIPath(replacement)).slice(1, -1)
510+
: `"+${replacement.runtime}+"`
511+
s.update(
512+
match.index,
513+
match.index + full.length,
514+
replacementString,
515+
)
516+
}
517+
}
518+
return result()
519+
},
509520
}
510-
}
511-
return result()
512-
},
521+
: {}),
513522

514523
generateBundle(opts, bundle) {
515524
// to avoid emitting duplicate assets for modern build and legacy build

0 commit comments

Comments
 (0)