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
Allow transform-runtime to insert runtime references with absolute paths. #8435
Allow transform-runtime to insert runtime references with absolute paths. #8435
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/8815/ |
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/8770/ |
function resolveAbsoluteRuntime(moduleName: string, dirname: string) { | ||
try { | ||
return path.dirname( | ||
resolve.sync(`${moduleName}/package.json`, { basedir: dirname }), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to look for package.json?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The runtime doesn't have a main
or index
because it's meant to be a collection of independent files. Since we want to resolve the directory location though, we do need a specific file to resolve, and package.json
fits those requirements well.
73c9547
to
5d9c662
Compare
5d9c662
to
67c7ba5
Compare
This allows users to run
transform-runtime
broadly across a whole project. By default,transform-runtime
imports from@babel/runtime/foo
directly, but that only works if@babel/runtime
is in the node modules of the file that is being compiled. This can be problematic for nested node modules, or npm-linked modules, among other cases. To avoid worrying about how the runtime module's location, this allows users to resolve the runtime once up front, and then insert absolute paths to the runtime into the output code.Using absolute paths is not desirable if files are compiled for use at a later time, but in contexts where a file is compiled and then immediately consumed, they can be quite helpful.
This PR exposes two approaches for resolving the runtime:
absoluteRuntime: true
- Resolve the runtime:cwd
if thetransform-runtime
plugin is passed as a programmatic arg to Babeltransform-runtime
is enabled via a config fileabsoluteRuntime: '/projects/thing/src'
__dirname
of some general build process script. e.g. will look at/projects/thing/src/node_modules/@babel/runtime/foo
/projects/thing/node_modules/@babel/runtime/foo
/projects/node_modules/@babel/runtime/foo
/node_modules/@babel/runtime/foo
cc @insin