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

Handle non-exported package.json files #108

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,27 @@ function tryRequire(id) {
}
}

const errorRegExp = /Package subpath '.\/package.json' is not defined by "exports" in (.*)/;
function tryParseErrorMessage(error) {
try {
const [, file] = errorRegExp.exec(error.message);
return file;
} catch (err) {
// if the matching fails, rethrow the original error for inspection
throw error;
}
}

function tryResolve(pkg, importer) {
try {
return relative.resolve(pkg, importer);
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') return null;
if (err.code === 'ERR_PACKAGE_PATH_NOT_EXPORTED')
// in the edge cases where the package.json file is not listed in the
// package's "exports" field, parse the target file name from the error
// message
return tryParseErrorMessage(err);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that parsing the error message is a good idea. I think the error message is nothing one could rely on, it could change at any point in time.

Instead, I believe that right now the plugin should probably just emit a warning if it cannot resolve the package.json so that package authors can start exporting it if necessary (it's not always necessary, in the case of uuid it is definitely not necessary for the svelte plugin to read uuid's package.json since it doesn't contain any svelte-specific information).

See: uuidjs/uuid#444 (comment)

We might even see Node.js change it's behavior again and always make package.json resolvable, see nodejs/node#33460

throw err;
}
}
Expand Down