@@ -39,6 +39,7 @@ import {
3939import { getConfigFromPkgJson , getName } from './lib/package-info' ;
4040import { shouldCssModules , cssModulesConfig } from './lib/css-modules' ;
4141import { EOL } from 'os' ;
42+ import MagicString from 'magic-string' ;
4243
4344// Extensions to use when resolving modules
4445const EXTENSIONS = [ '.ts' , '.tsx' , '.js' , '.jsx' , '.es6' , '.es' , '.mjs' ] ;
@@ -653,17 +654,36 @@ function createConfig(options, entry, format, writeMeta) {
653654 // So we remove the globalThis check, replacing it with `this||self` to match Rollup 1's output:
654655 renderChunk ( code , chunk , opts ) {
655656 if ( opts . format === 'umd' ) {
656- // minified:
657- code = code . replace (
657+ // Can swap this out with MagicString.replace() when we bump it:
658+ // https://github.com/developit/microbundle/blob/f815a01cb63d90b9f847a4dcad2a64e6b2f8596f/src/index.js#L657-L671
659+ const s = new MagicString ( code ) ;
660+
661+ const minified = code . match (
658662 / ( [ a - z A - Z $ _ ] + ) = " u n d e f i n e d " ! = t y p e o f g l o b a l T h i s \? g l o b a l T h i s : ( \1\| \| s e l f ) / ,
659- '$2' ,
660663 ) ;
661- // unminified:
662- code = code . replace (
664+ if ( minified ) {
665+ s . overwrite (
666+ minified . index ,
667+ minified . index + minified [ 0 ] . length ,
668+ minified [ 2 ] ,
669+ ) ;
670+ }
671+
672+ const unminified = code . match (
663673 / ( g l o b a l * = * ) t y p e o f + g l o b a l T h i s * ! = = * [ ' " ] u n d e f i n e d [ ' " ] * \? * g l o b a l T h i s * : * ( g l o b a l * \| \| * s e l f ) / ,
664- '$1$2' ,
665674 ) ;
666- return { code, map : null } ;
675+ if ( unminified ) {
676+ s . overwrite (
677+ unminified . index ,
678+ unminified . index + unminified [ 0 ] . length ,
679+ unminified [ 1 ] + unminified [ 2 ] ,
680+ ) ;
681+ }
682+
683+ return {
684+ code : s . toString ( ) ,
685+ map : s . generateMap ( { hires : true } ) ,
686+ } ;
667687 }
668688 } ,
669689 // Grab size info before writing files to disk:
0 commit comments