Using externals to avoid bundling lodash

If you now perform a build, you will see that quite a large bundle is created. Inspecting the file reveals that lodash has been bundled alongside it. For the sake of this tutorial, lodash is better treated as a peer dependency. This essentially means that the user would have lodash installed, effectively giving control of this external library to the user of this library.

This can be done through the configuration of externals, as in webpack.config.js, as follows:

const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'numbers-to-text.js'
}
},
externals: {
lodash: {
commonjs: 'lodash',
commonjs2: 'lodash',
amd: 'lodash',
root: '_'
}
}
};

The preceding code means that the library expects a dependency named lodash to be available in the user's environment.

Note that externals may be specified as an array if the plan is to use the library as a dependency in a parallel Webpack bundle.

It's also important to specify a limitation on the externals, and we will discuss this now.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.100.34