The Oldest True Stories in the World

Nothing stirred in the relentless midday heat. The gum trees appeared exhausted, nearly drained of life. The hunters crouched in the foliage, their long, sharp spears poised to unleash at a moment’s…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Introduction To Webpack 4

Part 2

So in Part 1 we learned how to define our mode environment, config basics including entry / output , rules & loaders, plugins, setting up babel & finally running everything with Webpack’s dev server.

So in part 2 of this tutorial we are going to run through setting up & managing different environments. We will also take a look at some more popular loaders, especially one’s that integrate / are required by React. Then we’ll install some plugins that will help us add functionality to our web development environment.

So we can use separate files to better organise our Webpack configs based on environments. To enable this we will create a function and assign it to a variable of modeConfig . The function takes an argument of env which then returns a require which returns another function that can be called with the env variable… sounds confusing? Let’s type this out first:

const modeConfig = env => require(`./build-utils/webpack.${env}`)(env);

So we call a function via the require because it’s just an ordinary function. So we call require and whatever we pass into the env as an argument will then go and require either webpack.production or webpack.development .

We can also add in some defaults to our Webpack config to provide some safety incase we don’t run webpack with the correct environment variable or properties:

So on the left of the module.exports argument we have a destructured es6 object with 2 variables, our mode environment variable & a presets variable which is an array. On the right we have the defaults or each respective parameter. In this case we set mode to a string & presets to an empty array.

The preset parameter is to enable us to merge in other configuration objects, arrays etc. To make this easier we are going to use webpack-merge , which is by default employingObject.assign, so let’s install it using Yarn:

Note: the --dev flag is to add it to our dev dependencies.

The first arugment to webpackMerge is a base config object, so we can have a basic configurations running irrespective of environment.

Also above we can see modeConfig(mode) which will return either a production or development configuration.

So now let’s move our entire development configuration object and return form within a commonJS module exports function:

Add a comment

Related posts:

Weten of het bier koud staat met koelkast 2.0

Tijdens onze vrijdagmiddagpresentaties drinken wij graag een lekker koud biertje. Het is dan natuurlijk wel handig om van tevoren te weten of er nog bier koud staat. Om dit te controleren kun je…

Every Arpeggio in the Known Universe

Are you an Arpeggio master? Do you know all the different types of arpeggios and how to use them in your playing? The Arpeggio is a very important tool when it comes to jazz and jazz guitar. This…

So You Want to be an Ally

The last few weeks have been overwhelming. From the deaths of George Floyd and Breonna Taylor to the incident in Central Park with Christian Cooper, the topic of existing while black (especially in…