survivejs / webpack-book

From apprentice to master (CC BY-NC-ND)
https://survivejs.com/webpack/
2.42k stars 319 forks source link

exported function vs object for webpack.config.js #270

Closed jraller closed 7 years ago

jraller commented 7 years ago

In section 4.3 the following code is shown:

module.exports = (env) => {
  if (env === 'production') {
    return merge(commonConfig, productionConfig);
  }
  return merge(commonConfig, developmentConfig);
}

but this changes things so that a function is exported rather than an object. In order to utilize the webpack.config.js file with other tools it was pointed out to me that I needed to call what I had required in in order to get the function. Alternatively changing the code to

module.exports = ((env) => {
  if (env === 'production') {
    return merge(commonConfig, productionConfig);
  }
  return merge(commonConfig, developmentConfig);
})();

would retain the expected behavior for the webpack.config.js file by turning it into an function expression instead of a function definition.

bebraw commented 7 years ago

What other tools do you have in mind? Where are you planning to use the configuration?

If you follow the book convention, then the invocation pattern is like this:

const config = require('./webpack.config')(env); // Pass env now
sapegin commented 7 years ago

In that case env would be undefined. Which other tools you mean? For example in React Styleguidist we support the fist variant.

jraller commented 7 years ago

@sapegin makes a really good point about env being undefined and it appears not just when the file is required, I set up the interior of my function with a switch on env, but added a default that exports my test configuration.

The tool I'm using it with is WallabyJS https://wallabyjs.com/