@checkdigit/typescript-config 3.3.0-PR.30-3a83 → 3.3.0-PR.30-cf30
Sign up to get free protection for your applications and to get access to all the features.
- package/bin/builder.mjs +20 -9
- package/package.json +1 -1
- package/src/builder/builder.mts +13 -7
- package/src/builder/builder.spec.mts +51 -6
- package/src/builder/index.mts +8 -2
package/bin/builder.mjs
CHANGED
@@ -36,10 +36,9 @@ function setup(pluginBuild) {
|
|
36
36
|
return { path: newPath, external: true };
|
37
37
|
});
|
38
38
|
}
|
39
|
-
async function builder_default({ inDir: inDir2, outDir: outDir2 }) {
|
39
|
+
async function builder_default({ type: type2, inDir: inDir2, outDir: outDir2 }) {
|
40
40
|
const messages2 = [];
|
41
|
-
const
|
42
|
-
const allSourceFiles = await getFiles(sourceDirectory);
|
41
|
+
const allSourceFiles = await getFiles(inDir2);
|
43
42
|
const productionSourceFiles = allSourceFiles.filter(
|
44
43
|
// && !file.endsWith('.test.ts') && !file.endsWith('.spec.ts')
|
45
44
|
(file) => file.endsWith(".ts")
|
@@ -54,7 +53,10 @@ async function builder_default({ inDir: inDir2, outDir: outDir2 }) {
|
|
54
53
|
outDir: outDir2
|
55
54
|
});
|
56
55
|
const emitResult = program.emit();
|
57
|
-
const allDiagnostics =
|
56
|
+
const allDiagnostics = typescript.sortAndDeduplicateDiagnostics([
|
57
|
+
...typescript.getPreEmitDiagnostics(program),
|
58
|
+
...emitResult.diagnostics
|
59
|
+
]);
|
58
60
|
for (const diagnostic of allDiagnostics) {
|
59
61
|
if (diagnostic.file) {
|
60
62
|
assert.ok(diagnostic.start !== void 0);
|
@@ -66,17 +68,17 @@ async function builder_default({ inDir: inDir2, outDir: outDir2 }) {
|
|
66
68
|
}
|
67
69
|
}
|
68
70
|
if (emitResult.emitSkipped) {
|
69
|
-
throw new Error(
|
71
|
+
throw new Error(`TypeScript compilation failed ${JSON.stringify(messages2)}`);
|
70
72
|
}
|
71
73
|
const buildResult = await build({
|
72
74
|
entryPoints: productionSourceFiles,
|
73
75
|
bundle: true,
|
74
76
|
platform: "node",
|
75
|
-
format: "esm",
|
77
|
+
format: type2 === "module" ? "esm" : "cjs",
|
76
78
|
outdir: outDir2,
|
77
79
|
sourcemap: "inline",
|
78
80
|
sourcesContent: false,
|
79
|
-
outExtension: { ".js": ".mjs" },
|
81
|
+
outExtension: { ".js": type2 === "module" ? ".mjs" : ".cjs" },
|
80
82
|
plugins: [
|
81
83
|
{
|
82
84
|
name: "resolve-ts",
|
@@ -86,21 +88,30 @@ async function builder_default({ inDir: inDir2, outDir: outDir2 }) {
|
|
86
88
|
});
|
87
89
|
messages2.push(...buildResult.errors.map((error) => `esbuild error: ${error.text}`));
|
88
90
|
messages2.push(...buildResult.warnings.map((warning) => `esbuild warning: ${warning.text}`));
|
91
|
+
if (messages2.length > 0) {
|
92
|
+
throw new Error(`esbuild failed ${JSON.stringify(messages2)}`);
|
93
|
+
}
|
89
94
|
return messages2;
|
90
95
|
}
|
91
96
|
|
92
97
|
// src/builder/index.mts
|
93
98
|
var {
|
94
|
-
values: { inDir, outDir }
|
99
|
+
values: { type, inDir, outDir }
|
95
100
|
} = parseArgs({
|
96
101
|
options: {
|
102
|
+
type: { type: "string", short: "t", default: "module" },
|
97
103
|
inDir: { type: "string", short: "i", default: "src" },
|
98
104
|
outDir: { type: "string", short: "o", default: "build" }
|
99
105
|
}
|
100
106
|
});
|
107
|
+
assert2.ok(type === "module" || type === "commonjs", "type must be module or commonjs");
|
101
108
|
assert2.ok(inDir !== void 0, "inDir is required");
|
102
109
|
assert2.ok(outDir !== void 0, "outDir is required");
|
103
|
-
var messages = await builder_default({
|
110
|
+
var messages = await builder_default({
|
111
|
+
type,
|
112
|
+
inDir: path2.join(process.cwd(), inDir),
|
113
|
+
outDir: path2.join(process.cwd(), outDir)
|
114
|
+
});
|
104
115
|
if (messages.length > 0) {
|
105
116
|
console.warn(JSON.stringify(messages, void 0, 2));
|
106
117
|
process.exit(1);
|
package/package.json
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"name":"@checkdigit/typescript-config","version":"3.3.0-PR.30-
|
1
|
+
{"name":"@checkdigit/typescript-config","version":"3.3.0-PR.30-cf30","description":"Check Digit standard Typescript configuration","prettier":"@checkdigit/prettier-config","engines":{"node":">=16"},"bin":{"builder":"./bin/builder.mjs"},"peerDependencies":{"@types/node":">=16","esbuild":"0.17.18","typescript":">=5.0.4 <5.1"},"repository":{"type":"git","url":"git+https://github.com/checkdigit/typescript-config.git"},"author":"Check Digit, LLC","license":"MIT","bugs":{"url":"https://github.com/checkdigit/typescript-config/issues"},"homepage":"https://github.com/checkdigit/typescript-config#readme","scripts":{"preversion":"npm test","postversion":"git push && git push --tags","prepare":"npm run build-builder","lint:fix":"eslint -f unix --ext .ts,.mts src --fix","lint":"eslint -f unix --ext .ts,.mts src","prettier":"prettier --ignore-path .gitignore --list-different .","prettier:fix":"prettier --ignore-path .gitignore --write .","test":"npm run ci:compile && npm run ci:test && npm run ci:lint && npm run ci:style","build-builder":"esbuild src/builder/index.mts --bundle --platform=node --format=esm --external:typescript --external:esbuild --outfile=build-builder/builder.mjs && mkdir -p bin && { echo '#!/usr/bin/env node'; cat build-builder/builder.mjs; } > bin/builder.mjs && chmod +x bin/builder.mjs","build-tsc-cjs":"rimraf build-tsc-cjs && tsc --outDir build-tsc-cjs","build-esbuild-cjs":"rimraf build-esbuild-cjs && esbuild ./src/*.ts ./src/*/*.ts --platform=node --bundle --format=cjs --sourcemap=inline --sources-content=false --outdir=build-esbuild-cjs","build-esbuild-cjs-bundle":"rimraf build-esbuild-cjs-bundle && esbuild src/test/index.test.ts --bundle --platform=node --format=cjs --sourcemap=inline --sources-content=false --outfile=build-esbuild-cjs-bundle/test/index.test.cjs","build-esbuild-esm":"rimraf build-esbuild-esm && node bin/builder.mjs --outDir=build-esbuild-esm","build-esbuild-esm-bundle":"rimraf build-esbuild-esm-bundle && esbuild src/test/index.test.ts --bundle --platform=node --format=esm --sourcemap=inline --sources-content=false --outfile=build-esbuild-esm-bundle/test/index.test.mjs","test-jest-esm":"NODE_OPTIONS=\"--experimental-vm-modules\" jest --coverage=false src/*.mts src/*/*.mts src/*/*/*.mts","test-jest-cjs":"jest --coverage=false src/*.ts src/*/*.ts src/*/*/*.ts","test-tsc-cjs":"node --test build-tsc-cjs/test/index.test.js","test-esbuild-cjs":"node --test build-esbuild-cjs/test/index.test.js","test-esbuild-cjs-bundle":"node --test build-esbuild-cjs-bundle/test/index.test.cjs","test-esbuild-esm":"node --test build-esbuild-esm/test/index.test.mjs","test-esbuild-esm-bundle":"echo 'node -test build-esbuild-esm-bundle/test/index.test.mjs'","ci:test":"npm run test-jest-cjs && npm run test-jest-esm &&npm run test-tsc-cjs && npm run test-esbuild-cjs && npm run test-esbuild-cjs-bundle && npm run test-esbuild-esm && npm run test-esbuild-esm-bundle","ci:compile":"npm run build-builder && npm run build-tsc-cjs && npm run build-esbuild-cjs && npm run build-esbuild-cjs-bundle && npm run build-esbuild-esm && npm run build-esbuild-esm-bundle","ci:lint":"npm run lint","ci:style":"npm run prettier"},"devDependencies":{"@checkdigit/prettier-config":"^3.4.0","@types/debug":"^4.1.7","@types/jest":"^29.5.1","@types/uuid":"^9.0.1","@typescript-eslint/eslint-plugin":"^5.59.5","@typescript-eslint/parser":"^5.59.5","debug":"^4.3.4","eslint":"^8.40.0","eslint-config-prettier":"^8.8.0","get-port":"^6.1.2","got":"^11.8.6","jest":"^29.5.0","rimraf":"^5.0.0","ts-jest":"^29.1.0","uuid":"^9.0.0"},"eslintConfig":{"parser":"@typescript-eslint/parser","plugins":["@typescript-eslint"],"parserOptions":{"project":"./tsconfig.json"},"extends":["eslint:all","plugin:@typescript-eslint/recommended","plugin:@typescript-eslint/recommended-requiring-type-checking","plugin:@typescript-eslint/strict","prettier"],"rules":{"@typescript-eslint/non-nullable-type-assertion-style":"error","capitalized-comments":"off","one-var":"off","sort-keys":"off","sort-imports":"off","func-style":["error","declaration",{"allowArrowFunctions":true}],"no-magic-numbers":["error",{"ignore":[0,1,2]}],"no-undefined":"off","no-ternary":"off"},"overrides":[{"files":["*.spec.mts","*.test.mts","*.spec.ts","*.test.ts"],"rules":{"@typescript-eslint/non-nullable-type-assertion-style":"off","@typescript-eslint/ban-types":"off","@typescript-eslint/require-await":"off","@typescript-eslint/consistent-type-definitions":"off","@typescript-eslint/ban-ts-comment":"off","@typescript-eslint/no-unnecessary-condition":"off","@typescript-eslint/consistent-indexed-object-style":"off","@typescript-eslint/no-unused-vars":"off","@typescript-eslint/no-unsafe-member-access":"off","line-comment-position":"off","no-inline-comments":"off","no-param-reassign":"off","id-length":"off","no-magic-numbers":"off","func-names":"off","no-duplicate-imports":"off","symbol-description":"off","no-invalid-this":"off","max-lines-per-function":"off"}}]},"jest":{"moduleFileExtensions":["js","mjs","cjs","ts","mts","json","node"],"extensionsToTreatAsEsm":[".mts"],"transform":{"^.+\\.mts$":["ts-jest",{"isolatedModules":true,"diagnostics":false,"useESM":true}],"^.+\\.ts$":["ts-jest",{"isolatedModules":true,"diagnostics":false,"useESM":false}]},"collectCoverageFrom":["<rootDir>/src/**","!<rootDir>/src/**/*.spec.mts","!<rootDir>/src/**/*.test.mts","!<rootDir>/src/**/*.spec.ts","!<rootDir>/src/**/*.test.ts"],"testMatch":["<rootDir>/src/**/*.spec.ts","<rootDir>/src/**/*.spec.mts"]},"files":["tsconfig.json","SECURITY.md","/src/"]}
|
package/src/builder/builder.mts
CHANGED
@@ -8,6 +8,7 @@ import typescript from 'typescript';
|
|
8
8
|
import { PluginBuild, build } from 'esbuild';
|
9
9
|
|
10
10
|
export interface BuilderOptions {
|
11
|
+
type: 'module' | 'commonjs';
|
11
12
|
inDir: string;
|
12
13
|
outDir: string;
|
13
14
|
}
|
@@ -47,14 +48,13 @@ function setup(pluginBuild: PluginBuild) {
|
|
47
48
|
}
|
48
49
|
|
49
50
|
// eslint-disable-next-line func-names,max-lines-per-function,max-statements
|
50
|
-
export default async function ({ inDir, outDir }: BuilderOptions): Promise<string[]> {
|
51
|
+
export default async function ({ type, inDir, outDir }: BuilderOptions): Promise<string[]> {
|
51
52
|
const messages: string[] = [];
|
52
53
|
|
53
54
|
/**
|
54
55
|
* Emit declarations using typescript compiler
|
55
56
|
*/
|
56
|
-
const
|
57
|
-
const allSourceFiles = await getFiles(sourceDirectory);
|
57
|
+
const allSourceFiles = await getFiles(inDir);
|
58
58
|
const productionSourceFiles = allSourceFiles.filter(
|
59
59
|
// && !file.endsWith('.test.ts') && !file.endsWith('.spec.ts')
|
60
60
|
(file) => file.endsWith('.ts')
|
@@ -71,7 +71,10 @@ export default async function ({ inDir, outDir }: BuilderOptions): Promise<strin
|
|
71
71
|
outDir,
|
72
72
|
});
|
73
73
|
const emitResult = program.emit();
|
74
|
-
const allDiagnostics =
|
74
|
+
const allDiagnostics = typescript.sortAndDeduplicateDiagnostics([
|
75
|
+
...typescript.getPreEmitDiagnostics(program),
|
76
|
+
...emitResult.diagnostics,
|
77
|
+
]);
|
75
78
|
for (const diagnostic of allDiagnostics) {
|
76
79
|
if (diagnostic.file) {
|
77
80
|
assert.ok(diagnostic.start !== undefined);
|
@@ -84,7 +87,7 @@ export default async function ({ inDir, outDir }: BuilderOptions): Promise<strin
|
|
84
87
|
}
|
85
88
|
}
|
86
89
|
if (emitResult.emitSkipped) {
|
87
|
-
throw new Error(
|
90
|
+
throw new Error(`TypeScript compilation failed ${JSON.stringify(messages)}`);
|
88
91
|
}
|
89
92
|
|
90
93
|
/**
|
@@ -94,11 +97,11 @@ export default async function ({ inDir, outDir }: BuilderOptions): Promise<strin
|
|
94
97
|
entryPoints: productionSourceFiles,
|
95
98
|
bundle: true,
|
96
99
|
platform: 'node',
|
97
|
-
format: 'esm',
|
100
|
+
format: type === 'module' ? 'esm' : 'cjs',
|
98
101
|
outdir: outDir,
|
99
102
|
sourcemap: 'inline',
|
100
103
|
sourcesContent: false,
|
101
|
-
outExtension: { '.js': '.mjs' },
|
104
|
+
outExtension: { '.js': type === 'module' ? '.mjs' : '.cjs' },
|
102
105
|
plugins: [
|
103
106
|
{
|
104
107
|
name: 'resolve-ts',
|
@@ -109,5 +112,8 @@ export default async function ({ inDir, outDir }: BuilderOptions): Promise<strin
|
|
109
112
|
|
110
113
|
messages.push(...buildResult.errors.map((error) => `esbuild error: ${error.text}`));
|
111
114
|
messages.push(...buildResult.warnings.map((warning) => `esbuild warning: ${warning.text}`));
|
115
|
+
if (messages.length > 0) {
|
116
|
+
throw new Error(`esbuild failed ${JSON.stringify(messages)}`);
|
117
|
+
}
|
112
118
|
return messages;
|
113
119
|
}
|
@@ -42,7 +42,13 @@ describe('test builder', () => {
|
|
42
42
|
const inDir = path.join(os.tmpdir(), `in-dir-${id}`);
|
43
43
|
const outDir = path.join(os.tmpdir(), `out-dir-${id}`);
|
44
44
|
await write(inDir, { 'index.ts': 'bad code' });
|
45
|
-
await assert.rejects(builder({ inDir, outDir }), {
|
45
|
+
await assert.rejects(builder({ type: 'module', inDir, outDir }), {
|
46
|
+
message: `TypeScript compilation failed ${JSON.stringify([
|
47
|
+
`tsc: ${inDir}/index.ts (1,1): Unexpected keyword or identifier.`,
|
48
|
+
`tsc: ${inDir}/index.ts (1,1): Cannot find name 'bad'.`,
|
49
|
+
`tsc: ${inDir}/index.ts (1,5): Cannot find name 'code'.`,
|
50
|
+
])}`,
|
51
|
+
});
|
46
52
|
await assert.rejects(read(outDir), {
|
47
53
|
message: `ENOENT: no such file or directory, scandir '${outDir}'`,
|
48
54
|
});
|
@@ -52,7 +58,7 @@ describe('test builder', () => {
|
|
52
58
|
const id = uuid();
|
53
59
|
const inDir = path.join(os.tmpdir(), `in-dir-${id}`);
|
54
60
|
const outDir = path.join(os.tmpdir(), `out-dir-${id}`);
|
55
|
-
await assert.rejects(builder({ inDir, outDir }), {
|
61
|
+
await assert.rejects(builder({ type: 'module', inDir, outDir }), {
|
56
62
|
message: `ENOENT: no such file or directory, scandir '${inDir}'`,
|
57
63
|
});
|
58
64
|
await assert.rejects(read(outDir), {
|
@@ -65,7 +71,7 @@ describe('test builder', () => {
|
|
65
71
|
const inDir = path.join(os.tmpdir(), `in-dir-${id}`);
|
66
72
|
const outDir = path.join(os.tmpdir(), `out-dir-${id}`);
|
67
73
|
await write(inDir, {});
|
68
|
-
assert.deepEqual(await builder({ inDir, outDir }), []);
|
74
|
+
assert.deepEqual(await builder({ type: 'module', inDir, outDir }), []);
|
69
75
|
await assert.rejects(read(outDir), {
|
70
76
|
message: `ENOENT: no such file or directory, scandir '${outDir}'`,
|
71
77
|
});
|
@@ -73,13 +79,52 @@ describe('test builder', () => {
|
|
73
79
|
|
74
80
|
it('should build a single ESM module', async () => {
|
75
81
|
const id = uuid();
|
76
|
-
const inDir = path.join(os.tmpdir(), `in-dir-${id}
|
77
|
-
const outDir = path.join(os.tmpdir(), `out-dir-${id}
|
82
|
+
const inDir = path.join(os.tmpdir(), `in-dir-${id}`, 'src');
|
83
|
+
const outDir = path.join(os.tmpdir(), `out-dir-${id}`, 'build');
|
78
84
|
await write(inDir, singleModule);
|
79
|
-
assert.deepEqual(await builder({ inDir, outDir }), []);
|
85
|
+
assert.deepEqual(await builder({ type: 'module', inDir, outDir }), []);
|
80
86
|
assert.deepEqual(await read(outDir), {
|
81
87
|
'index.d.ts': 'export declare const hello = "world";\n',
|
82
88
|
'index.mjs': 'var hello = "world";\nexport {\n hello\n};\n',
|
83
89
|
});
|
84
90
|
});
|
91
|
+
|
92
|
+
it('should build a single CJS module', async () => {
|
93
|
+
const id = uuid();
|
94
|
+
const inDir = path.join(os.tmpdir(), `in-dir-${id}`, 'src');
|
95
|
+
const outDir = path.join(os.tmpdir(), `out-dir-${id}`, 'build');
|
96
|
+
await write(inDir, singleModule);
|
97
|
+
assert.deepEqual(await builder({ type: 'commonjs', inDir, outDir }), []);
|
98
|
+
assert.deepEqual(await read(outDir), {
|
99
|
+
'index.d.ts': 'export declare const hello = "world";\n',
|
100
|
+
'index.cjs':
|
101
|
+
'var __defProp = Object.defineProperty;\n' +
|
102
|
+
'var __getOwnPropDesc = Object.getOwnPropertyDescriptor;\n' +
|
103
|
+
'var __getOwnPropNames = Object.getOwnPropertyNames;\n' +
|
104
|
+
'var __hasOwnProp = Object.prototype.hasOwnProperty;\n' +
|
105
|
+
'var __export = (target, all) => {\n' +
|
106
|
+
' for (var name in all)\n' +
|
107
|
+
' __defProp(target, name, { get: all[name], enumerable: true });\n' +
|
108
|
+
'};\n' +
|
109
|
+
'var __copyProps = (to, from, except, desc) => {\n' +
|
110
|
+
' if (from && typeof from === "object" || typeof from === "function") {\n' +
|
111
|
+
' for (let key of __getOwnPropNames(from))\n' +
|
112
|
+
' if (!__hasOwnProp.call(to, key) && key !== except)\n' +
|
113
|
+
' __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n' +
|
114
|
+
' }\n' +
|
115
|
+
' return to;\n' +
|
116
|
+
'};\n' +
|
117
|
+
'var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);\n' +
|
118
|
+
'\n' +
|
119
|
+
'var src_exports = {};\n' +
|
120
|
+
'__export(src_exports, {\n' +
|
121
|
+
' hello: () => hello\n' +
|
122
|
+
'});\n' +
|
123
|
+
'module.exports = __toCommonJS(src_exports);\n' +
|
124
|
+
'var hello = "world";\n' +
|
125
|
+
'0 && (module.exports = {\n' +
|
126
|
+
' hello\n' +
|
127
|
+
'});\n',
|
128
|
+
});
|
129
|
+
});
|
85
130
|
});
|
package/src/builder/index.mts
CHANGED
@@ -9,18 +9,24 @@ import { parseArgs } from 'node:util';
|
|
9
9
|
import builder from './builder.mts';
|
10
10
|
|
11
11
|
const {
|
12
|
-
values: { inDir, outDir },
|
12
|
+
values: { type, inDir, outDir },
|
13
13
|
} = parseArgs({
|
14
14
|
options: {
|
15
|
+
type: { type: 'string', short: 't', default: 'module' },
|
15
16
|
inDir: { type: 'string', short: 'i', default: 'src' },
|
16
17
|
outDir: { type: 'string', short: 'o', default: 'build' },
|
17
18
|
},
|
18
19
|
});
|
19
20
|
|
21
|
+
assert.ok(type === 'module' || type === 'commonjs', 'type must be module or commonjs');
|
20
22
|
assert.ok(inDir !== undefined, 'inDir is required');
|
21
23
|
assert.ok(outDir !== undefined, 'outDir is required');
|
22
24
|
|
23
|
-
const messages = await builder({
|
25
|
+
const messages = await builder({
|
26
|
+
type,
|
27
|
+
inDir: path.join(process.cwd(), inDir),
|
28
|
+
outDir: path.join(process.cwd(), outDir),
|
29
|
+
});
|
24
30
|
if (messages.length > 0) {
|
25
31
|
// eslint-disable-next-line no-console
|
26
32
|
console.warn(JSON.stringify(messages, undefined, 2));
|