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 preset-env to toggle module handling based on flags from the caller (like babel-loader) #8485

Merged
merged 6 commits into from Aug 20, 2018

Conversation

Projects
None yet
6 participants
@loganfsmyth
Member

loganfsmyth commented Aug 17, 2018

Q                       A
Fixed Issues? Fixes #7517
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

What this PR essentially means is that utilities calling Babel can:

  • Pass a name to Babel, so that if there are errors, it can give more useful error messages
  • Pass misc flags that presets can hook into

For instance, babel-loader would be able to do:

babel.transform("code;", {
  filename,
  presets: ["@babel/preset-env"],
  caller: {
    name: "babel-loader",
    supportsStaticESM: true,
  },
});

and preset-env can automatically change its behavior from modules: "commonjs" to modules: false.

@babel-bot

This comment has been minimized.

Show comment
Hide comment
@babel-bot

babel-bot Aug 17, 2018

Collaborator

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

Collaborator

babel-bot commented Aug 17, 2018

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

@babel-bot

This comment has been minimized.

Show comment
Hide comment
@babel-bot

babel-bot Aug 17, 2018

Collaborator

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

Collaborator

babel-bot commented Aug 17, 2018

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

@loganfsmyth

This comment has been minimized.

Show comment
Hide comment
@loganfsmyth

loganfsmyth Aug 17, 2018

Member

@Andarist Any thoughts on this, since it'll probably fit nicely into your work on the Rollup plugin too.

Member

loganfsmyth commented Aug 17, 2018

@Andarist Any thoughts on this, since it'll probably fit nicely into your work on the Rollup plugin too.

export function buildPresetChain(
arg: PresetInstance,
context: *,
): ConfigChain | null {

This comment has been minimized.

@nicolo-ribaudo

nicolo-ribaudo Aug 17, 2018

Member

This function never returns null.

@nicolo-ribaudo

nicolo-ribaudo Aug 17, 2018

Member

This function never returns null.

This comment has been minimized.

@loganfsmyth

loganfsmyth Aug 17, 2018

Member

Oh good catch.

@loganfsmyth

loganfsmyth Aug 17, 2018

Member

Oh good catch.

@nicolo-ribaudo

This comment has been minimized.

Show comment
Hide comment
@nicolo-ribaudo

nicolo-ribaudo Aug 17, 2018

Member

Does Babel use the same config validation for config files and options directly passed to babel.transform? If not, we could disallow caller in .babelrc/babel.config.js since users should directly configure the preset.

Member

nicolo-ribaudo commented Aug 17, 2018

Does Babel use the same config validation for config files and options directly passed to babel.transform? If not, we could disallow caller in .babelrc/babel.config.js since users should directly configure the preset.

@loganfsmyth

This comment has been minimized.

Show comment
Hide comment
@loganfsmyth

loganfsmyth Aug 17, 2018

Member

If not, we could disallow caller in .babelrc/babel.config.js since users should directly configure the preset.

Already done! caller is only allowed as part of the programmatic call options to Babel.

Member

loganfsmyth commented Aug 17, 2018

If not, we could disallow caller in .babelrc/babel.config.js since users should directly configure the preset.

Already done! caller is only allowed as part of the programmatic call options to Babel.

moduleTransformations[modules] &&
// TODO: Remove the 'api.caller' check eventually. Just here to prevent
// unnecessary breakage in the short term for users on older betas/RCs.
(modules !== "auto" || !api.caller || !api.caller(supportsStaticESM))

This comment has been minimized.

@loganfsmyth

loganfsmyth Aug 19, 2018

Member

@nicolo-ribaudo Thoughts? Ended up exposing both and using the api.caller approach I mentioned.

@loganfsmyth

loganfsmyth Aug 19, 2018

Member

@nicolo-ribaudo Thoughts? Ended up exposing both and using the api.caller approach I mentioned.

});
it("`false` option returns commonjs", () => {
it("`false` option returns false", () => {

This comment has been minimized.

@existentialism

existentialism Aug 20, 2018

Member

🤦‍♂️

@existentialism

existentialism Aug 20, 2018

Member

🤦‍♂️

@loganfsmyth loganfsmyth merged commit 2a4f162 into babel:master Aug 20, 2018

4 checks passed

babel/repl REPL preview is available
Details
ci/circleci Your tests passed on CircleCI!
Details
codecov/project 80.55% (target 80%)
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@loganfsmyth loganfsmyth deleted the loganfsmyth:caller-option branch Aug 20, 2018

@hzoo

This comment has been minimized.

Show comment
Hide comment
@hzoo

hzoo Aug 20, 2018

Member

This is really great! Something we should document for our integrations to also take advantage of.

Member

hzoo commented Aug 20, 2018

This is really great! Something we should document for our integrations to also take advantage of.

@Andarist

This comment has been minimized.

Show comment
Hide comment
@Andarist

Andarist Aug 21, 2018

Member

🚀 super! Sorry I've missed this PR before, but this is super awesome - preventing the users misconfiguring their tool in clean way, gonna land this in rollup-plugin-babel once it gets released

Member

Andarist commented Aug 21, 2018

🚀 super! Sorry I've missed this PR before, but this is super awesome - preventing the users misconfiguring their tool in clean way, gonna land this in rollup-plugin-babel once it gets released

abernix added a commit to abernix/hexo-renderer-react-emotion that referenced this pull request Aug 23, 2018

Upgrade to `@babel/*-rc.2.
This resolves a frustrating error I encountered where I was receiving:

    ReferenceError: Unknown option: .caller

...when trying to use `@babel/register`.  This mysteriously went away when I
upgraded to `rc.2`, which was inspired after I honed in on related work to
the `caller` option in babel/babel#8485 after debugging
Babel's `loadOptions` (in `7.0.0-rc.1`) with the Node.js debugger.

@SimenB SimenB referenced this pull request Sep 10, 2018

Open

[WIP] Upgrade entire suite to Babel 7 #6949

3 of 3 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment