From d9dcc9a16b3563863ee6973bebbe7f1a0d563d6c Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Fri, 5 May 2023 21:42:18 -0500 Subject: [PATCH 1/2] fix: Incorrect UMD sourcemaps --- package-lock.json | 71 ++++++++++++++++++++++----- package.json | 1 + src/index.js | 11 +++-- test/__snapshots__/index.test.js.snap | 2 +- 4 files changed, 70 insertions(+), 15 deletions(-) diff --git a/package-lock.json b/package-lock.json index adec7f6b..c8fd8a82 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "microbundle", - "version": "0.15.0", + "version": "0.15.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "microbundle", - "version": "0.15.0", + "version": "0.15.1", "license": "MIT", "dependencies": { "@babel/core": "^7.12.10", @@ -38,6 +38,7 @@ "gzip-size": "^6.0.0", "kleur": "^4.1.3", "lodash.merge": "^4.6.2", + "magic-string": "^0.30.0", "postcss": "^8.2.1", "pretty-bytes": "^5.4.1", "rollup": "^2.35.1", @@ -3036,6 +3037,11 @@ "node": ">=8" } }, + "node_modules/@jridgewell/sourcemap-codec": { + "version": "1.4.15", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.15.tgz", + "integrity": "sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg==" + }, "node_modules/@manypkg/find-root": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@manypkg/find-root/-/find-root-1.1.0.tgz", @@ -3195,6 +3201,14 @@ "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-2.0.2.tgz", "integrity": "sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==" }, + "node_modules/@rollup/plugin-commonjs/node_modules/magic-string": { + "version": "0.25.9", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.25.9.tgz", + "integrity": "sha512-RmF0AsMzgt25qzqqLc1+MbHmhdx0ojF2Fvs4XnOqz2ZOBXzzkEwc/dJQZCYHAn7v1jbVOjAZfK8msRn4BxO4VQ==", + "dependencies": { + "sourcemap-codec": "^1.4.8" + } + }, "node_modules/@rollup/plugin-json": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/@rollup/plugin-json/-/plugin-json-4.1.0.tgz", @@ -3260,6 +3274,14 @@ "magic-string": "^0.25.0" } }, + "node_modules/@surma/rollup-plugin-off-main-thread/node_modules/magic-string": { + "version": "0.25.9", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.25.9.tgz", + "integrity": "sha512-RmF0AsMzgt25qzqqLc1+MbHmhdx0ojF2Fvs4XnOqz2ZOBXzzkEwc/dJQZCYHAn7v1jbVOjAZfK8msRn4BxO4VQ==", + "dependencies": { + "sourcemap-codec": "^1.4.8" + } + }, "node_modules/@types/babel__core": { "version": "7.1.12", "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.1.12.tgz", @@ -11320,11 +11342,14 @@ } }, "node_modules/magic-string": { - "version": "0.25.7", - "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.25.7.tgz", - "integrity": "sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA==", + "version": "0.30.0", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.0.tgz", + "integrity": "sha512-LA+31JYDJLs82r2ScLrlz1GjSgu66ZV518eyWT+S8VhyQn/JL0u9MeBOvQMGYiPk1DBiSN9DDMOcXvigJZaViQ==", "dependencies": { - "sourcemap-codec": "^1.4.4" + "@jridgewell/sourcemap-codec": "^1.4.13" + }, + "engines": { + "node": ">=12" } }, "node_modules/make-dir": { @@ -16050,7 +16075,8 @@ "node_modules/sourcemap-codec": { "version": "1.4.8", "resolved": "https://registry.npmjs.org/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz", - "integrity": "sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==" + "integrity": "sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==", + "deprecated": "Please use @jridgewell/sourcemap-codec instead" }, "node_modules/spawndamnit": { "version": "2.0.0", @@ -20272,6 +20298,11 @@ } } }, + "@jridgewell/sourcemap-codec": { + "version": "1.4.15", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.15.tgz", + "integrity": "sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg==" + }, "@manypkg/find-root": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@manypkg/find-root/-/find-root-1.1.0.tgz", @@ -20410,6 +20441,14 @@ "version": "2.0.2", "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-2.0.2.tgz", "integrity": "sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==" + }, + "magic-string": { + "version": "0.25.9", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.25.9.tgz", + "integrity": "sha512-RmF0AsMzgt25qzqqLc1+MbHmhdx0ojF2Fvs4XnOqz2ZOBXzzkEwc/dJQZCYHAn7v1jbVOjAZfK8msRn4BxO4VQ==", + "requires": { + "sourcemap-codec": "^1.4.8" + } } } }, @@ -20470,6 +20509,16 @@ "ejs": "^3.1.6", "json5": "^2.2.0", "magic-string": "^0.25.0" + }, + "dependencies": { + "magic-string": { + "version": "0.25.9", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.25.9.tgz", + "integrity": "sha512-RmF0AsMzgt25qzqqLc1+MbHmhdx0ojF2Fvs4XnOqz2ZOBXzzkEwc/dJQZCYHAn7v1jbVOjAZfK8msRn4BxO4VQ==", + "requires": { + "sourcemap-codec": "^1.4.8" + } + } } }, "@types/babel__core": { @@ -26937,11 +26986,11 @@ } }, "magic-string": { - "version": "0.25.7", - "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.25.7.tgz", - "integrity": "sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA==", + "version": "0.30.0", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.0.tgz", + "integrity": "sha512-LA+31JYDJLs82r2ScLrlz1GjSgu66ZV518eyWT+S8VhyQn/JL0u9MeBOvQMGYiPk1DBiSN9DDMOcXvigJZaViQ==", "requires": { - "sourcemap-codec": "^1.4.4" + "@jridgewell/sourcemap-codec": "^1.4.13" } }, "make-dir": { diff --git a/package.json b/package.json index 6cf2034f..1bef5f4f 100644 --- a/package.json +++ b/package.json @@ -97,6 +97,7 @@ "gzip-size": "^6.0.0", "kleur": "^4.1.3", "lodash.merge": "^4.6.2", + "magic-string": "^0.30.0", "postcss": "^8.2.1", "pretty-bytes": "^5.4.1", "rollup": "^2.35.1", diff --git a/src/index.js b/src/index.js index 06a36d29..33e1b8e1 100644 --- a/src/index.js +++ b/src/index.js @@ -39,6 +39,7 @@ import { 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']; @@ -653,17 +654,21 @@ function createConfig(options, entry, format, writeMeta) { // 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') { + const s = new MagicString(code); // minified: - code = code.replace( + s.replace( /([a-zA-Z$_]+)="undefined"!=typeof globalThis\?globalThis:(\1\|\|self)/, '$2', ); // unminified: - code = code.replace( + s.replace( /(global *= *)typeof +globalThis *!== *['"]undefined['"] *\? *globalThis *: *(global *\|\| *self)/, '$1$2', ); - return { code, map: null }; + return { + code: s.toString(), + map: s.generateMap({ hires: true }), + }; } }, // Grab size info before writing files to disk: diff --git a/test/__snapshots__/index.test.js.snap b/test/__snapshots__/index.test.js.snap index 1155d4f2..cabd2132 100644 --- a/test/__snapshots__/index.test.js.snap +++ b/test/__snapshots__/index.test.js.snap @@ -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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lLXNvdXJjZS1tYXAudW1kLmpzIiwic291cmNlcyI6WyIuLi9zcmMvdHdvLmpzIiwiLi4vc3JjL2luZGV4LmpzIl0sInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCBhc3luYyBmdW5jdGlvbiB0d28oLi4uYXJncykge1xuXHRyZXR1cm4gYXJncy5yZWR1Y2UoKHRvdGFsLCB2YWx1ZSkgPT4gdG90YWwgKyB2YWx1ZSwgMCk7XG59XG4iLCJpbXBvcnQgeyB0d28gfSBmcm9tICcuL3R3byc7XG5cbmV4cG9ydCBkZWZhdW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scUJBQXpDLE1BQU8sVUFEUiJ9 " `; From 1b40474d369a8c8ea0995ae3d78ec5a66fae1ddc Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Fri, 5 May 2023 21:45:29 -0500 Subject: [PATCH 2/2] docs: Adding changeset --- .changeset/violet-falcons-dream.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/violet-falcons-dream.md diff --git a/.changeset/violet-falcons-dream.md b/.changeset/violet-falcons-dream.md new file mode 100644 index 00000000..9cd3e61b --- /dev/null +++ b/.changeset/violet-falcons-dream.md @@ -0,0 +1,5 @@ +--- +'microbundle': patch +--- + +Fixes positioning bug with UMD sourcemaps