Search code examples
node.jswebpackwebpack-5

Webpack config externals how to mix array and object syntax


Currently I have a webpack config with the externals set like so:

externals: [nodeExternals(), "inferno-helmet"]

I'm trying to add the sharp image processing library to my project. According to the docs, in order to work with a project that uses webpack, externals must be set like so:

externals: {
  'sharp': 'commonjs sharp'
}

I figure for sharp and inferno-helmet, the externals would look like this:

externals: {
    'sharp': 'commonjs sharp',
    'inferno-helmet': 'inferno-helmet',
    // ?
}

I don't understand how to properly use node externals with this syntax. The README for the webpack-node-externals only shows examples of passing an array to externals. How can I include all three of the node externals, inferno-helmet, and sharp in the externals?


Solution

  • It turns out this is a valid answer:

    externals: [nodeExternals(), "inferno-helmet", {sharp: 'commonjs sharp'}]