Webpack path与publicPath的区别详解
作者:HeliumLau 时间:2024-04-29 13:37:49
前言
在webpack模块化开发的过程中,发现webpack.config.js配置文件的输出路径总有一个path与publicPath,不解其意。
module.exports = {
output: {
path: path.resolve("./examples/dist"),
filename: "app.js",
publicPath: "What should I put here?"
}
}
正文
官方解释
publicPath: The output.path from the view of the Javascript / HTML page.
从JS/HTML页面来看的输出路径
我的理解
output.path 储存你所有输出文件的本地文件目录。(绝对路径)
举个例子:
path.join(__dirname, “build/”)
webpack将会把所有的文件输出到localdisk/path-to-your-project/build/
output.publicPath
你上传所有打包文件的位置(相对于服务器根目录)
path:用来存放打包后文件的输出目录
publicPath:指定资源文件引用的目录
用处:例如在express中,指定了public/dist是网站的根目录,网站的源文件存放在public中,那么就需要设置path:”./dist”指定打包输出到该目录,而publicPath就需要设置为”/”,表示当前路径。
publicPath取决于你的网站根目录的位置,因为打包的文件都在网站根目录了,这些文件的引用都是基于该目录的。假设网站根目录为public,引用的图片路径是'./img.png',如果publicPath为'/',图片显示不了,因为图片都打包放在了dist中,那么你就要把publicPath设置为”/dist”。
举个例子:
/assets/
假设你将这个工程部署在服务器 http://server/
通过将output.publicPath设置为/assets/,这个工程将会在http://server/assets/找到webpack资源。
在这种前提下,所有与webpack相关的路径都会被重写成以/assets/开头。
src="picture.jpg" Re-writes ➡ src="/assets/picture.jpg"
Accessed by: (http://server/assets/picture.jpg)
src="/img/picture.jpg" Re-writes ➡ src="/assets/img/picture.jpg"
Accessed by: (http://server/assets/img/picture.jpg)
重要
如果你在用style-loader或者css sourceMap,你就需要设置publicPath。把它设置成服务器地址的绝对路径,比如http://server/assets/,这样资源可以被正确加载。
来源:https://blog.csdn.net/HeliumLau/article/details/70666433
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
这些有问题的细节设计
![](https://img.aspxhome.com/file/UploadPic/20094/20/wangwang-84s.png)
vue3+vite使用环境变量.env的一些配置情况详细说明
![](https://img.aspxhome.com/file/2023/7/133167_0s.png)
mysql 复制原理与实践应用详解
Python实现快速排序的方法详解
从console.log说起(console.log详细介绍)
![](https://img.aspxhome.com/file/2023/0/117500_0s.jpg)
python rsync服务器之间文件夹同步脚本
ASP程序与SQL存储过程结合使用详解
MySQL中用通用查询日志找出查询次数最多的语句的教程
Python 的赋值,浅拷贝和深拷贝详解
![](https://img.aspxhome.com/file/2023/2/131702_0s.png)
使用Python通过win32 COM打开Excel并添加Sheet的方法
![](https://img.aspxhome.com/file/2023/5/114625_0s.jpg)
pycharm下配置pyqt5的教程(anaconda虚拟环境下+tensorflow)
![](https://img.aspxhome.com/file/2023/2/128342_0s.png)
Python3.4编程实现简单抓取爬虫功能示例
解析mysql修改为utf8后仍然有乱码的问题
apache和nginx下vue页面刷新404的解决方案
![](https://img.aspxhome.com/file/2023/8/133178_0s.png)
python启动应用程序和终止应用程序的方法
死锁问题详解
![](https://img.aspxhome.com/file/2023/9/131859_0s.png)
详解django自定义中间件处理
python实现统计代码行数的方法
Python利用requests模块下载图片实例代码
基于Python的微信机器人开发 微信登录和获取好友列表实现解析
![](https://img.aspxhome.com/file/2023/6/64716_0s.png)