@@ -10,7 +10,6 @@ const isNonJsRequest = (request: string): boolean => nonJsRe.test(request)
1010const escapedDotRE = / (?< ! \\ ) \\ ./ g
1111
1212export function definePlugin ( config : ResolvedConfig ) : Plugin {
13- const isBundled = config . isBundled
1413 const isBuild = config . command === 'build'
1514 const isBuildLib = isBuild && config . build . lib
1615
@@ -28,27 +27,28 @@ export function definePlugin(config: ResolvedConfig): Plugin {
2827 } )
2928 }
3029
31- // during dev, import.meta properties are handled by importAnalysis plugin.
32- const importMetaKeys : Record < string , string > = { }
33- const importMetaEnvKeys : Record < string , string > = { }
34- const importMetaFallbackKeys : Record < string , string > = { }
35- if ( isBuild ) {
36- importMetaKeys [ 'import.meta.hot' ] = `undefined`
37- }
38- if ( isBundled ) {
39- for ( const key in config . env ) {
40- const val = JSON . stringify ( config . env [ key ] )
41- importMetaKeys [ `import.meta.env.${ key } ` ] = val
42- importMetaEnvKeys [ key ] = val
43- }
44- // these will be set to a proper value in `generatePattern`
45- importMetaKeys [ 'import.meta.env.SSR' ] = `undefined`
46- importMetaFallbackKeys [ 'import.meta.env' ] = `undefined`
47- }
48-
4930 function generatePattern ( environment : Environment ) {
31+ const isBundled = environment . config . isBundled
5032 const keepProcessEnv = environment . config . keepProcessEnv
5133
34+ // during dev, import.meta properties are handled by importAnalysis plugin.
35+ const importMetaKeys : Record < string , string > = { }
36+ const importMetaEnvKeys : Record < string , string > = { }
37+ const importMetaFallbackKeys : Record < string , string > = { }
38+ if ( isBuild ) {
39+ importMetaKeys [ 'import.meta.hot' ] = `undefined`
40+ }
41+ if ( isBundled ) {
42+ for ( const key in config . env ) {
43+ const val = JSON . stringify ( config . env [ key ] )
44+ importMetaKeys [ `import.meta.env.${ key } ` ] = val
45+ importMetaEnvKeys [ key ] = val
46+ }
47+ // these will be set to a proper value below
48+ importMetaKeys [ 'import.meta.env.SSR' ] = `undefined`
49+ importMetaFallbackKeys [ 'import.meta.env' ] = `undefined`
50+ }
51+
5252 const userDefine : Record < string , string > = { }
5353 const userDefineEnv : Record < string , any > = { }
5454 for ( const key in environment . config . define ) {
@@ -113,24 +113,27 @@ export function definePlugin(config: ResolvedConfig): Plugin {
113113 return pattern
114114 }
115115
116- if ( isBundled ) {
117- return {
118- name : 'vite:define' ,
119- options ( option ) {
120- const [ define , _pattern , importMetaEnvVal ] = getPattern (
121- this . environment ,
122- )
123- define [ 'import.meta.env' ] = importMetaEnvVal
124- define [ 'import.meta.env.*' ] = 'undefined'
125- option . transform ??= { }
126- option . transform . define = { ...option . transform . define , ...define }
127- } ,
128- }
129- }
130-
131116 return {
132117 name : 'vite:define' ,
133118
119+ applyToEnvironment ( environment ) {
120+ if ( environment . config . isBundled ) {
121+ return {
122+ name : 'vite:define' ,
123+ options ( option ) {
124+ const [ define , _pattern , importMetaEnvVal ] = getPattern (
125+ this . environment ,
126+ )
127+ define [ 'import.meta.env' ] = importMetaEnvVal
128+ define [ 'import.meta.env.*' ] = 'undefined'
129+ option . transform ??= { }
130+ option . transform . define = { ...option . transform . define , ...define }
131+ } ,
132+ }
133+ }
134+ return true
135+ } ,
136+
134137 transform : {
135138 handler ( code , id ) {
136139 if ( this . environment . config . consumer === 'client' ) {
0 commit comments