@checkdigit/typescript-config 3.6.0-PR.38-9dab → 3.6.0-PR.38-eb05
Sign up to get free protection for your applications and to get access to all the features.
package/package.json
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"name":"@checkdigit/typescript-config","version":"3.6.0-PR.38-
|
1
|
+
{"name":"@checkdigit/typescript-config","version":"3.6.0-PR.38-eb05","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.19","typescript":">=5.1.3 <5.2"},"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-types":"rimraf build-types && npx builder --type=types --outDir=build-types","build-cjs":"rimraf build-cjs && npx builder --type=commonjs --outDir=build-cjs","build-cjs-bundle":"rimraf build-cjs-bundle && npx builder --type=commonjs --entryPoint=test/index.test.ts --outDir=build-cjs-bundle --outFile=test/index.test.cjs --minify --sourceMap","build-cjs-bundle-no-external":"rimraf build-cjs-bundle-no-external && npx builder --type=commonjs --external=./node_modules/* --entryPoint=test/index.test.ts --outDir=build-cjs-bundle-no-external --outFile=test/index.test.cjs","build-esm":"rimraf build-esm && npx builder --type=module --outDir=build-esm","build-esm-bundle":"rimraf build-esm-bundle && npx builder --type=module --outDir=build-esm-bundle --entryPoint=test/index.test.ts --outFile=test/index.test.mjs","build-esm-bundle-no-external":"rimraf build-esm-bundle-no-external && npx builder --type=module --external=./node_modules/* --outDir=build-esm-bundle-no-external --entryPoint=test/index.test.ts --outFile=test/index.test.mjs --minify","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-cjs":"node --test build-cjs/test/index.test.cjs","test-cjs-bundle":"node --test build-cjs-bundle/test/index.test.cjs","test-cjs-bundle-no-external":"node --test build-cjs-bundle-no-external/test/index.test.cjs","test-esm":"node --test build-esm/test/index.test.mjs","test-esm-bundle":"echo \"node --test build-esm-bundle/test/index.test.mjs\"","test-esm-bundle-no-external":"node --test build-esm-bundle-no-external/test/index.test.mjs","ci:test":"npm run test-jest-cjs && npm run test-jest-esm && npm run test-cjs && npm run test-cjs-bundle && npm run test-cjs-bundle-no-external && npm run test-esm && npm run test-esm-bundle && npm run test-esm-bundle-no-external","ci:compile":"npm run build-builder && npm run build-types && npm run build-cjs && npm run build-cjs-bundle && npm run build-cjs-bundle-no-external && npm run build-esm && npm run build-esm-bundle && npm run build-esm-bundle-no-external","ci:lint":"npm run lint","ci:style":"npm run prettier"},"devDependencies":{"@checkdigit/prettier-config":"^3.4.0","@types/debug":"^4.1.8","@types/jest":"^29.5.2","@types/uuid":"^9.0.1","@typescript-eslint/eslint-plugin":"^5.59.9","@typescript-eslint/parser":"^5.59.9","debug":"^4.3.4","eslint":"^8.42.0","eslint-config-prettier":"^8.8.0","get-port":"^7.0.0","got":"^11.8.6","jest":"^29.5.0","rimraf":"^5.0.1","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","max-statements":"off","no-await-in-loop":"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"]}
|
package/src/builder/builder.mts
DELETED
@@ -1,236 +0,0 @@
|
|
1
|
-
// builder/builder.mts
|
2
|
-
|
3
|
-
import { strict as assert } from 'node:assert';
|
4
|
-
import { promises as fs } from 'node:fs';
|
5
|
-
import path from 'node:path';
|
6
|
-
import typescript from 'typescript';
|
7
|
-
|
8
|
-
import { PluginBuild, build } from 'esbuild';
|
9
|
-
|
10
|
-
export interface BuilderOptions {
|
11
|
-
/**
|
12
|
-
* whether to produce Typescript types, ESM or CommonJS code
|
13
|
-
*/
|
14
|
-
type: 'module' | 'commonjs' | 'types';
|
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 | undefined;
|
21
|
-
|
22
|
-
/**
|
23
|
-
* source code
|
24
|
-
*/
|
25
|
-
inDir: string;
|
26
|
-
|
27
|
-
/**
|
28
|
-
* build directory
|
29
|
-
*/
|
30
|
-
outDir: string;
|
31
|
-
|
32
|
-
/**
|
33
|
-
* build file, relative to the outDir
|
34
|
-
*/
|
35
|
-
outFile?: string | undefined;
|
36
|
-
|
37
|
-
/**
|
38
|
-
* external modules to exclude from the bundle
|
39
|
-
*/
|
40
|
-
external?: string[] | undefined;
|
41
|
-
|
42
|
-
/**
|
43
|
-
* whether to minify output
|
44
|
-
*/
|
45
|
-
minify?: boolean | undefined;
|
46
|
-
|
47
|
-
/**
|
48
|
-
* whether to include sourcemap
|
49
|
-
*/
|
50
|
-
sourceMap?: boolean | undefined;
|
51
|
-
}
|
52
|
-
|
53
|
-
/**
|
54
|
-
* Recursively obtains all files in a directory
|
55
|
-
* @param {string} directory
|
56
|
-
* @returns {Promise<string[]>}
|
57
|
-
*/
|
58
|
-
async function getFiles(directory: string): Promise<string[]> {
|
59
|
-
const entries = await fs.readdir(directory, { withFileTypes: true });
|
60
|
-
const files = await Promise.all(
|
61
|
-
entries.map((entry) => {
|
62
|
-
const result = path.resolve(directory, entry.name);
|
63
|
-
return entry.isDirectory() ? getFiles(result) : result;
|
64
|
-
})
|
65
|
-
);
|
66
|
-
return files.flat();
|
67
|
-
}
|
68
|
-
|
69
|
-
function excludeSourceMaps(filter: RegExp) {
|
70
|
-
return (pluginBuild: PluginBuild) => {
|
71
|
-
// ignore source maps for any Javascript file that matches filter
|
72
|
-
pluginBuild.onLoad({ filter }, async (args) => {
|
73
|
-
if (args.path.endsWith('.js') || args.path.endsWith('.mjs') || args.path.endsWith('.cjs')) {
|
74
|
-
return {
|
75
|
-
contents: `${await fs.readFile(
|
76
|
-
args.path,
|
77
|
-
'utf8'
|
78
|
-
)}\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIiJdLCJtYXBwaW5ncyI6IkEifQ==`,
|
79
|
-
loader: 'default',
|
80
|
-
};
|
81
|
-
}
|
82
|
-
return undefined;
|
83
|
-
});
|
84
|
-
};
|
85
|
-
}
|
86
|
-
|
87
|
-
function resolveTypescriptPaths(type: 'module' | 'commonjs') {
|
88
|
-
const extension = type === 'module' ? 'mjs' : 'cjs';
|
89
|
-
return (pluginBuild: PluginBuild) => {
|
90
|
-
// rewrite paths based on standard node resolution
|
91
|
-
pluginBuild.onResolve({ filter: /.*/u }, async (resolved) => {
|
92
|
-
if (resolved.kind === 'entry-point' || !resolved.path.startsWith('.') || resolved.path.endsWith('.js')) {
|
93
|
-
return { external: resolved.kind !== 'entry-point' };
|
94
|
-
}
|
95
|
-
let isDirectory = false;
|
96
|
-
try {
|
97
|
-
const stats = await fs.lstat(path.join(resolved.resolveDir, resolved.path));
|
98
|
-
isDirectory = stats.isDirectory();
|
99
|
-
} catch {
|
100
|
-
// do nothing
|
101
|
-
}
|
102
|
-
let newPath = resolved.path;
|
103
|
-
newPath += isDirectory ? `/index.${extension}` : `.${extension}`;
|
104
|
-
return { path: newPath, external: true };
|
105
|
-
});
|
106
|
-
};
|
107
|
-
}
|
108
|
-
|
109
|
-
// eslint-disable-next-line func-names,max-lines-per-function,max-statements
|
110
|
-
export default async function ({
|
111
|
-
type,
|
112
|
-
entryPoint,
|
113
|
-
inDir,
|
114
|
-
outDir,
|
115
|
-
outFile,
|
116
|
-
external = [],
|
117
|
-
minify = false,
|
118
|
-
sourceMap,
|
119
|
-
}: BuilderOptions): Promise<string[]> {
|
120
|
-
const messages: string[] = [];
|
121
|
-
|
122
|
-
assert.ok(
|
123
|
-
(entryPoint === undefined && outFile === undefined) || (entryPoint !== undefined && outFile !== undefined),
|
124
|
-
'entryPoint and outFile must both be provided'
|
125
|
-
);
|
126
|
-
|
127
|
-
const allSourceFiles = await getFiles(inDir);
|
128
|
-
const productionSourceFiles =
|
129
|
-
entryPoint === undefined ? allSourceFiles.filter((file) => file.endsWith('.ts')) : [path.join(inDir, entryPoint)];
|
130
|
-
|
131
|
-
/**
|
132
|
-
* Emit declarations using typescript compiler, if type is 'types'. Otherwise, just compile to ensure the build is good.
|
133
|
-
*/
|
134
|
-
const program = typescript.createProgram(productionSourceFiles, {
|
135
|
-
module: typescript.ModuleKind.ESNext,
|
136
|
-
moduleResolution: typescript.ModuleResolutionKind.Bundler,
|
137
|
-
target: typescript.ScriptTarget.ESNext,
|
138
|
-
declaration: true,
|
139
|
-
noEmit: type !== 'types',
|
140
|
-
noEmitOnError: true,
|
141
|
-
emitDeclarationOnly: type === 'types',
|
142
|
-
rootDir: inDir,
|
143
|
-
outDir,
|
144
|
-
noLib: false,
|
145
|
-
skipLibCheck: true,
|
146
|
-
strict: true,
|
147
|
-
preserveConstEnums: true,
|
148
|
-
noImplicitReturns: true,
|
149
|
-
noUnusedLocals: true,
|
150
|
-
noUnusedParameters: true,
|
151
|
-
alwaysStrict: true,
|
152
|
-
verbatimModuleSyntax: false,
|
153
|
-
noFallthroughCasesInSwitch: true,
|
154
|
-
forceConsistentCasingInFileNames: true,
|
155
|
-
emitDecoratorMetadata: true,
|
156
|
-
experimentalDecorators: true,
|
157
|
-
resolveJsonModule: true,
|
158
|
-
esModuleInterop: true,
|
159
|
-
noUncheckedIndexedAccess: true,
|
160
|
-
noPropertyAccessFromIndexSignature: true,
|
161
|
-
allowUnusedLabels: false,
|
162
|
-
allowUnreachableCode: false,
|
163
|
-
noImplicitOverride: true,
|
164
|
-
useUnknownInCatchVariables: true,
|
165
|
-
exactOptionalPropertyTypes: true,
|
166
|
-
});
|
167
|
-
const emitResult = program.emit();
|
168
|
-
const allDiagnostics = typescript.sortAndDeduplicateDiagnostics([
|
169
|
-
...typescript.getPreEmitDiagnostics(program),
|
170
|
-
...emitResult.diagnostics,
|
171
|
-
]);
|
172
|
-
for (const diagnostic of allDiagnostics) {
|
173
|
-
if (diagnostic.file) {
|
174
|
-
assert.ok(diagnostic.start !== undefined);
|
175
|
-
const { line, character } = typescript.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
|
176
|
-
const message = typescript.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
|
177
|
-
messages.push(`tsc: ${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
|
178
|
-
} else {
|
179
|
-
// eslint-disable-next-line no-console
|
180
|
-
messages.push(`tsc: ${typescript.flattenDiagnosticMessageText(diagnostic.messageText, '\n')}`);
|
181
|
-
}
|
182
|
-
}
|
183
|
-
|
184
|
-
if (messages.length > 0) {
|
185
|
-
throw new Error(`tsc failed ${JSON.stringify(messages)}`);
|
186
|
-
}
|
187
|
-
|
188
|
-
if (type === 'types') {
|
189
|
-
return [];
|
190
|
-
}
|
191
|
-
|
192
|
-
/**
|
193
|
-
* Emit ESM javascript using esbuild
|
194
|
-
*/
|
195
|
-
const buildResult = await build({
|
196
|
-
entryPoints: productionSourceFiles,
|
197
|
-
bundle: true,
|
198
|
-
minify,
|
199
|
-
platform: 'node',
|
200
|
-
format: type === 'module' ? 'esm' : 'cjs',
|
201
|
-
sourcesContent: false,
|
202
|
-
sourcemap: sourceMap ? 'inline' : false,
|
203
|
-
...(outFile === undefined
|
204
|
-
? {
|
205
|
-
// individual files
|
206
|
-
outdir: outDir,
|
207
|
-
outExtension: { '.js': type === 'module' ? '.mjs' : '.cjs' },
|
208
|
-
plugins: [
|
209
|
-
{
|
210
|
-
name: 'resolve-typescript-paths',
|
211
|
-
setup: resolveTypescriptPaths(type),
|
212
|
-
},
|
213
|
-
],
|
214
|
-
}
|
215
|
-
: {
|
216
|
-
// bundling
|
217
|
-
outfile: path.join(outDir, outFile),
|
218
|
-
legalComments: 'none',
|
219
|
-
external,
|
220
|
-
plugins: [
|
221
|
-
{
|
222
|
-
name: 'exclude-source-maps',
|
223
|
-
setup: excludeSourceMaps(/node_modules/u),
|
224
|
-
},
|
225
|
-
],
|
226
|
-
}),
|
227
|
-
});
|
228
|
-
|
229
|
-
messages.push(...buildResult.errors.map((error) => `esbuild error: ${error.text}`));
|
230
|
-
messages.push(...buildResult.warnings.map((warning) => `esbuild warning: ${warning.text}`));
|
231
|
-
if (messages.length > 0) {
|
232
|
-
throw new Error(`esbuild failed ${JSON.stringify(messages)}`);
|
233
|
-
}
|
234
|
-
|
235
|
-
return messages;
|
236
|
-
}
|
package/src/builder/index.mts
DELETED
@@ -1,44 +0,0 @@
|
|
1
|
-
// builder/index.mts
|
2
|
-
|
3
|
-
import { strict as assert } from 'node:assert';
|
4
|
-
import path from 'node:path';
|
5
|
-
import { parseArgs } from 'node:util';
|
6
|
-
|
7
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
8
|
-
// @ts-expect-error
|
9
|
-
import builder from './builder.mts';
|
10
|
-
|
11
|
-
const {
|
12
|
-
values: { type, inDir, outDir, entryPoint, outFile, external, minify, sourceMap },
|
13
|
-
} = parseArgs({
|
14
|
-
options: {
|
15
|
-
type: { type: 'string', short: 't', default: 'module' },
|
16
|
-
inDir: { type: 'string', short: 'i', default: 'src' },
|
17
|
-
outDir: { type: 'string', short: 'o', default: 'build' },
|
18
|
-
entryPoint: { type: 'string', short: 'e', default: undefined },
|
19
|
-
outFile: { type: 'string', short: 'f', default: undefined },
|
20
|
-
external: { type: 'string', short: 'x', multiple: true, default: [] },
|
21
|
-
minify: { type: 'boolean', short: 'm', default: false },
|
22
|
-
sourceMap: { type: 'boolean', short: 's', default: false },
|
23
|
-
},
|
24
|
-
});
|
25
|
-
|
26
|
-
assert.ok(type === 'module' || type === 'commonjs' || type === 'types', 'type must be types, module or commonjs');
|
27
|
-
assert.ok(inDir !== undefined, 'inDir is required');
|
28
|
-
assert.ok(outDir !== undefined, 'outDir is required');
|
29
|
-
|
30
|
-
const messages = await builder({
|
31
|
-
type,
|
32
|
-
inDir: path.join(process.cwd(), inDir),
|
33
|
-
outDir: path.join(process.cwd(), outDir),
|
34
|
-
entryPoint,
|
35
|
-
outFile,
|
36
|
-
external,
|
37
|
-
minify,
|
38
|
-
sourceMap,
|
39
|
-
});
|
40
|
-
if (messages.length > 0) {
|
41
|
-
// eslint-disable-next-line no-console
|
42
|
-
console.warn(JSON.stringify(messages, undefined, 2));
|
43
|
-
process.exit(1);
|
44
|
-
}
|