From 11ca667f947b7b302125c2fa9f508d044fdb8d49 Mon Sep 17 00:00:00 2001 From: Roman Dvornov Date: Sun, 12 Dec 2021 22:22:45 +0100 Subject: [PATCH] Add transformation to CJS --- .github/workflows/build.yml | 3 -- .gitignore | 2 + CHANGELOG.md | 2 +- package-lock.json | 25 ++++++++++++ package.json | 10 ++++- scripts/esm-to-cjs.js | 76 +++++++++++++++++++++++++++++++++++++ test/command-action.js | 6 +-- 7 files changed, 116 insertions(+), 8 deletions(-) create mode 100644 scripts/esm-to-cjs.js diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a018ba8..d214bc2 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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" diff --git a/.gitignore b/.gitignore index 08b2553..5205102 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ node_modules +cjs +cjs-test \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a80fb4..6e4ac39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/package-lock.json b/package-lock.json index bad876f..672cf49 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "c8": "^7.10.0", "eslint": "^8.4.1", "mocha": "^9.1.3", + "rollup": "^2.61.1", "test-console": "^1.1.0" }, "engines": { @@ -1748,6 +1749,21 @@ "node": ">=4" } }, + "node_modules/rollup": { + "version": "2.61.1", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-2.61.1.tgz", + "integrity": "sha512-BbTXlEvB8d+XFbK/7E5doIcRtxWPRiqr0eb5vQ0+2paMM04Ye4PZY5nHOQef2ix24l/L0SpLd5hwcH15QHPdvA==", + "dev": true, + "bin": { + "rollup": "dist/bin/rollup" + }, + "engines": { + "node": ">=10.0.0" + }, + "optionalDependencies": { + "fsevents": "~2.3.2" + } + }, "node_modules/safe-buffer": { "version": "5.2.0", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.0.tgz", @@ -3301,6 +3317,15 @@ "integrity": "sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==", "dev": true }, + "rollup": { + "version": "2.61.1", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-2.61.1.tgz", + "integrity": "sha512-BbTXlEvB8d+XFbK/7E5doIcRtxWPRiqr0eb5vQ0+2paMM04Ye4PZY5nHOQef2ix24l/L0SpLd5hwcH15QHPdvA==", + "dev": true, + "requires": { + "fsevents": "~2.3.2" + } + }, "safe-buffer": { "version": "5.2.0", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.0.tgz", diff --git a/package.json b/package.json index e9f4978..ccae108 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "./package.json": "./package.json" }, "files": [ + "cjs", "lib" ], "engines": { @@ -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" } } diff --git a/scripts/esm-to-cjs.js b/scripts/esm-to-cjs.js new file mode 100644 index 0000000..8416f54 --- /dev/null +++ b/scripts/esm-to-cjs.js @@ -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(); diff --git a/test/command-action.js b/test/command-action.js index 083a6ec..19299de 100644 --- a/test/command-action.js +++ b/test/command-action.js @@ -1,4 +1,4 @@ -import { equal, notDeepEqual, deepEqual } from 'assert'; +import { equal, deepEqual, strictEqual } from 'assert'; import * as clap from 'clap'; describe('action()', () => { @@ -6,7 +6,7 @@ describe('action()', () => { const calls = []; const command = clap.command('test [foo]') .option('--bar', 'bar option') - .action((...args) => { + .action(function(...args) { calls.push({ this: this, arguments: args @@ -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 },