-
Notifications
You must be signed in to change notification settings - Fork 304
/
Copy pathbuild-dts.ts
130 lines (111 loc) · 3.44 KB
/
build-dts.ts
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { existsSync } from 'fs';
import fs from 'fs/promises';
import path from 'path';
import glob from 'fast-glob';
import { legacy, resolve } from 'resolve.exports';
import { rollup } from 'rollup';
import dts from 'rollup-plugin-dts';
import { nodeExternals } from 'rollup-plugin-node-externals';
function resolveEntry<PackageJson>(
pkg: PackageJson,
entryName?: string,
): string {
const entryPath = entryName
? resolve(pkg, entryName, { browser: false, require: true })
: legacy(pkg, { browser: false, fields: ['main'] })!;
if (!entryPath || entryPath.length === 0) {
throw new Error('No entry found. Invalid package.json?');
}
if (Array.isArray(entryPath)) {
return entryPath[0];
}
return entryPath;
}
async function buildEntry(packageDir: string, entryPath: string) {
const dtsEntryPathAbsolute = path
.join(packageDir, entryPath)
.replace(path.extname(entryPath), '.d.ts');
const dtsEntryPath = path.relative(process.cwd(), dtsEntryPathAbsolute);
const outDir = path.dirname(dtsEntryPath);
if (!existsSync(dtsEntryPath)) {
console.warn('Skipping', dtsEntryPath, '(Not Found)');
return;
}
console.log('Bundling', dtsEntryPath);
try {
const bundle = await rollup({
input: dtsEntryPath,
plugins: [
nodeExternals({
packagePath: path.resolve(packageDir, 'package.json'),
deps: true,
devDeps: false,
exclude: ['@vanilla-extract/private'], // always bundle
include: ['@jest/transform'], // don't bundle
}),
dts({
compilerOptions: {
incremental: false,
noEmitOnError: false,
},
respectExternal: true,
}),
],
});
// There is a chance that two `buildEntry`s will run at the same time
// and one will attempt to read while the other is writing. To fix that,
// we'll defer the overwrite until all the bundles are ready
return async () => {
await bundle.write({
dir: outDir,
entryFileNames: '[name].ts',
minifyInternalExports: false,
});
await bundle.close();
};
} catch (e: any) {
console.error('Error bundling', dtsEntryPath);
console.error(e);
throw e;
}
}
async function removePreconstructDeclarations(
packageDir: string,
entryPath: string,
) {
await fs.rm(path.join(packageDir, entryPath, '../declarations'), {
force: true,
recursive: true,
});
}
(async () => {
const packages = await glob('packages/*', {
onlyDirectories: true,
absolute: true,
});
const entryPaths: [string, string][] = [];
for (const packageDir of packages) {
const pkg = require(path.resolve(packageDir, 'package.json'));
if (pkg.exports) {
const pkgExports = Object.keys(pkg.exports);
for (const entryName of pkgExports) {
if (entryName.endsWith('package.json')) continue;
entryPaths.push([packageDir, resolveEntry(pkg, entryName)]);
}
} else {
entryPaths.push([packageDir, resolveEntry(pkg)]);
}
}
await Promise.all(
entryPaths.map(([packageDir, entryPath]) =>
buildEntry(packageDir, entryPath),
),
).then((writes) => writes.map((write) => write?.()));
// Entry points might reference each other so remove old declaration files
// after we're done with everything
await Promise.all(
entryPaths.map(([packageDir, entryPath]) =>
removePreconstructDeclarations(packageDir, entryPath),
),
);
})();