# 輸出(Output)
配置 `output` 選項可以控制 webpack 如何向硬盤寫入編譯文件。注意,即使可以存在多個`入口`起點,但只指定一個`輸出`配置。
## 用法(Usage)
在 webpack 中配置 `output` 屬性的最低要求是,將它的值設置為一個對象,包括以下兩點:
- `filename` 用于輸出文件的文件名。
- 目標輸出目錄 `path` 的絕對路徑。
**webpack.config.js**
```js
const config = {
output: {
filename: 'bundle.js',
path: '/home/proj/public/assets'
}
};
module.exports = config;
```
此配置將一個單獨的 `bundle.js` 文件輸出到 `/home/proj/public/assets` 目錄中。
## 多個入口起點
如果配置創建了多個單獨的 "chunk"(例如,使用多個入口起點或使用像 CommonsChunkPlugin 這樣的插件),則應該使用[占位符(substitutions)](https://doc.webpack-china.org/configuration/output#output-filename)來確保每個文件具有唯一的名稱。
```
{
entry: {
app: './src/app.js',
search: './src/search.js'
},
output: {
filename: '[name].js',
path: __dirname + '/dist'
}
}
// 寫入到硬盤:./dist/app.js, ./dist/search.js
```
## 高級進階
以下是使用 CDN 和資源 hash 的復雜示例:
**config.js**
```
output: {
path: "/home/proj/cdn/assets/[hash]",
publicPath: "http://cdn.example.com/assets/[hash]/"
}
```
在編譯時不知道最終輸出文件的 `publicPath` 的情況下,`publicPath` 可以留空,并且在入口起點文件運行時動態設置。如果你在編譯時不知道 `publicPath`,你可以先忽略它,并且在入口起點設置 `__webpack_public_path__`。
```
__webpack_public_path__ = myRuntimePublicPath
// 剩余的應用程序入口
```