Skip to content

Commit 2a78479

Browse files
authored
Merge pull request #263 from msgpack/cjs
give .cjs ext to CJS files
2 parents 04417cd + c81694e commit 2a78479

File tree

4 files changed

+46
-31
lines changed

4 files changed

+46
-31
lines changed

package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
"description": "MessagePack for ECMA-262/JavaScript/TypeScript",
55
"author": "The MessagePack community",
66
"license": "ISC",
7-
"main": "./dist.cjs/index.js",
7+
"main": "./dist.cjs/index.cjs",
88
"module": "./dist.esm/index.mjs",
99
"cdn": "./dist.umd/msgpack.min.js",
1010
"unpkg": "./dist.umd/msgpack.min.js",
1111
"types": "./dist.esm/index.d.ts",
1212
"sideEffects": false,
1313
"scripts": {
1414
"build": "npm publish --dry-run",
15-
"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",
15+
"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",
1616
"prepublishOnly": "npm run test:dist",
1717
"clean": "rimraf build dist dist.*",
1818
"test": "mocha 'test/**/*.test.ts'",
1919
"test:dist": "npm run lint && npm run test && npm run test:deno",
2020
"test:cover": "npm run cover:clean && npx nyc --no-clean npm run 'test' && npm run cover:report",
21-
"test:deno": "deno test test/deno_test.ts",
21+
"test:deno": "deno test --allow-read test/deno_*.ts",
2222
"test:bun": "bun test test/bun.spec.ts",
2323
"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",
2424
"cover:clean": "rimraf .nyc_output coverage/",
@@ -89,10 +89,10 @@
8989
"webpack-cli": "latest"
9090
},
9191
"files": [
92-
"mod.ts",
9392
"src/**/*.*",
94-
"dist/**/*.*",
93+
"dist.cjs/**/*.*",
94+
"dist.esm/**/*.*",
9595
"dist.umd/**/*.*",
96-
"dist.esm/**/*.*"
96+
"mod.ts"
9797
]
9898
}

test/deno_cjs_test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env deno test --allow-read
2+
3+
/* eslint-disable */
4+
import { deepStrictEqual } from "node:assert";
5+
import { test } from "node:test";
6+
import * as msgpack from "../dist.cjs/index.cjs";
7+
8+
test("Hello, world!", () => {
9+
const encoded = msgpack.encode("Hello, world!");
10+
const decoded = msgpack.decode(encoded);
11+
deepStrictEqual(decoded, "Hello, world!");
12+
});

tools/esmify.ts

-25
This file was deleted.

tools/fix-ext.mts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import fs from "node:fs";
2+
3+
const mode = process.argv[2]; // --cjs or --mjs
4+
const files = process.argv.slice(3);
5+
6+
const ext = mode === "--cjs" ? "cjs" : "mjs";
7+
8+
console.info(`Fixing ${mode} files with extension ${ext}`);
9+
10+
for (const file of files) {
11+
const fileMjs = file.replace(/\.js$/, `.${ext}`);
12+
console.info(`Processing ${file} => ${fileMjs}`);
13+
// .js => .mjs
14+
const content = fs.readFileSync(file).toString("utf-8");
15+
const newContent = content
16+
.replace(/\bfrom "(\.\.?\/[^"]+)(?:\.js)?";/g, `from "$1.${ext}";`)
17+
.replace(/\bimport "(\.\.?\/[^"]+)(?:\.js)?";/g, `import "$1.${ext}";`)
18+
.replace(/\brequire\("(\.\.?\/[^"]+)(?:\.js)?"\)/g, `require("$1.${ext}");`)
19+
.replace(/\/\/# sourceMappingURL=(.+)\.js\.map$/, `//# sourceMappingURL=$1.${ext}.map`);
20+
fs.writeFileSync(fileMjs, newContent);
21+
fs.unlinkSync(file);
22+
23+
// .js.map => .mjs.map
24+
const mapping = JSON.parse(fs.readFileSync(`${file}.map`).toString("utf-8"));
25+
mapping.file = mapping.file.replace(/\.js$/, ext);
26+
fs.writeFileSync(`${fileMjs}.map`, JSON.stringify(mapping));
27+
fs.unlinkSync(`${file}.map`);
28+
}

0 commit comments

Comments
 (0)