Skip to content

Commit e7fc006

Browse files
committed
fix: worker resolve
1 parent 6cae00e commit e7fc006

File tree

2 files changed

+115
-102
lines changed

2 files changed

+115
-102
lines changed

packages/vite/src/node/plugins/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export async function resolvePlugins(
107107
optimizeDeps: true,
108108
externalize: true,
109109
},
110-
isWorker,
110+
isWorker ? config : undefined,
111111
)
112112
: [
113113
resolvePlugin({

packages/vite/src/node/plugins/resolve.ts

Lines changed: 114 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
} from '../utils'
3737
import { optimizedDepInfoFromFile, optimizedDepInfoFromId } from '../optimizer'
3838
import type { DepsOptimizer } from '../optimizer'
39-
import type { Environment, SSROptions } from '..'
39+
import type { Environment, ResolvedConfig, SSROptions } from '..'
4040
import type { PackageCache, PackageData } from '../packages'
4141
import { canExternalizeFile, shouldExternalize } from '../external'
4242
import {
@@ -52,7 +52,7 @@ import {
5252
splitFileAndPostfix,
5353
withTrailingSlash,
5454
} from '../../shared/utils'
55-
import type { PartialEnvironment } from '../baseEnvironment'
55+
import type { ResolvedEnvironmentOptions } from '../config'
5656

5757
const normalizedClientEntry = normalizePath(CLIENT_ENTRY)
5858
const normalizedEnvEntry = normalizePath(ENV_ENTRY)
@@ -182,118 +182,131 @@ export interface ResolvePluginOptionsWithOverrides
182182

183183
const perEnvironmentOrWorkerPlugin = (
184184
name: string,
185-
isWorker: boolean,
186-
f: (env: PartialEnvironment | undefined) => Plugin,
185+
configIfWorker: ResolvedConfig | undefined,
186+
f: (env: {
187+
name: string
188+
config: ResolvedConfig & ResolvedEnvironmentOptions
189+
}) => Plugin,
187190
): Plugin => {
188-
if (isWorker) {
189-
return f(undefined)
191+
if (configIfWorker) {
192+
return f({
193+
name: 'client',
194+
config: { ...configIfWorker, consumer: 'client' },
195+
})
190196
}
191197
return perEnvironmentPlugin(name, f)
192198
}
193199

194200
export function oxcResolvePlugin(
195201
resolveOptions: ResolvePluginOptionsWithOverrides,
196-
isWorker: boolean,
202+
configIfWorker: ResolvedConfig | undefined,
197203
): (RolldownPlugin | Plugin)[] {
198204
return [
199205
optimizerResolvePlugin(resolveOptions),
200206
importGlobSubpathImportsResolvePlugin(resolveOptions),
201-
perEnvironmentOrWorkerPlugin('vite:resolve-builtin', isWorker, (env) => {
202-
const environment = env as Environment
203-
// The resolve plugin is used for createIdResolver and the depsOptimizer should be
204-
// disabled in that case, so deps optimization is opt-in when creating the plugin.
205-
const depsOptimizer =
206-
resolveOptions.optimizeDeps && environment.mode === 'dev'
207-
? environment.depsOptimizer
208-
: undefined
207+
perEnvironmentOrWorkerPlugin(
208+
'vite:resolve-builtin',
209+
configIfWorker,
210+
(env) => {
211+
const environment = env as Environment
212+
// The resolve plugin is used for createIdResolver and the depsOptimizer should be
213+
// disabled in that case, so deps optimization is opt-in when creating the plugin.
214+
const depsOptimizer =
215+
resolveOptions.optimizeDeps && environment?.mode === 'dev'
216+
? environment.depsOptimizer
217+
: undefined
209218

210-
const options: InternalResolveOptions = {
211-
...environment.config.resolve,
212-
...resolveOptions, // plugin options + resolve options overrides
213-
}
214-
const noExternal =
215-
Array.isArray(options.noExternal) || options.noExternal === true
216-
? options.noExternal
217-
: [options.noExternal]
218-
if (
219-
Array.isArray(noExternal) &&
220-
noExternal.some((e) => typeof e !== 'string')
221-
) {
222-
throw new Error('RegExp is not supported for noExternal for now')
223-
}
224-
const filteredNoExternal = noExternal as true | string[]
225-
226-
return viteResolvePlugin({
227-
resolveOptions: {
228-
isBuild: options.isBuild,
229-
isProduction: options.isProduction,
230-
asSrc: options.asSrc ?? false,
231-
preferRelative: options.preferRelative ?? false,
232-
root: options.root,
233-
scan: options.scan ?? false,
234-
235-
mainFields: options.mainFields,
236-
conditions: options.conditions,
237-
externalConditions: options.externalConditions,
238-
extensions: options.extensions,
239-
tryIndex: options.tryIndex ?? true,
240-
tryPrefix: options.tryPrefix,
241-
preserveSymlinks: options.preserveSymlinks,
242-
},
243-
environmentConsumer: environment.config.consumer,
244-
environmentName: environment.name,
245-
external: options.external,
246-
noExternal: filteredNoExternal,
247-
finalizeBareSpecifier: !depsOptimizer
248-
? undefined
249-
: (resolvedId, rawId, importer) => {
250-
// if we reach here, it's a valid dep import that hasn't been optimized.
251-
const isJsType = isOptimizable(resolvedId, depsOptimizer.options)
252-
const exclude = depsOptimizer?.options.exclude
253-
254-
// check for deep import, e.g. "my-lib/foo"
255-
const deepMatch = deepImportRE.exec(rawId)
256-
// package name doesn't include postfixes
257-
// trim them to support importing package with queries (e.g. `import css from 'normalize.css?inline'`)
258-
const pkgId = deepMatch
259-
? deepMatch[1] || deepMatch[2]
260-
: cleanUrl(rawId)
261-
262-
const skipOptimization =
263-
depsOptimizer.options.noDiscovery ||
264-
!isJsType ||
265-
(importer && isInNodeModules(importer)) ||
266-
exclude?.includes(pkgId) ||
267-
exclude?.includes(rawId) ||
268-
SPECIAL_QUERY_RE.test(resolvedId)
269-
270-
let newId = resolvedId
271-
if (skipOptimization) {
272-
// excluded from optimization
273-
// Inject a version query to npm deps so that the browser
274-
// can cache it without re-validation, but only do so for known js types.
275-
// otherwise we may introduce duplicated modules for externalized files
276-
// from pre-bundled deps.
277-
const versionHash = depsOptimizer!.metadata.browserHash
278-
if (versionHash && isJsType) {
279-
newId = injectQuery(newId, `v=${versionHash}`)
280-
}
281-
} else {
282-
// this is a missing import, queue optimize-deps re-run and
283-
// get a resolved its optimized info
284-
const optimizedInfo = depsOptimizer!.registerMissingImport(
285-
rawId,
286-
newId,
219+
const options: InternalResolveOptions = {
220+
...environment.config.resolve,
221+
...resolveOptions, // plugin options + resolve options overrides
222+
}
223+
const noExternal =
224+
Array.isArray(options.noExternal) || options.noExternal === true
225+
? options.noExternal
226+
: [options.noExternal]
227+
if (
228+
Array.isArray(noExternal) &&
229+
noExternal.some((e) => typeof e !== 'string')
230+
) {
231+
throw new Error('RegExp is not supported for noExternal for now')
232+
}
233+
const filteredNoExternal = noExternal as true | string[]
234+
235+
return viteResolvePlugin({
236+
resolveOptions: {
237+
isBuild: options.isBuild,
238+
isProduction: options.isProduction,
239+
asSrc: options.asSrc ?? false,
240+
preferRelative: options.preferRelative ?? false,
241+
root: options.root,
242+
scan: options.scan ?? false,
243+
244+
mainFields: options.mainFields,
245+
conditions: options.conditions,
246+
externalConditions: options.externalConditions,
247+
extensions: options.extensions,
248+
tryIndex: options.tryIndex ?? true,
249+
tryPrefix: options.tryPrefix,
250+
preserveSymlinks: options.preserveSymlinks,
251+
},
252+
environmentConsumer: environment.config.consumer,
253+
environmentName: environment.name,
254+
external: options.external,
255+
noExternal: filteredNoExternal,
256+
finalizeBareSpecifier: !depsOptimizer
257+
? undefined
258+
: (resolvedId, rawId, importer) => {
259+
// if we reach here, it's a valid dep import that hasn't been optimized.
260+
const isJsType = isOptimizable(
261+
resolvedId,
262+
depsOptimizer.options,
287263
)
288-
newId = depsOptimizer!.getOptimizedDepId(optimizedInfo)
289-
}
290-
return newId
291-
},
292-
finalizeOtherSpecifiers(resolvedId, rawId) {
293-
return ensureVersionQuery(resolvedId, rawId, options, depsOptimizer)
294-
},
295-
}) as unknown as Plugin
296-
}),
264+
const exclude = depsOptimizer?.options.exclude
265+
266+
// check for deep import, e.g. "my-lib/foo"
267+
const deepMatch = deepImportRE.exec(rawId)
268+
// package name doesn't include postfixes
269+
// trim them to support importing package with queries (e.g. `import css from 'normalize.css?inline'`)
270+
const pkgId = deepMatch
271+
? deepMatch[1] || deepMatch[2]
272+
: cleanUrl(rawId)
273+
274+
const skipOptimization =
275+
depsOptimizer.options.noDiscovery ||
276+
!isJsType ||
277+
(importer && isInNodeModules(importer)) ||
278+
exclude?.includes(pkgId) ||
279+
exclude?.includes(rawId) ||
280+
SPECIAL_QUERY_RE.test(resolvedId)
281+
282+
let newId = resolvedId
283+
if (skipOptimization) {
284+
// excluded from optimization
285+
// Inject a version query to npm deps so that the browser
286+
// can cache it without re-validation, but only do so for known js types.
287+
// otherwise we may introduce duplicated modules for externalized files
288+
// from pre-bundled deps.
289+
const versionHash = depsOptimizer!.metadata.browserHash
290+
if (versionHash && isJsType) {
291+
newId = injectQuery(newId, `v=${versionHash}`)
292+
}
293+
} else {
294+
// this is a missing import, queue optimize-deps re-run and
295+
// get a resolved its optimized info
296+
const optimizedInfo = depsOptimizer!.registerMissingImport(
297+
rawId,
298+
newId,
299+
)
300+
newId = depsOptimizer!.getOptimizedDepId(optimizedInfo)
301+
}
302+
return newId
303+
},
304+
finalizeOtherSpecifiers(resolvedId, rawId) {
305+
return ensureVersionQuery(resolvedId, rawId, options, depsOptimizer)
306+
},
307+
}) as unknown as Plugin
308+
},
309+
),
297310
]
298311
}
299312

0 commit comments

Comments
 (0)