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(worker): hide "The emitted file overwrites" warning if the content is same #16094

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 35 additions & 11 deletions packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path'
import MagicString from 'magic-string'
import type { EmittedAsset, OutputChunk } from 'rollup'
import type { OutputChunk } from 'rollup'
import type { ResolvedConfig } from '../config'
import type { Plugin } from '../plugin'
import type { ViteDevServer } from '../server'
Expand All @@ -14,9 +14,11 @@ import {
import { cleanUrl } from '../../shared/utils'
import { fileToUrl } from './asset'

type WorkerBundleAsset = { fileName: string; source: string | Uint8Array }

interface WorkerCache {
// save worker all emit chunk avoid rollup make the same asset unique.
assets: Map<string, EmittedAsset>
assets: Map<string, WorkerBundleAsset>

// worker bundle don't deps on any more worker runtime info an id only had a result.
// save worker bundled file id to avoid repeated execution of bundles
Expand All @@ -38,11 +40,10 @@ const workerCache = new WeakMap<ResolvedConfig, WorkerCache>()

function saveEmitWorkerAsset(
config: ResolvedConfig,
asset: EmittedAsset,
asset: WorkerBundleAsset,
): void {
const fileName = asset.fileName!
const workerMap = workerCache.get(config.mainConfig || config)!
workerMap.assets.set(fileName, asset)
workerMap.assets.set(asset.fileName, asset)
}

async function bundleWorkerEntry(
Expand Down Expand Up @@ -96,7 +97,6 @@ async function bundleWorkerEntry(
saveEmitWorkerAsset(config, {
fileName: outputChunk.fileName,
source: outputChunk.code,
type: 'asset',
})
}
})
Expand All @@ -121,7 +121,6 @@ function emitSourcemapForWorkerEntry(
const mapFileName = chunk.fileName + '.map'
saveEmitWorkerAsset(config, {
fileName: mapFileName,
type: 'asset',
source: data,
})
}
Expand Down Expand Up @@ -156,7 +155,6 @@ export async function workerFileToUrl(
saveEmitWorkerAsset(config, {
fileName,
source: outputChunk.code,
type: 'asset',
})
workerMap.bundle.set(id, fileName)
}
Expand Down Expand Up @@ -410,16 +408,42 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
return result()
},

generateBundle(opts) {
generateBundle(opts, bundle) {
// @ts-expect-error asset emits are skipped in legacy bundle
if (opts.__vite_skip_asset_emit__ || isWorker) {
return
}
const workerMap = workerCache.get(config)!
workerMap.assets.forEach((asset) => {
this.emitFile(asset)
workerMap.assets.delete(asset.fileName!)
const duplicateAsset = bundle[asset.fileName]
if (duplicateAsset) {
const content =
duplicateAsset.type === 'asset'
? duplicateAsset.source
: duplicateAsset.code
// don't emit if the file name and the content is same
if (isSameContent(content, asset.source)) {
return
}
}

this.emitFile({
type: 'asset',
fileName: asset.fileName,
source: asset.source,
})
})
workerMap.assets.clear()
},
}
}

function isSameContent(a: string | Uint8Array, b: string | Uint8Array) {
if (typeof a === 'string') {
if (typeof b === 'string') {
return a === b
}
return Buffer.from(a).equals(b)
}
return Buffer.from(b).equals(a)
}