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): fix applyToEnvironment hooks on worker build #18793

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 5 additions & 4 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResolvedConfig>
rollupOptions: RollupOptions
}

Expand Down Expand Up @@ -1340,7 +1338,10 @@ export async function resolveConfig(
.map((hook) => hook(workerResolved)),
)

return { plugins: resolvedWorkerPlugins, config: workerResolved }
return {
...workerResolved,
plugins: resolvedWorkerPlugins,
}
}

const resolvedWorkerOptions: ResolvedWorkerOptions = {
Expand Down
7 changes: 4 additions & 3 deletions packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC this line is the main fix, and the rest are refactors?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also updating config with new plugins like { ...config, plugins: resolvedPlugins } is needed before init.


const bundle = await rollup({
...rollupOptions,
input,
plugins: resolvedPlugins.map((p) =>
plugins: workerEnvironment.plugins.map((p) =>
injectEnvironmentToHooks(workerEnvironment, p),
),
onwarn(warning, warn) {
Expand Down
1 change: 1 addition & 0 deletions playground/worker/__tests__/es/worker-es.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
2 changes: 1 addition & 1 deletion playground/worker/__tests__/iife/worker-iife.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"`,
)
})

Expand Down
1 change: 1 addition & 0 deletions playground/worker/dep-cjs/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.test = '[cjs ok]'
5 changes: 5 additions & 0 deletions playground/worker/dep-cjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@vitejs/test-worker-dep-cjs",
"type": "commonjs",
"exports": "./index.cjs"
}
1 change: 1 addition & 0 deletions playground/worker/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ <h2 class="format-iife">format iife:</h2>
<div>mode: <span class="mode"></span></div>
<div>bundle-with-plugin: <span class="bundle-with-plugin"></span></div>
<div>asset-url: <span class="asset-url"></span></div>
<div>dep-cjs: <span class="dep-cjs"></span></div>
</div>

<p>
Expand Down
13 changes: 12 additions & 1 deletion playground/worker/my-worker.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
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'
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({
Expand All @@ -16,6 +25,7 @@ self.onmessage = (e) => {
viteSvg,
metaUrl,
name,
depCjs,
})
}
}
Expand All @@ -27,6 +37,7 @@ self.postMessage({
viteSvg,
metaUrl,
name,
depCjs,
})

// for sourcemap
Expand Down
3 changes: 2 additions & 1 deletion playground/worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
1 change: 1 addition & 0 deletions playground/worker/worker/main-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.