Skip to content

feat: expose webpack target via babel caller#826

Merged
existentialism merged 2 commits into
babel:masterfrom
DylanPiercey:add-webpack-target-caller
Feb 28, 2020
Merged

feat: expose webpack target via babel caller#826
existentialism merged 2 commits into
babel:masterfrom
DylanPiercey:add-webpack-target-caller

Conversation

@DylanPiercey
Copy link
Copy Markdown
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

@DylanPiercey DylanPiercey force-pushed the add-webpack-target-caller branch from 074e48a to d5483a1 Compare February 27, 2020 16:58
@nicolo-ribaudo nicolo-ribaudo requested review from JLHwung and removed request for JLHwung February 27, 2020 17:12
@nicolo-ribaudo
Copy link
Copy Markdown
Member

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

@DylanPiercey
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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).

Comment thread README.md Outdated
Comment thread README.md
Comment thread README.md Outdated
@DylanPiercey DylanPiercey force-pushed the add-webpack-target-caller branch from d5483a1 to a47605c Compare February 28, 2020 04:58
@existentialism
Copy link
Copy Markdown
Member

@DylanPiercey thanks!

@bpinto
Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown

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
Copy Markdown
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
Copy Markdown

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.

4 participants