Closed yesmeck closed 4 years ago
We ran into the same issue too.
Supposed solution:
Find those requires in conditional statement like if / else if / switch case / conditional operator
, and wrap those module in a lazy execution mode, code as below:
import getFooProduction from './foo.production-wrapper';
import getFoo from './foo-wrapper';
function foo() {
if (env.production) {
return getFooProduction();
}
return getFoo();
}
I tried using { ignore: [ './foo.production', './foo' ] } to prevent transpiling conditional requires
,
BUT sadly, the ignore files will not be copied to dist folder, so an Module Not Found
error would be thrown when try to load ./foo.production
/ ./foo
.
I think this solution maybe better than that I supposed above, ignore specified modules & copy those required modules to dist folder
I think what we need is to run dead code elimination before running the commonjs plugin. Rollup already supports dead code elimination. Not sure if it is possible to extract the logic into a plugin.
Currently, I'm using the re plugin to strip unwanted modules from the bundle i.e. replace the entire module with an empty string.
The conditions are determined at runtime, rollup-plugin-re won't work in my case.
There is currently no way to translate a synchronous conditional require call to an ES module, which is required to make this work. Possible options:
@lukastaegert I see thanks for your reply.
We came across a situation that there is a third party library require module conditionally.
Now this plugin will hoist all requires to the top scope, which will lead both modules to be evaluated, but we want them to be evaluated conditionally. Can we evaluating conditional required module delayed?