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: errors in worker handling #7236

Merged
merged 33 commits into from Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
806ea51
chore(deps): update all non-major dependencies
renovate-bot Mar 8, 2022
ccc789c
test: legacy worker
poyoho Mar 9, 2022
f19b8b1
fix: worker options
poyoho Mar 9, 2022
ccbf13d
Merge branch 'vitejs:main' into main
poyoho Mar 9, 2022
bc7f9aa
Revert "chore(deps): update all non-major dependencies"
poyoho Mar 9, 2022
2690dd5
Revert "chore(deps): update all non-major dependencies"
poyoho Mar 9, 2022
ff1d716
chore: rebase
poyoho Mar 9, 2022
3070519
fix: worker options
poyoho Mar 9, 2022
c7b7809
Revert "chore(deps): update all non-major dependencies"
poyoho Mar 9, 2022
0c9e0b3
Revert "chore(deps): update all non-major dependencies"
poyoho Mar 9, 2022
1912a16
test: legacy worker
poyoho Mar 9, 2022
ab7c306
feat: ignore importAnalysis in worker
poyoho Mar 9, 2022
f3b15e9
chore: remove worker test
poyoho Mar 9, 2022
ec1e532
chore: remove test
poyoho Mar 9, 2022
897c005
chore: code style
poyoho Mar 9, 2022
6b8dad2
feat: supplementary test
poyoho Mar 9, 2022
9a8b713
refactor: isWorker move into funtion params
poyoho Mar 9, 2022
9ccc1dd
chore: code style
poyoho Mar 9, 2022
fe8ac39
Revert "refactor: isWorker move into funtion params"
poyoho Mar 10, 2022
baec513
chore: ignore import analyse in worker
poyoho Mar 10, 2022
81d07f0
fix: ignore dynamic import analyse in worker
poyoho Mar 10, 2022
5870ca2
chore: code style
poyoho Mar 10, 2022
7a4db00
feat: module import
poyoho Mar 10, 2022
71fe7b5
feat: one import module
poyoho Mar 10, 2022
a167395
chore: nice output for worker dev
poyoho Mar 11, 2022
df56fa3
refactor: worker test
poyoho Mar 16, 2022
1664dce
test: add format: es worker test
poyoho Mar 16, 2022
59d60d9
fix: test
poyoho Mar 16, 2022
7892f20
test: add es worker test
poyoho Mar 16, 2022
7764280
feat: rename the config file to vite.config.js
poyoho Mar 16, 2022
19af9e6
chore: rebase
poyoho Mar 16, 2022
f3e8b93
test: remove comment
poyoho Mar 16, 2022
0d6ebe8
feat: nice error message
poyoho Mar 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions packages/playground/worker/__tests__/worker.spec.ts
Expand Up @@ -55,7 +55,7 @@ test('worker emitted', async () => {
await untilUpdated(() => page.textContent('.nested-worker'), 'pong')
await untilUpdated(
() => page.textContent('.nested-worker-dynamic-import'),
'"msg":"pong"'
'{"msg":"module","msg1":"module1","msg2":"module2","msg3":"module3"}'
)
})

