Skip to content

Commit

Permalink
Add transformation to CJS
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Dec 12, 2021
1 parent 56597a9 commit 11ca667
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 8 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ jobs:
cache: "npm"
- run: npm ci
- run: npm run lint
- run: npm run bundle-and-test
env:
REPORTER: "min"
- run: npm run coverage
env:
REPORTER: "min"
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
cjs
cjs-test
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

## 3.0.0-beta.1

Expand Down
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"./package.json": "./package.json"
},
"files": [
"cjs",
"lib"
],
"engines": {
Expand All @@ -35,12 +36,19 @@
"c8": "^7.10.0",
"eslint": "^8.4.1",
"mocha": "^9.1.3",
"rollup": "^2.61.1",
"test-console": "^1.1.0"
},
"scripts": {
"lint": "eslint lib test",
"lint-and-test": "npm run lint && npm test",
"test": "mocha --reporter ${REPORTER:-progress}",
"coverage": "c8 --reporter=lcovonly npm test"
"test:cjs": "mocha cjs-test --reporter ${REPORTER:-progress}",
"build": "npm run esm-to-cjs",
"build-and-test": "npm run esm-to-cjs-and-test",
"esm-to-cjs": "node scripts/esm-to-cjs",
"esm-to-cjs-and-test": "npm run esm-to-cjs && npm run test:cjs",
"coverage": "c8 --reporter=lcovonly npm test",
"prepublishOnly": "npm run lint-and-test && npm run build-and-test"
}
}
76 changes: 76 additions & 0 deletions scripts/esm-to-cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import fs from 'fs';
import path from 'path';
import { rollup } from 'rollup';

const external = [
'fs',
'path',
'assert',
'test-console',
'ansi-colors',
'clap'
];

function removeCreateRequire(id) {
return fs.readFileSync(id, 'utf8')
.replace(/import .+ from 'module';/, '')
.replace(/const require = .+;/, '');
}

function replaceContent(map) {
return {
name: 'file-content-replacement',
load(id) {
const key = path.relative('', id);

if (map.hasOwnProperty(key)) {
return map[key](id);
}
}
};
}

function readDir(dir) {
return fs.readdirSync(dir)
.filter(fn => fn.endsWith('.js'))
.map(fn => `${dir}/${fn}`);
}

async function build(outputDir, ...entryPoints) {
const startTime = Date.now();

console.log();
console.log(`Convert ESM to CommonJS (output: ${outputDir})`);

const res = await rollup({
external,
input: entryPoints,
plugins: [
replaceContent({
'lib/version.js': removeCreateRequire
})
]
});
await res.write({
dir: outputDir,
entryFileNames: '[name].cjs',
format: 'cjs',
exports: 'auto',
preserveModules: true,
interop: false,
esModule: false,
generatedCode: {
constBindings: true
}
});
await res.close();

console.log(`Done in ${Date.now() - startTime}ms`);
}

async function buildAll() {
await build('./cjs', 'lib/index.js');
await build('./cjs-test', ...readDir('test'));
}

buildAll();
6 changes: 3 additions & 3 deletions test/command-action.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { equal, notDeepEqual, deepEqual } from 'assert';
import { equal, deepEqual, strictEqual } from 'assert';
import * as clap from 'clap';

describe('action()', () => {
it('should have an expected input', () => {
const calls = [];
const command = clap.command('test [foo]')
.option('--bar', 'bar option')
.action((...args) => {
.action(function(...args) {
calls.push({
this: this,
arguments: args
Expand All @@ -16,7 +16,7 @@ describe('action()', () => {
command.run(['abc', '--', 'rest', 'args']);

equal(calls.length, 1);
notDeepEqual(calls[0].this, command);
strictEqual(calls[0].this, null);
deepEqual(calls[0].arguments, [{
commandPath: ['test'],
options: { bar: false },
Expand Down

0 comments on commit 11ca667

Please sign in to comment.