@checkdigit/typescript-config 3.3.0-PR.30-cf30 → 3.3.0-PR.30-a6df
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 +108 -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-a6df","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
@@ -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,17 @@ 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
|
+
|
17
25
|
async function write(dir: string, files: Record<string, string>): Promise<void> {
|
18
26
|
await fs.mkdir(dir, { recursive: true });
|
19
27
|
await Promise.all(Object.entries(files).map(([name, content]) => fs.writeFile(path.join(dir, name), content)));
|
@@ -87,6 +95,32 @@ describe('test builder', () => {
|
|
87
95
|
'index.d.ts': 'export declare const hello = "world";\n',
|
88
96
|
'index.mjs': 'var hello = "world";\nexport {\n hello\n};\n',
|
89
97
|
});
|
98
|
+
|
99
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
100
|
+
const output = await import(path.join(outDir, 'index.mjs'));
|
101
|
+
assert.equal(output.hello, 'world');
|
102
|
+
});
|
103
|
+
|
104
|
+
it('should build an ESM module that imports a second ESM module', async () => {
|
105
|
+
const id = uuid();
|
106
|
+
const inDir = path.join(os.tmpdir(), `in-dir-${id}`, 'src');
|
107
|
+
const outDir = path.join(os.tmpdir(), `out-dir-${id}`, 'build');
|
108
|
+
await write(inDir, twoModules);
|
109
|
+
assert.deepEqual(await builder({ type: 'module', inDir, outDir }), []);
|
110
|
+
assert.deepEqual(await read(outDir), {
|
111
|
+
'index.d.ts': 'declare const _default: string;\nexport default _default;\n',
|
112
|
+
'index.mjs':
|
113
|
+
'import { hello } from "./thing.mjs";\n' +
|
114
|
+
'var src_default = hello + "world";\n' +
|
115
|
+
'export {\n' +
|
116
|
+
' src_default as default\n' +
|
117
|
+
'};\n',
|
118
|
+
'thing.d.ts': 'export declare const hello = "world";\n',
|
119
|
+
'thing.mjs': 'var hello = "world";\nexport {\n hello\n};\n',
|
120
|
+
});
|
121
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
122
|
+
const output = await import(path.join(outDir, 'index.mjs'));
|
123
|
+
assert.equal(output.default, 'worldworld');
|
90
124
|
});
|
91
125
|
|
92
126
|
it('should build a single CJS module', async () => {
|
@@ -127,4 +161,78 @@ describe('test builder', () => {
|
|
127
161
|
'});\n',
|
128
162
|
});
|
129
163
|
});
|
164
|
+
|
165
|
+
it('should build a CJS module that requires a second CJS module', async () => {
|
166
|
+
const id = uuid();
|
167
|
+
const inDir = path.join(os.tmpdir(), `in-dir-${id}`, 'src');
|
168
|
+
const outDir = path.join(os.tmpdir(), `out-dir-${id}`, 'build');
|
169
|
+
await write(inDir, twoModules);
|
170
|
+
assert.deepEqual(await builder({ type: 'commonjs', inDir, outDir }), []);
|
171
|
+
assert.deepEqual(await read(outDir), {
|
172
|
+
'index.cjs':
|
173
|
+
'var __defProp = Object.defineProperty;\n' +
|
174
|
+
'var __getOwnPropDesc = Object.getOwnPropertyDescriptor;\n' +
|
175
|
+
'var __getOwnPropNames = Object.getOwnPropertyNames;\n' +
|
176
|
+
'var __hasOwnProp = Object.prototype.hasOwnProperty;\n' +
|
177
|
+
'var __export = (target, all) => {\n' +
|
178
|
+
' for (var name in all)\n' +
|
179
|
+
' __defProp(target, name, { get: all[name], enumerable: true });\n' +
|
180
|
+
'};\n' +
|
181
|
+
'var __copyProps = (to, from, except, desc) => {\n' +
|
182
|
+
' if (from && typeof from === "object" || typeof from === "function") {\n' +
|
183
|
+
' for (let key of __getOwnPropNames(from))\n' +
|
184
|
+
' if (!__hasOwnProp.call(to, key) && key !== except)\n' +
|
185
|
+
' __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n' +
|
186
|
+
' }\n' +
|
187
|
+
' return to;\n' +
|
188
|
+
'};\n' +
|
189
|
+
'var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);\n' +
|
190
|
+
'\n' +
|
191
|
+
'var src_exports = {};\n' +
|
192
|
+
'__export(src_exports, {\n' +
|
193
|
+
' default: () => src_default\n' +
|
194
|
+
'});\n' +
|
195
|
+
'module.exports = __toCommonJS(src_exports);\n' +
|
196
|
+
'var import_thing = require("./thing.cjs");\n' +
|
197
|
+
'var src_default = import_thing.hello + "world";\n',
|
198
|
+
'index.d.ts': 'declare const _default: string;\nexport default _default;\n',
|
199
|
+
'thing.cjs':
|
200
|
+
'var __defProp = Object.defineProperty;\n' +
|
201
|
+
'var __getOwnPropDesc = Object.getOwnPropertyDescriptor;\n' +
|
202
|
+
'var __getOwnPropNames = Object.getOwnPropertyNames;\n' +
|
203
|
+
'var __hasOwnProp = Object.prototype.hasOwnProperty;\n' +
|
204
|
+
'var __export = (target, all) => {\n' +
|
205
|
+
' for (var name in all)\n' +
|
206
|
+
' __defProp(target, name, { get: all[name], enumerable: true });\n' +
|
207
|
+
'};\n' +
|
208
|
+
'var __copyProps = (to, from, except, desc) => {\n' +
|
209
|
+
' if (from && typeof from === "object" || typeof from === "function") {\n' +
|
210
|
+
' for (let key of __getOwnPropNames(from))\n' +
|
211
|
+
' if (!__hasOwnProp.call(to, key) && key !== except)\n' +
|
212
|
+
' __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n' +
|
213
|
+
' }\n' +
|
214
|
+
' return to;\n' +
|
215
|
+
'};\n' +
|
216
|
+
'var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);\n' +
|
217
|
+
'\n' +
|
218
|
+
'var thing_exports = {};\n' +
|
219
|
+
'__export(thing_exports, {\n' +
|
220
|
+
' hello: () => hello\n' +
|
221
|
+
'});\n' +
|
222
|
+
'module.exports = __toCommonJS(thing_exports);\n' +
|
223
|
+
'var hello = "world";\n' +
|
224
|
+
'0 && (module.exports = {\n' +
|
225
|
+
' hello\n' +
|
226
|
+
'});\n',
|
227
|
+
'thing.d.ts': 'export declare const hello = "world";\n',
|
228
|
+
});
|
229
|
+
|
230
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
231
|
+
const output1 = require(path.join(outDir, 'index.cjs'));
|
232
|
+
assert.equal(output1.default, 'worldworld');
|
233
|
+
|
234
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
235
|
+
const output2 = await import(path.join(outDir, 'index.cjs'));
|
236
|
+
assert.equal(output2.default.default, 'worldworld');
|
237
|
+
});
|
130
238
|
});
|