MCPcopy
hub / github.com/vitejs/vite / bundleWorkerEntry

Function bundleWorkerEntry

packages/vite/src/node/plugins/worker.ts:162–306  ·  view source on GitHub ↗
(
  config: ResolvedConfig,
  id: string,
)

Source from the content-addressed store, hash-verified

160const workerOutputCaches = new WeakMap<ResolvedConfig, WorkerOutputCache>()
161
162async function bundleWorkerEntry(
163 config: ResolvedConfig,
164 id: string,
165): Promise<WorkerBundle> {
166 const input = cleanUrl(id)
167
168 const workerOutput = workerOutputCaches.get(config.mainConfig || config)!
169 workerOutput.removeBundleIfInvalidated(input)
170
171 const bundleInfo = workerOutput.getWorkerBundle(input)
172 if (bundleInfo) {
173 return bundleInfo
174 }
175
176 const newBundleChain = [...config.bundleChain, input]
177 if (config.bundleChain.includes(input)) {
178 throw new Error(
179 'Circular worker imports detected. Vite does not support it. ' +
180 `Import chain: ${newBundleChain.map((id) => prettifyUrl(id, config.root)).join(' -> ')}`,
181 )
182 }
183
184 // bundle the file as entry to support imports
185 const { rolldown } = await import('rolldown')
186 const { plugins, rolldownOptions, format } = config.worker
187 const workerConfig = await plugins(newBundleChain)
188 const workerEnvironment = new BuildEnvironment('client', workerConfig) // TODO: should this be 'worker'?
189 await workerEnvironment.init()
190
191 const chunkMetadataMap = new ChunkMetadataMap()
192 const workerBuildTarget = workerEnvironment.config.build.target
193 const bundle = await rolldown({
194 ...rolldownOptions,
195 input,
196 plugins: workerEnvironment.plugins.map((p) =>
197 injectEnvironmentToHooks(workerEnvironment, chunkMetadataMap, p),
198 ),
199 onLog(level, log) {
200 onRollupLog(level, log, workerEnvironment)
201 },
202 transform: {
203 target: workerBuildTarget === false ? undefined : workerBuildTarget,
204 ...rolldownOptions.transform,
205 define: {
206 ...rolldownOptions.transform?.define,
207 // disable builtin process.env.NODE_ENV replacement as it is handled by the define plugin
208 'process.env.NODE_ENV': 'process.env.NODE_ENV',
209 },
210 },
211 // TODO: remove this and enable rolldown's CSS support later
212 moduleTypes: {
213 '.css': 'js',
214 ...rolldownOptions.moduleTypes,
215 },
216 preserveEntrySignatures: false,
217 experimental: {
218 ...rolldownOptions.experimental,
219 viteMode: true,

Callers 2

workerFileToUrlFunction · 0.85
handlerFunction · 0.85

Calls 11

initMethod · 0.95
cleanUrlFunction · 0.90
prettifyUrlFunction · 0.90
injectEnvironmentToHooksFunction · 0.90
normalizePathFunction · 0.90
pluginsFunction · 0.85
getWorkerBundleMethod · 0.80
saveWorkerBundleMethod · 0.80
closeMethod · 0.65
getMethod · 0.45

Tested by

no test coverage detected