@checkdigit/typescript-config 3.3.0-PR.34-c87d → 3.4.0-PR.36-4133
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
|
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:
|
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 (
|
124
|
-
throw new Error(`
|
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 +1 @@
|
|
1
|
-
{"name":"@checkdigit/typescript-config","version":"3.
|
1
|
+
{"name":"@checkdigit/typescript-config","version":"3.4.0-PR.36-4133","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.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-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.1","@types/uuid":"^9.0.1","@typescript-eslint/eslint-plugin":"^5.59.7","@typescript-eslint/parser":"^5.59.7","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/"]}
|
package/src/builder/builder.mts
CHANGED
@@ -11,7 +11,7 @@ export interface BuilderOptions {
|
|
11
11
|
/**
|
12
12
|
* whether to produce ESM or CommonJS code
|
13
13
|
*/
|
14
|
-
type: 'module' | 'commonjs';
|
14
|
+
type: 'module' | 'commonjs' | 'types';
|
15
15
|
|
16
16
|
/**
|
17
17
|
* the entry point for the bundle, relative to the inDir. if not provided, the files in the inDir will be processed
|
@@ -124,20 +124,21 @@ export default async function ({
|
|
124
124
|
'entryPoint and outFile must both be provided'
|
125
125
|
);
|
126
126
|
|
127
|
-
/**
|
128
|
-
* Emit declarations using typescript compiler
|
129
|
-
*/
|
130
127
|
const allSourceFiles = await getFiles(inDir);
|
131
128
|
const productionSourceFiles =
|
132
129
|
entryPoint === undefined ? allSourceFiles.filter((file) => file.endsWith('.ts')) : [path.join(inDir, entryPoint)];
|
133
130
|
|
131
|
+
/**
|
132
|
+
* Emit declarations using typescript compiler, if type is 'types'. Otherwise, just compile to ensure the build is good.
|
133
|
+
*/
|
134
134
|
const program = typescript.createProgram(productionSourceFiles, {
|
135
135
|
module: typescript.ModuleKind.ESNext,
|
136
136
|
moduleResolution: typescript.ModuleResolutionKind.Bundler,
|
137
137
|
target: typescript.ScriptTarget.ESNext,
|
138
138
|
declaration: true,
|
139
|
+
noEmit: type !== 'types',
|
139
140
|
noEmitOnError: true,
|
140
|
-
emitDeclarationOnly:
|
141
|
+
emitDeclarationOnly: type === 'types',
|
141
142
|
rootDir: inDir,
|
142
143
|
outDir,
|
143
144
|
noLib: false,
|
@@ -179,8 +180,13 @@ export default async function ({
|
|
179
180
|
messages.push(`tsc: ${typescript.flattenDiagnosticMessageText(diagnostic.messageText, '\n')}`);
|
180
181
|
}
|
181
182
|
}
|
182
|
-
|
183
|
-
|
183
|
+
|
184
|
+
if (messages.length > 0) {
|
185
|
+
throw new Error(`tsc failed ${JSON.stringify(messages)}`);
|
186
|
+
}
|
187
|
+
|
188
|
+
if (type === 'types') {
|
189
|
+
return [];
|
184
190
|
}
|
185
191
|
|
186
192
|
/**
|
@@ -225,5 +231,6 @@ export default async function ({
|
|
225
231
|
if (messages.length > 0) {
|
226
232
|
throw new Error(`esbuild failed ${JSON.stringify(messages)}`);
|
227
233
|
}
|
234
|
+
|
228
235
|
return messages;
|
229
236
|
}
|
package/src/builder/index.mts
CHANGED
@@ -23,7 +23,7 @@ const {
|
|
23
23
|
},
|
24
24
|
});
|
25
25
|
|
26
|
-
assert.ok(type === 'module' || type === 'commonjs', 'type must be module or commonjs');
|
26
|
+
assert.ok(type === 'module' || type === 'commonjs' || type === 'types', 'type must be types, module or commonjs');
|
27
27
|
assert.ok(inDir !== undefined, 'inDir is required');
|
28
28
|
assert.ok(outDir !== undefined, 'outDir is required');
|
29
29
|
|
@@ -1,484 +0,0 @@
|
|
1
|
-
// builder/builder.spec.mts
|
2
|
-
|
3
|
-
import { strict as assert } from 'node:assert';
|
4
|
-
import { promises as fs } from 'node:fs';
|
5
|
-
import { createRequire } from 'node:module';
|
6
|
-
import os from 'node:os';
|
7
|
-
import path from 'node:path';
|
8
|
-
|
9
|
-
import { v4 as uuid } from 'uuid';
|
10
|
-
|
11
|
-
// @ts-expect-error
|
12
|
-
import builder from './builder.mts';
|
13
|
-
|
14
|
-
const require = createRequire(import.meta.url);
|
15
|
-
|
16
|
-
const singleModule = {
|
17
|
-
[`index.ts`]: `export const hello = 'world';`,
|
18
|
-
};
|
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
|
-
|
29
|
-
const importExternalModule = {
|
30
|
-
[`index.ts`]: `
|
31
|
-
import { hello as test } from 'test-esm-module';
|
32
|
-
import util from 'node:util';
|
33
|
-
export const hello = { test, message: util.format('hello %s', 'world') };
|
34
|
-
`,
|
35
|
-
};
|
36
|
-
|
37
|
-
const testNodeModules = {
|
38
|
-
[`test-cjs-module`]: {
|
39
|
-
source: {
|
40
|
-
[`index.js`]: `module.exports.goodbye = 'world';`,
|
41
|
-
[`index.d.ts`]: `export declare const goodbye = "world";\n`,
|
42
|
-
},
|
43
|
-
},
|
44
|
-
[`test-esm-module`]: {
|
45
|
-
type: 'module',
|
46
|
-
source: {
|
47
|
-
[`index.js`]: `export const hello = 'world';`,
|
48
|
-
[`index.d.ts`]: `export declare const hello = "world";\n`,
|
49
|
-
},
|
50
|
-
},
|
51
|
-
} as const;
|
52
|
-
|
53
|
-
interface NodeModule {
|
54
|
-
[name: string]: {
|
55
|
-
type?: 'module' | 'commonjs';
|
56
|
-
source: {
|
57
|
-
[file: string]: string;
|
58
|
-
};
|
59
|
-
};
|
60
|
-
}
|
61
|
-
|
62
|
-
async function writeNodeModules(directory: string, nodeModules: NodeModule) {
|
63
|
-
const nodeModulesDirectory = path.join(directory, 'node_modules');
|
64
|
-
for (const [name, nodeModule] of Object.entries(nodeModules)) {
|
65
|
-
const nodeModuleDirectory = path.join(nodeModulesDirectory, name);
|
66
|
-
await fs.mkdir(nodeModuleDirectory, { recursive: true });
|
67
|
-
await fs.writeFile(
|
68
|
-
path.join(nodeModuleDirectory, 'package.json'),
|
69
|
-
JSON.stringify({
|
70
|
-
type: nodeModule.type ?? 'commonjs',
|
71
|
-
})
|
72
|
-
);
|
73
|
-
for (const [file, content] of Object.entries(nodeModule.source)) {
|
74
|
-
await fs.writeFile(path.join(nodeModuleDirectory, file), content);
|
75
|
-
}
|
76
|
-
}
|
77
|
-
}
|
78
|
-
|
79
|
-
async function write(directory: string, files: Record<string, string>): Promise<void> {
|
80
|
-
await fs.mkdir(directory, { recursive: true });
|
81
|
-
await Promise.all(Object.entries(files).map(([name, content]) => fs.writeFile(path.join(directory, name), content)));
|
82
|
-
}
|
83
|
-
|
84
|
-
async function read(dir: string): Promise<Record<string, string>> {
|
85
|
-
const files = await fs.readdir(dir);
|
86
|
-
return Object.fromEntries(
|
87
|
-
await Promise.all(
|
88
|
-
files.map(async (name) => [
|
89
|
-
name,
|
90
|
-
(
|
91
|
-
await fs.readFile(path.join(dir, name), 'utf-8')
|
92
|
-
)
|
93
|
-
.split('\n')
|
94
|
-
.filter((line) => !line.startsWith('//'))
|
95
|
-
.join('\n'),
|
96
|
-
])
|
97
|
-
)
|
98
|
-
) as Record<string, string>;
|
99
|
-
}
|
100
|
-
|
101
|
-
describe('test builder', () => {
|
102
|
-
it('should not build bad code', async () => {
|
103
|
-
const id = uuid();
|
104
|
-
const inDir = path.join(os.tmpdir(), `in-dir-${id}`);
|
105
|
-
const outDir = path.join(os.tmpdir(), `out-dir-${id}`);
|
106
|
-
await write(inDir, { 'index.ts': 'bad code' });
|
107
|
-
await assert.rejects(builder({ type: 'module', inDir, outDir }), {
|
108
|
-
message: `TypeScript compilation failed ${JSON.stringify([
|
109
|
-
`tsc: ${inDir}/index.ts (1,1): Unexpected keyword or identifier.`,
|
110
|
-
`tsc: ${inDir}/index.ts (1,1): Cannot find name 'bad'.`,
|
111
|
-
`tsc: ${inDir}/index.ts (1,5): Cannot find name 'code'.`,
|
112
|
-
])}`,
|
113
|
-
});
|
114
|
-
await assert.rejects(read(outDir), {
|
115
|
-
message: `ENOENT: no such file or directory, scandir '${outDir}'`,
|
116
|
-
});
|
117
|
-
});
|
118
|
-
|
119
|
-
it('should not build from bad directory', async () => {
|
120
|
-
const id = uuid();
|
121
|
-
const inDir = path.join(os.tmpdir(), `in-dir-${id}`);
|
122
|
-
const outDir = path.join(os.tmpdir(), `out-dir-${id}`);
|
123
|
-
await assert.rejects(builder({ type: 'module', inDir, outDir }), {
|
124
|
-
message: `ENOENT: no such file or directory, scandir '${inDir}'`,
|
125
|
-
});
|
126
|
-
await assert.rejects(read(outDir), {
|
127
|
-
message: `ENOENT: no such file or directory, scandir '${outDir}'`,
|
128
|
-
});
|
129
|
-
});
|
130
|
-
|
131
|
-
it('should build from empty directory, but not create output directory', async () => {
|
132
|
-
const id = uuid();
|
133
|
-
const inDir = path.join(os.tmpdir(), `in-dir-${id}`);
|
134
|
-
const outDir = path.join(os.tmpdir(), `out-dir-${id}`);
|
135
|
-
await write(inDir, {});
|
136
|
-
assert.deepEqual(await builder({ type: 'module', inDir, outDir }), []);
|
137
|
-
await assert.rejects(read(outDir), {
|
138
|
-
message: `ENOENT: no such file or directory, scandir '${outDir}'`,
|
139
|
-
});
|
140
|
-
});
|
141
|
-
|
142
|
-
it('should build a single ESM module', async () => {
|
143
|
-
const id = uuid();
|
144
|
-
const inDir = path.join(os.tmpdir(), `in-dir-${id}`, 'src');
|
145
|
-
const outDir = path.join(os.tmpdir(), `out-dir-${id}`, 'build');
|
146
|
-
await write(inDir, singleModule);
|
147
|
-
assert.deepEqual(await builder({ type: 'module', inDir, outDir }), []);
|
148
|
-
assert.deepEqual(await read(outDir), {
|
149
|
-
'index.d.ts': 'export declare const hello = "world";\n',
|
150
|
-
'index.mjs': 'var hello = "world";\nexport {\n hello\n};\n',
|
151
|
-
});
|
152
|
-
|
153
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
154
|
-
const output = await import(path.join(outDir, 'index.mjs'));
|
155
|
-
assert.equal(output.hello, 'world');
|
156
|
-
});
|
157
|
-
|
158
|
-
it('should minify a single ESM module', async () => {
|
159
|
-
const id = uuid();
|
160
|
-
const inDir = path.join(os.tmpdir(), `in-dir-${id}`, 'src');
|
161
|
-
const outDir = path.join(os.tmpdir(), `out-dir-${id}`, 'build');
|
162
|
-
await write(inDir, singleModule);
|
163
|
-
assert.deepEqual(await builder({ type: 'module', inDir, outDir, minify: true }), []);
|
164
|
-
assert.deepEqual(await read(outDir), {
|
165
|
-
'index.d.ts': 'export declare const hello = "world";\n',
|
166
|
-
'index.mjs': 'var o="world";export{o as hello};\n',
|
167
|
-
});
|
168
|
-
|
169
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
170
|
-
const output = await import(path.join(outDir, 'index.mjs'));
|
171
|
-
assert.equal(output.hello, 'world');
|
172
|
-
});
|
173
|
-
|
174
|
-
it('should build a single ESM module that exports function as default', 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, exportDefaultFunctionModule);
|
179
|
-
assert.deepEqual(await builder({ type: 'module', inDir, outDir }), []);
|
180
|
-
assert.deepEqual(await read(outDir), {
|
181
|
-
'index.d.ts': 'export default function (): string;\n',
|
182
|
-
'index.mjs':
|
183
|
-
'function src_default() {\n' +
|
184
|
-
' return "hello world";\n' +
|
185
|
-
'}\n' +
|
186
|
-
'export {\n' +
|
187
|
-
' src_default as default\n' +
|
188
|
-
'};\n',
|
189
|
-
});
|
190
|
-
|
191
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
192
|
-
const output = await import(path.join(outDir, 'index.mjs'));
|
193
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
194
|
-
assert.equal(output.default(), 'hello world');
|
195
|
-
});
|
196
|
-
|
197
|
-
it('should build a single CJS module that exports function as default', async () => {
|
198
|
-
const id = uuid();
|
199
|
-
const inDir = path.join(os.tmpdir(), `in-dir-${id}`, 'src');
|
200
|
-
const outDir = path.join(os.tmpdir(), `out-dir-${id}`, 'build');
|
201
|
-
await write(inDir, exportDefaultFunctionModule);
|
202
|
-
assert.deepEqual(await builder({ type: 'commonjs', inDir, outDir }), []);
|
203
|
-
assert.deepEqual(await read(outDir), {
|
204
|
-
'index.cjs':
|
205
|
-
'var __defProp = Object.defineProperty;\n' +
|
206
|
-
'var __getOwnPropDesc = Object.getOwnPropertyDescriptor;\n' +
|
207
|
-
'var __getOwnPropNames = Object.getOwnPropertyNames;\n' +
|
208
|
-
'var __hasOwnProp = Object.prototype.hasOwnProperty;\n' +
|
209
|
-
'var __export = (target, all) => {\n' +
|
210
|
-
' for (var name in all)\n' +
|
211
|
-
' __defProp(target, name, { get: all[name], enumerable: true });\n' +
|
212
|
-
'};\n' +
|
213
|
-
'var __copyProps = (to, from, except, desc) => {\n' +
|
214
|
-
' if (from && typeof from === "object" || typeof from === "function") {\n' +
|
215
|
-
' for (let key of __getOwnPropNames(from))\n' +
|
216
|
-
' if (!__hasOwnProp.call(to, key) && key !== except)\n' +
|
217
|
-
' __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n' +
|
218
|
-
' }\n' +
|
219
|
-
' return to;\n' +
|
220
|
-
'};\n' +
|
221
|
-
'var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);\n' +
|
222
|
-
'\n' +
|
223
|
-
'var src_exports = {};\n' +
|
224
|
-
'__export(src_exports, {\n' +
|
225
|
-
' default: () => src_default\n' +
|
226
|
-
'});\n' +
|
227
|
-
'module.exports = __toCommonJS(src_exports);\n' +
|
228
|
-
'function src_default() {\n' +
|
229
|
-
' return "hello world";\n' +
|
230
|
-
'}\n',
|
231
|
-
'index.d.ts': 'export default function (): string;\n',
|
232
|
-
});
|
233
|
-
|
234
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
235
|
-
const output = require(path.join(outDir, 'index.cjs'));
|
236
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
237
|
-
assert.equal(output.default(), 'hello world');
|
238
|
-
});
|
239
|
-
|
240
|
-
it('should build an ESM module that imports a second ESM module', async () => {
|
241
|
-
const id = uuid();
|
242
|
-
const inDir = path.join(os.tmpdir(), `in-dir-${id}`, 'src');
|
243
|
-
const outDir = path.join(os.tmpdir(), `out-dir-${id}`, 'build');
|
244
|
-
await write(inDir, twoModules);
|
245
|
-
assert.deepEqual(await builder({ type: 'module', inDir, outDir }), []);
|
246
|
-
assert.deepEqual(await read(outDir), {
|
247
|
-
'index.d.ts': 'declare const _default: string;\nexport default _default;\n',
|
248
|
-
'index.mjs':
|
249
|
-
'import { hello } from "./thing.mjs";\n' +
|
250
|
-
'var src_default = hello + "world";\n' +
|
251
|
-
'export {\n' +
|
252
|
-
' src_default as default\n' +
|
253
|
-
'};\n',
|
254
|
-
'thing.d.ts': 'export declare const hello = "world";\n',
|
255
|
-
'thing.mjs': 'var hello = "world";\nexport {\n hello\n};\n',
|
256
|
-
});
|
257
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
258
|
-
const output = await import(path.join(outDir, 'index.mjs'));
|
259
|
-
assert.equal(output.default, 'worldworld');
|
260
|
-
});
|
261
|
-
|
262
|
-
it('should build a single CJS module', async () => {
|
263
|
-
const id = uuid();
|
264
|
-
const inDir = path.join(os.tmpdir(), `in-dir-${id}`, 'src');
|
265
|
-
const outDir = path.join(os.tmpdir(), `out-dir-${id}`, 'build');
|
266
|
-
await write(inDir, singleModule);
|
267
|
-
assert.deepEqual(await builder({ type: 'commonjs', inDir, outDir }), []);
|
268
|
-
assert.deepEqual(await read(outDir), {
|
269
|
-
'index.d.ts': 'export declare const hello = "world";\n',
|
270
|
-
'index.cjs':
|
271
|
-
'var __defProp = Object.defineProperty;\n' +
|
272
|
-
'var __getOwnPropDesc = Object.getOwnPropertyDescriptor;\n' +
|
273
|
-
'var __getOwnPropNames = Object.getOwnPropertyNames;\n' +
|
274
|
-
'var __hasOwnProp = Object.prototype.hasOwnProperty;\n' +
|
275
|
-
'var __export = (target, all) => {\n' +
|
276
|
-
' for (var name in all)\n' +
|
277
|
-
' __defProp(target, name, { get: all[name], enumerable: true });\n' +
|
278
|
-
'};\n' +
|
279
|
-
'var __copyProps = (to, from, except, desc) => {\n' +
|
280
|
-
' if (from && typeof from === "object" || typeof from === "function") {\n' +
|
281
|
-
' for (let key of __getOwnPropNames(from))\n' +
|
282
|
-
' if (!__hasOwnProp.call(to, key) && key !== except)\n' +
|
283
|
-
' __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n' +
|
284
|
-
' }\n' +
|
285
|
-
' return to;\n' +
|
286
|
-
'};\n' +
|
287
|
-
'var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);\n' +
|
288
|
-
'\n' +
|
289
|
-
'var src_exports = {};\n' +
|
290
|
-
'__export(src_exports, {\n' +
|
291
|
-
' hello: () => hello\n' +
|
292
|
-
'});\n' +
|
293
|
-
'module.exports = __toCommonJS(src_exports);\n' +
|
294
|
-
'var hello = "world";\n' +
|
295
|
-
'0 && (module.exports = {\n' +
|
296
|
-
' hello\n' +
|
297
|
-
'});\n',
|
298
|
-
});
|
299
|
-
});
|
300
|
-
|
301
|
-
it('should build a CJS module that requires a second CJS module', async () => {
|
302
|
-
const id = uuid();
|
303
|
-
const inDir = path.join(os.tmpdir(), `in-dir-${id}`, 'src');
|
304
|
-
const outDir = path.join(os.tmpdir(), `out-dir-${id}`, 'build');
|
305
|
-
await write(inDir, twoModules);
|
306
|
-
assert.deepEqual(await builder({ type: 'commonjs', inDir, outDir }), []);
|
307
|
-
assert.deepEqual(await read(outDir), {
|
308
|
-
'index.cjs':
|
309
|
-
'var __defProp = Object.defineProperty;\n' +
|
310
|
-
'var __getOwnPropDesc = Object.getOwnPropertyDescriptor;\n' +
|
311
|
-
'var __getOwnPropNames = Object.getOwnPropertyNames;\n' +
|
312
|
-
'var __hasOwnProp = Object.prototype.hasOwnProperty;\n' +
|
313
|
-
'var __export = (target, all) => {\n' +
|
314
|
-
' for (var name in all)\n' +
|
315
|
-
' __defProp(target, name, { get: all[name], enumerable: true });\n' +
|
316
|
-
'};\n' +
|
317
|
-
'var __copyProps = (to, from, except, desc) => {\n' +
|
318
|
-
' if (from && typeof from === "object" || typeof from === "function") {\n' +
|
319
|
-
' for (let key of __getOwnPropNames(from))\n' +
|
320
|
-
' if (!__hasOwnProp.call(to, key) && key !== except)\n' +
|
321
|
-
' __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n' +
|
322
|
-
' }\n' +
|
323
|
-
' return to;\n' +
|
324
|
-
'};\n' +
|
325
|
-
'var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);\n' +
|
326
|
-
'\n' +
|
327
|
-
'var src_exports = {};\n' +
|
328
|
-
'__export(src_exports, {\n' +
|
329
|
-
' default: () => src_default\n' +
|
330
|
-
'});\n' +
|
331
|
-
'module.exports = __toCommonJS(src_exports);\n' +
|
332
|
-
'var import_thing = require("./thing.cjs");\n' +
|
333
|
-
'var src_default = import_thing.hello + "world";\n',
|
334
|
-
'index.d.ts': 'declare const _default: string;\nexport default _default;\n',
|
335
|
-
'thing.cjs':
|
336
|
-
'var __defProp = Object.defineProperty;\n' +
|
337
|
-
'var __getOwnPropDesc = Object.getOwnPropertyDescriptor;\n' +
|
338
|
-
'var __getOwnPropNames = Object.getOwnPropertyNames;\n' +
|
339
|
-
'var __hasOwnProp = Object.prototype.hasOwnProperty;\n' +
|
340
|
-
'var __export = (target, all) => {\n' +
|
341
|
-
' for (var name in all)\n' +
|
342
|
-
' __defProp(target, name, { get: all[name], enumerable: true });\n' +
|
343
|
-
'};\n' +
|
344
|
-
'var __copyProps = (to, from, except, desc) => {\n' +
|
345
|
-
' if (from && typeof from === "object" || typeof from === "function") {\n' +
|
346
|
-
' for (let key of __getOwnPropNames(from))\n' +
|
347
|
-
' if (!__hasOwnProp.call(to, key) && key !== except)\n' +
|
348
|
-
' __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n' +
|
349
|
-
' }\n' +
|
350
|
-
' return to;\n' +
|
351
|
-
'};\n' +
|
352
|
-
'var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);\n' +
|
353
|
-
'\n' +
|
354
|
-
'var thing_exports = {};\n' +
|
355
|
-
'__export(thing_exports, {\n' +
|
356
|
-
' hello: () => hello\n' +
|
357
|
-
'});\n' +
|
358
|
-
'module.exports = __toCommonJS(thing_exports);\n' +
|
359
|
-
'var hello = "world";\n' +
|
360
|
-
'0 && (module.exports = {\n' +
|
361
|
-
' hello\n' +
|
362
|
-
'});\n',
|
363
|
-
'thing.d.ts': 'export declare const hello = "world";\n',
|
364
|
-
});
|
365
|
-
|
366
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
367
|
-
const output1 = require(path.join(outDir, 'index.cjs'));
|
368
|
-
assert.equal(output1.default, 'worldworld');
|
369
|
-
|
370
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
371
|
-
const output2 = await import(path.join(outDir, 'index.cjs'));
|
372
|
-
assert.equal(output2.default.default, 'worldworld');
|
373
|
-
});
|
374
|
-
|
375
|
-
it('should bundle an ESM module that imports a second ESM module', async () => {
|
376
|
-
const id = uuid();
|
377
|
-
const inDir = path.join(os.tmpdir(), `in-dir-${id}`, 'src');
|
378
|
-
const outDir = path.join(os.tmpdir(), `out-dir-${id}`, 'build');
|
379
|
-
await write(inDir, twoModules);
|
380
|
-
assert.deepEqual(
|
381
|
-
await builder({ type: 'module', entryPoint: 'index.ts', outFile: 'index.mjs', inDir, outDir }),
|
382
|
-
[]
|
383
|
-
);
|
384
|
-
assert.deepEqual(await read(outDir), {
|
385
|
-
'index.d.ts': 'declare const _default: string;\nexport default _default;\n',
|
386
|
-
'index.mjs':
|
387
|
-
'var hello = "world";\n' +
|
388
|
-
'\n' +
|
389
|
-
'var src_default = hello + "world";\n' +
|
390
|
-
'export {\n' +
|
391
|
-
' src_default as default\n' +
|
392
|
-
'};\n',
|
393
|
-
'thing.d.ts': 'export declare const hello = "world";\n',
|
394
|
-
});
|
395
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
396
|
-
const output = await import(path.join(outDir, 'index.mjs'));
|
397
|
-
assert.equal(output.default, 'worldworld');
|
398
|
-
});
|
399
|
-
|
400
|
-
it('should bundle an ESM module that imports external modules', async () => {
|
401
|
-
const id = uuid();
|
402
|
-
const moduleDir = path.join(os.tmpdir(), `in-dir-${id}`);
|
403
|
-
const inDir = path.join(moduleDir, 'src');
|
404
|
-
const outDir = path.join(os.tmpdir(), `out-dir-${id}`, 'build');
|
405
|
-
await write(inDir, importExternalModule);
|
406
|
-
await writeNodeModules(moduleDir, testNodeModules);
|
407
|
-
assert.deepEqual(
|
408
|
-
await builder({ type: 'module', entryPoint: 'index.ts', outFile: 'index.mjs', inDir, outDir }),
|
409
|
-
[]
|
410
|
-
);
|
411
|
-
assert.deepEqual(await read(outDir), {
|
412
|
-
'index.d.ts': 'export declare const hello: {\n test: string;\n message: string;\n};\n',
|
413
|
-
'index.mjs':
|
414
|
-
'var hello = "world";\n' +
|
415
|
-
'\n' +
|
416
|
-
'import util from "node:util";\n' +
|
417
|
-
'var hello2 = { test: hello, message: util.format("hello %s", "world") };\n' +
|
418
|
-
'export {\n' +
|
419
|
-
' hello2 as hello\n' +
|
420
|
-
'};\n',
|
421
|
-
});
|
422
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
423
|
-
const output = await import(path.join(outDir, 'index.mjs'));
|
424
|
-
assert.deepEqual(output.hello, {
|
425
|
-
message: 'hello world',
|
426
|
-
test: 'world',
|
427
|
-
});
|
428
|
-
});
|
429
|
-
|
430
|
-
it('should bundle an ESM module that imports external modules, but excludes them', async () => {
|
431
|
-
const id = uuid();
|
432
|
-
const moduleDir = path.join(os.tmpdir(), `in-dir-${id}`);
|
433
|
-
const inDir = path.join(moduleDir, 'src');
|
434
|
-
const outDir = path.join(os.tmpdir(), `out-dir-${id}`, 'build');
|
435
|
-
await write(inDir, importExternalModule);
|
436
|
-
await writeNodeModules(moduleDir, testNodeModules);
|
437
|
-
assert.deepEqual(
|
438
|
-
await builder({
|
439
|
-
type: 'module',
|
440
|
-
entryPoint: 'index.ts',
|
441
|
-
outFile: 'index.mjs',
|
442
|
-
inDir,
|
443
|
-
outDir,
|
444
|
-
external: ['*'],
|
445
|
-
}),
|
446
|
-
[]
|
447
|
-
);
|
448
|
-
assert.deepEqual(await read(outDir), {
|
449
|
-
'index.d.ts': 'export declare const hello: {\n test: string;\n message: string;\n};\n',
|
450
|
-
'index.mjs':
|
451
|
-
'import { hello as test } from "test-esm-module";\n' +
|
452
|
-
'import util from "node:util";\n' +
|
453
|
-
'var hello = { test, message: util.format("hello %s", "world") };\n' +
|
454
|
-
'export {\n' +
|
455
|
-
' hello\n' +
|
456
|
-
'};\n',
|
457
|
-
});
|
458
|
-
});
|
459
|
-
|
460
|
-
it('should bundle a commonjs module that imports external ESM modules', async () => {
|
461
|
-
const id = uuid();
|
462
|
-
const moduleDir = path.join(os.tmpdir(), `in-dir-${id}`);
|
463
|
-
const inDir = path.join(moduleDir, 'src');
|
464
|
-
const outDir = path.join(os.tmpdir(), `out-dir-${id}`, 'build');
|
465
|
-
await write(inDir, importExternalModule);
|
466
|
-
await writeNodeModules(moduleDir, testNodeModules);
|
467
|
-
assert.deepEqual(
|
468
|
-
await builder({
|
469
|
-
type: 'commonjs',
|
470
|
-
entryPoint: 'index.ts',
|
471
|
-
outFile: 'index.cjs',
|
472
|
-
inDir,
|
473
|
-
outDir,
|
474
|
-
}),
|
475
|
-
[]
|
476
|
-
);
|
477
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
478
|
-
const output = require(path.join(outDir, 'index.cjs'));
|
479
|
-
assert.deepEqual(output.hello, {
|
480
|
-
message: 'hello world',
|
481
|
-
test: 'world',
|
482
|
-
});
|
483
|
-
});
|
484
|
-
});
|
@@ -1,27 +0,0 @@
|
|
1
|
-
// typescript-5.0-esm.spec.mts
|
2
|
-
|
3
|
-
import { strict as assert } from 'node:assert';
|
4
|
-
|
5
|
-
import getPort from 'get-port'; // ESM version of get-port
|
6
|
-
import debug from 'debug';
|
7
|
-
|
8
|
-
describe('typescript-5.0 ESM', () => {
|
9
|
-
it('"this" is undefined', () => {
|
10
|
-
assert.ok(typeof this === 'undefined');
|
11
|
-
});
|
12
|
-
|
13
|
-
it('works with CJS modules', async () => {
|
14
|
-
assert.ok(typeof debug === 'function');
|
15
|
-
const log = debug('typescript-5.0-esm.spec.mts:test');
|
16
|
-
assert.ok(typeof log === 'function');
|
17
|
-
log.enabled = true;
|
18
|
-
log('hello');
|
19
|
-
debug.enable('typescript-5.0-esm.spec.mts:test');
|
20
|
-
assert.ok(!debug.enabled('test'));
|
21
|
-
assert.ok(debug.enabled('typescript-5.0-esm.spec.mts:test'));
|
22
|
-
});
|
23
|
-
|
24
|
-
it('works with ESM modules', async () => {
|
25
|
-
assert.equal(typeof (await getPort()), 'number');
|
26
|
-
});
|
27
|
-
});
|