Add support for manifest exports fallback arrays
MODIFY
packages/@romejs/codec-js-manifest/convert.ts
packages/@romejs/codec-js-manifest/convert.ts
@@ -32,7 +32,11 @@ export function convertManifestToJSON(manifest: Manifest): JSONManifest {
bugs: manifest.bugs,
main: manifest.main,
- exports: exportsToObject(manifest.exports),
+
+ // TODO we now support fallbacks which means manifest.exports is lossy
+ //exports: exportsToObject(manifest.exports),
+ // rome-suppress-next-line lint/noExplicitAny
+ exports: (manifest.raw.exports as any),
author: manifest.author,
contributors: manifest.contributors,
@@ -102,6 +106,7 @@ function exportsToObject(
return obj;
}
+exportsToObject;
function maybeArray<T>(items: Array<T>): undefined | Array<T> {
if (items.length === 0) {

MODIFY
packages/@romejs/codec-js-manifest/index.ts
packages/@romejs/codec-js-manifest/index.ts
@@ -439,13 +439,37 @@ function normalizeExports(consumer: Consumer): boolean | ManifestExports {
);
}
+ const conditions = normalizeExportsConditions(value);
+
+ if (dotConditions.size > 0) {
+ exports.set(createRelativeFilePath('.'), dotConditions);
+ }
+
+ exports.set(value.getKey().asExplicitRelativeFilePath(), conditions);
+ }
+
+ return exports;
+}
+
+function normalizeExportsConditions(value: Consumer): ManifestExportConditions {
const conditions: ManifestExportConditions = new Map();
+ const unknown = value.asUnknown();
- if (typeof value.asUnknown() === 'string') {
+ if (typeof unknown === 'string') {
conditions.set('default', {
consumer: value,
relative: value.asExplicitRelativeFilePath(),
});
+ } else if (Array.isArray(unknown)) {
+ // Find the first item that passes validation
+ for (const elem of value.asArray()) {
+ const {result, diagnostics} = elem.captureSync(
+ (consumer) => normalizeExportsConditions(consumer),
+ );
+ if (diagnostics.length === 0) {
+ return result;
+ }
+ }
} else {
for (const [type, relativeAlias] of value.asMap()) {
conditions.set(type, {
@@ -455,14 +479,7 @@ function normalizeExports(consumer: Consumer): boolean | ManifestExports {
}
}
- if (dotConditions.size > 0) {
- exports.set(createRelativeFilePath('.'), dotConditions);
- }
-
- exports.set(value.getKey().asExplicitRelativeFilePath(), conditions);
- }
-
- return exports;
+ return conditions;
}
function normalizeBugs(

MODIFY
packages/@romejs/consume/Consumer.ts
packages/@romejs/consume/Consumer.ts
@@ -138,6 +138,26 @@ export default class Consumer {
definitions: Array<ConsumePropertyDefinition>;
diagnostics: Diagnostics;
}> {
+ const {definitions, diagnostics, consumer} = this._capture();
+ const result = await callback(consumer);
+ return {result, definitions, diagnostics};
+ }
+
+ captureSync<T>(callback: (consumer: Consumer) => T): {
+ result: T;
+ definitions: Array<ConsumePropertyDefinition>;
+ diagnostics: Diagnostics;
+ } {
+ const {definitions, diagnostics, consumer} = this._capture();
+ const result = callback(consumer);
+ return {result, definitions, diagnostics};
+ }
+
+ _capture<T>(): {
+ consumer: Consumer;
+ definitions: Array<ConsumePropertyDefinition>;
+ diagnostics: Diagnostics;
+ } {
let diagnostics: Diagnostics = [];
const definitions: Array<ConsumePropertyDefinition> = [];
@@ -154,9 +174,7 @@ export default class Consumer {
diagnostics.push(diag);
},
});
-
- const result = await callback(consumer);
- return {result, definitions, diagnostics};
+ return {consumer, definitions, diagnostics};
}
async captureDiagnostics<T>(
@@ -986,11 +1004,7 @@ export default class Consumer {
return this._asNumber(def);
}
- asNumberInRange(opts: {
- min?: number;
- max?: number;
- default?: number;
- }): number
+ asNumberInRange(opts: {min?: number; max?: number; default?: number}): number
asNumberInRange(opts: {
min: Number0;

Add support for manifest exports fallback arrays