Skip to content

Commit 1882210

Browse files
motiz88facebook-github-bot
authored andcommitted
Support conditional exports when enumerating entry points (#51698)
Summary: Pull Request resolved: #51698 Changelog: [Internal] TSIA Reviewed By: hoxyq Differential Revision: D75682786 fbshipit-source-id: 3d70c36f26d87f0be003784aa76bec34640b38d2
1 parent a218b63 commit 1882210

File tree

1 file changed

+61
-40
lines changed

1 file changed

+61
-40
lines changed

scripts/build/build.js

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -283,60 +283,81 @@ async function getEntryPoints(
283283

284284
const exportsEntries = Object.entries(pkg.exports);
285285

286-
for (const [subpath, target] of exportsEntries) {
287-
if (typeof target !== 'string') {
288-
throw new Error(
289-
`Invalid exports field in package.json for ${packageName}. ` +
290-
`exports["${subpath}"] must be a string target.`,
291-
);
286+
for (const [subpath, targetOrConditionsObject] of exportsEntries) {
287+
const targets /*: string[] */ = [];
288+
if (
289+
typeof targetOrConditionsObject === 'object' &&
290+
targetOrConditionsObject != null
291+
) {
292+
for (const [condition, target] of Object.entries(
293+
targetOrConditionsObject,
294+
)) {
295+
if (typeof target !== 'string') {
296+
throw new Error(
297+
`Invalid exports field in package.json for ${packageName}. ` +
298+
`exports["${subpath}"]["${condition}"] must be a string target.`,
299+
);
300+
}
301+
targets.push(target);
302+
}
303+
} else {
304+
if (typeof targetOrConditionsObject !== 'string') {
305+
throw new Error(
306+
`Invalid exports field in package.json for ${packageName}. ` +
307+
`exports["${subpath}"] must be a string target.`,
308+
);
309+
}
310+
targets.push(targetOrConditionsObject);
292311
}
293312

294-
// Skip non-JS files
295-
if (!target.endsWith('.js')) {
296-
continue;
297-
}
313+
for (const target of targets) {
314+
// Skip non-JS files
315+
if (!target.endsWith('.js')) {
316+
continue;
317+
}
298318

299-
if (target.includes('*')) {
300-
console.warn(
301-
`${chalk.yellow('Warning')}: Encountered subpath pattern ${subpath}` +
302-
` in package.json exports for ${packageName}. Matched entry points ` +
303-
'will not be validated.',
304-
);
305-
continue;
306-
}
319+
if (target.includes('*')) {
320+
console.warn(
321+
`${chalk.yellow('Warning')}: Encountered subpath pattern ${subpath}` +
322+
` in package.json exports for ${packageName}. Matched entry points ` +
323+
'will not be validated.',
324+
);
325+
continue;
326+
}
307327

308-
// Normalize to original path if previously rewritten
309-
const original = normalizeExportsTarget(target);
328+
// Normalize to original path if previously rewritten
329+
const original = normalizeExportsTarget(target);
310330

311-
if (original.endsWith('.flow.js')) {
312-
throw new Error(
313-
`Package ${packageName} defines exports["${subpath}"] = "${original}". ` +
314-
'Expecting a .js wrapper file. See other monorepo packages for examples.',
315-
);
316-
}
331+
if (original.endsWith('.flow.js')) {
332+
throw new Error(
333+
`Package ${packageName} defines exports["${subpath}"] = "${original}". ` +
334+
'Expecting a .js wrapper file. See other monorepo packages for examples.',
335+
);
336+
}
317337

318-
// Our special case for wrapper files that need to be stripped
319-
const resolvedTarget = path.resolve(PACKAGES_DIR, packageName, original);
320-
const resolvedFlowTarget = resolvedTarget.replace(/\.js$/, '.flow.js');
338+
// Our special case for wrapper files that need to be stripped
339+
const resolvedTarget = path.resolve(PACKAGES_DIR, packageName, original);
340+
const resolvedFlowTarget = resolvedTarget.replace(/\.js$/, '.flow.js');
321341

322-
try {
323-
await Promise.all([
324-
fs.access(resolvedTarget),
325-
fs.access(resolvedFlowTarget),
326-
]);
327-
} catch {
328-
throw new Error(
329-
`${resolvedFlowTarget} does not exist when building ${packageName}.
342+
try {
343+
await Promise.all([
344+
fs.access(resolvedTarget),
345+
fs.access(resolvedFlowTarget),
346+
]);
347+
} catch {
348+
throw new Error(
349+
`${resolvedFlowTarget} does not exist when building ${packageName}.
330350
331351
From package.json exports["${subpath}"]:
332352
- found: ${path.relative(REPO_ROOT, resolvedTarget)}
333353
- missing: ${path.relative(REPO_ROOT, resolvedFlowTarget)}
334354
335355
This is needed so users can directly import this entry point from the monorepo.`,
336-
);
337-
}
356+
);
357+
}
338358

339-
entryPoints.add(resolvedFlowTarget);
359+
entryPoints.add(resolvedFlowTarget);
360+
}
340361
}
341362

342363
return entryPoints;

0 commit comments

Comments
 (0)