Skip to content

Commit 11ca667

Browse files
committedDec 12, 2021
Add transformation to CJS
1 parent 56597a9 commit 11ca667

File tree

7 files changed

+116
-8
lines changed

7 files changed

+116
-8
lines changed
 

‎.github/workflows/build.yml

-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ jobs:
1616
cache: "npm"
1717
- run: npm ci
1818
- run: npm run lint
19-
- run: npm run bundle-and-test
20-
env:
21-
REPORTER: "min"
2219
- run: npm run coverage
2320
env:
2421
REPORTER: "min"

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
node_modules
2+
cjs
3+
cjs-test

‎CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
- Allowed args after and between options
44
- Replaced `chalk` with `ansi-colors`
5-
- Migrated to ESM
5+
- Migrated to ESM (CommonJS is still supported)
66

77
## 3.0.0-beta.1
88

‎package-lock.json

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"./package.json": "./package.json"
2424
},
2525
"files": [
26+
"cjs",
2627
"lib"
2728
],
2829
"engines": {
@@ -35,12 +36,19 @@
3536
"c8": "^7.10.0",
3637
"eslint": "^8.4.1",
3738
"mocha": "^9.1.3",
39+
"rollup": "^2.61.1",
3840
"test-console": "^1.1.0"
3941
},
4042
"scripts": {
4143
"lint": "eslint lib test",
4244
"lint-and-test": "npm run lint && npm test",
4345
"test": "mocha --reporter ${REPORTER:-progress}",
44-
"coverage": "c8 --reporter=lcovonly npm test"
46+
"test:cjs": "mocha cjs-test --reporter ${REPORTER:-progress}",
47+
"build": "npm run esm-to-cjs",
48+
"build-and-test": "npm run esm-to-cjs-and-test",
49+
"esm-to-cjs": "node scripts/esm-to-cjs",
50+
"esm-to-cjs-and-test": "npm run esm-to-cjs && npm run test:cjs",
51+
"coverage": "c8 --reporter=lcovonly npm test",
52+
"prepublishOnly": "npm run lint-and-test && npm run build-and-test"
4553
}
4654
}

‎scripts/esm-to-cjs.js

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
import { rollup } from 'rollup';
4+
5+
const external = [
6+
'fs',
7+
'path',
8+
'assert',
9+
'test-console',
10+
'ansi-colors',
11+
'clap'
12+
];
13+
14+
function removeCreateRequire(id) {
15+
return fs.readFileSync(id, 'utf8')
16+
.replace(/import .+ from 'module';/, '')
17+
.replace(/const require = .+;/, '');
18+
}
19+
20+
function replaceContent(map) {
21+
return {
22+
name: 'file-content-replacement',
23+
load(id) {
24+
const key = path.relative('', id);
25+
26+
if (map.hasOwnProperty(key)) {
27+
return map[key](id);
28+
}
29+
}
30+
};
31+
}
32+
33+
function readDir(dir) {
34+
return fs.readdirSync(dir)
35+
.filter(fn => fn.endsWith('.js'))
36+
.map(fn => `${dir}/${fn}`);
37+
}
38+
39+
async function build(outputDir, ...entryPoints) {
40+
const startTime = Date.now();
41+
42+
console.log();
43+
console.log(`Convert ESM to CommonJS (output: ${outputDir})`);
44+
45+
const res = await rollup({
46+
external,
47+
input: entryPoints,
48+
plugins: [
49+
replaceContent({
50+
'lib/version.js': removeCreateRequire
51+
})
52+
]
53+
});
54+
await res.write({
55+
dir: outputDir,
56+
entryFileNames: '[name].cjs',
57+
format: 'cjs',
58+
exports: 'auto',
59+
preserveModules: true,
60+
interop: false,
61+
esModule: false,
62+
generatedCode: {
63+
constBindings: true
64+
}
65+
});
66+
await res.close();
67+
68+
console.log(`Done in ${Date.now() - startTime}ms`);
69+
}
70+
71+
async function buildAll() {
72+
await build('./cjs', 'lib/index.js');
73+
await build('./cjs-test', ...readDir('test'));
74+
}
75+
76+
buildAll();

‎test/command-action.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { equal, notDeepEqual, deepEqual } from 'assert';
1+
import { equal, deepEqual, strictEqual } from 'assert';
22
import * as clap from 'clap';
33

44
describe('action()', () => {
55
it('should have an expected input', () => {
66
const calls = [];
77
const command = clap.command('test [foo]')
88
.option('--bar', 'bar option')
9-
.action((...args) => {
9+
.action(function(...args) {
1010
calls.push({
1111
this: this,
1212
arguments: args
@@ -16,7 +16,7 @@ describe('action()', () => {
1616
command.run(['abc', '--', 'rest', 'args']);
1717

1818
equal(calls.length, 1);
19-
notDeepEqual(calls[0].this, command);
19+
strictEqual(calls[0].this, null);
2020
deepEqual(calls[0].arguments, [{
2121
commandPath: ['test'],
2222
options: { bar: false },

0 commit comments

Comments
 (0)
Please sign in to comment.