|
1 | 1 | import { existsSync } from 'fs';
|
2 | 2 | import fs from 'fs/promises';
|
3 | 3 | import path from 'path';
|
| 4 | +import { pathToFileURL } from 'url'; |
4 | 5 |
|
5 | 6 | import glob from 'fast-glob';
|
6 | 7 | import { legacy, resolve } from 'resolve.exports';
|
@@ -90,41 +91,45 @@ async function removePreconstructDeclarations(
|
90 | 91 | });
|
91 | 92 | }
|
92 | 93 |
|
93 |
| -(async () => { |
94 |
| - const packages = await glob('packages/*', { |
95 |
| - onlyDirectories: true, |
96 |
| - absolute: true, |
97 |
| - }); |
| 94 | +const packages = await glob('packages/*', { |
| 95 | + onlyDirectories: true, |
| 96 | + absolute: true, |
| 97 | +}); |
98 | 98 |
|
99 |
| - const entryPaths: [string, string][] = []; |
| 99 | +const entryPaths: [string, string][] = []; |
100 | 100 |
|
101 |
| - for (const packageDir of packages) { |
102 |
| - const pkg = require(path.resolve(packageDir, 'package.json')); |
| 101 | +for (const packageDir of packages) { |
| 102 | + const pkg = await import( |
| 103 | + // oathToFileURL enables the result of `path.resolve` to work with `import()` on windows |
| 104 | + pathToFileURL(path.resolve(packageDir, 'package.json')).toString(), |
| 105 | + { |
| 106 | + with: { type: 'json' }, |
| 107 | + } |
| 108 | + ); |
103 | 109 |
|
104 |
| - if (pkg.exports) { |
105 |
| - const pkgExports = Object.keys(pkg.exports); |
| 110 | + if (pkg.exports) { |
| 111 | + const pkgExports = Object.keys(pkg.exports); |
106 | 112 |
|
107 |
| - for (const entryName of pkgExports) { |
108 |
| - if (entryName.endsWith('package.json')) continue; |
| 113 | + for (const entryName of pkgExports) { |
| 114 | + if (entryName.endsWith('package.json')) continue; |
109 | 115 |
|
110 |
| - entryPaths.push([packageDir, resolveEntry(pkg, entryName)]); |
111 |
| - } |
112 |
| - } else { |
113 |
| - entryPaths.push([packageDir, resolveEntry(pkg)]); |
| 116 | + entryPaths.push([packageDir, resolveEntry(pkg, entryName)]); |
114 | 117 | }
|
| 118 | + } else { |
| 119 | + entryPaths.push([packageDir, resolveEntry(pkg)]); |
115 | 120 | }
|
| 121 | +} |
116 | 122 |
|
117 |
| - await Promise.all( |
118 |
| - entryPaths.map(([packageDir, entryPath]) => |
119 |
| - buildEntry(packageDir, entryPath), |
120 |
| - ), |
121 |
| - ).then((writes) => writes.map((write) => write?.())); |
122 |
| - |
123 |
| - // Entry points might reference each other so remove old declaration files |
124 |
| - // after we're done with everything |
125 |
| - await Promise.all( |
126 |
| - entryPaths.map(([packageDir, entryPath]) => |
127 |
| - removePreconstructDeclarations(packageDir, entryPath), |
128 |
| - ), |
129 |
| - ); |
130 |
| -})(); |
| 123 | +await Promise.all( |
| 124 | + entryPaths.map(([packageDir, entryPath]) => |
| 125 | + buildEntry(packageDir, entryPath), |
| 126 | + ), |
| 127 | +).then((writes) => writes.map((write) => write?.())); |
| 128 | + |
| 129 | +// Entry points might reference each other so remove old declaration files |
| 130 | +// after we're done with everything |
| 131 | +await Promise.all( |
| 132 | + entryPaths.map(([packageDir, entryPath]) => |
| 133 | + removePreconstructDeclarations(packageDir, entryPath), |
| 134 | + ), |
| 135 | +); |
0 commit comments