Skip to content

Commit

Permalink
Upgrade enhanced-resolve to enable subpath exports in jest config
Browse files Browse the repository at this point in the history
A newer version of `enhanced-resolve` supports [node subpath exports](https://nodejs.org/api/packages.html#subpath-exports) so in this PR I've upgraded to the latest release in fusion-cli. I noticed that the jest resolver is copied into `jest-resolver-fusion` package with a different `enhanced-resolve` version. Should that be updated as well?

Most of this upgrade was straightforward besides handing the `es5-ext` package paths. Enhanced-resolve will escape `#` characters in file paths. I did some digging in webpack and found that they are using the async resolve method that will include the unescaped path, but in the sync variant it will only output escaped. Before the unescaped path was available, webpack would regex replace the `\0#` substrings so that's what I've implemented here.

I tested this change in web-code by resolving `fusion-cli/enhanced-resolve` to `5.10.0` and patching `fusion-cli` with the changes included in this PR.

Phab revision: https://code.uberinternal.com/D7832723
Passing CI build: https://buildkite.com/uber/web-code/builds/248921
  • Loading branch information
chasestarr authored and fusionjs-sync-bot[bot] committed Jul 15, 2022
1 parent f4e0375 commit 91783e2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
29 changes: 27 additions & 2 deletions fusion-cli/build/jest/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const nodeResolver = ResolverFactory.createResolver({
useSyncFileSystemCalls: true,
mainFields: ['main'],
aliasFields: ['es2015', 'es2017'],
conditionNames: ['node', 'require'],
});

const browserResolver = ResolverFactory.createResolver({
Expand All @@ -21,8 +22,31 @@ const browserResolver = ResolverFactory.createResolver({
useSyncFileSystemCalls: true,
mainFields: ['main'],
aliasFields: ['browser', 'es2015', 'es2017'],
conditionNames: ['node', 'require'],
});

// The es5-ext package has files within a '#' directory. Ex. es5-ext/string/#/contains
//
// Enhanced-resolve will escape '#' characters by prefixing with a null terminator '\0'
// https://github.com/webpack/enhanced-resolve/pull/255/files
//
// Webpack will then find instances of this escape pattern and convert back to '#'
// this commit below shows how webpack is transitioning from regex replace to a new
// enhanced-resolve feature that provides an unescaped path.
// https://github.com/webpack/webpack/commit/0197867237cd1ef4c1e13d5fcafc71f4585bff9d
//
// This feature is only available on the async resolve method on Resolver.
// https://github.com/webpack/enhanced-resolve/blob/4b16e4c7ee9fc79f6a544a2018baaedb2f7a340f/lib/Resolver.js#L237
//
// Because this file uses the resolveSync method, the unescaped path is not available,
// it will take the webpack's original approach of regex replace.
function unescapePathFragment(resolverResult) {
if (typeof resolverResult === 'string') {
return resolverResult.replace(/\0/g, '');
}
return resolverResult;
}

module.exports = function enhancedResolve(modulePath, opts) {
try {
if (opts.browser === true) {
Expand All @@ -32,7 +56,7 @@ module.exports = function enhancedResolve(modulePath, opts) {
modulePath
);
if (browserResult !== false) {
return browserResult;
return unescapePathFragment(browserResult);
}
// Fallback to non-browser field if enhanced-resolve produces `false`.
// Some packages (e.g. object-inspect) use falsy browser field values to
Expand All @@ -41,7 +65,8 @@ module.exports = function enhancedResolve(modulePath, opts) {
// So we simply fall back to resolution without browser field in this case
}

return nodeResolver.resolveSync({}, opts.basedir, modulePath);
const nodeResult = nodeResolver.resolveSync({}, opts.basedir, modulePath);
return unescapePathFragment(nodeResult);
} catch (err) {
// Upon failure to resolve, enhanced-resolve throws with a different
// error message than native Node and no error.code property.
Expand Down
2 changes: 1 addition & 1 deletion fusion-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"chalk": "^4.1.2",
"compression-webpack-plugin": "^9.2.0",
"core-js": "^3.17.1",
"enhanced-resolve": "5.0.0-beta.4",
"enhanced-resolve": "^5.10.0",
"enzyme-adapter-react-16": "^1.15.6",
"enzyme-to-json": "^3.6.2",
"esbuild": "^0.14.34",
Expand Down

0 comments on commit 91783e2

Please sign in to comment.