File tree 4 files changed +32
-1
lines changed
4 files changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -12,3 +12,9 @@ test('import.meta.env.LEGACY', async () => {
12
12
test ( 'transpiles down iterators correctly' , async ( ) => {
13
13
expect ( await page . textContent ( '#iterators' ) ) . toMatch ( 'hello' )
14
14
} )
15
+
16
+ test ( 'wraps with iife' , async ( ) => {
17
+ expect ( await page . textContent ( '#babel-helpers' ) ) . toMatch (
18
+ 'exposed babel helpers: false'
19
+ )
20
+ } )
Original file line number Diff line number Diff line change 1
1
< h1 id ="app "> </ h1 >
2
2
< div id ="env "> </ div >
3
3
< div id ="iterators "> </ div >
4
+ < div id ="babel-helpers "> </ div >
4
5
< script type ="module " src ="./main.js "> </ script >
Original file line number Diff line number Diff line change @@ -21,3 +21,10 @@ document.getElementById('env').textContent = `is legacy: ${isLegacy}`
21
21
document . getElementById ( 'iterators' ) . textContent = [ ...new Set ( [ 'hello' ] ) ] . join (
22
22
''
23
23
)
24
+
25
+ // babel-helpers
26
+
27
+ document . getElementById ( 'babel-helpers' ) . textContent =
28
+ // Using `String.raw` to inject `@babel/plugin-transform-template-literals`
29
+ // helpers.
30
+ String . raw `exposed babel helpers: ${ window . _templateObject != null } `
Original file line number Diff line number Diff line change @@ -276,7 +276,8 @@ function viteLegacyPlugin(options = {}) {
276
276
( ) => ( {
277
277
plugins : [
278
278
recordAndRemovePolyfillBabelPlugin ( legacyPolyfills ) ,
279
- replaceLegacyEnvBabelPlugin ( )
279
+ replaceLegacyEnvBabelPlugin ( ) ,
280
+ wrapIIFEBabelPlugin ( )
280
281
]
281
282
} )
282
283
] ,
@@ -600,6 +601,22 @@ function replaceLegacyEnvBabelPlugin() {
600
601
} )
601
602
}
602
603
604
+ function wrapIIFEBabelPlugin ( ) {
605
+ return ( { types : t , template } ) => {
606
+ const buildIIFE = template ( ';(function(){%%body%%})();' )
607
+
608
+ return {
609
+ name : 'vite-wrap-iife' ,
610
+ post ( { path } ) {
611
+ if ( ! this . isWrapped ) {
612
+ this . isWrapped = true
613
+ path . replaceWith ( t . program ( buildIIFE ( { body : path . node . body } ) ) )
614
+ }
615
+ }
616
+ }
617
+ }
618
+ }
619
+
603
620
module . exports = viteLegacyPlugin
604
621
605
622
viteLegacyPlugin . default = viteLegacyPlugin
You can’t perform that action at this time.
0 commit comments