【发布时间】:2017-04-26 15:51:44
【问题描述】:
我的问题是这个
https://github.com/petehunt/webpack-howto/issues/46
或者 - 我如何让 webpack 根据我的环境将脚本标签包含到 HTML 中?如果我在生产中运行,我只希望包含某个脚本标签。
这是我当前的 webpack 文件的样子(我使用的是 webpack 2)。
const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const VENDOR_LIBS = [
'axios', 'react', 'react-dom', 'react-router', 'react-apollo', 'prop-types'
];
module.exports = {
entry: {
bundle: './client/src/index.js',
vendor: VENDOR_LIBS
},
output: {
path: path.join(__dirname, 'dist'),
publicPath: '/',
filename: '[name].[chunkhash].js'
},
module: {
rules: [
{
use: 'babel-loader',
test: /\.js$/,
exclude: /node_modules/
},
{
test: /\.scss$/,
use: [{
loader: "style-loader"
}, {
loader: "css-loader"
}, {
loader: "sass-loader"
}]
},
{
test: /\.(jpe?g|png|gif|svg|)$/,
use: [
{
loader: 'url-loader',
options: {limit: 40000}
},
'image-webpack-loader'
]
}
]
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
names: ['vendor', 'manifest']
}),
new HtmlWebpackPlugin({
template: './client/src/index.html'
})
]
};
【问题讨论】:
-
了解这个脚本标签的用途以及为什么只在生产中需要它可能会有所帮助。