Skip to content

Commit aae1611

Browse files
authored
fix: Incorrect UMD sourcemaps (#1043)
1 parent 4b11285 commit aae1611

File tree

6 files changed

+51
-20
lines changed

6 files changed

+51
-20
lines changed

.changeset/violet-falcons-dream.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'microbundle': patch
3+
---
4+
5+
Fixes positioning bug with UMD sourcemaps

.github/workflows/size.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ jobs:
2121
uses: preactjs/compressed-size-action@v2
2222
with:
2323
pattern: 'test/fixtures/**/dist/!(*.map)'
24-
build-script: 'test'
24+
# We're using this to report size differences, not test, so update snapshots if they fail.
25+
# The CI can catch the test failures instead.
26+
build-script: 'test -- -u'
2527
repo-token: '${{ secrets.GITHUB_TOKEN }}'

package-lock.json

+14-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
"gzip-size": "^6.0.0",
9898
"kleur": "^4.1.3",
9999
"lodash.merge": "^4.6.2",
100+
"magic-string": "^0.25.9",
100101
"postcss": "^8.2.1",
101102
"pretty-bytes": "^5.4.1",
102103
"rollup": "^2.35.1",

src/index.js

+27-7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
import { getConfigFromPkgJson, getName } from './lib/package-info';
4040
import { shouldCssModules, cssModulesConfig } from './lib/css-modules';
4141
import { EOL } from 'os';
42+
import MagicString from 'magic-string';
4243

4344
// Extensions to use when resolving modules
4445
const 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-zA-Z$_]+)="undefined"!=typeof globalThis\?globalThis:(\1\|\|self)/,
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
/(global *= *)typeof +globalThis *!== *['"]undefined['"] *\? *globalThis *: *(global *\|\| *self)/,
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:

test/__snapshots__/index.test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -1930,7 +1930,7 @@ exports[`fixtures build inline-source-map with microbundle 4`] = `
19301930
19311931
exports[`fixtures build inline-source-map with microbundle 5`] = `
19321932
"!function(e,n){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=n():\\"function\\"==typeof define&&define.amd?define(n):(e||self).inlineSourceMap=n()}(this,function(){var e=function(){try{var e=arguments;return Promise.resolve([].slice.call(e).reduce(function(e,n){return e+n},0))}catch(e){return Promise.reject(e)}};return function(){try{var n=arguments,r=[].slice.call(n);return Promise.resolve(e.apply(void 0,r)).then(function(n){return Promise.resolve(e.apply(void 0,r)).then(function(e){return[n,e]})})}catch(e){return Promise.reject(e)}}});
1933-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lLXNvdXJjZS1tYXAudW1kLmpzIiwic291cmNlcyI6WyIuLi9zcmMvdHdvLmpzIiwiLi4vc3JjL2luZGV4LmpzIl0sInNvdXJjZX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MiOlsidHdvIiwicmVkdWNlIiwidG90YWwiLCJ2YWx1ZSIsImFyZ3MiXSwibWFwcGluZ3MiOiIwTEFBc0JBLGlDQUNyQix1QkFBTyxpQkFBS0MsT0FBTyxTQUFDQyxFQUFPQyxVQUFVRCxFQUFRQyxHQUFPLElBRHJELDBFQ0VpQ0MsMENBQ2xCSixlQUFPSSw0Q0FBYUosZUFBT0kscUJBQXpDLE1BQU8sVUFEUiJ9
19341934
"
19351935
`;
19361936

0 commit comments

Comments
 (0)