Saturday, 23 March 2019

Possibility about conditional export ES6 module based on process.env.NODE_ENV?

I wrote a utility library and I want to tree-shaking them when my user publishes their app.

In Webpack v4, you need to make your module ES6 to support tree-shaking, but I also want to split my development build and my production build into different files.

What I want is exactly like react's NPM module:

// index.js
'use strict';

if (process.env.NODE_ENV === 'production') {
  module.exports = require('./cjs/react.production.min.js');
} else {
  module.exports = require('./cjs/react.development.js');
}

This leads me questions.

If I make my utility modules all commonjs, I will never get tree-shaking, my app gets so huge.

If I make my utility modules all ES6 static export, I will have to include development message in production code.

And publishing two modules (eg: my-utility and my-utility-es) will not helping, because in development, my code looks like this:

import { someFunc } from 'my-utility';

but in production code, I will have to change it to this:

import { someFunc } from 'my-utility-es';

How can I solve this problem?



from Possibility about conditional export ES6 module based on process.env.NODE_ENV?

No comments:

Post a Comment