(config: ResolvedConfig, id: string)
| 87 | } |
| 88 | |
| 89 | async function bundleWorkerEntry(config: ResolvedConfig, id: string): Promise<BundledWorkerChunk> { |
| 90 | const input = cleanUrl(id); |
| 91 | const bundleChain = config.bundleChain ?? []; |
| 92 | const newBundleChain = [...bundleChain, input]; |
| 93 | if (bundleChain.includes(input)) { |
| 94 | throw new Error( |
| 95 | `Circular worker imports detected. Vite does not support it. Import chain: ${newBundleChain.join(' -> ')}` |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | const { plugins, format } = config.worker; |
| 100 | // Vite 8 exposes `rolldownOptions`; Vite 7 only `rollupOptions`. |
| 101 | const workerOptionsCarrier = config.worker as { rolldownOptions?: any; rollupOptions?: any }; |
| 102 | const workerBundlerOptions = workerOptionsCarrier.rolldownOptions ?? workerOptionsCarrier.rollupOptions; |
| 103 | |
| 104 | const workerConfig = await plugins(newBundleChain); |
| 105 | const workerEnvironment = new BuildEnvironment('client', workerConfig); |
| 106 | await workerEnvironment.init(); |
| 107 | |
| 108 | const wrappedPlugins = workerEnvironment.plugins.map(p => injectEnvironmentToHooks(workerEnvironment, p)); |
| 109 | |
| 110 | const workerOutputConfig = workerBundlerOptions.output; |
| 111 | const outputConfig = workerOutputConfig |
| 112 | ? Array.isArray(workerOutputConfig) |
| 113 | ? workerOutputConfig[0] || {} |
| 114 | : workerOutputConfig |
| 115 | : {}; |
| 116 | |
| 117 | const generateOptions = { |
| 118 | entryFileNames: path.posix.join(config.build.assetsDir, '[name]-[hash].js'), |
| 119 | chunkFileNames: path.posix.join(config.build.assetsDir, '[name]-[hash].js'), |
| 120 | assetFileNames: path.posix.join(config.build.assetsDir, '[name]-[hash].[ext]'), |
| 121 | ...outputConfig, |
| 122 | format, |
| 123 | sourcemap: config.build.sourcemap |
| 124 | }; |
| 125 | |
| 126 | let chunk: BundledWorkerChunk; |
| 127 | if (detectBundler(config) === BundlerKind.Rolldown) { |
| 128 | chunk = await bundleWorkerEntryRolldown( |
| 129 | config, |
| 130 | input, |
| 131 | workerBundlerOptions, |
| 132 | wrappedPlugins, |
| 133 | generateOptions, |
| 134 | workerEnvironment |
| 135 | ); |
| 136 | } else { |
| 137 | chunk = await bundleWorkerEntryRollup(config, input, workerBundlerOptions, wrappedPlugins, generateOptions); |
| 138 | } |
| 139 | |
| 140 | return emitSourcemapForWorkerEntry(config, chunk); |
| 141 | } |
| 142 | |
| 143 | // Rollup path (Vite 7 and earlier). |
| 144 | async function bundleWorkerEntryRollup( |
no test coverage detected