2
2
const path = require ( 'path' )
3
3
const { createHash } = require ( 'crypto' )
4
4
const { build } = require ( 'vite' )
5
+ const MagicString = require ( 'magic-string' ) . default
5
6
6
7
// lazy load babel since it's not used during dev
7
8
let babel
@@ -17,6 +18,8 @@ const safari10NoModuleFix = `!function(){var e=document,t=e.createElement("scrip
17
18
const legacyEntryId = 'vite-legacy-entry'
18
19
const systemJSInlineCode = `System.import(document.getElementById('${ legacyEntryId } ').getAttribute('data-src'))`
19
20
21
+ const legacyEnvVarMarker = `__VITE_IS_LEGACY__`
22
+
20
23
/**
21
24
* @param {import('.').Options } options
22
25
* @returns {import('vite').Plugin[] }
@@ -184,6 +187,28 @@ function viteLegacyPlugin(options = {}) {
184
187
// analyze and record modern polyfills
185
188
detectPolyfills ( raw , { esmodules : true } , modernPolyfills )
186
189
}
190
+
191
+ if ( raw . includes ( legacyEnvVarMarker ) ) {
192
+ const re = new RegExp ( `"${ legacyEnvVarMarker } "` , 'g' )
193
+ if ( config . build . sourcemap ) {
194
+ const s = new MagicString ( raw )
195
+ let match
196
+ while ( ( match = re . exec ( raw ) ) ) {
197
+ s . overwrite (
198
+ match . index ,
199
+ match . index + legacyEnvVarMarker . length + 2 ,
200
+ `false`
201
+ )
202
+ }
203
+ return {
204
+ code : s . toString ( ) ,
205
+ map : s . generateMap ( { hires : true } )
206
+ }
207
+ } else {
208
+ return raw . replace ( re , `false` )
209
+ }
210
+ }
211
+
187
212
return null
188
213
}
189
214
@@ -210,7 +235,10 @@ function viteLegacyPlugin(options = {}) {
210
235
// preset so we can catch the injected import statements...
211
236
[
212
237
( ) => ( {
213
- plugins : [ recordAndRemovePolyfillBabelPlugin ( legacyPolyfills ) ]
238
+ plugins : [
239
+ recordAndRemovePolyfillBabelPlugin ( legacyPolyfills ) ,
240
+ replaceLegacyEnvBabelPlugin ( )
241
+ ]
214
242
} )
215
243
] ,
216
244
[
@@ -339,7 +367,37 @@ function viteLegacyPlugin(options = {}) {
339
367
}
340
368
}
341
369
342
- return [ legacyGenerateBundlePlugin , legacyPostPlugin ]
370
+ let envInjectionFaled = false
371
+ /**
372
+ * @type {import('vite').Plugin }
373
+ */
374
+ const legacyEnvPlugin = {
375
+ name : 'legacy-env' ,
376
+
377
+ config ( _ , env ) {
378
+ if ( env ) {
379
+ return {
380
+ define : {
381
+ 'import.meta.env.LEGACY' :
382
+ env . command === 'serve' ? false : legacyEnvVarMarker
383
+ }
384
+ }
385
+ } else {
386
+ envInjectionFaled = true
387
+ }
388
+ } ,
389
+
390
+ configResolved ( config ) {
391
+ if ( envInjectionFaled ) {
392
+ config . logger . warn (
393
+ `[@vitejs/plugin-legacy] import.meta.env.LEGACY was not injected due ` +
394
+ `to incompatible vite version (requires vite@^2.0.0-beta.69).`
395
+ )
396
+ }
397
+ }
398
+ }
399
+
400
+ return [ legacyGenerateBundlePlugin , legacyPostPlugin , legacyEnvPlugin ]
343
401
}
344
402
345
403
/**
@@ -488,6 +546,19 @@ function recordAndRemovePolyfillBabelPlugin(polyfills) {
488
546
} )
489
547
}
490
548
549
+ function replaceLegacyEnvBabelPlugin ( ) {
550
+ return ( { types : t } ) => ( {
551
+ name : 'vite-replace-env-legacy' ,
552
+ visitor : {
553
+ StringLiteral ( path ) {
554
+ if ( path . node . value === legacyEnvVarMarker ) {
555
+ path . replaceWith ( t . booleanLiteral ( true ) )
556
+ }
557
+ }
558
+ }
559
+ } )
560
+ }
561
+
491
562
module . exports = viteLegacyPlugin
492
563
493
564
viteLegacyPlugin . default = viteLegacyPlugin
0 commit comments