Skip to content

Commit

Permalink
Merge pull request #263 from msgpack/cjs
Browse files Browse the repository at this point in the history
give .cjs ext to CJS files
  • Loading branch information
gfx authored Feb 11, 2025
2 parents 04417cd + c81694e commit 2a78479
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 31 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
"description": "MessagePack for ECMA-262/JavaScript/TypeScript",
"author": "The MessagePack community",
"license": "ISC",
"main": "./dist.cjs/index.js",
"main": "./dist.cjs/index.cjs",
"module": "./dist.esm/index.mjs",
"cdn": "./dist.umd/msgpack.min.js",
"unpkg": "./dist.umd/msgpack.min.js",
"types": "./dist.esm/index.d.ts",
"sideEffects": false,
"scripts": {
"build": "npm publish --dry-run",
"prepare": "npm run clean && webpack --bail && tsc --build tsconfig.dist.cjs.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.mts --mjs dist.esm/*.js dist.esm/*/*.js && tsimp tools/fix-ext.mts --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:deno": "deno test --allow-read test/deno_*.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/",
Expand Down Expand Up @@ -89,10 +89,10 @@
"webpack-cli": "latest"
},
"files": [
"mod.ts",
"src/**/*.*",
"dist/**/*.*",
"dist.cjs/**/*.*",
"dist.esm/**/*.*",
"dist.umd/**/*.*",
"dist.esm/**/*.*"
"mod.ts"
]
}
12 changes: 12 additions & 0 deletions test/deno_cjs_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env deno test --allow-read

/* eslint-disable */
import { deepStrictEqual } from "node:assert";
import { test } from "node:test";
import * as msgpack from "../dist.cjs/index.cjs";

test("Hello, world!", () => {
const encoded = msgpack.encode("Hello, world!");
const decoded = msgpack.decode(encoded);
deepStrictEqual(decoded, "Hello, world!");
});
25 changes: 0 additions & 25 deletions tools/esmify.ts

This file was deleted.

28 changes: 28 additions & 0 deletions tools/fix-ext.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import fs from "node:fs";

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$/, `.${ext}`);
console.info(`Processing ${file} => ${fileMjs}`);
// .js => .mjs
const content = fs.readFileSync(file).toString("utf-8");
const newContent = content
.replace(/\bfrom "(\.\.?\/[^"]+)(?:\.js)?";/g, `from "$1.${ext}";`)
.replace(/\bimport "(\.\.?\/[^"]+)(?:\.js)?";/g, `import "$1.${ext}";`)
.replace(/\brequire\("(\.\.?\/[^"]+)(?:\.js)?"\)/g, `require("$1.${ext}");`)
.replace(/\/\/# sourceMappingURL=(.+)\.js\.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$/, ext);
fs.writeFileSync(`${fileMjs}.map`, JSON.stringify(mapping));
fs.unlinkSync(`${file}.map`);
}

0 comments on commit 2a78479

Please sign in to comment.