Webpack Configuration: Conditionally Imported Module

So I want:

if (process.env.NODE_ENV === 'production') { import StatsPlugin from 'webpack-stats-plugin'; } 

But eslint says:

 Parsing error: 'import' and 'export' may only appear at the top level 

I am using the babel-eslint parser.

Does this mean that I cannot load modules conditionally?

+5
source share
1 answer

Dynamic synchronous import is not possible using ES2015 modules. Dynamically, you can import material with asynchronous import through import() .

Why don't you just import it and apply it conditionally?

 import StatsPlugin from 'webpack-stats-plugin'; ... if (process.env.NODE_ENV === 'production') { config.plugins.push(new Statsplugin()) } 
+6
source

Source: https://habr.com/ru/post/1242134/


All Articles