Sid Jain
Sid Jain

Reputation: 177

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?

Upvotes: 8

Views: 5341

Answers (1)

Johannes Ewald
Johannes Ewald

Reputation: 17805

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())
}

Upvotes: 5

Related Questions