在构建项目的时候 想单独打包配置文件,生产上可以自行改变配置文件
项目结构是这样的
|_node_modules/
|_src/
|_components/
|_containers/
|_reducers/
|_actions
|_utils/
|_index.js
|_config.js
其中 index.js是项目入口,config.js是项目的配置文件
打包的时候 想将config.js单独抽离出来 打包成一个单独的文件
方便生产环境定制参数
我应该如何做
附上我的webpack.config.js
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: {
app: './src/index.js',
vendor: ['react', 'react-dom', 'react-router','react-redux','./src/service']
},
output: {
path: __dirname + '/build/',
publicPath: './',
filename: 'js/bundle.js',
chunkFilename: 'js/[chunkhash].js'
},
module: {
rules: [
{
test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)$/,
use: [{
loader: 'file-loader',
options: {
name: 'assets/[hash].[ext]'
}
}]
},
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: ['babel-loader']
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.less$/,
use: ['style-loader', 'css-loader', 'less-loader']
}
]
},
// devServer: {
// host: '0.0.0.0',
// hot: true,
// clientLogLevel: 'info',
// historyApiFallback: true,
// contentBase: './build/'
// },
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
filename: 'js/vendor.js'
}),
new HtmlWebpackPlugin({
filename: 'index.html',
template: './index.html',
hash: true
}),
// new webpack.HotModuleReplacementPlugin(),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
})
]
};
我自己的解决方案分享下:
在html中单独引入一个global.js
然后在项目中再写一个配置文件 这个配置文件webpack打包引用 这样写
项目中需要用配置的地方直接引入 config.js
使用的时候在页面开头引入global.js,再引入打包文件,这样可以比较完美的做到解耦配置文件