Expand All @@ -64,7 +64,7 @@ if (isBuild) {
// assert correct files
test('inlined code generation', async () => {
const files = fs.readdirSync(assetsDir)
expect(files.length).toBe(11)
expect(files.length).toBe(14)
const index = files.find((f) => f.includes('index'))
const content = fs.readFileSync(path.resolve(assetsDir, index), 'utf-8')
const worker = files.find((f) => f.includes('my-worker'))
Expand All @@ -85,7 +85,14 @@ if (isBuild) {
})
}

test('classic worker is run', async () => {
test('module worker', async () => {
expect(await page.textContent('.shared-worker-import-meta-url')).toMatch(
'A string'
)
expect(await page.textContent('.worker-import-meta-url')).toMatch('A string/')
})

test('classic worker', async () => {
expect(await page.textContent('.classic-worker')).toMatch('A classic')
expect(await page.textContent('.classic-shared-worker')).toMatch('A classic')
})
5 changes: 4 additions & 1 deletion packages/playground/worker/classic-worker.js
Expand Up @@ -3,12 +3,15 @@ function text(el, text) {
document.querySelector(el).textContent = text
}

const classicWorker = new Worker(
let classicWorker = new Worker(
new URL('./newUrl/classic-worker.js', import.meta.url) /* , */ ,
// test comment

)

// just test for case: ') ... ,' mean no worker options parmas
bluwy marked this conversation as resolved.
Show resolved Hide resolved
classicWorker = new Worker(new URL('./newUrl/classic-worker.js', import.meta.url))

classicWorker.addEventListener('message', ({ data }) => {
text('.classic-worker', data)
})
Expand Down
10 changes: 2 additions & 8 deletions packages/playground/worker/index.html
Expand Up @@ -96,10 +96,7 @@
/* @vite-ignore */ workerOptions
)
w.addEventListener('message', (ev) =>
text(
'.worker-import-meta-url',
'worker import.meta.url' + JSON.stringify(ev.data)
)
text('.worker-import-meta-url', ev.data)
)
bluwy marked this conversation as resolved.
Show resolved Hide resolved

const genWorkerName = () => 'module'
Expand All @@ -112,10 +109,7 @@
}
)
w2.port.addEventListener('message', (ev) => {
text(
'.shared-worker-import-meta-url',
'shared-worker import.meta.url' + JSON.stringify(ev.data)
)
text('.shared-worker-import-meta-url', ev.data)
})
poyoho marked this conversation as resolved.
Show resolved Hide resolved
w2.port.start()

Expand Down
1 change: 1 addition & 0 deletions packages/playground/worker/modules/module.js
@@ -0,0 +1 @@
export const msg = 'module'
1 change: 1 addition & 0 deletions packages/playground/worker/modules/module1.js
@@ -0,0 +1 @@
export const msg1 = 'module1'
3 changes: 3 additions & 0 deletions packages/playground/worker/modules/module2.js
@@ -0,0 +1,3 @@
export * from './module'
export * from './module1'
export const msg2 = 'module2'
2 changes: 2 additions & 0 deletions packages/playground/worker/modules/module3.js
@@ -0,0 +1,2 @@
export * from './module'
export const msg3 = 'module3'
6 changes: 3 additions & 3 deletions packages/playground/worker/newUrl/url-worker.js
@@ -1,3 +1,3 @@
import constant from './module'

self.postMessage(constant)
import('./module').then((module) => {
self.postMessage(module.default + import.meta.env.BASE_URL)
})
15 changes: 7 additions & 8 deletions packages/playground/worker/sub-worker.js
Expand Up @@ -3,11 +3,10 @@ self.onmessage = (event) => {
self.postMessage('pong')
}
}
const data = import('./workerImport')
data.then((data) => {
const { mode, msg } = data
self.postMessage({
mode,
msg
})
})

Promise.all([import('./modules/module2'), import('./modules/module3')]).then(
(data) => {
const _data = { ...data[0], ...data[1] }
self.postMessage(_data)
}
)
1 change: 1 addition & 0 deletions packages/vite/src/node/build.ts
Expand Up @@ -306,6 +306,7 @@ export function resolveBuildPlugins(config: ResolvedConfig): {
post: Plugin[]
} {
const options = config.build

return {
pre: [
watchPackageDataPlugin(config),
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/config.ts
Expand Up @@ -248,6 +248,7 @@ export type ResolvedConfig = Readonly<
cacheDir: string
command: 'build' | 'serve'
mode: string
isWorker: boolean
isProduction: boolean
env: Record<string, any>
resolve: ResolveOptions & {
Expand Down Expand Up @@ -474,6 +475,7 @@ export async function resolveConfig(
cacheDir,
command,
mode,
isWorker: false,
isProduction,
plugins: userPlugins,
server,
Expand Down Expand Up @@ -506,7 +508,7 @@ export async function resolveConfig(
// flat config.worker.plugin
const [workerPrePlugins, workerNormalPlugins, workerPostPlugins] =
sortUserPlugins(config.worker?.plugins as Plugin[])
const workerResolved = { ...resolved }
const workerResolved: ResolvedConfig = { ...resolved, isWorker: true }
resolved.worker.plugins = await resolvePlugins(
workerResolved,
workerPrePlugins,
Expand Down
9 changes: 7 additions & 2 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Expand Up @@ -86,6 +86,7 @@ function preload(baseModule: () => Promise<{}>, deps?: string[]) {
export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
const ssr = !!config.build.ssr
const insertPreload = !(ssr || !!config.build.lib)
const isWorker = config.isWorker

const scriptRel = config.build.polyfillModulePreload
? `'modulepreload'`
Expand Down Expand Up @@ -120,6 +121,11 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
return
}

if (isWorker) {
// preload method use `document` and can't run in the worker
return
}

await init

let imports: readonly ImportSpecifier[] = []
Expand All @@ -132,7 +138,6 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
if (!imports.length) {
return null
}

let s: MagicString | undefined
const str = () => s || (s = new MagicString(source))
let needPreloadHelper = false
Expand Down Expand Up @@ -238,7 +243,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
},

generateBundle({ format }, bundle) {
if (format !== 'es' || ssr) {
if (format !== 'es' || ssr || isWorker) {
return
}

Expand Down
6 changes: 6 additions & 0 deletions packages/vite/src/node/plugins/workerImportMetaUrl.ts
Expand Up @@ -31,8 +31,14 @@ function getWorkerType(
}
const endIndex = noCommentsCode.indexOf(')', i)

// case: ') ... ,' mean no worker options params
if (commaIndex > endIndex) {
return 'classic'
}

// need to find in comment code
let workerOptsString = code.substring(commaIndex + 1, endIndex)

const hasViteIgnore = /\/\*\s*@vite-ignore\s*\*\//.test(workerOptsString)
if (hasViteIgnore) {
return 'ignore'
Expand Down