@checkdigit/typescript-config 3.3.0-PR.30-cf30 → 3.3.0-PR.30-dcb8
Sign up to get free protection for your applications and to get access to all the features.
- package/bin/builder.mjs +19 -16
- package/package.json +1 -1
- package/src/builder/builder.mts +20 -17
- package/src/builder/builder.spec.mts +178 -0
package/bin/builder.mjs
CHANGED
@@ -20,21 +20,24 @@ async function getFiles(directory) {
|
|
20
20
|
);
|
21
21
|
return files.flat();
|
22
22
|
}
|
23
|
-
function setup(
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
23
|
+
function setup(type2) {
|
24
|
+
const extension = type2 === "module" ? "mjs" : "cjs";
|
25
|
+
return (pluginBuild) => {
|
26
|
+
pluginBuild.onResolve({ filter: /.*/u }, async (resolved) => {
|
27
|
+
if (resolved.kind === "entry-point" || !resolved.path.startsWith(".") || resolved.path.endsWith(".js")) {
|
28
|
+
return { external: resolved.kind !== "entry-point" };
|
29
|
+
}
|
30
|
+
let isDirectory = false;
|
31
|
+
try {
|
32
|
+
const stats = await fs.lstat(path.join(resolved.resolveDir, resolved.path));
|
33
|
+
isDirectory = stats.isDirectory();
|
34
|
+
} catch {
|
35
|
+
}
|
36
|
+
let newPath = resolved.path;
|
37
|
+
newPath += isDirectory ? `/index.${extension}` : `.${extension}`;
|
38
|
+
return { path: newPath, external: true };
|
39
|
+
});
|
40
|
+
};
|
38
41
|
}
|
39
42
|
async function builder_default({ type: type2, inDir: inDir2, outDir: outDir2 }) {
|
40
43
|
const messages2 = [];
|
@@ -82,7 +85,7 @@ async function builder_default({ type: type2, inDir: inDir2, outDir: outDir2 })
|
|
82
85
|
plugins: [
|
83
86
|
{
|
84
87
|
name: "resolve-ts",
|
85
|
-
setup
|
88
|
+
setup: setup(type2)
|
86
89
|
}
|
87
90
|
]
|
88
91
|
});
|
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-dcb8","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
@@ -29,22 +29,25 @@ async function getFiles(directory: string): Promise<string[]> {
|
|
29
29
|
return files.flat();
|
30
30
|
}
|
31
31
|
|
32
|
-
function setup(
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
32
|
+
function setup(type: 'module' | 'commonjs') {
|
33
|
+
const extension = type === 'module' ? 'mjs' : 'cjs';
|
34
|
+
return (pluginBuild: PluginBuild) => {
|
35
|
+
pluginBuild.onResolve({ filter: /.*/u }, async (resolved) => {
|
36
|
+
if (resolved.kind === 'entry-point' || !resolved.path.startsWith('.') || resolved.path.endsWith('.js')) {
|
37
|
+
return { external: resolved.kind !== 'entry-point' };
|
38
|
+
}
|
39
|
+
let isDirectory = false;
|
40
|
+
try {
|
41
|
+
const stats = await fs.lstat(path.join(resolved.resolveDir, resolved.path));
|
42
|
+
isDirectory = stats.isDirectory();
|
43
|
+
} catch {
|
44
|
+
// do nothing
|
45
|
+
}
|
46
|
+
let newPath = resolved.path;
|
47
|
+
newPath += isDirectory ? `/index.${extension}` : `.${extension}`;
|
48
|
+
return { path: newPath, external: true };
|
49
|
+
});
|
50
|
+
};
|
48
51
|
}
|
49
52
|
|
50
53
|
// eslint-disable-next-line func-names,max-lines-per-function,max-statements
|
@@ -105,7 +108,7 @@ export default async function ({ type, inDir, outDir }: BuilderOptions): Promise
|
|
105
108
|
plugins: [
|
106
109
|
{
|
107
110
|
name: 'resolve-ts',
|
108
|
-
setup,
|
111
|
+
setup: setup(type),
|
109
112
|
},
|
110
113
|
],
|
111
114
|
});
|
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
import { strict as assert } from 'node:assert';
|
4
4
|
import { promises as fs } from 'node:fs';
|
5
|
+
import { createRequire } from 'node:module';
|
5
6
|
import os from 'node:os';
|
6
7
|
import path from 'node:path';
|
7
8
|
|
@@ -10,10 +11,21 @@ import { v4 as uuid } from 'uuid';
|
|
10
11
|
// @ts-expect-error
|
11
12
|
import builder from './builder.mts';
|
12
13
|
|
14
|
+
const require = createRequire(import.meta.url);
|
15
|
+
|
13
16
|
const singleModule = {
|
14
17
|
[`index.ts`]: `export const hello = 'world';`,
|
15
18
|
};
|
16
19
|
|
20
|
+
const twoModules = {
|
21
|
+
[`index.ts`]: `import { hello } from './thing';\nexport default hello + 'world';\n`,
|
22
|
+
[`thing.ts`]: `export const hello = 'world';`,
|
23
|
+
};
|
24
|
+
|
25
|
+
const exportDefaultFunctionModule = {
|
26
|
+
[`index.ts`]: `export default function () { return 'hello world' }\n`,
|
27
|
+
};
|
28
|
+
|
17
29
|
async function write(dir: string, files: Record<string, string>): Promise<void> {
|
18
30
|
await fs.mkdir(dir, { recursive: true });
|
19
31
|
await Promise.all(Object.entries(files).map(([name, content]) => fs.writeFile(path.join(dir, name), content)));
|
@@ -87,6 +99,98 @@ describe('test builder', () => {
|
|
87
99
|
'index.d.ts': 'export declare const hello = "world";\n',
|
88
100
|
'index.mjs': 'var hello = "world";\nexport {\n hello\n};\n',
|
89
101
|
});
|
102
|
+
|
103
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
104
|
+
const output = await import(path.join(outDir, 'index.mjs'));
|
105
|
+
assert.equal(output.hello, 'world');
|
106
|
+
});
|
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
|
+
|
174
|
+
it('should build an ESM module that imports a second ESM module', async () => {
|
175
|
+
const id = uuid();
|
176
|
+
const inDir = path.join(os.tmpdir(), `in-dir-${id}`, 'src');
|
177
|
+
const outDir = path.join(os.tmpdir(), `out-dir-${id}`, 'build');
|
178
|
+
await write(inDir, twoModules);
|
179
|
+
assert.deepEqual(await builder({ type: 'module', inDir, outDir }), []);
|
180
|
+
assert.deepEqual(await read(outDir), {
|
181
|
+
'index.d.ts': 'declare const _default: string;\nexport default _default;\n',
|
182
|
+
'index.mjs':
|
183
|
+
'import { hello } from "./thing.mjs";\n' +
|
184
|
+
'var src_default = hello + "world";\n' +
|
185
|
+
'export {\n' +
|
186
|
+
' src_default as default\n' +
|
187
|
+
'};\n',
|
188
|
+
'thing.d.ts': 'export declare const hello = "world";\n',
|
189
|
+
'thing.mjs': 'var hello = "world";\nexport {\n hello\n};\n',
|
190
|
+
});
|
191
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
192
|
+
const output = await import(path.join(outDir, 'index.mjs'));
|
193
|
+
assert.equal(output.default, 'worldworld');
|
90
194
|
});
|
91
195
|
|
92
196
|
it('should build a single CJS module', async () => {
|
@@ -127,4 +231,78 @@ describe('test builder', () => {
|
|
127
231
|
'});\n',
|
128
232
|
});
|
129
233
|
});
|
234
|
+
|
235
|
+
it('should build a CJS module that requires a second CJS module', async () => {
|
236
|
+
const id = uuid();
|
237
|
+
const inDir = path.join(os.tmpdir(), `in-dir-${id}`, 'src');
|
238
|
+
const outDir = path.join(os.tmpdir(), `out-dir-${id}`, 'build');
|
239
|
+
await write(inDir, twoModules);
|
240
|
+
assert.deepEqual(await builder({ type: 'commonjs', inDir, outDir }), []);
|
241
|
+
assert.deepEqual(await read(outDir), {
|
242
|
+
'index.cjs':
|
243
|
+
'var __defProp = Object.defineProperty;\n' +
|
244
|
+
'var __getOwnPropDesc = Object.getOwnPropertyDescriptor;\n' +
|
245
|
+
'var __getOwnPropNames = Object.getOwnPropertyNames;\n' +
|
246
|
+
'var __hasOwnProp = Object.prototype.hasOwnProperty;\n' +
|
247
|
+
'var __export = (target, all) => {\n' +
|
248
|
+
' for (var name in all)\n' +
|
249
|
+
' __defProp(target, name, { get: all[name], enumerable: true });\n' +
|
250
|
+
'};\n' +
|
251
|
+
'var __copyProps = (to, from, except, desc) => {\n' +
|
252
|
+
' if (from && typeof from === "object" || typeof from === "function") {\n' +
|
253
|
+
' for (let key of __getOwnPropNames(from))\n' +
|
254
|
+
' if (!__hasOwnProp.call(to, key) && key !== except)\n' +
|
255
|
+
' __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n' +
|
256
|
+
' }\n' +
|
257
|
+
' return to;\n' +
|
258
|
+
'};\n' +
|
259
|
+
'var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);\n' +
|
260
|
+
'\n' +
|
261
|
+
'var src_exports = {};\n' +
|
262
|
+
'__export(src_exports, {\n' +
|
263
|
+
' default: () => src_default\n' +
|
264
|
+
'});\n' +
|
265
|
+
'module.exports = __toCommonJS(src_exports);\n' +
|
266
|
+
'var import_thing = require("./thing.cjs");\n' +
|
267
|
+
'var src_default = import_thing.hello + "world";\n',
|
268
|
+
'index.d.ts': 'declare const _default: string;\nexport default _default;\n',
|
269
|
+
'thing.cjs':
|
270
|
+
'var __defProp = Object.defineProperty;\n' +
|
271
|
+
'var __getOwnPropDesc = Object.getOwnPropertyDescriptor;\n' +
|
272
|
+
'var __getOwnPropNames = Object.getOwnPropertyNames;\n' +
|
273
|
+
'var __hasOwnProp = Object.prototype.hasOwnProperty;\n' +
|
274
|
+
'var __export = (target, all) => {\n' +
|
275
|
+
' for (var name in all)\n' +
|
276
|
+
' __defProp(target, name, { get: all[name], enumerable: true });\n' +
|
277
|
+
'};\n' +
|
278
|
+
'var __copyProps = (to, from, except, desc) => {\n' +
|
279
|
+
' if (from && typeof from === "object" || typeof from === "function") {\n' +
|
280
|
+
' for (let key of __getOwnPropNames(from))\n' +
|
281
|
+
' if (!__hasOwnProp.call(to, key) && key !== except)\n' +
|
282
|
+
' __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n' +
|
283
|
+
' }\n' +
|
284
|
+
' return to;\n' +
|
285
|
+
'};\n' +
|
286
|
+
'var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);\n' +
|
287
|
+
'\n' +
|
288
|
+
'var thing_exports = {};\n' +
|
289
|
+
'__export(thing_exports, {\n' +
|
290
|
+
' hello: () => hello\n' +
|
291
|
+
'});\n' +
|
292
|
+
'module.exports = __toCommonJS(thing_exports);\n' +
|
293
|
+
'var hello = "world";\n' +
|
294
|
+
'0 && (module.exports = {\n' +
|
295
|
+
' hello\n' +
|
296
|
+
'});\n',
|
297
|
+
'thing.d.ts': 'export declare const hello = "world";\n',
|
298
|
+
});
|
299
|
+
|
300
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
301
|
+
const output1 = require(path.join(outDir, 'index.cjs'));
|
302
|
+
assert.equal(output1.default, 'worldworld');
|
303
|
+
|
304
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
305
|
+
const output2 = await import(path.join(outDir, 'index.cjs'));
|
306
|
+
assert.equal(output2.default.default, 'worldworld');
|
307
|
+
});
|
130
308
|
});
|