Skip to content

Commit

Permalink
Fix ESM build issues with TSC (#146)
Browse files Browse the repository at this point in the history
* Fix ESM build issues with TSC

* Use `rimraf@3.0.2`

* Use Perl
  • Loading branch information
Mrtenz committed Mar 9, 2024
1 parent b36a4d0 commit 147e95b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -34,7 +34,7 @@
},
"scripts": {
"prepublishOnly": "yarn build",
"build": "yarn build:cjs && yarn build:esm",
"build": "rimraf dist && yarn build:cjs && yarn build:esm",
"build:cjs": "tsc --project .",
"build:esm": "tsc --project tsconfig.esm.json && yarn build:esm:rename",
"build:esm:rename": "./scripts/rename-esm.sh",
Expand All @@ -54,6 +54,7 @@
"eslint-plugin-jest": "^24.1.3",
"eslint-plugin-node": "^11.1.0",
"jest": "^26.4.2",
"rimraf": "^3.0.2",
"ts-jest": "^26.4.0",
"typescript": "^4.0.5"
},
Expand Down
13 changes: 13 additions & 0 deletions scripts/rename-esm.sh
Expand Up @@ -9,5 +9,18 @@ if [[ ${RUNNER_DEBUG:-0} == 1 ]]; then
fi

for file in dist/esm/*.js; do
# Rename the `.js` files to `.mjs`.
mv "$file" "${file%.js}.mjs"

# Replace the sourceMappingURL to point to the new `.mjs.map` file.
sourceMap=$(basename "${file%.js}.mjs.map")
perl -i -pe "s|//# sourceMappingURL=.*?\.js\.map|//# sourceMappingURL=$sourceMap|" "${file%.js}.mjs"
done

for file in dist/esm/*.js.map; do
# Rename the `.js.map` files to `.mjs.map`.
mv "$file" "${file%.js.map}.mjs.map"

# Replace the file references in the source map to point to the new `.mjs` file.
perl -i -pe 's/\.js/\.mjs/g' "${file%.js.map}.mjs.map"
done
4 changes: 4 additions & 0 deletions tsconfig.esm.json
Expand Up @@ -10,5 +10,9 @@
},
"include": [
"./**/*.ts"
],
"exclude": [
"node_modules",
"dist"
]
}
4 changes: 4 additions & 0 deletions tsconfig.json
Expand Up @@ -9,5 +9,9 @@
},
"include": [
"./**/*.ts"
],
"exclude": [
"node_modules",
"dist"
]
}

0 comments on commit 147e95b

Please sign in to comment.