Skip to content
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

give .cjs ext to CJS files #263

Merged
merged 3 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
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`);
}
Loading