前言
在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="https://www.gxlcms.com/assets/picture.jpg" Accessed by: (http://server/assets/picture.jpg) src="https://www.gxlcms.com/img/picture.jpg" Re-writes ➡ src="https://www.gxlcms.com/assets/img/picture.jpg" Accessed by: (http://server/assets/img/picture.jpg)
重要
如果你在用style-loader或者css sourceMap,你就需要設置publicPath。把它設置成服務器地址的絕對路徑,比如http://server/assets/,這樣資源可以被正確加載。
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com