As with any Webpack project, we need to configure it. This requires some extra attention when dealing with a custom library. Several unusual things must be achieved with this configuration as opposed to a typical one. Let's consider these objectives now. The library should be bundled in a way that achieves the following goals:
- The use of externals to avoid bundling lodash so the user is required to load it
- Specifying the external limitations of the library
- Exposing the library as a variable called numbersToText
- Setting the library name to numbers-to-text
- Permitting the access to the Node.js library indie
Also, note that the user must be able to access and have use of the library in the following ways:
- Through importing numbersToText from numbers-to-text as an ECMAScript 2015 (ES 2015) module
- Through the CommonJS module, such as using the require('webpack-numbers')method
- Through a global variable when included through such methods as a script tag
With all that in mind, the first thing to do is set up the Webpack configuration through the normal files we use for this webpack.config.js file, as follows:
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'numbers-to-text.js'
}
};
That is the basic configuration, but we'll now move on to the next goal identified: the externalizing of lodash.