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: legacy no emit worker #9500

Merged
merged 8 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
19 changes: 12 additions & 7 deletions packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,19 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
return result()
}
}
if (!isWorker) {
const workerMap = workerCache.get(config)!
workerMap.assets.forEach((asset) => {
this.emitFile(asset)
workerMap.assets.delete(asset.fileName!)
})
}
return result()
},

generateBundle(opts) {
poyoho marked this conversation as resolved.
Show resolved Hide resolved
// @ts-ignore 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!)
})
}
}
}
16 changes: 10 additions & 6 deletions playground/legacy/__tests__/legacy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@ import {
untilUpdated
} from '~utils'

test('should load the worker', async () => {
await untilUpdated(() => page.textContent('.worker-message'), 'module')
})

test('should work', async () => {
expect(await page.textContent('#app')).toMatch('Hello')
await untilUpdated(() => page.textContent('#app'), 'Hello')
})

test('import.meta.env.LEGACY', async () => {
expect(await page.textContent('#env')).toMatch(isBuild ? 'true' : 'false')
await untilUpdated(() => page.textContent('#env'), isBuild ? 'true' : 'false')
})

// https://github.com/vitejs/vite/issues/3400
test('transpiles down iterators correctly', async () => {
expect(await page.textContent('#iterators')).toMatch('hello')
await untilUpdated(() => page.textContent('#iterators'), 'hello')
})

test('wraps with iife', async () => {
expect(await page.textContent('#babel-helpers')).toMatch(
await untilUpdated(
() => page.textContent('#babel-helpers'),
'exposed babel helpers: false'
)
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved
})
Expand All @@ -48,8 +53,7 @@ test('generates assets', async () => {
'immutable-chunk: 404',
'immutable-chunk-legacy: 404',
'polyfills-legacy: 404'
].join('\n'),
true
].join('\n')
poyoho marked this conversation as resolved.
Show resolved Hide resolved
)
})

Expand Down
3 changes: 3 additions & 0 deletions playground/legacy/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ <h1 id="app"></h1>
<div id="assets"></div>
<button id="dynamic-css-button">dynamic css</button>
<div id="dynamic-css"></div>

<p>## worker message:</p>
<div class="worker-message"></div>
<script type="module" src="./main.js"></script>
7 changes: 7 additions & 0 deletions playground/legacy/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import './style.css'
import './vite.svg'
import MyWorker from './worker?worker'

async function run() {
const { fn } = await import('./async.js')
Expand Down Expand Up @@ -54,3 +55,9 @@ document
function text(el, text) {
document.querySelector(el).textContent = text
}

const worker = new MyWorker()
worker.postMessage('ping')
worker.addEventListener('message', (ev) => {
text('.worker-message', JSON.stringify(ev.data))
})
1 change: 1 addition & 0 deletions playground/legacy/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const module = 'module'
5 changes: 5 additions & 0 deletions playground/legacy/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { module } from './module'

self.onmessage = () => {
self.postMessage(module)
}