Just sharing a problem and solution I spent hours on debugging:
I have a codebase that I want to build as a library with webpack, and include in another project. However, when I import the output file in the other library, it returns undefined
.
This is the (simplified) webpack config:
{
entry: './index.js',
mode: 'development',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'app/my-app.[name].js'
library: 'my-app',
libraryTarget: 'umd'
},
optimization: {
runtimeChunk: 'single',
splitChunks: {
chunks: 'all',
cacheGroups: {
vendors: {
name: 'vendors',
test: /[\\/](node_modules|libraries)[\\/]/
}
}
},
minimizer: [new TerserPlugin({
cache: true,
sourceMap: true,
parallel: true,
exclude: [/my-app.vendors.js/]
})]
},
}
From the other project, I would import the library as follows:
const lib = require('./lib/my-app/dist/my-app.main');
console.log(lib);
Without any errors on the page, the console would simply show undefined
.