Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: try harder to resolve .mjs files for the browser entries #21161

Merged
merged 3 commits into from
Feb 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions code/lib/core-common/src/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ export function filterPresetsConfig(presetsConfig: PresetConfig[]): PresetConfig
});
}

function resolvePathToMjs(filePath: string): string {
const { dir, name } = parse(filePath);
const mjsPath = join(dir, `${name}.mjs`);
if (safeResolve(mjsPath)) {
return mjsPath;
}
return filePath;
}

function resolvePresetFunction<T = any>(
input: T[] | Function,
presetOptions: any,
Expand Down Expand Up @@ -76,7 +85,7 @@ export const resolveAddonName = (
// we remove the extension
// this is a bit of a hack to try to find .mjs files
// node can only ever resolve .js files; it does not look at the exports field in package.json
managerEntries: [join(fdir, fname)],
managerEntries: [resolvePathToMjs(join(fdir, fname))],
};
}
if (name.match(/\/(preset)(\.(js|mjs|ts|tsx|jsx))?$/)) {
Expand All @@ -98,15 +107,21 @@ export const resolveAddonName = (
// serve it up correctly when yarn pnp or pnpm is being used.
// Vite will be broken in such cases, because it does not process absolute paths,
// and it will try to import from the bare import, breaking in pnp/pnpm.
const absolutizeExport = (exportName: string) => {
return resolve(`${name}${exportName}`);
const absolutizeExport = (exportName: string, preferMJS: boolean) => {
const found = resolve(`${name}${exportName}`);

if (found) {
return preferMJS ? resolvePathToMjs(found) : found;
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
}
return undefined;
};

const managerFile = absolutizeExport(`/manager`);
const registerFile = absolutizeExport(`/register`) || absolutizeExport(`/register-panel`);
const managerFile = absolutizeExport(`/manager`, true);
const registerFile =
absolutizeExport(`/register`, true) || absolutizeExport(`/register-panel`, true);
const previewFile = checkExists(`/preview`);
const previewFileAbsolute = absolutizeExport('/preview');
const presetFile = absolutizeExport(`/preset`);
const previewFileAbsolute = absolutizeExport('/preview', true);
const presetFile = absolutizeExport(`/preset`, false);

if (!(managerFile || previewFile) && presetFile) {
return {
Expand All @@ -119,19 +134,11 @@ export const resolveAddonName = (
const managerEntries = [];

if (managerFile) {
// we remove the extension
// this is a bit of a hack to try to find .mjs files
// node can only ever resolve .js files; it does not look at the exports field in package.json
const { dir: fdir, name: fname } = parse(managerFile);
managerEntries.push(join(fdir, fname));
managerEntries.push(managerFile);
}
// register file is the old way of registering addons
if (!managerFile && registerFile && !presetFile) {
// we remove the extension
// this is a bit of a hack to try to find .mjs files
// node can only ever resolve .js files; it does not look at the exports field in package.json
const { dir: fdir, name: fname } = parse(registerFile);
managerEntries.push(join(fdir, fname));
managerEntries.push(registerFile);
}

return {
Expand Down