当前位置: 移动技术网 > IT编程>开发语言>JavaScript > Webpack path与publicPath的区别详解

Webpack path与publicPath的区别详解

2018年05月11日  | 移动技术网IT编程  | 我要评论

前言

在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/,这样资源可以被正确加载。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网