The value of publicPath allow you to specify a base path for all the assets within your applicaion. It's useful if you want to serve your assets from a different location. All urls and paths of your app are prefixed with the value of output.publicPath
.
module.exports = {
output: {
publicPath: "/assets/"
}
}
- Src attribute
src="main.js"
becomesrc="/assets/main.js"
accessed byhttp://server/assets/main.js
. - Href attribute
href="webfont.woff"
becomehref="/assets/webfont.woff"">
accessed byhttp://server/assets/webfont.woff
- Css url
url("./image.jpg")
becomeurl("/assets/image.jpg")
accessed byhttp://server/assets/image.jpg
. - Dynamic import
import("./module")
becomeimport("/assets/module")
accessed byhttp://server/assets/module.js
.
When using webpack-dev-server you should set publicPath: "/"
(devServer serve all assets from the root of the localhost).
If you use publicPath to transform everything to absolute paths you'll not be able to run your project via file://
protocol, you must use a local http://
development server:
# cd into output folder then run the following command
python3 -m http.server
Open http://localhost:8000/ to see your website.