Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack config: Conditionally import module

So I want to do:

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'm using babel-eslint parser.

Does this imply I can't load modules conditionally?

like image 551
Sid Jain Avatar asked Feb 02 '16 21:02

Sid Jain


1 Answers

Dynamic synchronous imports are not possible with ES2015 modules. It's only possible to import stuff dynamically with asynchronous imports via 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())
}
like image 59
Johannes Ewald Avatar answered Nov 01 '22 03:11

Johannes Ewald