Skip to content

Commit 095eb4f

Browse files
committed
fix: Rewrite to support older MagicString version
1 parent f815a01 commit 095eb4f

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/index.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -654,17 +654,32 @@ function createConfig(options, entry, format, writeMeta) {
654654
// So we remove the globalThis check, replacing it with `this||self` to match Rollup 1's output:
655655
renderChunk(code, chunk, opts) {
656656
if (opts.format === 'umd') {
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
657659
const s = new MagicString(code);
658-
// minified:
659-
s.replace(
660+
661+
const minified = code.match(
660662
/([a-zA-Z$_]+)="undefined"!=typeof globalThis\?globalThis:(\1\|\|self)/,
661-
'$2',
662663
);
663-
// unminified:
664-
s.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(
665673
/(global *= *)typeof +globalThis *!== *['"]undefined['"] *\? *globalThis *: *(global *\|\| *self)/,
666-
'$1$2',
667674
);
675+
if (unminified) {
676+
s.overwrite(
677+
unminified.index,
678+
unminified.index + unminified[0].length,
679+
unminified[1] + unminified[2],
680+
);
681+
}
682+
668683
return {
669684
code: s.toString(),
670685
map: s.generateMap({ hires: true }),

0 commit comments

Comments
 (0)