diff --git a/package.json b/package.json index 1e5621f..d897dbe 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" }, diff --git a/scripts/rename-esm.sh b/scripts/rename-esm.sh index 526904d..bc05704 100755 --- a/scripts/rename-esm.sh +++ b/scripts/rename-esm.sh @@ -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 diff --git a/tsconfig.esm.json b/tsconfig.esm.json index e2b6d0b..8cf127e 100644 --- a/tsconfig.esm.json +++ b/tsconfig.esm.json @@ -10,5 +10,9 @@ }, "include": [ "./**/*.ts" + ], + "exclude": [ + "node_modules", + "dist" ] } diff --git a/tsconfig.json b/tsconfig.json index 413414b..b0e533a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,5 +9,9 @@ }, "include": [ "./**/*.ts" + ], + "exclude": [ + "node_modules", + "dist" ] }