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

feat: expose webpack target via babel caller #826

Merged
merged 2 commits into from Feb 28, 2020

Conversation

DylanPiercey
Copy link
Contributor

Please check if the PR fulfills these requirements

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior? (You can also link to an open issue here)
Currently it is not possible to determine inside of a babel.config.js file what the webpack target being bundled for is.

What is the new behavior?
Exposes the current webpack target using the babel caller api.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information:
I'm happy to add a test if necessary. However there are not existing tests for the already injected caller data. I think it'd require a bit of effort to setup correctly since we'd probably want to write a fake plugin which just checked what the caller was.

Implements #787

@nicolo-ribaudo
Copy link
Member

target can also be a function, but I wonder how it interacts with config caching 🤔

@DylanPiercey
Copy link
Contributor Author

@nicolo-ribaudo hmm. I thought it was resolved to a string by the time it got to the loader. Could very well be wrong on that though. I'm pretty sure caller will error if passed a function since they serialize it for the cache key.

@nicolo-ribaudo
Copy link
Member

I thought it was resolved to a string by the time it got to the loader.

Oh ok, I didn't know about it 👍

@DylanPiercey
Copy link
Contributor Author

Yes it looks like it is resolved to a string since the target for the loader is actually added by a plugin here: https://github.com/webpack/webpack/blob/0d4607c68e04a659fa58499e1332c97d5376368a/lib/LoaderTargetPlugin.js#L29 which is always passed a string. In the function target example in the webpack docs they actually use this plugin inside the function (https://webpack.js.org/configuration/target/#function).

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@existentialism
Copy link
Member

@DylanPiercey thanks!

@bpinto
Copy link

bpinto commented Jun 7, 2022

Is this working? I can't get anything other than web as my target, even though it's configured to use a browserlist environment target: 'browserslist:app'.

@DylanPiercey
Copy link
Contributor Author

@bpinto this.target in the context of a webpack loader is the normalized target, not the raw one, so it becomes web for anything that uses browserslist.

@bpinto
Copy link

bpinto commented Jun 7, 2022

@DylanPiercey Is there a way to use webpack target (raw) for babel-loader browserlistEnv option? Thank you.

I was trying the following:

  presets: [
    [
      '@babel/preset-env',
      {
        browserslistEnv: api.caller(caller => caller.target),
        corejs: '3.22',
        useBuiltIns: 'entry'
      }
    ],

@DylanPiercey
Copy link
Contributor Author

@bpinto I think it probably could be exposed as well, but it'd need to be a new property on the caller. Currently it is not exposed.

@DylanPiercey DylanPiercey deleted the add-webpack-target-caller branch June 7, 2022 20:38
@bpinto
Copy link

bpinto commented Jun 7, 2022

@DylanPiercey #943

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants