File tree Expand file tree Collapse file tree 4 files changed +32
-1
lines changed
Expand file tree Collapse file tree 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 () => {
1212test ( 'transpiles down iterators correctly' , async ( ) => {
1313 expect ( await page . textContent ( '#iterators' ) ) . toMatch ( 'hello' )
1414} )
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 11< h1 id ="app "> </ h1 >
22< div id ="env "> </ div >
33< div id ="iterators "> </ div >
4+ < div id ="babel-helpers "> </ div >
45< 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}`
2121document . getElementById ( 'iterators' ) . textContent = [ ...new Set ( [ 'hello' ] ) ] . join (
2222 ''
2323)
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 = {}) {
276276 ( ) => ( {
277277 plugins : [
278278 recordAndRemovePolyfillBabelPlugin ( legacyPolyfills ) ,
279- replaceLegacyEnvBabelPlugin ( )
279+ replaceLegacyEnvBabelPlugin ( ) ,
280+ wrapIIFEBabelPlugin ( )
280281 ]
281282 } )
282283 ] ,
@@ -600,6 +601,22 @@ function replaceLegacyEnvBabelPlugin() {
600601 } )
601602}
602603
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+
603620module . exports = viteLegacyPlugin
604621
605622viteLegacyPlugin . default = viteLegacyPlugin
You can’t perform that action at this time.
0 commit comments