-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostprocess-files.cjs
More file actions
101 lines (92 loc) · 3.15 KB
/
postprocess-files.cjs
File metadata and controls
101 lines (92 loc) · 3.15 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// @ts-check
const fs = require('fs');
const path = require('path');
const distDir =
process.env['DIST_PATH'] ?
path.resolve(process.env['DIST_PATH'])
: path.resolve(__dirname, '..', '..', 'dist');
async function* walk(dir) {
for await (const d of await fs.promises.opendir(dir)) {
const entry = path.join(dir, d.name);
if (d.isDirectory()) yield* walk(entry);
else if (d.isFile()) yield entry;
}
}
async function postprocess() {
for await (const file of walk(distDir)) {
if (!/(\.d)?[cm]?ts$/.test(file)) continue;
const code = await fs.promises.readFile(file, 'utf8');
// strip out lib="dom", types="node", and types="react" references; these
// are needed at build time, but would pollute the user's TS environment
let transformed = code.replace(
/^ *\/\/\/ *<reference +(lib="dom"|types="(node|react)").*?\n/gm,
// replace with same number of characters to avoid breaking source maps
(match) => ' '.repeat(match.length - 1) + '\n',
);
// TypeScript's declaration emitter collapses /** @ts-ignore */ onto the same
// line as the type declaration, which doesn't work. So we convert to // @ts-ignore
// on its own line to properly suppresses errors.
if (file.endsWith('.d.ts') || file.endsWith('.d.mts') || file.endsWith('.d.cts')) {
transformed = transformed.replace(/\/\*\* @ts-ignore\b[^*]*\*\/ /gm, '// @ts-ignore\n');
}
if (transformed !== code) {
console.error(`wrote ${path.relative(process.cwd(), file)}`);
await fs.promises.writeFile(file, transformed, 'utf8');
}
}
const newExports = {
'.': {
require: {
types: './index.d.ts',
default: './index.js',
},
types: './index.d.mts',
default: './index.mjs',
},
};
for (const entry of await fs.promises.readdir(distDir, { withFileTypes: true })) {
if (entry.isDirectory() && entry.name !== 'src' && entry.name !== 'internal' && entry.name !== 'bin') {
const subpath = './' + entry.name;
newExports[subpath + '/*.mjs'] = {
default: subpath + '/*.mjs',
};
newExports[subpath + '/*.js'] = {
default: subpath + '/*.js',
};
newExports[subpath + '/*'] = {
import: subpath + '/*.mjs',
require: subpath + '/*.js',
};
} else if (entry.isFile() && /\.[cm]?js$/.test(entry.name)) {
const { name, ext } = path.parse(entry.name);
const subpathWithoutExt = './' + name;
const subpath = './' + entry.name;
newExports[subpathWithoutExt] ||= { import: undefined, require: undefined };
const isModule = ext[1] === 'm';
if (isModule) {
newExports[subpathWithoutExt].import = subpath;
} else {
newExports[subpathWithoutExt].require = subpath;
}
newExports[subpath] = {
default: subpath,
};
}
}
await fs.promises.writeFile(
'dist/package.json',
JSON.stringify(
Object.assign(
/** @type {Record<String, unknown>} */ (
JSON.parse(await fs.promises.readFile('dist/package.json', 'utf-8'))
),
{
exports: newExports,
},
),
null,
2,
),
);
}
postprocess();