-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-react.mjs
More file actions
55 lines (54 loc) · 2.47 KB
/
Copy pathbuild-react.mjs
File metadata and controls
55 lines (54 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (c) 2026 Tevinch. SPDX-License-Identifier: MIT
import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';
import { dirname, join, resolve } from 'node:path';
import { mkdir, writeFile } from 'node:fs/promises';
// Build tools stay outside the distributed package. See README for setup.
const dependencies = process.argv[2];
if (!dependencies) throw new Error('Usage: node build-react.mjs /path/to/node_modules');
const dependencyRoot = resolve(dependencies);
const require = createRequire(import.meta.url);
const ts = require(join(dependencyRoot, 'typescript/lib/typescript.js'));
const directory = dirname(fileURLToPath(import.meta.url));
const sourceDirectory = resolve(directory, '../../examples/react-markdown-table');
const source = join(sourceDirectory, 'markdown-table-generator.tsx');
const output = join(directory, 'react');
const options = {
target: ts.ScriptTarget.ES2022,
module: ts.ModuleKind.ESNext,
moduleResolution: ts.ModuleResolutionKind.Bundler,
jsx: ts.JsxEmit.ReactJSX,
strict: true,
skipLibCheck: true,
declaration: true,
noEmitOnError: true,
rootDir: sourceDirectory,
outDir: output,
types: [],
paths: {
react: [join(dependencyRoot, '@types/react/index.d.ts')],
'react/*': [join(dependencyRoot, '@types/react/*')],
},
};
const program = ts.createProgram([source], options);
const files = new Map();
const emitted = program.emit(undefined, (name, text) => files.set(name, text));
const diagnostics = [...ts.getPreEmitDiagnostics(program), ...emitted.diagnostics];
if (emitted.emitSkipped || diagnostics.length) {
console.error(ts.formatDiagnosticsWithColorAndContext(diagnostics, {
getCanonicalFileName: name => name,
getCurrentDirectory: () => process.cwd(),
getNewLine: () => '\n',
}));
process.exitCode = 1;
} else {
const js = files.get(join(output, 'markdown-table-generator.js'));
const declaration = files.get(join(output, 'markdown-table-generator.d.ts'));
if (!js || !declaration) throw new Error('Expected React output was not generated.');
const oldImport = '../../javascript/markdown-table/index.mjs';
if (js.split(oldImport).length !== 2) throw new Error('Expected one conversion-module import.');
await mkdir(output, { recursive: true });
await writeFile(join(output, 'index.js'), js.replace(oldImport, '../index.mjs'));
await writeFile(join(output, 'index.d.ts'), declaration.replaceAll(oldImport, '../index.mjs'));
console.log(`Built React entry with TypeScript ${ts.version}.`);
}