diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index a6f0bd5a3ff13d..6c65a2aaaa9012 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -522,9 +522,7 @@ export interface LegacyOptions { export interface ResolvedWorkerOptions { format: 'es' | 'iife' - plugins: ( - bundleChain: string[], - ) => Promise<{ plugins: Plugin[]; config: ResolvedConfig }> + plugins: (bundleChain: string[]) => Promise rollupOptions: RollupOptions } @@ -1340,7 +1338,10 @@ export async function resolveConfig( .map((hook) => hook(workerResolved)), ) - return { plugins: resolvedWorkerPlugins, config: workerResolved } + return { + ...workerResolved, + plugins: resolvedWorkerPlugins, + } } const resolvedWorkerOptions: ResolvedWorkerOptions = { diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 3a31e5e091eb5d..b7eea41aac0a6a 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -75,13 +75,14 @@ async function bundleWorkerEntry( // bundle the file as entry to support imports const { rollup } = await import('rollup') const { plugins, rollupOptions, format } = config.worker - const { plugins: resolvedPlugins, config: workerConfig } = - await plugins(newBundleChain) + const workerConfig = await plugins(newBundleChain) const workerEnvironment = new BuildEnvironment('client', workerConfig) // TODO: should this be 'worker'? + await workerEnvironment.init() + const bundle = await rollup({ ...rollupOptions, input, - plugins: resolvedPlugins.map((p) => + plugins: workerEnvironment.plugins.map((p) => injectEnvironmentToHooks(workerEnvironment, p), ), onwarn(warning, warn) { diff --git a/playground/worker/__tests__/es/worker-es.spec.ts b/playground/worker/__tests__/es/worker-es.spec.ts index ff85d075ff117a..46cfa0fd969134 100644 --- a/playground/worker/__tests__/es/worker-es.spec.ts +++ b/playground/worker/__tests__/es/worker-es.spec.ts @@ -14,6 +14,7 @@ test('normal', async () => { () => page.textContent('.asset-url'), isBuild ? '/es/assets/worker_asset-vite.svg' : '/es/vite.svg', ) + await untilUpdated(() => page.textContent('.dep-cjs'), '[cjs ok]') }) test('named', async () => { diff --git a/playground/worker/__tests__/iife/worker-iife.spec.ts b/playground/worker/__tests__/iife/worker-iife.spec.ts index 8ffba3f62daeca..4c53d0ce25ef8e 100644 --- a/playground/worker/__tests__/iife/worker-iife.spec.ts +++ b/playground/worker/__tests__/iife/worker-iife.spec.ts @@ -189,7 +189,7 @@ test.runIf(isServe)('sourcemap is correct after env is injected', async () => { const content = await (await response).text() const { mappings } = decodeSourceMapUrl(content) expect(mappings).toMatchInlineSnapshot( - `";;AAAA,SAAS,OAAO,kBAAkB;AAClC,SAAS,MAAM,WAAW;AAC1B,SAAS,wBAAwB;AACjC,OAAO,aAAa;AACpB,MAAM,UAAU,YAAY;AAE5B,KAAK,YAAY,CAAC,MAAM;AACtB,MAAI,EAAE,SAAS,QAAQ;AACrB,SAAK,YAAY,EAAE,KAAK,MAAM,kBAAkB,SAAS,SAAS,KAAK,CAAC;AAAA,EAC1E;AACA,MAAI,EAAE,SAAS,gBAAgB;AAC7B,SAAK,YAAY;AAAA,MACf,KAAK;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC;AAAA,EACH;AACF;AACA,KAAK,YAAY;AAAA,EACf;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAGD,QAAQ,IAAI,cAAc"`, + `";;AAAA,SAAS,OAAO,kBAAkB;AAClC,OAAO,YAAY;AACnB,SAAS,MAAM,WAAW;AAC1B,SAAS,wBAAwB;AACjC,OAAO,aAAa;AACpB,MAAM,UAAU,YAAY;AAE5B,KAAK,YAAY,CAAC,MAAM;AACtB,MAAI,EAAE,SAAS,QAAQ;AACrB,SAAK,YAAY;AAAA,MACf;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC;AAAA,EACH;AACA,MAAI,EAAE,SAAS,gBAAgB;AAC7B,SAAK,YAAY;AAAA,MACf,KAAK;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC;AAAA,EACH;AACF;AACA,KAAK,YAAY;AAAA,EACf;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAGD,QAAQ,IAAI,cAAc"`, ) }) diff --git a/playground/worker/dep-cjs/index.cjs b/playground/worker/dep-cjs/index.cjs new file mode 100644 index 00000000000000..353a69637f11a6 --- /dev/null +++ b/playground/worker/dep-cjs/index.cjs @@ -0,0 +1 @@ +exports.test = '[cjs ok]' diff --git a/playground/worker/dep-cjs/package.json b/playground/worker/dep-cjs/package.json new file mode 100644 index 00000000000000..4392ed373d19e7 --- /dev/null +++ b/playground/worker/dep-cjs/package.json @@ -0,0 +1,5 @@ +{ + "name": "@vitejs/test-worker-dep-cjs", + "type": "commonjs", + "exports": "./index.cjs" +} diff --git a/playground/worker/index.html b/playground/worker/index.html index d6b3806b3293ae..8ece3711324053 100644 --- a/playground/worker/index.html +++ b/playground/worker/index.html @@ -18,6 +18,7 @@

format iife:

mode:
bundle-with-plugin:
asset-url:
+
dep-cjs:

diff --git a/playground/worker/my-worker.ts b/playground/worker/my-worker.ts index ea49a402325954..122d4e1e22bccb 100644 --- a/playground/worker/my-worker.ts +++ b/playground/worker/my-worker.ts @@ -1,4 +1,5 @@ import { msg as msgFromDep } from '@vitejs/test-dep-to-optimize' +import depCjs from '@vitejs/test-worker-dep-cjs' import { mode, msg } from './modules/workerImport.js' import { bundleWithPlugin } from './modules/test-plugin' import viteSvg from './vite.svg' @@ -6,7 +7,15 @@ const metaUrl = import.meta.url self.onmessage = (e) => { if (e.data === 'ping') { - self.postMessage({ msg, mode, bundleWithPlugin, viteSvg, metaUrl, name }) + self.postMessage({ + msg, + mode, + bundleWithPlugin, + viteSvg, + metaUrl, + name, + depCjs, + }) } if (e.data === 'ping-unicode') { self.postMessage({ @@ -16,6 +25,7 @@ self.onmessage = (e) => { viteSvg, metaUrl, name, + depCjs, }) } } @@ -27,6 +37,7 @@ self.postMessage({ viteSvg, metaUrl, name, + depCjs, }) // for sourcemap diff --git a/playground/worker/package.json b/playground/worker/package.json index e9b2dd7876e027..02e99873551af4 100644 --- a/playground/worker/package.json +++ b/playground/worker/package.json @@ -29,6 +29,7 @@ }, "dependencies": { "@vitejs/test-dep-self-reference-url-worker": "file:./dep-self-reference-url-worker", - "@vitejs/test-dep-to-optimize": "file:./dep-to-optimize" + "@vitejs/test-dep-to-optimize": "file:./dep-to-optimize", + "@vitejs/test-worker-dep-cjs": "file:./dep-cjs" } } diff --git a/playground/worker/worker/main-module.js b/playground/worker/worker/main-module.js index a59bc903800b12..ed694895296f0e 100644 --- a/playground/worker/worker/main-module.js +++ b/playground/worker/worker/main-module.js @@ -21,6 +21,7 @@ worker.addEventListener('message', (e) => { text('.mode', e.data.mode) text('.bundle-with-plugin', e.data.bundleWithPlugin) text('.asset-url', e.data.viteSvg) + text('.dep-cjs', e.data.depCjs.test) }) const namedWorker = new myWorker({ name: 'namedWorker' }) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c9a26e942e1160..461326d448fb52 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1629,6 +1629,11 @@ importers: '@vitejs/test-dep-to-optimize': specifier: file:./dep-to-optimize version: file:playground/worker/dep-to-optimize + '@vitejs/test-worker-dep-cjs': + specifier: file:./dep-cjs + version: file:playground/worker/dep-cjs + + playground/worker/dep-cjs: {} playground/worker/dep-self-reference-url-worker: {} @@ -3479,6 +3484,9 @@ packages: '@vitejs/test-ts-transpiled-exports@file:playground/ssr-deps/ts-transpiled-exports': resolution: {directory: playground/ssr-deps/ts-transpiled-exports, type: directory} + '@vitejs/test-worker-dep-cjs@file:playground/worker/dep-cjs': + resolution: {directory: playground/worker/dep-cjs, type: directory} + '@vitejs/test-worker-exports@file:playground/ssr-webworker/worker-exports': resolution: {directory: playground/ssr-webworker/worker-exports, type: directory} @@ -9082,6 +9090,8 @@ snapshots: '@vitejs/test-ts-transpiled-exports@file:playground/ssr-deps/ts-transpiled-exports': {} + '@vitejs/test-worker-dep-cjs@file:playground/worker/dep-cjs': {} + '@vitejs/test-worker-exports@file:playground/ssr-webworker/worker-exports': {} '@vitest/expect@2.1.5':