Skip to content

Commit

Permalink
fix: force commonjs resolving for chevrotain
Browse files Browse the repository at this point in the history
  • Loading branch information
CompuIves committed Jan 15, 2025
1 parent 448c25b commit fad8994
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
11 changes: 8 additions & 3 deletions packages/sandpack-core/src/resolver/utils/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export function normalizePackageExport(

export function extractPathFromExport(
exportValue: PackageExportType,
pkgRoot: string
pkgRoot: string,
checkedExportKeys: string[] = EXPORTS_KEYS
): string | false {
if (!exportValue) {
return false;
Expand All @@ -46,13 +47,17 @@ export function extractPathFromExport(
}

if (typeof exportValue === 'object') {
for (const key of EXPORTS_KEYS) {
for (const key of checkedExportKeys) {
const exportFilename = exportValue[key];
if (exportFilename !== undefined) {
if (typeof exportFilename === 'string') {
return normalizePackageExport(exportFilename, pkgRoot);
}
return extractPathFromExport(exportFilename, pkgRoot);
return extractPathFromExport(
exportFilename,
pkgRoot,
checkedExportKeys
);
}
}
return false;
Expand Down
15 changes: 14 additions & 1 deletion packages/sandpack-core/src/resolver/utils/pkg-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ const PKG_ALIAS_FIELDS = ['browser', 'alias'];

type AliasesDict = { [key: string]: string };

const COMMONJS_EXPORT_KEYS = ['browser', 'development', 'default', 'require'];
function forceCommonJs(name: string, version: string): boolean {
return name === 'chevrotain' && version.startsWith('10.');
}

export interface ProcessedPackageJSON {
aliases: AliasesDict;
hasExports: boolean;
Expand Down Expand Up @@ -43,7 +48,15 @@ export function processPackageJSON(
} else if (typeof content.exports === 'object') {
const exportKeys = Object.keys(content.exports);
if (!exportKeys[0].startsWith('.')) {
const resolvedExport = extractPathFromExport(content.exports, pkgRoot);
const checkedExportKeys = forceCommonJs(content.name, content.version)
? COMMONJS_EXPORT_KEYS
: undefined;

const resolvedExport = extractPathFromExport(
content.exports,
pkgRoot,
checkedExportKeys
);
if (!resolvedExport) {
throw new Error(`Could not find a valid export for ${pkgRoot}`);
}
Expand Down

0 comments on commit fad8994

Please sign in to comment.