speedskater / babel-plugin-rewire

A babel plugin adding the ability to rewire module dependencies. This enables to mock modules for testing purposes.
843 stars 90 forks source link

Webpack bundle size increases dramatically with rewire #228

Open toanqc opened 4 years ago

toanqc commented 4 years ago

I installed babel-plugin-rewire modules to my project. npm install babel-plugin-rewire And config the .babelrc plugin with "plugins": ["transform-object-rest-spread", "rewire"]

Now when I build the project, I see that the webpack bundle is increased dramatically (almost 2 MB) From app.js 6.49 MB 0 [emitted] [big] app To app.js 8.33 MB 0 [emitted] [big] app

Is this expected? Is there a way that I can use rewire so that I can test my private unit test and remove it from the webpack bundle?

Mesqalito commented 4 years ago

You should probably use .babelrc.js and provide conditional config:

module.exports = api => ({
    // ...
    plugins: [
        ...api.env('test') ? ['babel-plugin-rewire'] : []
    ]
});

This way rewire should only run when you run tests. Also check if env variable is set to test if your test runner doesn't do it for you already.