@checkdigit/typescript-config 3.3.0-PR.30-a6df → 3.3.0-PR.30-1eae
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/builder.mjs +16 -13
- package/package.json +1 -1
- package/src/builder/builder.mts +42 -13
- package/src/builder/builder.spec.mts +95 -0
package/bin/builder.mjs
CHANGED
@@ -39,13 +39,14 @@ function setup(type2) {
|
|
39
39
|
});
|
40
40
|
};
|
41
41
|
}
|
42
|
-
async function builder_default({ type: type2, inDir: inDir2, outDir: outDir2 }) {
|
42
|
+
async function builder_default({ type: type2, entryPoint, inDir: inDir2, outDir: outDir2, outFile }) {
|
43
43
|
const messages2 = [];
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
(file) => file.endsWith(".ts")
|
44
|
+
assert.ok(
|
45
|
+
entryPoint === void 0 && outFile === void 0 || entryPoint !== void 0 && outFile !== void 0,
|
46
|
+
"entryPoint and outFile must both be provided"
|
48
47
|
);
|
48
|
+
const allSourceFiles = await getFiles(inDir2);
|
49
|
+
const productionSourceFiles = entryPoint === void 0 ? allSourceFiles.filter((file) => file.endsWith(".ts")) : [path.join(inDir2, entryPoint)];
|
49
50
|
const configFile = typescript.readConfigFile("./tsconfig.json", (name) => typescript.sys.readFile(name));
|
50
51
|
const compilerOptions = typescript.parseJsonConfigFileContent(configFile.config, typescript.sys, "./");
|
51
52
|
const program = typescript.createProgram(productionSourceFiles, {
|
@@ -78,16 +79,18 @@ async function builder_default({ type: type2, inDir: inDir2, outDir: outDir2 })
|
|
78
79
|
bundle: true,
|
79
80
|
platform: "node",
|
80
81
|
format: type2 === "module" ? "esm" : "cjs",
|
81
|
-
outdir: outDir2,
|
82
82
|
sourcemap: "inline",
|
83
83
|
sourcesContent: false,
|
84
|
-
|
85
|
-
|
86
|
-
{
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
84
|
+
...outFile === void 0 ? {
|
85
|
+
outdir: outDir2,
|
86
|
+
outExtension: { ".js": type2 === "module" ? ".mjs" : ".cjs" },
|
87
|
+
plugins: [
|
88
|
+
{
|
89
|
+
name: "resolve-ts",
|
90
|
+
setup: setup(type2)
|
91
|
+
}
|
92
|
+
]
|
93
|
+
} : { outfile: path.join(outDir2, outFile) }
|
91
94
|
});
|
92
95
|
messages2.push(...buildResult.errors.map((error) => `esbuild error: ${error.text}`));
|
93
96
|
messages2.push(...buildResult.warnings.map((warning) => `esbuild warning: ${warning.text}`));
|
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-1eae","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.6","@typescript-eslint/parser":"^5.59.6","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","max-lines":"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,9 +8,31 @@ import typescript from 'typescript';
|
|
8
8
|
import { PluginBuild, build } from 'esbuild';
|
9
9
|
|
10
10
|
export interface BuilderOptions {
|
11
|
+
/**
|
12
|
+
* whether to produce ESM or CommonJS code
|
13
|
+
*/
|
11
14
|
type: 'module' | 'commonjs';
|
15
|
+
|
16
|
+
/**
|
17
|
+
* the entry point for the bundle, relative to the inDir. if not provided, the files in the inDir will be processed
|
18
|
+
* as individual unbundled files
|
19
|
+
*/
|
20
|
+
entryPoint?: string;
|
21
|
+
|
22
|
+
/**
|
23
|
+
* source code
|
24
|
+
*/
|
12
25
|
inDir: string;
|
26
|
+
|
27
|
+
/**
|
28
|
+
* build directory
|
29
|
+
*/
|
13
30
|
outDir: string;
|
31
|
+
|
32
|
+
/**
|
33
|
+
* build file, relative to the outDir
|
34
|
+
*/
|
35
|
+
outFile?: string;
|
14
36
|
}
|
15
37
|
|
16
38
|
/**
|
@@ -51,17 +73,20 @@ function setup(type: 'module' | 'commonjs') {
|
|
51
73
|
}
|
52
74
|
|
53
75
|
// eslint-disable-next-line func-names,max-lines-per-function,max-statements
|
54
|
-
export default async function ({ type, inDir, outDir }: BuilderOptions): Promise<string[]> {
|
76
|
+
export default async function ({ type, entryPoint, inDir, outDir, outFile }: BuilderOptions): Promise<string[]> {
|
55
77
|
const messages: string[] = [];
|
56
78
|
|
79
|
+
assert.ok(
|
80
|
+
(entryPoint === undefined && outFile === undefined) || (entryPoint !== undefined && outFile !== undefined),
|
81
|
+
'entryPoint and outFile must both be provided'
|
82
|
+
);
|
83
|
+
|
57
84
|
/**
|
58
85
|
* Emit declarations using typescript compiler
|
59
86
|
*/
|
60
87
|
const allSourceFiles = await getFiles(inDir);
|
61
|
-
const productionSourceFiles =
|
62
|
-
|
63
|
-
(file) => file.endsWith('.ts')
|
64
|
-
);
|
88
|
+
const productionSourceFiles =
|
89
|
+
entryPoint === undefined ? allSourceFiles.filter((file) => file.endsWith('.ts')) : [path.join(inDir, entryPoint)];
|
65
90
|
|
66
91
|
const configFile = typescript.readConfigFile('./tsconfig.json', (name) => typescript.sys.readFile(name));
|
67
92
|
const compilerOptions = typescript.parseJsonConfigFileContent(configFile.config, typescript.sys, './');
|
@@ -101,16 +126,20 @@ export default async function ({ type, inDir, outDir }: BuilderOptions): Promise
|
|
101
126
|
bundle: true,
|
102
127
|
platform: 'node',
|
103
128
|
format: type === 'module' ? 'esm' : 'cjs',
|
104
|
-
outdir: outDir,
|
105
129
|
sourcemap: 'inline',
|
106
130
|
sourcesContent: false,
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
131
|
+
...(outFile === undefined
|
132
|
+
? {
|
133
|
+
outdir: outDir,
|
134
|
+
outExtension: { '.js': type === 'module' ? '.mjs' : '.cjs' },
|
135
|
+
plugins: [
|
136
|
+
{
|
137
|
+
name: 'resolve-ts',
|
138
|
+
setup: setup(type),
|
139
|
+
},
|
140
|
+
],
|
141
|
+
}
|
142
|
+
: { outfile: path.join(outDir, outFile) }),
|
114
143
|
});
|
115
144
|
|
116
145
|
messages.push(...buildResult.errors.map((error) => `esbuild error: ${error.text}`));
|
@@ -22,6 +22,10 @@ const twoModules = {
|
|
22
22
|
[`thing.ts`]: `export const hello = 'world';`,
|
23
23
|
};
|
24
24
|
|
25
|
+
const exportDefaultFunctionModule = {
|
26
|
+
[`index.ts`]: `export default function () { return 'hello world' }\n`,
|
27
|
+
};
|
28
|
+
|
25
29
|
async function write(dir: string, files: Record<string, string>): Promise<void> {
|
26
30
|
await fs.mkdir(dir, { recursive: true });
|
27
31
|
await Promise.all(Object.entries(files).map(([name, content]) => fs.writeFile(path.join(dir, name), content)));
|
@@ -101,6 +105,72 @@ describe('test builder', () => {
|
|
101
105
|
assert.equal(output.hello, 'world');
|
102
106
|
});
|
103
107
|
|
108
|
+
it('should build a single ESM module that exports function as default', async () => {
|
109
|
+
const id = uuid();
|
110
|
+
const inDir = path.join(os.tmpdir(), `in-dir-${id}`, 'src');
|
111
|
+
const outDir = path.join(os.tmpdir(), `out-dir-${id}`, 'build');
|
112
|
+
await write(inDir, exportDefaultFunctionModule);
|
113
|
+
assert.deepEqual(await builder({ type: 'module', inDir, outDir }), []);
|
114
|
+
assert.deepEqual(await read(outDir), {
|
115
|
+
'index.d.ts': 'export default function (): string;\n',
|
116
|
+
'index.mjs':
|
117
|
+
'function src_default() {\n' +
|
118
|
+
' return "hello world";\n' +
|
119
|
+
'}\n' +
|
120
|
+
'export {\n' +
|
121
|
+
' src_default as default\n' +
|
122
|
+
'};\n',
|
123
|
+
});
|
124
|
+
|
125
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
126
|
+
const output = await import(path.join(outDir, 'index.mjs'));
|
127
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
128
|
+
assert.equal(output.default(), 'hello world');
|
129
|
+
});
|
130
|
+
|
131
|
+
it('should build a single CJS module that exports function as default', async () => {
|
132
|
+
const id = uuid();
|
133
|
+
const inDir = path.join(os.tmpdir(), `in-dir-${id}`, 'src');
|
134
|
+
const outDir = path.join(os.tmpdir(), `out-dir-${id}`, 'build');
|
135
|
+
await write(inDir, exportDefaultFunctionModule);
|
136
|
+
assert.deepEqual(await builder({ type: 'commonjs', inDir, outDir }), []);
|
137
|
+
assert.deepEqual(await read(outDir), {
|
138
|
+
'index.cjs':
|
139
|
+
'var __defProp = Object.defineProperty;\n' +
|
140
|
+
'var __getOwnPropDesc = Object.getOwnPropertyDescriptor;\n' +
|
141
|
+
'var __getOwnPropNames = Object.getOwnPropertyNames;\n' +
|
142
|
+
'var __hasOwnProp = Object.prototype.hasOwnProperty;\n' +
|
143
|
+
'var __export = (target, all) => {\n' +
|
144
|
+
' for (var name in all)\n' +
|
145
|
+
' __defProp(target, name, { get: all[name], enumerable: true });\n' +
|
146
|
+
'};\n' +
|
147
|
+
'var __copyProps = (to, from, except, desc) => {\n' +
|
148
|
+
' if (from && typeof from === "object" || typeof from === "function") {\n' +
|
149
|
+
' for (let key of __getOwnPropNames(from))\n' +
|
150
|
+
' if (!__hasOwnProp.call(to, key) && key !== except)\n' +
|
151
|
+
' __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n' +
|
152
|
+
' }\n' +
|
153
|
+
' return to;\n' +
|
154
|
+
'};\n' +
|
155
|
+
'var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);\n' +
|
156
|
+
'\n' +
|
157
|
+
'var src_exports = {};\n' +
|
158
|
+
'__export(src_exports, {\n' +
|
159
|
+
' default: () => src_default\n' +
|
160
|
+
'});\n' +
|
161
|
+
'module.exports = __toCommonJS(src_exports);\n' +
|
162
|
+
'function src_default() {\n' +
|
163
|
+
' return "hello world";\n' +
|
164
|
+
'}\n',
|
165
|
+
'index.d.ts': 'export default function (): string;\n',
|
166
|
+
});
|
167
|
+
|
168
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
169
|
+
const output = require(path.join(outDir, 'index.cjs'));
|
170
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
171
|
+
assert.equal(output.default(), 'hello world');
|
172
|
+
});
|
173
|
+
|
104
174
|
it('should build an ESM module that imports a second ESM module', async () => {
|
105
175
|
const id = uuid();
|
106
176
|
const inDir = path.join(os.tmpdir(), `in-dir-${id}`, 'src');
|
@@ -235,4 +305,29 @@ describe('test builder', () => {
|
|
235
305
|
const output2 = await import(path.join(outDir, 'index.cjs'));
|
236
306
|
assert.equal(output2.default.default, 'worldworld');
|
237
307
|
});
|
308
|
+
|
309
|
+
it('should bundle an ESM module that imports a second ESM module', async () => {
|
310
|
+
const id = uuid();
|
311
|
+
const inDir = path.join(os.tmpdir(), `in-dir-${id}`, 'src');
|
312
|
+
const outDir = path.join(os.tmpdir(), `out-dir-${id}`, 'build');
|
313
|
+
await write(inDir, twoModules);
|
314
|
+
assert.deepEqual(
|
315
|
+
await builder({ type: 'module', entryPoint: 'index.ts', outFile: 'index.mjs', inDir, outDir }),
|
316
|
+
[]
|
317
|
+
);
|
318
|
+
assert.deepEqual(await read(outDir), {
|
319
|
+
'index.d.ts': 'declare const _default: string;\nexport default _default;\n',
|
320
|
+
'index.mjs':
|
321
|
+
'var hello = "world";\n' +
|
322
|
+
'\n' +
|
323
|
+
'var src_default = hello + "world";\n' +
|
324
|
+
'export {\n' +
|
325
|
+
' src_default as default\n' +
|
326
|
+
'};\n',
|
327
|
+
'thing.d.ts': 'export declare const hello = "world";\n',
|
328
|
+
});
|
329
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
330
|
+
const output = await import(path.join(outDir, 'index.mjs'));
|
331
|
+
assert.equal(output.default, 'worldworld');
|
332
|
+
});
|
238
333
|
});
|