Skip to content

Commit

Permalink
fix: useless chunk cache
Browse files Browse the repository at this point in the history
  • Loading branch information
poyoho committed Apr 19, 2022
1 parent 2920e58 commit 47a4150
Showing 1 changed file with 2 additions and 26 deletions.
28 changes: 2 additions & 26 deletions packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ import type Rollup from 'rollup'
import { ENV_PUBLIC_PATH } from '../constants'
import path from 'path'
import { onRollupWarning } from '../build'
import type { TransformPluginContext, EmittedAsset } from 'rollup'
import type { TransformPluginContext } from 'rollup'

interface WorkerCache {
// let the worker chunks all emit in the top-level emitFile
// <chunk_filename, WorkerEmitAsset>
chunks: Map<string, EmittedAsset>

// worker bundle don't deps on any more worker runtime info an id only had an result.
// save worker bundled file id to avoid repeated execution of bundles
// <input_filename, hash>
Expand All @@ -22,18 +18,6 @@ interface WorkerCache {
const WorkerFileId = 'worker_file'
const workerCache = new WeakMap<ResolvedConfig, WorkerCache>()

function emitWorkerChunks(config: ResolvedConfig, asset: EmittedAsset): string {
const fileName = asset.fileName!
const workerMap = workerCache.get(config.rawConfig || config)!
if (workerMap.chunks.has(fileName)) {
return (workerMap.chunks.get(fileName)! as any).hash
}
const hash = getAssetHash(fileName)
;(asset as any).hash = hash
workerMap.chunks.set(fileName, asset)
return hash
}

// Nested worker construction is a recursive process. The outputChunk of asset type can be output directly.
// But the outputChunk of the chunk type needs to use the asset type to emitFile,
// which will cause it to become an asset in the recursive process.
Expand Down Expand Up @@ -89,20 +73,13 @@ export async function bundleWorkerEntry(
if (outputChunk.type === 'asset') {
ctx.emitFile(outputChunk)
} else if (outputChunk.type === 'chunk') {
emitWorkerChunks(config, {
ctx.emitFile({
fileName: outputChunk.fileName,
source: outputChunk.code,
type: 'asset'
})
}
})
if (!config.isWorker) {
const workerMap = workerCache.get(config.rawConfig || config)!
workerMap.chunks.forEach((asset) => {
ctx.emitFile(asset)
})
workerMap.chunks.clear()
}
} finally {
await bundle.close()
}
Expand Down Expand Up @@ -193,7 +170,6 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
return
}
workerCache.set(config, {
chunks: new Map(),
bundle: new Map()
})
},
Expand Down

0 comments on commit 47a4150

Please sign in to comment.