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

Provide plugins with insight into webpack target #787

Closed
DylanPiercey opened this issue May 9, 2019 · 1 comment
Closed

Provide plugins with insight into webpack target #787

DylanPiercey opened this issue May 9, 2019 · 1 comment

Comments

@DylanPiercey
Copy link
Contributor

DylanPiercey commented May 9, 2019

I'm submitting a feature request
I am currently using server side and client side bundling with webpack and babel-loader. I believe this pattern is going to increase since it improves performance (especially with lambda's), and can even simplify the toolchain developers use (shared config between server and browser).

One small change I think could help simplify basic configs though would be to make it so that the webpack target was passed into the babel caller option.

Currently if I wan't to use a .babelrc or babel.config.js I have to pass this information myself so that I can efficiently use @babel/preset-env with { node: "current" } for the server and just the defaults in the browser.

To facilitate this my webpack config becomes something like:

const serverConfig = {
  ...sharedConfig,
  module: {
    rules: [
      {
        test: /\.js$/,
        use: [
          {
            loader: "babel-loader",
            options: {
              caller: {
                target: "node"
              }
            }
          }
        ]
      },
      ...
    ]
  }
}

const browserConfig = {
  ...sharedConfig,
  module: {
    rules: [
      {
        test: /\.js$/,
        use: [
          {
            loader: "babel-loader",
            options: {
              caller: {
                target: "web"
              }
            }
          }
        ]
      },
      ...
    ]
  }
}

This is a lot of config that I can't share just so that I can compile my files with babel differently depending on environment.

My babel.config.js would look something like:

module.exports = api => {
  return {
    presets: [
      [
        "@babel/env",
        {
          loose: true,
          targets: api.caller(isNode) ? {
            node: "current"
          } : undefined
        }
      ]
    ]
  };
};

function isNode(caller) {
  return caller.target === "node";
}

If the target was made available to babel via caller I could instead add just the following to my shared config which simplifies it a lot!

const sharedConfig = {
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: "babel-loader"
      },
      ...
    ]
  }
}

Webpack Version:
4.30.0

Babel Core Version:
7.4.4

Babel Loader Version:
8.0.5

Please tell us about your environment:
OSX 10.x

Current behavior:
The current webpack output target is not made available to babel plugins.

Expected/desired behavior:
Provide the current webpack output target via caller to babel plugins.


Happy to create a PR for this, and perhaps even standardize this across the different babel bundler plugins.

@DylanPiercey
Copy link
Contributor Author

#826 was merged which implemented the feature. Closing this 😄

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

No branches or pull requests

1 participant