Skip to content Skip to sidebar Skip to footer

Webpack 4 Universal Library Target

According to the Webpack 4 documentation, if I specify libraryTarget: 'umd' it should result in the following output: (function webpackUniversalModuleDefinition(root, factory) {

Solution 1:

This would be a bug in Webpack 4. Webpack 3 produces a proper bundle.

This issue should be fixed with this feature, until it's done the suggested workaround is using globalObject:

output: {
    path: resolve(__dirname, 'umd'),
    filename: 'lib.js',
    libraryTarget: 'umd',
    library: 'lib',
    umdNamedDefine: true,
    globalObject: `(typeof self !== 'undefined' ? self : this)`
},

Solution 2:

According to the docs include output.globalObject into webpack.config.js:

module.exports = {
  output: {
    libraryTarget: 'umd',
    globalObject: 'this'
  }
}

To make UMD build available on both browsers and Node.js, set output.globalObject option to 'this'.

Post a Comment for "Webpack 4 Universal Library Target"