Skip to content

Commit

Permalink
fix: source maps for esm modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Edgar-P-yan committed May 15, 2024
1 parent 2f38a92 commit 0098e9b
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 15 deletions.
1 change: 1 addition & 0 deletions dist/index.esm.js

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

2 changes: 1 addition & 1 deletion dist/index.esm.js.map

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

1 change: 1 addition & 0 deletions dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import cjsModule from 'node:module';
const __filename = cjsUrl.fileURLToPath(import.meta.url);
const __dirname = cjsPath.dirname(__filename);
const require = cjsModule.createRequire(import.meta.url);
// -- End Shims --


function getTrayBinPath(debug = false, copyDir = false) {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.mjs.map

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 @@ -49,6 +49,7 @@
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.3.1",
"magic-string": "^0.30.10",
"prettier": "^2.8.1",
"rimraf": "^5.0.5",
"rollup": "^2.79.1",
Expand Down
54 changes: 42 additions & 12 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { readFile } from 'node:fs/promises';

import typescript2 from 'rollup-plugin-typescript2';
import json from '@rollup/plugin-json';
import MagicString from 'magic-string';

const packageJSON = JSON.parse(await readFile('./package.json', 'utf-8'));

Expand Down Expand Up @@ -70,13 +71,16 @@ const options = {
export default options;

/**
* The original ESM shim plugin is broken, it inserts the shims
* in wrong places causing syntax errors. This is a very simple
* solution, which is not better, but at list does not brake for
* this specific code.
* An alternative to @rollup/plugin-esm-shim (https://github.com/rollup/plugins/tree/master/packages/esm-shim).
*
* @todo post the improvement to the issue https://github.com/rollup/plugins/issues/1709
* @todo use magic-string to also generate mappings. Right now this plugin removes mappings for esm builds.
* The original ESM shim plugin has a bug: it inserts the shims
* in wrong places causing syntax errors. This slightly modified
* version of it is a very simple solution, which surely will not work
* in every case, but at list does not brake specifically for my code.
*
* Whats different from the original?
* The regex used to find import statements is changed to a more
* simple one /^import\s.*';$/gm and the whole login is a lot simpler.
*/
function esmShimCustom() {
const ESMShim = `
Expand All @@ -87,14 +91,22 @@ import cjsModule from 'node:module';
const __filename = cjsUrl.fileURLToPath(import.meta.url);
const __dirname = cjsPath.dirname(__filename);
const require = cjsModule.createRequire(import.meta.url);
// -- End Shims --
`;
const CJSyntaxRegex = /__filename|__dirname|require\(|require\.resolve\(/;

return {
name: 'esm-shim-custom',

renderChunk(/** @type {string} */ code, _chunk, opts) {
if (opts.format === 'es') {
if (code.includes(ESMShim) || !CJSyntaxRegex.test(code)) {
return null;
}

let endIndexOfLastImport = -1;

// Find the last import statement and its ending index
for (let match of code.matchAll(/^import\s.*';$/gm)) {
if (match.length === 0 || typeof match.index !== 'number') {
continue;
Expand All @@ -103,14 +115,32 @@ const require = cjsModule.createRequire(import.meta.url);
endIndexOfLastImport = match.index + match[0].length;
}

if (endIndexOfLastImport > -1) {
code =
code.slice(0, endIndexOfLastImport) +
ESMShim +
code.slice(endIndexOfLastImport);
const s = new MagicString(code);
s.appendRight(endIndexOfLastImport, ESMShim);

const sourceMap = s.generateMap({
includeContent: true,
});

/** @type {string[] | undefined} */
let sourcesContent;
if (Array.isArray(sourceMap.sourcesContent)) {
sourcesContent = [];
for (let i = 0; i < sourceMap.sourcesContent.length; i++) {
const content = sourceMap.sourcesContent[i];
if (typeof content === 'string') {
sourcesContent.push(content);
}
}
}

return code;
return {
code: s.toString(),
map: {
...sourceMap,
sourcesContent,
},
};
}

return null;
Expand Down
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@
resolved "https://registry.yarnpkg.com/@jridgewell/set-array/-/set-array-1.1.2.tgz#7c6cf998d6d20b914c0a55a91ae928ff25965e72"
integrity sha512-xnkseuNADM0gt2bs+BvhO0p78Mk762YnZdsuzFV018NoG1Sj1SCQvpSqa7XUaTam5vAGasABV9qXASMKnFMwMw==

"@jridgewell/sourcemap-codec@^1.4.10", "@jridgewell/sourcemap-codec@^1.4.14":
"@jridgewell/sourcemap-codec@^1.4.10", "@jridgewell/sourcemap-codec@^1.4.14", "@jridgewell/sourcemap-codec@^1.4.15":
version "1.4.15"
resolved "https://registry.yarnpkg.com/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.15.tgz#d7c6e6755c78567a951e04ab52ef0fd26de59f32"
integrity sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg==
Expand Down Expand Up @@ -2531,6 +2531,13 @@ lunr@^2.3.9:
resolved "https://registry.yarnpkg.com/lunr/-/lunr-2.3.9.tgz#18b123142832337dd6e964df1a5a7707b25d35e1"
integrity sha512-zTU3DaZaF3Rt9rhN3uBMGQD3dD2/vFQqnvZCDv4dl5iOzq2IZQqTxu90r4E5J+nP70J3ilqVCrbho2eWaeW8Ow==

magic-string@^0.30.10:
version "0.30.10"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.30.10.tgz#123d9c41a0cb5640c892b041d4cfb3bd0aa4b39e"
integrity sha512-iIRwTIf0QKV3UAnYK4PU8uiEc4SRh5jX0mwpIwETPpHdhVM4f53RSwS/vXvN1JhGX+Cs7B8qIq3d6AH49O5fAQ==
dependencies:
"@jridgewell/sourcemap-codec" "^1.4.15"

make-dir@^3.0.2:
version "3.1.0"
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-3.1.0.tgz#415e967046b3a7f1d185277d84aa58203726a13f"
Expand Down

0 comments on commit 0098e9b

Please sign in to comment.