Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between publicPath and contentBase in webpack

I started using webpack and now confused between the publicPath and contentBase. The documentation isn't clear on the use case.

like image 272
Nikhil Avatar asked Jul 20 '20 08:07

Nikhil


People also ask

What is publicPath Webpack?

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.

Where is Webpack-dev-server?

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]


1 Answers

publicPath

Exists in both webpack and webpack-dev-server. Keyword is Output!

First webpack without dev-server:

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.

Dev-server

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.

contentBase

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!

contentBasePublicPath

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">
like image 144
ippi Avatar answered Oct 08 '22 09:10

ippi