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
chore: replace resolve with create-require #12396
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/33244/ |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 860dc83:
|
/cc @ljharb. |
|
Only when patched by Yarn at install time, which is not possible when bundled (as mentioned in the OP) since Yarn can't detect it.
The only part from it that is needed is the polyfill https://github.com/nuxt-contrib/create-require/blob/f45cb926b1a6cffb1b879377933155c7ac523a76/create-require.js#L30-L37 which i'm perfectly fine with inlining until it can be removed in Babel 8. The full version when inlined would look like this one from gatsby https://github.com/gatsbyjs/gatsby/blob/3e9279563f76cef4e6a25ccb52aef85b5f6c2b6d/packages/gatsby-core-utils/src/create-require-from-path.ts |
While I have you here @ljharb, seems |
In the |
@merceyz resolve v1 has It would be a great idea for babel to upgrade to using v2 of resolve, if possible. |
Even better
Ah, my bad, was looking at the readme for the
Using the builtin |
regular require also won’t work any better than |
PnP is always able to control the builtin require, it always goes through
Assuming the people making the bundle runs under Yarn 2 then sure, a bundled resolve will have PnP support, but in this case they aren't and so it doesn't have PnP support. |
This would still be true with require or require.resolve tho - the results of those get written into the bundle (bundles primarily being for the web, where no filesystem exists). If you make a node bundle, then it’d still be the native require which pnp can hook into. |
The problem is specifically for bundles targeting Node.js (e.g. Next.js's build system is bundled, including Babel). Anyway, we are already removing However, I'd like to propose an alternative PR: main...nicolo-ribaudo:require-resolve-node-6
By doing so,
Note that customizing our build process to support older Node.js versions is something we already did: for example, before #12288 we were using a custom plugin to fix resolution of dynamic import paths in some Node.js versions. |
Your alternative is fantastic, it gets the job done and it's easy to remove the polyfill when Babel 8 rolls around so i'm all for it 👍 |
This comment has been minimized.
This comment has been minimized.
We only use it for resolving plugins and presets, for transpiling we use |
resolve
withcreate-require
Babel seems to only need
resolve
because of itsbasedir
option, however Node supports usingrequire.resolve(id, { paths: [baseDir] })
orcreateRequire*(baseDir).resolve(id)
for this. Since Babel still supports Node 6, which doesn't have any of these, a polyfill is used forcreateRequire
.create-require
can be removed in Babel 8 in favour of just usingrequire.resolve
The benefit of this change is that PnP support is preserved if someone feels like bundling Babel (vercel/next.js#18768)