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

Allow transform-runtime to insert runtime references with absolute paths. #8435

Merged
merged 1 commit into from Aug 14, 2018

Conversation

loganfsmyth
Copy link
Member

Q                       A
Fixed Issues? Fixes feature request in #8266 (comment) for runtime transform
Patch: Bug Fix?
Major: Breaking Change? N
Minor: New Feature? Y
Tests Added + Pass?
Documentation PR Link
Any Dependency Changes?
License MIT

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:
    • relative to Babel's cwd if the transform-runtime plugin is passed as a programmatic arg to Babel
    • relative to the config file, if transform-runtime is enabled via a config file
  • absoluteRuntime: '/projects/thing/src'
    • Resolve the runtime relative to some specific location, for instance if you wanted to pass the __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

@loganfsmyth loganfsmyth added the PR: New Feature 🚀 A type of pull request used for our changelog categories label Aug 8, 2018
@babel-bot
Copy link
Collaborator

babel-bot commented Aug 8, 2018

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/8815/

@babel-bot
Copy link
Collaborator

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 }),
Copy link
Member

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?

Copy link
Member Author

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.

@loganfsmyth loganfsmyth merged commit 1e0b649 into babel:master Aug 14, 2018
@loganfsmyth loganfsmyth deleted the absolulte-runtime-option branch August 14, 2018 23:11
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 4, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: New Feature 🚀 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants