output
選項影響編譯輸出。output
選項控制 webpack 如何向硬盤寫入編譯文件箭跳。注意委乌,即使可以存在多個入口
點鞠柄,但只指定一個輸出
配置。
如果你用了哈希([hash]
或 [chunkhash]
),請確保模塊具有一致的順序。可以使用 OccurrenceOrderPlugin
或 recordsPath
等脂。
用法
webpack配置中的output
屬性的最低要求是將其設(shè)置為一個對象,包括以下兩點:
你編譯出來文件名
隨你命名撑蚌,但是我們通常使用 : main.js 或者 bundle.js 或者 index.js
上遥。
一個 output.path
必須是一個 絕對路徑 ,該路徑指向你想打包到的目錄争涌。
webpack.config.js
const config = {
output: {
filename: 'bundle.js',
path: '/home/proj/public/assets'
}
};
module.exports = config;
選項
以下列出可以向 output 屬性傳入的值粉楚。
output.chunkFilename
非入口塊的文件名,作為 output.path 目錄中的相對路徑亮垫。
[id]
被 chunk 的 id 替換模软。
[name]
被 chunk 的 name 替換(或者當(dāng) chunk 沒有 name 時使用 id 替換)。
[hash]
被編譯的 hash 替換饮潦。
[chunkhash]
被 chunk 的 hash 替換燃异。
output.crossOriginLoading
選項啟用跨域加載(cross-origin loading) chunk。
可能的值有:
false
- 禁用跨域加載
anonymous
- 啟用跨域加載继蜡。當(dāng)使用 anonymous
時回俐,發(fā)送請求不帶憑據(jù)(credential)。
use-credentials
- 啟用跨域加載稀并。發(fā)送請求帶憑據(jù)(credential)仅颇。
更多跨域加載信息請查看MDN
默認(rèn)值:false
output.devtoolLineToLine
所有/指定
模塊啟用行到行映射模式。行到行映射模式使用一個簡單的 SourceMap碘举,其中生成源碼的每一行都映射到原始源碼的同一行忘瓦。這是一個可以性能優(yōu)化的地方。當(dāng)你需要更好的性能殴俱,并且要確保輸入行和生成行匹配時,才會考慮啟用枚抵。
true 在所有模塊啟用(不推薦)
{test, include, exclude}對象 在特定文件啟用(類似于 module.loaders)线欲。
默認(rèn)值:disabled
output.filename
指定硬盤每個輸出文件的名稱。在這里你不能指定絕對路徑汽摹!·output.path· 選項規(guī)定了文件被寫入硬盤的位置李丰。·filename· 僅用于命名每個文件逼泣。
單入口
{
entry: './src/app.js',
output: {
filename: 'bundle.js',
path: __dirname + '/build'
}
}
// writes to disk: ./build/bundle.js
多入口
如果你的配置創(chuàng)建了多個 "chunk"(例如使用多個入口點或使用類似 CommonsChunkPlugin 的插件)趴泌,你應(yīng)該使用以下的替換方式來確保每個文件名都不重復(fù)舟舒。
[name]
被 chunk 的 name 替換。
[hash]
被編譯(compilation)的 hash 替換嗜憔。
[chunkhash]
被 chunk 的 hash 替換秃励。
{
entry: {
app: './src/app.js',
search: './src/search.js'
},
output: {
filename: '[name].js',
path: __dirname + '/build'
}
}
// writes to disk: ./build/app.js, ./build/search.js
output.hotUpdateChunkFilename
熱更新塊 的文件名。它們在output.path
目錄中吉捶。
[id] 被 chunk 的 id 替換夺鲜。
[hash] 被編譯的 hash 替換。(最后一個 hash 存儲在記錄中)
默認(rèn)值:
"[id].[hash].hot-update.js"
output.hotUpdateFunction
webpack 用于異步加載熱更新塊的 JSONP 函數(shù)呐舔。
默認(rèn)值:
"webpackHotUpdate"
output.hotUpdateMainFilename
熱更新主文件的文件名币励。
[hash]
被編譯的 hash 替換。(最后一個 hash 存儲在記錄中)
默認(rèn)值:
"[hash].hot-update.json"
output.jsonpFunction
webpack 用于異步加載塊(chunk)的 JSONP 函數(shù)珊拼。
較短的函數(shù)可能會減少文件大小食呻。當(dāng)單頁有多個 webpack 實例時,請使用不同的標(biāo)識符澎现。
默認(rèn)值:
"webpackJsonp"
output.library
如果設(shè)置過仅胞,可將包導(dǎo)出為庫。output.library
是庫的名稱昔头。
如果你正在編寫庫并且想要將其發(fā)布為一個單文件饼问,請使用此選項。
output.libraryTarget
導(dǎo)出庫的格式
"var"
- 通過設(shè)置一個變量導(dǎo)出:var Library = xxx
(默認(rèn))
"this"
- 通過設(shè)置this
的一個屬性導(dǎo)出:this["Library"] = xxx
"commonjs"
- 通過設(shè)置exports
的一個屬性導(dǎo)出:exports["Library"] = xxx
"commonjs2"
- 通過設(shè)置module.exports
:module.exports = xxx
"amd"
- 導(dǎo)出到 AMD(可選命名 - 通過 library 選項設(shè)置名稱)
"umd"
- 導(dǎo)出到 AMD揭斧,CommonJS2 或者 root 的一個屬性
默認(rèn)值:
"var"
如果 output.library
未設(shè)置莱革,但是 output.libraryTarget
被設(shè)置為var
以外的值,則導(dǎo)出對象的每個屬性都被復(fù)制(除了 amd
讹开,commonjs2
和 umd
)盅视。
output.path
以絕對路徑作為導(dǎo)出目錄(必選項)。
[hash] 被編譯的 hash 替換旦万。
config.js
output: {
path: "/home/proj/public/assets",
publicPath: "/assets/"
}
index.html
<head>
<link href="/assets/spinner.gif"/>
</head>
接下來是一個更復(fù)雜的例子闹击,來說明對資源使用 CDN 和 hash。
config.js
output: {
path: "/home/proj/cdn/assets/[hash]",
publicPath: "http://cdn.example.com/assets/[hash]/"
}
注意: 在編譯時不知道最終輸出文件的publicPath
的情況下成艘,publicPath
可以留空赏半,并且在入口點文件運行時動態(tài)設(shè)置。如果你在編譯時不知道publicPath
淆两,你可以先忽略它断箫,并且在入口點設(shè)置 __webpack_public_path__
。
__webpack_public_path__ = myRuntimePublicPath
// 其他的應(yīng)用程序入口
output.sourceMapFilename
JavaScript 文件 SourceMap 的文件名秋冰。它們在output.path
目錄中仲义。
[file] 被 JavaScript 文件的文件名替換。
[id] 被 chunk 的 id 替換。
[hash] 被編譯(compilation)的 hash 替換埃撵。
默認(rèn)值:
"[file].map"
下一篇:加載器