4

The Webpack documentation for devServer.contentBase says

It is also possible to serve from multiple directories:

webpack.config.js

module.exports = {
  //...
  devServer: {
    contentBase: [path.join(__dirname, 'public'), path.join(__dirname, 'assets')]
  }
};

Usage via the CLI

webpack-dev-server --content-base /path/to/content/dir

I don't know how to pass multiple paths via CLI.

In my package.json

"scripts": {
    "build": "webpack",
    "start:dev": "webpack-dev-server --content-base dist"

  },

I tried passing following values for start:dev command but none of them worked.

webpack-dev-server --content-base dist src/css
webpack-dev-server --content-base dist, src/css
webpack-dev-server --content-base 'dist src/css'
webpack-dev-server --content-base 'dist', 'src/css'
webpack-dev-server --content-base [ 'dist', 'src/css' ]

I found one related post but couldn't figure out the solution.

Also is there any standard being followed which specifies how an argument accepting an array of values should be specified on command-line? If yes then please provide a reference to that.

Jignesh Gohel
  • 6,236
  • 6
  • 53
  • 89
  • I just checked the [source code of webpack-dev-server](https://github.com/webpack/webpack-dev-server/blob/0b89fd94b78f6e049614ab466a7eab7d6a2a33f2/bin/webpack-dev-server.js#L188) it has something to check for arrays. I passed an array in Powershell but it didn't work. Try it with you shell may it works! – Ali Doustkani Feb 02 '19 at 11:41

2 Answers2

0

Figured it out looking at the following source code snippet

if (Array.isArray(options.contentBase)) {
   options.contentBase = options.contentBase.map((p) => path.resolve(p));
 }

We need to pass multiple --content-base arguments in following manner

"webpack-dev-server --content-base dist --content-base src/css"

Thanks to the source code hint provided by Ali Doustkani in his comment to my original post.

Jignesh Gohel
  • 6,236
  • 6
  • 53
  • 89
0

Just pass the paths in an array.

devServer: {
  static: [
    path.join(__dirname, 'public'),
    path.join(__dirname, 'assets'),
  ],
}

With that you don't need --content-base anymore.