@@ -35,9 +35,13 @@ import {
35
35
safeRealpathSync ,
36
36
tryStatSync ,
37
37
} from '../utils'
38
- import { optimizedDepInfoFromFile , optimizedDepInfoFromId } from '../optimizer'
38
+ import {
39
+ isDepOptimizationDisabled ,
40
+ optimizedDepInfoFromFile ,
41
+ optimizedDepInfoFromId ,
42
+ } from '../optimizer'
39
43
import type { DepsOptimizer } from '../optimizer'
40
- import { type Environment , perEnvironmentPlugin } from '..'
44
+ import type { Environment } from '..'
41
45
import type { PackageCache , PackageData } from '../packages'
42
46
import { canExternalizeFile , shouldExternalize } from '../external'
43
47
import {
@@ -168,15 +172,42 @@ export interface ResolvePluginOptionsWithOverrides
168
172
const perEnvironmentOrWorkerPlugin = (
169
173
name : string ,
170
174
overrideEnvConfig : ( ResolvedConfig & ResolvedEnvironmentOptions ) | undefined ,
171
- f : ( env : {
172
- name : string
173
- config : ResolvedConfig & ResolvedEnvironmentOptions
174
- } ) => Plugin ,
175
- ) : Plugin => {
175
+ f : (
176
+ env : {
177
+ name : string
178
+ config : ResolvedConfig & ResolvedEnvironmentOptions
179
+ } ,
180
+ getEnvironment : ( ) => Environment ,
181
+ ) => Plugin ,
182
+ ) : Plugin [ ] => {
183
+ const envs : Record < string , Environment > = { }
184
+ const getEnvironmentPlugin : Plugin = {
185
+ name : `${ name } :get-environment` ,
186
+ buildStart ( ) {
187
+ envs [ this . environment . name ] = this . environment
188
+ } ,
189
+ perEnvironmentStartEndDuringDev : true ,
190
+ }
191
+ const createGetEnvironment = ( name : string ) => ( ) => envs [ name ]
192
+
176
193
if ( overrideEnvConfig ) {
177
- return f ( { name : 'client' , config : overrideEnvConfig } )
194
+ return [
195
+ getEnvironmentPlugin ,
196
+ f (
197
+ { name : 'client' , config : overrideEnvConfig } ,
198
+ createGetEnvironment ( 'client' ) ,
199
+ ) ,
200
+ ]
178
201
}
179
- return perEnvironmentPlugin ( name , f )
202
+ return [
203
+ getEnvironmentPlugin ,
204
+ {
205
+ name,
206
+ applyToEnvironment ( environment ) {
207
+ return f ( environment , createGetEnvironment ( environment . name ) )
208
+ } ,
209
+ } ,
210
+ ]
180
211
}
181
212
182
213
export function oxcResolvePlugin (
@@ -188,20 +219,27 @@ export function oxcResolvePlugin(
188
219
? [ optimizerResolvePlugin ( resolveOptions ) ]
189
220
: [ ] ) ,
190
221
importGlobSubpathImportsResolvePlugin ( resolveOptions ) ,
191
- perEnvironmentOrWorkerPlugin (
222
+ ... perEnvironmentOrWorkerPlugin (
192
223
'vite:resolve-builtin' ,
193
224
overrideEnvConfig ,
194
- ( env ) => {
195
- const environment = env as Environment
225
+ ( partialEnv , getEnv ) => {
196
226
// The resolve plugin is used for createIdResolver and the depsOptimizer should be
197
227
// disabled in that case, so deps optimization is opt-in when creating the plugin.
198
- const depsOptimizer =
199
- resolveOptions . optimizeDeps && environment ?. mode === 'dev'
200
- ? environment . depsOptimizer
201
- : undefined
228
+ const depsOptimizerEnabled =
229
+ resolveOptions . optimizeDeps &&
230
+ ! resolveOptions . isBuild &&
231
+ ! isDepOptimizationDisabled ( partialEnv . config . optimizeDeps )
232
+ const getDepsOptimizer = ( ) => {
233
+ const env = getEnv ( )
234
+ if ( env . mode !== 'dev' )
235
+ throw new Error ( 'The environment mode should be dev' )
236
+ if ( ! env . depsOptimizer )
237
+ throw new Error ( 'The environment should have a depsOptimizer' )
238
+ return env . depsOptimizer
239
+ }
202
240
203
241
const options : InternalResolveOptions = {
204
- ...environment . config . resolve ,
242
+ ...partialEnv . config . resolve ,
205
243
...resolveOptions , // plugin options + resolve options overrides
206
244
}
207
245
const noExternal =
@@ -229,15 +267,16 @@ export function oxcResolvePlugin(
229
267
tryPrefix : options . tryPrefix ,
230
268
preserveSymlinks : options . preserveSymlinks ,
231
269
} ,
232
- environmentConsumer : environment . config . consumer ,
233
- environmentName : environment . name ,
234
- builtins : environment . config . resolve . builtins ,
270
+ environmentConsumer : partialEnv . config . consumer ,
271
+ environmentName : partialEnv . name ,
272
+ builtins : partialEnv . config . resolve . builtins ,
235
273
external : options . external ,
236
274
noExternal : noExternal ,
237
275
dedupe : options . dedupe ,
238
- finalizeBareSpecifier : ! depsOptimizer
276
+ finalizeBareSpecifier : ! depsOptimizerEnabled
239
277
? undefined
240
278
: ( resolvedId , rawId , importer ) => {
279
+ const depsOptimizer = getDepsOptimizer ( )
241
280
// if we reach here, it's a valid dep import that hasn't been optimized.
242
281
const isJsType = isOptimizable (
243
282
resolvedId ,
@@ -283,9 +322,10 @@ export function oxcResolvePlugin(
283
322
}
284
323
return newId
285
324
} ,
286
- finalizeOtherSpecifiers : ! depsOptimizer
325
+ finalizeOtherSpecifiers : ! depsOptimizerEnabled
287
326
? undefined
288
327
: ( resolvedId , rawId ) => {
328
+ const depsOptimizer = getDepsOptimizer ( )
289
329
const newResolvedId = ensureVersionQuery (
290
330
resolvedId ,
291
331
rawId ,
0 commit comments