Webpack: various (Define Plugin) settings for each entry point

For some of my package files, I would like to create different versions (for example, one with and without administrator functionality).

It seems that DefinePlugin is usually used for this kind of thing, but there are also downloaders like if-loader or ifdef-loader .

Now the problem is that they all seem to have a configuration among all entry points. I would need to set a different configuration (for example, {with_admin_mode: true} and {with_admin_mode: false} ) for different entry points.

Although I put the configuration at the top of the actual JS files of the entry point, I don’t know how to correctly create a global variable in all modules that will be detected as true == false and deleted.

+10
webpack
source share
2 answers

Try looking at webpack-merge - Smart Merging should help you solve your business.

This can help in creating a dynamic configuration with specific entry points, plugins, and what you will ever want depends on some env variable.

It’s good practice to split the configuration into different files. You can make some general settings with the same things through all entry points, for example, bootloaders, and then make custom configurations for with_admin_mode and without_admin_node .

0
source share

Sorry, I wanted to write offerPlugin, not definePlugin. DefinePlugin will replace the variable in your code directly and will not create a global one.

You can use securityPlugin to set the global configuration variable from the module file you created:

in the configuration of your web package:

 plugins: [ new webpack.ProvidePlugin({ 'config': 'config' }) ... ], resolve: { alias: { 'config': path.resolve(__dirname, './config') }, extensions: ['.js'] } 

and config will be set as global for exporting the config.js file.

Then you can access this global and change it at different entry points. For example, you can do config.with_admin_mode = true; in entry1.js and config.with_admin_mode = false; in entry2.js.

Another solution would be to set a global variable in the window directly from your modules, for example. window.with_admin_mode = true;

0
source share

All Articles