鍍金池/ 問(wèn)答/HTML/ output的publicpath和devServer的publicpath有什

output的publicpath和devServer的publicpath有什么關(guān)系

有一種情況,一直想不明白,就是output的publicpath和devServer的publicpath不一致的時(shí)候,為什么會(huì)引用不到相關(guān)的靜態(tài)資源呢?

截取部分webpack.config.js配置:

output:{
    path: path.resolve(__dirname,'../dist'),
    filename: '[name].js',
    publicPath: 'http://localhost:1207/static/'
},
devServer: {
    contentBase: path.resolve(__dirname,'../dist') ,
    host: 'localhost',
    port: 1207,
    publicPath: 'http://localhost:1207/web/'
},

上面這種情況,output的publicpath和devServer的publicpath不一致,index.html中引用不了相關(guān)靜態(tài)資源,我說(shuō)一下我的理解和疑惑:

我的理解:

devServer啟動(dòng)后,相當(dāng)于在內(nèi)存中開(kāi)啟了一個(gè)node服務(wù)器,靜態(tài)資源的訪(fǎng)問(wèn)入口是devServer配置的publicpath(http://localhost:1207/web/),但是由于output配置的publicpath是http://localhost:1207/static/,所以index.html引用的靜態(tài)資源路徑就是http://localhost:1207/static/xxx.js而不是http://localhost:1207/web/xxx.js,所以報(bào)錯(cuò)

我的疑惑:

  1. output和devServer的publicpath不一致,為什么會(huì)訪(fǎng)問(wèn)不到output的publicpath(http://localhost:1207/static/)下的靜態(tài)資源?output的publicpath是絕對(duì)路徑為什么訪(fǎng)問(wèn)不了?
  2. devServer構(gòu)建編譯打包,output和devServer之間的publicpath有什么關(guān)系?
回答
編輯回答
夢(mèng)囈

我剛總結(jié)的,希望對(duì)你有幫助
https://www.jianshu.com/p/cbe...

2017年12月18日 23:14
編輯回答
氕氘氚

理解這段話(huà)你應(yīng)該就知道原因了

webpack-dev-server輸出的文件只存在于內(nèi)存中,不輸出真實(shí)的文件

2017年9月11日 21:40