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 16 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
2 changes: 1 addition & 1 deletion packages/playground/worker/__tests__/worker.spec.ts
Expand Up @@ -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(12)
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 Down
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
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)
})
5 changes: 3 additions & 2 deletions packages/vite/src/node/build.ts
Expand Up @@ -306,6 +306,7 @@ export function resolveBuildPlugins(config: ResolvedConfig): {
post: Plugin[]
} {
const options = config.build
const isWorker = config.isWorker
Copy link
Member

@patak-dev patak-dev Mar 9, 2022

Choose a reason for hiding this comment

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

I think we should pass isWorker as a param to resolveBuildPlugins instead. Adding it to the config will make it visible to the final user

return {
pre: [
watchPackageDataPlugin(config),
Expand All @@ -319,14 +320,14 @@ export function resolveBuildPlugins(config: ResolvedConfig): {
: [])
],
post: [
buildImportAnalysisPlugin(config),
!isWorker && buildImportAnalysisPlugin(config),
buildEsbuildPlugin(config),
...(options.minify ? [terserPlugin(config)] : []),
...(options.manifest ? [manifestPlugin(config)] : []),
...(options.ssrManifest ? [ssrManifestPlugin(config)] : []),
bluwy marked this conversation as resolved.
Show resolved Hide resolved
buildReporterPlugin(config),
loadFallbackPlugin()
]
].filter(Boolean) as Plugin[]
}
}

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
6 changes: 5 additions & 1 deletion packages/vite/src/node/plugins/index.ts
Expand Up @@ -26,6 +26,7 @@ export async function resolvePlugins(
postPlugins: Plugin[]
): Promise<Plugin[]> {
const isBuild = config.command === 'build'
const isWorker = config.isWorker

const buildPlugins = isBuild
? (await import('../build')).resolveBuildPlugins(config)
Expand Down Expand Up @@ -73,6 +74,9 @@ export async function resolvePlugins(
// internal server-only plugins are always applied after everything else
...(isBuild
? []
: [clientInjectionsPlugin(config), importAnalysisPlugin(config)])
: [
clientInjectionsPlugin(config),
!isWorker && importAnalysisPlugin(config)
])
].filter(Boolean) as Plugin[]
}
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 parmas
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