@checkdigit/typescript-config 3.4.0 → 3.6.0-PR.38-9dab

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -25,7 +25,7 @@ types, and `esbuild` for generating code.
25
25
 
26
26
  #### Options
27
27
 
28
- - `--type` the type of module to generate. Defaults to `module` (ESM). Valid values are `commonjs` and `module`.
28
+ - `--type` the type of output to generate. Defaults to `module` (ESM). Valid values are `commonjs`, `module` or `types`.
29
29
  - `--entryPoint` the entry point for the bundle, relative to the inDir. if not provided, the files in the inDir will
30
30
  be processed as individual unbundled files.
31
31
  - `--inDir` the input source code directory.
package/bin/builder.mjs CHANGED
@@ -78,8 +78,9 @@ async function builder_default({
78
78
  moduleResolution: typescript.ModuleResolutionKind.Bundler,
79
79
  target: typescript.ScriptTarget.ESNext,
80
80
  declaration: true,
81
+ noEmit: type2 !== "types",
81
82
  noEmitOnError: true,
82
- emitDeclarationOnly: true,
83
+ emitDeclarationOnly: type2 === "types",
83
84
  rootDir: inDir2,
84
85
  outDir: outDir2,
85
86
  noLib: false,
@@ -120,8 +121,11 @@ async function builder_default({
120
121
  messages2.push(`tsc: ${typescript.flattenDiagnosticMessageText(diagnostic.messageText, "\n")}`);
121
122
  }
122
123
  }
123
- if (emitResult.emitSkipped) {
124
- throw new Error(`TypeScript compilation failed ${JSON.stringify(messages2)}`);
124
+ if (messages2.length > 0) {
125
+ throw new Error(`tsc failed ${JSON.stringify(messages2)}`);
126
+ }
127
+ if (type2 === "types") {
128
+ return [];
125
129
  }
126
130
  const buildResult = await build({
127
131
  entryPoints: productionSourceFiles,
@@ -177,7 +181,7 @@ var {
177
181
  sourceMap: { type: "boolean", short: "s", default: false }
178
182
  }
179
183
  });
180
- assert2.ok(type === "module" || type === "commonjs", "type must be module or commonjs");
184
+ assert2.ok(type === "module" || type === "commonjs" || type === "types", "type must be types, module or commonjs");
181
185
  assert2.ok(inDir !== void 0, "inDir is required");
182
186
  assert2.ok(outDir !== void 0, "outDir is required");
183
187
  var messages = await builder_default({
package/package.json CHANGED
@@ -1,196 +1 @@
1
- {
2
- "name": "@checkdigit/typescript-config",
3
- "version": "3.4.0",
4
- "description": "Check Digit standard Typescript configuration",
5
- "prettier": "@checkdigit/prettier-config",
6
- "engines": {
7
- "node": ">=16"
8
- },
9
- "bin": {
10
- "builder": "./bin/builder.mjs"
11
- },
12
- "peerDependencies": {
13
- "@types/node": ">=16",
14
- "esbuild": "0.17.19",
15
- "typescript": ">=5.0.4 <5.1"
16
- },
17
- "repository": {
18
- "type": "git",
19
- "url": "git+https://github.com/checkdigit/typescript-config.git"
20
- },
21
- "author": "Check Digit, LLC",
22
- "license": "MIT",
23
- "bugs": {
24
- "url": "https://github.com/checkdigit/typescript-config/issues"
25
- },
26
- "homepage": "https://github.com/checkdigit/typescript-config#readme",
27
- "scripts": {
28
- "preversion": "npm test",
29
- "postversion": "git push && git push --tags",
30
- "prepare": "npm run build-builder",
31
- "lint:fix": "eslint -f unix --ext .ts,.mts src --fix",
32
- "lint": "eslint -f unix --ext .ts,.mts src",
33
- "prettier": "prettier --ignore-path .gitignore --list-different .",
34
- "prettier:fix": "prettier --ignore-path .gitignore --write .",
35
- "test": "npm run ci:compile && npm run ci:test && npm run ci:lint && npm run ci:style",
36
- "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",
37
- "build-cjs": "rimraf build-cjs && npx builder --type=commonjs --outDir=build-cjs",
38
- "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",
39
- "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",
40
- "build-esm": "rimraf build-esm && npx builder --type=module --outDir=build-esm",
41
- "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",
42
- "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",
43
- "test-jest-esm": "NODE_OPTIONS=\"--experimental-vm-modules\" jest --coverage=false src/*.mts src/*/*.mts src/*/*/*.mts",
44
- "test-jest-cjs": "jest --coverage=false src/*.ts src/*/*.ts src/*/*/*.ts",
45
- "test-cjs": "node --test build-cjs/test/index.test.cjs",
46
- "test-cjs-bundle": "node --test build-cjs-bundle/test/index.test.cjs",
47
- "test-cjs-bundle-no-external": "node --test build-cjs-bundle-no-external/test/index.test.cjs",
48
- "test-esm": "node --test build-esm/test/index.test.mjs",
49
- "test-esm-bundle": "echo \"node --test build-esm-bundle/test/index.test.mjs\"",
50
- "test-esm-bundle-no-external": "node --test build-esm-bundle-no-external/test/index.test.mjs",
51
- "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",
52
- "ci:compile": "npm run build-builder && 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",
53
- "ci:lint": "npm run lint",
54
- "ci:style": "npm run prettier"
55
- },
56
- "devDependencies": {
57
- "@checkdigit/prettier-config": "^3.4.0",
58
- "@types/debug": "^4.1.7",
59
- "@types/jest": "^29.5.1",
60
- "@types/uuid": "^9.0.1",
61
- "@typescript-eslint/eslint-plugin": "^5.59.6",
62
- "@typescript-eslint/parser": "^5.59.6",
63
- "debug": "^4.3.4",
64
- "eslint": "^8.40.0",
65
- "eslint-config-prettier": "^8.8.0",
66
- "get-port": "^6.1.2",
67
- "got": "^11.8.6",
68
- "jest": "^29.5.0",
69
- "rimraf": "^5.0.1",
70
- "ts-jest": "^29.1.0",
71
- "uuid": "^9.0.0"
72
- },
73
- "eslintConfig": {
74
- "parser": "@typescript-eslint/parser",
75
- "plugins": [
76
- "@typescript-eslint"
77
- ],
78
- "parserOptions": {
79
- "project": "./tsconfig.json"
80
- },
81
- "extends": [
82
- "eslint:all",
83
- "plugin:@typescript-eslint/recommended",
84
- "plugin:@typescript-eslint/recommended-requiring-type-checking",
85
- "plugin:@typescript-eslint/strict",
86
- "prettier"
87
- ],
88
- "rules": {
89
- "@typescript-eslint/non-nullable-type-assertion-style": "error",
90
- "capitalized-comments": "off",
91
- "one-var": "off",
92
- "sort-keys": "off",
93
- "sort-imports": "off",
94
- "func-style": [
95
- "error",
96
- "declaration",
97
- {
98
- "allowArrowFunctions": true
99
- }
100
- ],
101
- "no-magic-numbers": [
102
- "error",
103
- {
104
- "ignore": [
105
- 0,
106
- 1,
107
- 2
108
- ]
109
- }
110
- ],
111
- "no-undefined": "off",
112
- "no-ternary": "off"
113
- },
114
- "overrides": [
115
- {
116
- "files": [
117
- "*.spec.mts",
118
- "*.test.mts",
119
- "*.spec.ts",
120
- "*.test.ts"
121
- ],
122
- "rules": {
123
- "@typescript-eslint/non-nullable-type-assertion-style": "off",
124
- "@typescript-eslint/ban-types": "off",
125
- "@typescript-eslint/require-await": "off",
126
- "@typescript-eslint/consistent-type-definitions": "off",
127
- "@typescript-eslint/ban-ts-comment": "off",
128
- "@typescript-eslint/no-unnecessary-condition": "off",
129
- "@typescript-eslint/consistent-indexed-object-style": "off",
130
- "@typescript-eslint/no-unused-vars": "off",
131
- "@typescript-eslint/no-unsafe-member-access": "off",
132
- "line-comment-position": "off",
133
- "no-inline-comments": "off",
134
- "no-param-reassign": "off",
135
- "id-length": "off",
136
- "no-magic-numbers": "off",
137
- "func-names": "off",
138
- "no-duplicate-imports": "off",
139
- "symbol-description": "off",
140
- "no-invalid-this": "off",
141
- "max-lines-per-function": "off",
142
- "max-lines": "off",
143
- "max-statements": "off",
144
- "no-await-in-loop": "off"
145
- }
146
- }
147
- ]
148
- },
149
- "jest": {
150
- "moduleFileExtensions": [
151
- "js",
152
- "mjs",
153
- "cjs",
154
- "ts",
155
- "mts",
156
- "json",
157
- "node"
158
- ],
159
- "extensionsToTreatAsEsm": [
160
- ".mts"
161
- ],
162
- "transform": {
163
- "^.+\\.mts$": [
164
- "ts-jest",
165
- {
166
- "isolatedModules": true,
167
- "diagnostics": false,
168
- "useESM": true
169
- }
170
- ],
171
- "^.+\\.ts$": [
172
- "ts-jest",
173
- {
174
- "isolatedModules": true,
175
- "diagnostics": false,
176
- "useESM": false
177
- }
178
- ]
179
- },
180
- "collectCoverageFrom": [
181
- "<rootDir>/src/**",
182
- "!<rootDir>/src/**/*.spec.mts",
183
- "!<rootDir>/src/**/*.test.mts",
184
- "!<rootDir>/src/**/*.spec.ts",
185
- "!<rootDir>/src/**/*.test.ts"
186
- ],
187
- "testMatch": [
188
- "<rootDir>/src/**/*.spec.ts",
189
- "<rootDir>/src/**/*.spec.mts"
190
- ]
191
- },
192
- "files": [
193
- "tsconfig.json",
194
- "SECURITY.md"
195
- ]
196
- }
1
+ {"name":"@checkdigit/typescript-config","version":"3.6.0-PR.38-9dab","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.8","@typescript-eslint/parser":"^5.59.8","debug":"^4.3.4","eslint":"^8.41.0","eslint-config-prettier":"^8.8.0","get-port":"^6.1.2","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","/src/"]}
@@ -0,0 +1,236 @@
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
+ }
@@ -0,0 +1,44 @@
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
+ }
@@ -0,0 +1,3 @@
1
+ // module-directory/index.ts
2
+
3
+ export default () => 'module-directory-index';