Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Incorrect UMD sourcemaps #1043

Merged
merged 4 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-falcons-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'microbundle': patch
---

Fixes positioning bug with UMD sourcemaps
4 changes: 3 additions & 1 deletion .github/workflows/size.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ jobs:
uses: preactjs/compressed-size-action@v2
with:
pattern: 'test/fixtures/**/dist/!(*.map)'
build-script: 'test'
# We're using this to report size differences, not test, so update snapshots if they fail.
# The CI can catch the test failures instead.
build-script: 'test -- -u'
repo-token: '${{ secrets.GITHUB_TOKEN }}'
25 changes: 14 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"gzip-size": "^6.0.0",
"kleur": "^4.1.3",
"lodash.merge": "^4.6.2",
"magic-string": "^0.25.9",
"postcss": "^8.2.1",
"pretty-bytes": "^5.4.1",
"rollup": "^2.35.1",
Expand Down
34 changes: 27 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import { getConfigFromPkgJson, getName } from './lib/package-info';
import { shouldCssModules, cssModulesConfig } from './lib/css-modules';
import { EOL } from 'os';
import MagicString from 'magic-string';

// Extensions to use when resolving modules
const EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx', '.es6', '.es', '.mjs'];
Expand Down Expand Up @@ -71,7 +72,7 @@
options.pkg.name = pkgName;

if (options.sourcemap === 'inline') {
console.log(

Check warning on line 75 in src/index.js

View workflow job for this annotation

GitHub Actions / build (12.x)

Unexpected console statement

Check warning on line 75 in src/index.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Unexpected console statement

Check warning on line 75 in src/index.js

View workflow job for this annotation

GitHub Actions / build (12.x)

Unexpected console statement

Check warning on line 75 in src/index.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Unexpected console statement
'Warning: inline sourcemaps should only be used for debugging purposes.',
);
} else if (options.sourcemap === 'false') {
Expand Down Expand Up @@ -644,7 +645,7 @@
options.visualize && visualizer(),
// NOTE: OMT only works with amd and esm
// Source: https://github.com/surma/rollup-plugin-off-main-thread#config
useWorkerLoader && (format === 'es' || modern) && OMT(),

Check warning on line 648 in src/index.js

View workflow job for this annotation

GitHub Actions / build (12.x)

A function with a name starting with an uppercase letter should only be used as a constructor

Check warning on line 648 in src/index.js

View workflow job for this annotation

GitHub Actions / build (14.x)

A function with a name starting with an uppercase letter should only be used as a constructor

Check warning on line 648 in src/index.js

View workflow job for this annotation

GitHub Actions / build (12.x)

A function with a name starting with an uppercase letter should only be used as a constructor

Check warning on line 648 in src/index.js

View workflow job for this annotation

GitHub Actions / build (14.x)

A function with a name starting with an uppercase letter should only be used as a constructor
/** @type {import('rollup').Plugin} */
({
name: 'postprocessing',
Expand All @@ -653,17 +654,36 @@
// So we remove the globalThis check, replacing it with `this||self` to match Rollup 1's output:
renderChunk(code, chunk, opts) {
if (opts.format === 'umd') {
// minified:
code = code.replace(
// Can swap this out with MagicString.replace() when we bump it:
// https://github.com/developit/microbundle/blob/f815a01cb63d90b9f847a4dcad2a64e6b2f8596f/src/index.js#L657-L671
const s = new MagicString(code);

const minified = code.match(
/([a-zA-Z$_]+)="undefined"!=typeof globalThis\?globalThis:(\1\|\|self)/,
'$2',
);
// unminified:
code = code.replace(
if (minified) {
s.overwrite(
minified.index,
minified.index + minified[0].length,
minified[2],
);
}

const unminified = code.match(
/(global *= *)typeof +globalThis *!== *['"]undefined['"] *\? *globalThis *: *(global *\|\| *self)/,
'$1$2',
);
return { code, map: null };
if (unminified) {
s.overwrite(
unminified.index,
unminified.index + unminified[0].length,
unminified[1] + unminified[2],
);
}

return {
code: s.toString(),
map: s.generateMap({ hires: true }),
};
}
},
// Grab size info before writing files to disk:
Expand Down
2 changes: 1 addition & 1 deletion test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1930,7 +1930,7 @@ exports[`fixtures build inline-source-map with microbundle 4`] = `

exports[`fixtures build inline-source-map with microbundle 5`] = `
"!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)}}});
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lLXNvdXJjZS1tYXAudW1kLmpzIiwic291cmNlcyI6WyIuLi9zcmMvdHdvLmpzIiwiLi4vc3JjL2luZGV4LmpzIl0sInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCBhc3luYyBmdW5jdGlvbiB0d28oLi4uYXJncykge1xuXHRyZXR1cm4gYXJncy5yZWR1Y2UoKHRvdGFsLCB2YWx1ZSkgPT4gdG90YWwgKyB2YWx1ZSwgMCk7XG59XG4iLCJpbXBvcnQgeyB0d28gfSBmcm9tICcuL3R3byc7XG5cbmV4cG9ydCBkZWZhdWx0IGFzeW5jIGZ1bmN0aW9uKC4uLmFyZ3MpIHtcblx0cmV0dXJuIFthd2FpdCB0d28oLi4uYXJncyksIGF3YWl0IHR3byguLi5hc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