Output配置output
配置选项告诉webpack如何将编译后的文件写入磁盘。请注意,虽然可以有多个entry
点,但只output
指定了一个配置。
- A
filename
to use for the output file(s).
webpack.config.js
module.exports = {
output: {
filename: 'bundle.js',
}
};
This configuration would output a single bundle.js
file into the dist
directory.
如果您的配置创建了多个“块”(与多个入口点一样或使用CommonsChunkPlugin等插件时),则应使用替换来确保每个文件都具有唯一名称。
module.exports = {
entry: {
app: './src/app.js',
search: './src/search.js'
},
output: {
filename: '[name].js',
path: __dirname + '/dist'
}
};
// writes to disk: ./dist/app.js, ./dist/search.js
高级
以下是使用CDN和资产哈希的更复杂的示例:
config.js
module.exports = {
//...
output: {
path: '/home/proj/cdn/assets/[hash]',
publicPath: 'http://cdn.example.com/assets/[hash]/'
}
};
如果publicPath
在编译时不知道输出文件的最终结果,则可以将其留空并在运行时通过__webpack_public_path__
入口点文件中的变量动态设置:
__webpack_public_path__ = myRuntimePublicPath;
// rest of your application entry