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

(wmr) - resolve default over node #389

Merged
merged 18 commits into from
Mar 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions packages/wmr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
"premove": "^3.0.1",
"prettier": "^2.0.5",
"puppeteer": "^3.3.0",
"resolve.exports": "^1.0.2",
"rollup": "^2.39.0",
"rollup-plugin-preserve-shebang": "^1.0.1",
"sade": "^1.7.3",
Expand Down
60 changes: 12 additions & 48 deletions packages/wmr/src/plugins/npm-plugin/resolve.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import { resolve as _resolveExports } from 'resolve.exports';

function resolveExports(pkg, key) {
const conditions = [process.env.NODE_ENV === 'production' ? 'production' : 'development'];
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved

return _resolveExports(pkg, key, {
browser: true,
conditions
});
}

/**
* @param {string} path
* @param {object} context
Expand All @@ -23,7 +34,7 @@ export async function resolveModule(path, { readFile, hasFile, module, internal
if (!internal && pkg.exports) {
const entry = path ? `./${path}` : '.';

const mapped = resolveExportMap(pkg.exports, entry, ENV_KEYS);
const mapped = resolveExports(pkg.exports, entry);
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved

if (!mapped) {
throw new Error(`Unknown package export ${entry} in ${module}.\n\n${JSON.stringify(pkg.exports, null, 2)}`);
Expand Down Expand Up @@ -76,50 +87,3 @@ function getLegacyEntry(pkg) {
const entry = mainFields.find(p => p && typeof p === 'string');
return '/' + entry.replace(/^\.?\//, '');
}

const ENV_KEYS = ['esmodules', 'import', 'module', 'require', 'browser', 'default', 'node'];

/** Get the best resolution for an entry from an Export Map
* @param {Object} exp `package.exports`
* @param {string} entry `./foo` or `.`
* @param {string[]} envKeys package environment keys
* @returns {string | boolean} a resolved path, or a boolean indicating if the given entry is exposed
*/
function resolveExportMap(exp, entry, envKeys) {
if (typeof exp === 'string') {
// {"exports":"./foo.js"}
// {"exports":{"./foo":"./foo.js"}}
return exp;
}
let isFileListing;
let isDirectoryExposed = false;

let fallbacks = [];
for (let i in exp) {
if (isFileListing === undefined) isFileListing = i[0] === '.';
if (isFileListing) {
// {"exports":{".":"./index.js"}}
if (i === entry) {
return resolveExportMap(exp[i], entry, envKeys);
}
if (!isDirectoryExposed && i.endsWith('/') && entry.startsWith(i)) {
isDirectoryExposed = true;
}
} else if (envKeys.includes(i)) {
// intentionally de-prioritize "require" and "default" keys
if (i === 'require' || i === 'default') {
fallbacks.push(i);
} else {
// {"exports":{"import":"./foo.js"}}
return resolveExportMap(exp[i], entry, envKeys);
}
}
}

// None of the in-order keys matched - fall back to require/default in the order specified
for (let i of fallbacks) {
return resolveExportMap(exp[i], entry, envKeys);
}

return isDirectoryExposed;
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6791,6 +6791,11 @@ resolve-url@^0.2.1:
resolved "https://registry.yarnpkg.com/resolve-url/-/resolve-url-0.2.1.tgz#2c637fe77c893afd2a663fe21aa9080068e2052a"
integrity sha1-LGN/53yJOv0qZj/iGqkIAGjiBSo=

resolve.exports@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/resolve.exports/-/resolve.exports-1.0.2.tgz#98c82ba8a4d3f9fcc32cbfa6f950b803d77b6c21"
integrity sha512-1+PDdTR3xrGWB/NzXLkzS1+PQlJ+BOR2baBGJSVat4HasiY1mnkyAQws3FUTmBDB79oK54QFaDM8Ig9nUtJwvQ==

resolve@^1.10.0, resolve@^1.11.0, resolve@^1.12.0, resolve@^1.13.1, resolve@^1.17.0, resolve@^1.18.1:
version "1.20.0"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.20.0.tgz#629a013fb3f70755d6f0b7935cc1c2c5378b1975"
Expand Down