I started using webpack and now confused between the publicPath and contentBase. The documentation isn't clear on the use case.
publicPath specifies the virtual directory in web server from where bundled file, app. js is going to get served up from. Keep in mind, the word server when using publicPath can be either webpack-dev-server or express server or other server that you can use with webpack.
webpack-dev-server serves bundled files from the directory defined in output. path , i.e., files will be available under http://[devServer.host]:[devServer.port]/[output.publicPath]/[output.filename]
Exists in both webpack and webpack-dev-server. Keyword is Output!
Say you have a domain example.com
and you have your web-app at example.com/app/
. Now normally, urls would be /bundle.abc123.js
. But by changing the publicPath it can become /app/bundle.abc123.js
.
Same thing as webpack. You can now run a devserver that serves at http://localhost:8080/app/
simple! It's only about where to output the files.
Exists only in webpack-dev-server. It's only needed if you want to serve static files. For example, you want a folder of mp4 vacation movies to be available for the app, but you don't want to run them through the bundle, because that would be silly. Those asset files almost never change, but they need to be available for the app.
contentBase: path.join(__dirname, 'movies')
now you can use those from your app on your dev server
<video src="/movies/vacation.mp4">
So this controls what static files to add to your devserver. keyword Input!
And then last we have:
contentBasePublicPath: '/assets'
which is same as publicPath, but only for files you added using contentBase.
<video src="/assets/movies/vacation.mp4">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With