Skip to content

Commit

Permalink
Add improved CommonJS compatibility banner (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlansley authored Oct 3, 2023
1 parent 9d53581 commit 44f3a62
Show file tree
Hide file tree
Showing 7 changed files with 390 additions and 228 deletions.
512 changes: 325 additions & 187 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@checkdigit/typescript-config",
"version": "5.0.0",
"version": "5.1.0",
"description": "Check Digit standard Typescript configuration",
"prettier": "@checkdigit/prettier-config",
"engines": {
Expand All @@ -11,7 +11,7 @@
},
"peerDependencies": {
"@types/node": ">=18",
"esbuild": "0.19.3",
"esbuild": "0.19.4",
"typescript": ">=5.2.2 <5.3"
},
"repository": {
Expand Down Expand Up @@ -57,19 +57,21 @@
"ci:style": "npm run prettier"
},
"devDependencies": {
"@apidevtools/json-schema-ref-parser": "^11.1.0",
"@checkdigit/prettier-config": "^4.1.0",
"@types/debug": "^4.1.9",
"@types/jest": "^29.5.5",
"@types/uuid": "^9.0.4",
"@typescript-eslint/eslint-plugin": "^6.7.3",
"@typescript-eslint/parser": "^6.7.3",
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
"debug": "^4.3.4",
"eslint": "^8.50.0",
"eslint-config-prettier": "^9.0.0",
"get-port": "^7.0.0",
"got": "^11.8.6",
"jest": "^29.7.0",
"rimraf": "^5.0.2",
"node-fetch": "^3.3.2",
"rimraf": "^5.0.5",
"ts-jest": "^29.1.1",
"uuid": "^9.0.1"
},
Expand Down
9 changes: 8 additions & 1 deletion src/builder/builder.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import typescript from 'typescript';

import { type PluginBuild, build } from 'esbuild';

const commonJsCompatabilityBanner = `import { createRequire as __createRequire } from "node:module";
import { fileURLToPath as __fileURLToPath } from "node:url";
import { default as __path } from "node:path";
const __filename = __fileURLToPath(import.meta.url);
const __dirname = __path.dirname(__filename);
const require = __createRequire(import.meta.url);`;

export interface BuilderOptions {
/**
* whether to produce Typescript types, ESM or CommonJS code
Expand Down Expand Up @@ -202,7 +209,7 @@ export default async function ({
banner:
type === 'module' && outFile !== undefined
? {
js: 'import { createRequire as __createRequire } from "node:module";\nconst require = __createRequire(import.meta.url);',
js: commonJsCompatabilityBanner,
}
: {},
sourcemap: sourceMap ? 'inline' : false,
Expand Down
57 changes: 29 additions & 28 deletions src/builder/builder.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import builder from './builder.mts';

const require = createRequire(import.meta.url);

const commonJsCompatabilityBanner = `import { createRequire as __createRequire } from "node:module";
import { fileURLToPath as __fileURLToPath } from "node:url";
import { default as __path } from "node:path";
const __filename = __fileURLToPath(import.meta.url);
const __dirname = __path.dirname(__filename);
const require = __createRequire(import.meta.url);`;

const singleModule = {
[`index.ts`]: `export const hello = 'world';`,
};
Expand Down Expand Up @@ -383,15 +390,13 @@ describe('test builder', () => {
);
assert.deepEqual(await read(outDir), {
'index.mjs':
'import { createRequire as __createRequire } from "node:module";\n' +
'const require = __createRequire(import.meta.url);\n' +
'\n' +
'var hello = "world";\n' +
'\n' +
'var src_default = hello + "world";\n' +
'export {\n' +
' src_default as default\n' +
'};\n',
`${commonJsCompatabilityBanner}\n\n` +
`var hello = "world";\n` +
`\n` +
`var src_default = hello + "world";\n` +
`export {\n` +
` src_default as default\n` +
`};\n`,
});
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const output = await import(path.join(outDir, 'index.mjs'));
Expand All @@ -411,16 +416,14 @@ describe('test builder', () => {
);
assert.deepEqual(await read(outDir), {
'index.mjs':
'import { createRequire as __createRequire } from "node:module";\n' +
'const require = __createRequire(import.meta.url);\n' +
'\n' +
'var hello = "world";\n' +
'\n' +
'import util from "node:util";\n' +
'var hello2 = { test: hello, message: util.format("hello %s", "world") };\n' +
'export {\n' +
' hello2 as hello\n' +
'};\n',
`${commonJsCompatabilityBanner}\n\n` +
`var hello = "world";\n` +
`\n` +
`import util from "node:util";\n` +
`var hello2 = { test: hello, message: util.format("hello %s", "world") };\n` +
`export {\n` +
` hello2 as hello\n` +
`};\n`,
});
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const output = await import(path.join(outDir, 'index.mjs'));
Expand Down Expand Up @@ -450,15 +453,13 @@ describe('test builder', () => {
);
assert.deepEqual(await read(outDir), {
'index.mjs':
'import { createRequire as __createRequire } from "node:module";\n' +
'const require = __createRequire(import.meta.url);\n' +
'\n' +
'import { hello as test } from "test-esm-module";\n' +
'import util from "node:util";\n' +
'var hello = { test, message: util.format("hello %s", "world") };\n' +
'export {\n' +
' hello\n' +
'};\n',
`${commonJsCompatabilityBanner}\n\n` +
`import { hello as test } from "test-esm-module";\n` +
`import util from "node:util";\n` +
`var hello = { test, message: util.format("hello %s", "world") };\n` +
`export {\n` +
` hello\n` +
`};\n`,
});
});

Expand Down
19 changes: 19 additions & 0 deletions src/test/commonjs.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// commonjs.spec.ts

import { strict as assert } from 'node:assert';

import got from 'got';
import jsonSchemaRefParser from '@apidevtools/json-schema-ref-parser';

import { describe, it } from '../describe-it.test';

describe('commonjs', () => {
it('should work with CJS version of got', async () => {
assert.equal(typeof got, 'function');
assert.equal(typeof got.put, 'function');
});

it('should work with CJS module that uses __dirname', async () => {
assert.equal(typeof jsonSchemaRefParser.parse, 'function');
});
});
2 changes: 2 additions & 0 deletions src/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import './lib/es2023.spec';
import './lib/esnext.full.spec';

import './commonjs.spec';

import './typescript/typescript-4.4.spec';
import './typescript/typescript-4.5.spec';
import './typescript/typescript-4.6.spec';
Expand Down
7 changes: 0 additions & 7 deletions src/test/typescript/typescript-5.0.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import { strict as assert } from 'node:assert';

import got from 'got'; // CJS version of got

import moduleDefault, { test } from './module.test';
import moduleDirectory from './module-directory';

Expand All @@ -26,11 +24,6 @@ describe('typescript-5.0', () => {
assert.equal(moduleDirectory(), 'module-directory-index');
});

it('should work with CJS modules', async () => {
assert.equal(typeof got, 'function');
assert.equal(typeof got.put, 'function');
});

it('supports const type parameters', async () => {
type HasNames = { names: readonly string[] };

Expand Down

0 comments on commit 44f3a62

Please sign in to comment.