Skip to content

apply pure ESM policy #259

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
setup dist.cjs
  • Loading branch information
gfx committed Feb 9, 2025
commit cb2b30e3e52284febbbd6dd456ba3a65d1c40f29
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@
"description": "MessagePack for ECMA-262/JavaScript/TypeScript",
"author": "The MessagePack community",
"license": "ISC",
"main": "./dist/index.js",
"main": "./dist.cjs/index.js",
"module": "./dist.esm/index.mjs",
"cdn": "./dist.umd/msgpack.min.js",
"unpkg": "./dist.umd/msgpack.min.js",
"types": "./dist/index.d.ts",
"types": "./dist.esm/index.d.ts",
"sideEffects": false,
"scripts": {
"build": "npm publish --dry-run",
"prepare": "npm run clean && webpack --bail && tsc --build tsconfig.dist.json tsconfig.dist.esm.json && ts-node tools/esmify.ts dist.esm/*.js dist.esm/*/*.js",
"prepare": "npm run clean && webpack --bail && tsc --build tsconfig.dist.cjs.json tsconfig.dist.esm.json && tsimp tools/fix-ext.ts --mjs dist.esm/*.js dist.esm/*/*.js && tsimp tools/fix-ext.ts --cjs dist.cjs/*.js dist.cjs/*/*.js",
"prepublishOnly": "npm run test:dist",
"clean": "rimraf build dist dist.*",
"test": "mocha 'test/**/*.test.ts'",
"test:dist": "npm run lint && npm run test && npm run test:deno",
"test:cover": "npm run cover:clean && npx nyc --no-clean npm run 'test' && npm run cover:report",
"test:deno": "deno test test/deno_test.ts",
"test:bun": "bun test test/bun.spec.ts",
"test:fuzz": "npm exec --yes -- jsfuzz@git+https://gitlab.com/gitlab-org/security-products/analyzers/fuzzers/jsfuzz.git#39e6cf16613a0e30c7a7953f62e64292dbd5d3f3 --fuzzTime 60 --no-versifier test/decode.jsfuzz.js corpus",
"cover:clean": "rimraf .nyc_output coverage/",
"cover:report": "npx nyc report --reporter=text-summary --reporter=html --reporter=json",
Expand Down Expand Up @@ -81,7 +82,7 @@
"rimraf": "latest",
"ts-loader": "latest",
"ts-node": "latest",
"tsimp": "^2.0.12",
"tsimp": "latest",
"typescript": "latest",
"webpack": "latest",
"webpack-cli": "latest"
Expand Down
16 changes: 11 additions & 5 deletions tools/esmify.ts → tools/fix-ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@

import fs from "fs";

const files = process.argv.slice(2);
const mode = process.argv[2]; // --cjs or --mjs
const files = process.argv.slice(3);

const ext = mode === "--cjs" ? "cjs" : "mjs";

console.info(`Fixing ${mode} files with extension ${ext}`);

for (const file of files) {
const fileMjs = file.replace(/\.js$/, ".mjs");
const fileMjs = file.replace(/\.js$/, `.${ext}`);
console.info(`Processing ${file} => ${fileMjs}`);
// .js => .mjs
const content = fs.readFileSync(file).toString("utf-8");
const newContent = content.replace(/\bfrom "(\.\.?\/[^"]+)";/g, 'from "$1.mjs";')
const newContent = content.replace(/\bfrom "(\.\.?\/[^"]+).js";/g, `from "$1.${ext}";`)
.replace(/\brequire\("(\.\.?\/[^"]+).js"\)/g, `require("$1.${ext}");`)
.replace(/\/\/# sourceMappingURL=(.+)\.js\.map$/,
"//# sourceMappingURL=$1.mjs.map");
`//# sourceMappingURL=$1.${ext}.map`);
fs.writeFileSync(fileMjs, newContent);
fs.unlinkSync(file);

// .js.map => .mjs.map
const mapping = JSON.parse(fs.readFileSync(`${file}.map`).toString("utf-8"));
mapping.file = mapping.file.replace(/\.js$/, ".mjs");
mapping.file = mapping.file.replace(/\.js$/, ext);
fs.writeFileSync(`${fileMjs}.map`, JSON.stringify(mapping));
fs.unlinkSync(`${file}.map`);
}
5 changes: 3 additions & 2 deletions tsconfig.dist.json → tsconfig.dist.cjs.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"declaration": true,
"module": "CommonJS",
"outDir": "./dist.cjs",
"declaration": false,
"noEmitOnError": true,
"noEmit": false,
"rewriteRelativeImportExtensions": true,
Expand Down
6 changes: 4 additions & 2 deletions tsconfig.dist.esm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "es2020",
"module": "ES2020",
"outDir": "./dist.esm",
"declaration": false,
"declaration": true,
"noEmitOnError": true,
"noEmit": false,
"rewriteRelativeImportExtensions": true,
"incremental": false
},
"include": ["src/**/*.ts"]
Expand Down
Loading