@mui/internal-code-infra 0.0.4-canary.67 → 0.0.4-canary.69
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/utils/build.d.mts +9 -26
- package/build/utils/typescript.d.mts +6 -16
- package/package.json +4 -5
- package/src/brokenLinksChecker/crawlWorker.mjs +2 -2
- package/src/cli/cmdBuild.mjs +14 -71
- package/src/utils/build.mjs +18 -49
- package/src/utils/build.test.mjs +50 -144
- package/src/utils/typescript.mjs +23 -42
- package/src/utils/typescript.test.mjs +28 -44
package/src/utils/typescript.mjs
CHANGED
|
@@ -111,30 +111,21 @@ export async function copyDeclarations(sourceDirectory, destinationDirectory, op
|
|
|
111
111
|
* @param {Object} param0
|
|
112
112
|
* @param {string} param0.inputDir
|
|
113
113
|
* @param {string} param0.buildDir
|
|
114
|
-
* @param {
|
|
115
|
-
* @param {boolean} [param0.isFlat]
|
|
114
|
+
* @param {import('../utils/build.mjs').BundleType[]} param0.bundles
|
|
116
115
|
* @param {'module' | 'commonjs'} [param0.packageType]
|
|
117
116
|
* @returns
|
|
118
117
|
*/
|
|
119
|
-
export async function moveAndTransformDeclarations({
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
isFlat,
|
|
124
|
-
packageType,
|
|
125
|
-
}) {
|
|
126
|
-
// Directly copy to the bundle directory if there's only one bundle, mainly for esm, since
|
|
127
|
-
// the js files are inside 'esm' folder. resolve-imports plugin needs d.ts to be alongside js files to
|
|
128
|
-
// resolve paths correctly.
|
|
129
|
-
const toCopyDir = bundles.length === 1 ? path.join(buildDir, bundles[0].dir) : buildDir;
|
|
130
|
-
await fs.cp(inputDir, toCopyDir, {
|
|
118
|
+
export async function moveAndTransformDeclarations({ inputDir, buildDir, bundles, packageType }) {
|
|
119
|
+
// d.ts files sit alongside the js files in the build root so the resolve-imports
|
|
120
|
+
// plugin can resolve paths correctly.
|
|
121
|
+
await fs.cp(inputDir, buildDir, {
|
|
131
122
|
recursive: true,
|
|
132
123
|
force: false,
|
|
133
124
|
});
|
|
134
125
|
|
|
135
|
-
const dtsFiles = await globby('**/*.d.ts', { absolute: true, cwd:
|
|
126
|
+
const dtsFiles = await globby('**/*.d.ts', { absolute: true, cwd: buildDir });
|
|
136
127
|
if (dtsFiles.length === 0) {
|
|
137
|
-
console.log(`No d.ts files found in ${
|
|
128
|
+
console.log(`No d.ts files found in ${buildDir}. Skipping transformation.`);
|
|
138
129
|
return;
|
|
139
130
|
}
|
|
140
131
|
|
|
@@ -144,37 +135,30 @@ export async function moveAndTransformDeclarations({
|
|
|
144
135
|
// Normalize to native separators to make path comparisons reliable on Windows
|
|
145
136
|
const nativeDtsFile = path.normalize(dtsFile);
|
|
146
137
|
const content = await fs.readFile(nativeDtsFile, 'utf8');
|
|
147
|
-
const relativePath = path.relative(
|
|
138
|
+
const relativePath = path.relative(buildDir, nativeDtsFile);
|
|
148
139
|
|
|
149
|
-
const writesToOriginalPath =
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
isFlat,
|
|
154
|
-
isType: true,
|
|
155
|
-
packageType,
|
|
156
|
-
});
|
|
157
|
-
const outFileRelative = relativePath.replace(/\.d\.ts$/, newFileExtension);
|
|
158
|
-
const outFilePath = path.join(buildDir, bundle.dir, outFileRelative);
|
|
159
|
-
// Ensure both paths are normalized before comparison (fixes Windows posix vs win32 separators)
|
|
160
|
-
return path.resolve(outFilePath) === path.resolve(nativeDtsFile);
|
|
140
|
+
const writesToOriginalPath = bundles.some((bundle) => {
|
|
141
|
+
const newFileExtension = getOutExtension(bundle, {
|
|
142
|
+
isType: true,
|
|
143
|
+
packageType,
|
|
161
144
|
});
|
|
145
|
+
const outFileRelative = relativePath.replace(/\.d\.ts$/, newFileExtension);
|
|
146
|
+
const outFilePath = path.join(buildDir, outFileRelative);
|
|
147
|
+
// Ensure both paths are normalized before comparison (fixes Windows posix vs win32 separators)
|
|
148
|
+
return path.resolve(outFilePath) === path.resolve(nativeDtsFile);
|
|
149
|
+
});
|
|
162
150
|
|
|
163
151
|
await Promise.all(
|
|
164
152
|
bundles.map(async (bundle) => {
|
|
165
|
-
const importExtension = getOutExtension(bundle
|
|
166
|
-
isFlat,
|
|
153
|
+
const importExtension = getOutExtension(bundle, {
|
|
167
154
|
packageType,
|
|
168
155
|
});
|
|
169
|
-
const newFileExtension = getOutExtension(bundle
|
|
170
|
-
isFlat,
|
|
156
|
+
const newFileExtension = getOutExtension(bundle, {
|
|
171
157
|
isType: true,
|
|
172
158
|
packageType,
|
|
173
159
|
});
|
|
174
|
-
const outFileRelative =
|
|
175
|
-
|
|
176
|
-
: relativePath;
|
|
177
|
-
const outFilePath = path.join(buildDir, bundle.dir, outFileRelative);
|
|
160
|
+
const outFileRelative = relativePath.replace(/\.d\.ts$/, newFileExtension);
|
|
161
|
+
const outFilePath = path.join(buildDir, outFileRelative);
|
|
178
162
|
|
|
179
163
|
const babelPlugins = [
|
|
180
164
|
[pluginTypescriptSyntax, { dts: true }],
|
|
@@ -194,7 +178,7 @@ export async function moveAndTransformDeclarations({
|
|
|
194
178
|
}
|
|
195
179
|
}),
|
|
196
180
|
);
|
|
197
|
-
if (
|
|
181
|
+
if (!writesToOriginalPath) {
|
|
198
182
|
await fs.unlink(nativeDtsFile);
|
|
199
183
|
}
|
|
200
184
|
},
|
|
@@ -208,9 +192,8 @@ export async function moveAndTransformDeclarations({
|
|
|
208
192
|
* After copying, babel transformations are applied to the copied files because they need to be alongside the actual js files for proper resolution.
|
|
209
193
|
*
|
|
210
194
|
* @param {Object} param0
|
|
211
|
-
* @param {boolean} [param0.isFlat = false] - Whether to place generated declaration files in a flattened directory.
|
|
212
195
|
* @param {boolean} [param0.verbose = false] - Whether to log additional information while generating and moving declaration files.
|
|
213
|
-
* @param {
|
|
196
|
+
* @param {import('../utils/build.mjs').BundleType[]} param0.bundles - The bundles to create declarations for.
|
|
214
197
|
* @param {string} param0.srcDir - The source directory.
|
|
215
198
|
* @param {string} param0.buildDir - The build directory.
|
|
216
199
|
* @param {string} param0.cwd - The current working directory.
|
|
@@ -225,7 +208,6 @@ export async function createTypes({
|
|
|
225
208
|
cwd,
|
|
226
209
|
skipTsc,
|
|
227
210
|
useTsgo = false,
|
|
228
|
-
isFlat = false,
|
|
229
211
|
packageType,
|
|
230
212
|
verbose,
|
|
231
213
|
}) {
|
|
@@ -253,7 +235,6 @@ export async function createTypes({
|
|
|
253
235
|
inputDir: tmpDir,
|
|
254
236
|
buildDir,
|
|
255
237
|
bundles,
|
|
256
|
-
isFlat,
|
|
257
238
|
packageType,
|
|
258
239
|
});
|
|
259
240
|
} finally {
|
|
@@ -118,8 +118,8 @@ describe('moveAndTransformDeclarations', () => {
|
|
|
118
118
|
createFile(path.join(inputDir, 'index.d.ts'), 'export const test: string;'),
|
|
119
119
|
]);
|
|
120
120
|
|
|
121
|
-
/** @type {
|
|
122
|
-
const bundles = [
|
|
121
|
+
/** @type {BundleType[]} */
|
|
122
|
+
const bundles = ['esm'];
|
|
123
123
|
|
|
124
124
|
// Mock babel transformAsync to avoid actual babel transformations
|
|
125
125
|
vi.doMock('@babel/core', () => ({
|
|
@@ -130,18 +130,17 @@ describe('moveAndTransformDeclarations', () => {
|
|
|
130
130
|
inputDir,
|
|
131
131
|
buildDir,
|
|
132
132
|
bundles,
|
|
133
|
-
isFlat: false,
|
|
134
133
|
packageType: 'module',
|
|
135
134
|
});
|
|
136
135
|
|
|
137
136
|
// Verify file exists in the correct location
|
|
138
|
-
const dtsFile = path.join(buildDir, '
|
|
137
|
+
const dtsFile = path.join(buildDir, 'index.d.ts');
|
|
139
138
|
const stat = await fs.stat(dtsFile).catch(() => null);
|
|
140
139
|
expect(stat).not.toBeNull();
|
|
141
140
|
expect(stat?.isFile()).toBe(true);
|
|
142
141
|
});
|
|
143
142
|
|
|
144
|
-
it('preserves original path file
|
|
143
|
+
it('preserves original path file for a single esm bundle', async () => {
|
|
145
144
|
const cwd = await makeTempDir();
|
|
146
145
|
const inputDir = path.join(cwd, 'input');
|
|
147
146
|
const buildDir = path.join(cwd, 'build');
|
|
@@ -151,19 +150,18 @@ describe('moveAndTransformDeclarations', () => {
|
|
|
151
150
|
createFile(path.join(inputDir, 'index.d.ts'), 'export const test: string;'),
|
|
152
151
|
]);
|
|
153
152
|
|
|
154
|
-
/** @type {
|
|
155
|
-
const bundles = [
|
|
153
|
+
/** @type {BundleType[]} */
|
|
154
|
+
const bundles = ['esm'];
|
|
156
155
|
|
|
157
156
|
await moveAndTransformDeclarations({
|
|
158
157
|
inputDir,
|
|
159
158
|
buildDir,
|
|
160
159
|
bundles,
|
|
161
|
-
isFlat: false,
|
|
162
160
|
packageType: 'module',
|
|
163
161
|
});
|
|
164
162
|
|
|
165
|
-
//
|
|
166
|
-
const dtsFile = path.join(buildDir, '
|
|
163
|
+
// Files are copied to the build root
|
|
164
|
+
const dtsFile = path.join(buildDir, 'index.d.ts');
|
|
167
165
|
const stat = await fs.stat(dtsFile).catch(() => null);
|
|
168
166
|
expect(stat?.isFile()).toBe(true);
|
|
169
167
|
});
|
|
@@ -189,8 +187,8 @@ describe('moveAndTransformDeclarations', () => {
|
|
|
189
187
|
const content = 'export const normalized: boolean;';
|
|
190
188
|
await Promise.all([createFile(path.join(inputDir, 'test.d.ts'), content)]);
|
|
191
189
|
|
|
192
|
-
/** @type {
|
|
193
|
-
const bundles = [
|
|
190
|
+
/** @type {BundleType[]} */
|
|
191
|
+
const bundles = ['esm'];
|
|
194
192
|
|
|
195
193
|
// Mock babel transformAsync to capture filename
|
|
196
194
|
const transformMock = vi.fn(async (code) => ({ code }));
|
|
@@ -202,12 +200,11 @@ describe('moveAndTransformDeclarations', () => {
|
|
|
202
200
|
inputDir,
|
|
203
201
|
buildDir,
|
|
204
202
|
bundles,
|
|
205
|
-
isFlat: false,
|
|
206
203
|
packageType: 'module',
|
|
207
204
|
});
|
|
208
205
|
|
|
209
206
|
// Verify the file was read correctly by checking it exists
|
|
210
|
-
const dtsFile = path.join(buildDir, '
|
|
207
|
+
const dtsFile = path.join(buildDir, 'test.d.ts');
|
|
211
208
|
const stat = await fs.stat(dtsFile).catch(() => null);
|
|
212
209
|
expect(stat?.isFile()).toBe(true);
|
|
213
210
|
});
|
|
@@ -220,20 +217,19 @@ describe('moveAndTransformDeclarations', () => {
|
|
|
220
217
|
const content = 'export const flat: string;';
|
|
221
218
|
await Promise.all([createFile(path.join(inputDir, 'index.d.ts'), content)]);
|
|
222
219
|
|
|
223
|
-
/** @type {
|
|
220
|
+
/** @type {BundleType[]} */
|
|
224
221
|
// ESM + module packageType keeps .d.ts extension in flat builds
|
|
225
|
-
const bundles = [
|
|
222
|
+
const bundles = ['esm'];
|
|
226
223
|
|
|
227
224
|
await moveAndTransformDeclarations({
|
|
228
225
|
inputDir,
|
|
229
226
|
buildDir,
|
|
230
227
|
bundles,
|
|
231
|
-
isFlat: true,
|
|
232
228
|
packageType: 'module',
|
|
233
229
|
});
|
|
234
230
|
|
|
235
231
|
// Since extension doesn't change (.d.ts -> .d.ts), file should remain
|
|
236
|
-
const outputFile = path.join(buildDir, '
|
|
232
|
+
const outputFile = path.join(buildDir, 'index.d.ts');
|
|
237
233
|
const outputStat = await fs.stat(outputFile).catch(() => null);
|
|
238
234
|
expect(outputStat?.isFile()).toBe(true);
|
|
239
235
|
});
|
|
@@ -245,26 +241,25 @@ describe('moveAndTransformDeclarations', () => {
|
|
|
245
241
|
|
|
246
242
|
const content = 'export const transformed: string;';
|
|
247
243
|
await Promise.all([createFile(path.join(inputDir, 'index.d.ts'), content)]);
|
|
248
|
-
/** @type {
|
|
244
|
+
/** @type {BundleType[]} */
|
|
249
245
|
|
|
250
246
|
// CJS bundle with module packageType creates .d.cts
|
|
251
|
-
const bundles = [
|
|
247
|
+
const bundles = ['cjs'];
|
|
252
248
|
|
|
253
249
|
await moveAndTransformDeclarations({
|
|
254
250
|
inputDir,
|
|
255
251
|
buildDir,
|
|
256
252
|
bundles,
|
|
257
|
-
isFlat: true,
|
|
258
253
|
packageType: 'module',
|
|
259
254
|
});
|
|
260
255
|
|
|
261
256
|
// Transformed file with new extension should exist
|
|
262
|
-
const outputFile = path.join(buildDir, '
|
|
257
|
+
const outputFile = path.join(buildDir, 'index.d.cts');
|
|
263
258
|
const outputStat = await fs.stat(outputFile).catch(() => null);
|
|
264
259
|
expect(outputStat?.isFile()).toBe(true);
|
|
265
260
|
|
|
266
261
|
// Original .d.ts should be removed
|
|
267
|
-
const originalFile = path.join(buildDir, '
|
|
262
|
+
const originalFile = path.join(buildDir, 'index.d.ts');
|
|
268
263
|
const originalStat = await fs.stat(originalFile).catch(() => null);
|
|
269
264
|
expect(originalStat).toBeNull();
|
|
270
265
|
});
|
|
@@ -291,26 +286,25 @@ describe('moveAndTransformDeclarations', () => {
|
|
|
291
286
|
|
|
292
287
|
const content = 'export const component: string;';
|
|
293
288
|
await Promise.all([createFile(path.join(inputDir, 'component.d.ts'), content)]);
|
|
294
|
-
/** @type {
|
|
289
|
+
/** @type {BundleType[]} */
|
|
295
290
|
|
|
296
291
|
// ESM + commonjs packageType creates .d.mts
|
|
297
|
-
const bundles = [
|
|
292
|
+
const bundles = ['esm'];
|
|
298
293
|
|
|
299
294
|
await moveAndTransformDeclarations({
|
|
300
295
|
inputDir,
|
|
301
296
|
buildDir,
|
|
302
297
|
bundles,
|
|
303
|
-
isFlat: true,
|
|
304
298
|
packageType: 'commonjs',
|
|
305
299
|
});
|
|
306
300
|
|
|
307
301
|
// The .d.mts file should exist
|
|
308
|
-
const transformedFile = path.join(buildDir, '
|
|
302
|
+
const transformedFile = path.join(buildDir, 'component.d.mts');
|
|
309
303
|
const transformedStat = await fs.stat(transformedFile).catch(() => null);
|
|
310
304
|
expect(transformedStat?.isFile()).toBe(true);
|
|
311
305
|
|
|
312
306
|
// Original .d.ts should be removed because extension changed
|
|
313
|
-
const originalFile = path.join(buildDir, '
|
|
307
|
+
const originalFile = path.join(buildDir, 'component.d.ts');
|
|
314
308
|
const originalStat = await fs.stat(originalFile).catch(() => null);
|
|
315
309
|
expect(originalStat).toBeNull();
|
|
316
310
|
});
|
|
@@ -323,35 +317,25 @@ describe('moveAndTransformDeclarations', () => {
|
|
|
323
317
|
const content = 'export const multi: string;';
|
|
324
318
|
await Promise.all([createFile(path.join(inputDir, 'index.d.ts'), content)]);
|
|
325
319
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
const bundles = [
|
|
329
|
-
{ type: 'esm', dir: 'esm' },
|
|
330
|
-
{ type: 'cjs', dir: 'cjs' },
|
|
331
|
-
];
|
|
320
|
+
/** @type {BundleType[]} */
|
|
321
|
+
const bundles = ['esm', 'cjs'];
|
|
332
322
|
|
|
333
323
|
await moveAndTransformDeclarations({
|
|
334
324
|
inputDir,
|
|
335
325
|
buildDir,
|
|
336
326
|
bundles,
|
|
337
|
-
isFlat: true,
|
|
338
327
|
packageType: 'module',
|
|
339
328
|
});
|
|
340
329
|
|
|
341
|
-
// Each bundle
|
|
342
|
-
// ESM with module packageType keeps .d.ts
|
|
343
|
-
const esmFile = path.join(buildDir, '
|
|
330
|
+
// Each bundle writes its own transformed copy to the build root.
|
|
331
|
+
// ESM with module packageType keeps .d.ts (and is the original, so it stays)
|
|
332
|
+
const esmFile = path.join(buildDir, 'index.d.ts');
|
|
344
333
|
const esmStat = await fs.stat(esmFile).catch(() => null);
|
|
345
334
|
expect(esmStat?.isFile()).toBe(true);
|
|
346
335
|
|
|
347
336
|
// CJS with module packageType gets .d.cts
|
|
348
|
-
const cjsFile = path.join(buildDir, '
|
|
337
|
+
const cjsFile = path.join(buildDir, 'index.d.cts');
|
|
349
338
|
const cjsStat = await fs.stat(cjsFile).catch(() => null);
|
|
350
339
|
expect(cjsStat?.isFile()).toBe(true);
|
|
351
|
-
|
|
352
|
-
// Original in buildDir should be removed in flat mode since writesToOriginalPath is false
|
|
353
|
-
const originalFile = path.join(buildDir, 'index.d.ts');
|
|
354
|
-
const originalStat = await fs.stat(originalFile).catch(() => null);
|
|
355
|
-
expect(originalStat).toBeNull();
|
|
356
340
|
});
|
|
357
341
|
});
|