mirror of
https://github.com/wassname/react-webpack-template.git
synced 2026-06-27 18:25:20 +08:00
Added own configuration objects for different webpack environments.
This commit is contained in:
+42
-111
@@ -1,116 +1,47 @@
|
||||
var webpack = require('webpack');
|
||||
var args = require('minimist')(process.argv.slice(2));
|
||||
'use strict';
|
||||
|
||||
var path = require('path');
|
||||
var env = args.env || 'dev';
|
||||
var args = require('minimist')(process.argv.slice(2));
|
||||
|
||||
var port = 8000;
|
||||
var srcPath = __dirname + '/src/';
|
||||
var publicPath = '/assets/';
|
||||
// List of allowed environments
|
||||
var allowedEnvs = ['dev', 'dist', 'test'];
|
||||
|
||||
var config = {
|
||||
port: port,
|
||||
debug: true,
|
||||
output: {
|
||||
path: path.join(__dirname, 'dist/assets'),
|
||||
filename: 'app.js',
|
||||
publicPath: publicPath
|
||||
},
|
||||
devServer: {
|
||||
contentBase: './src/',
|
||||
historyApiFallback: true,
|
||||
hot: true,
|
||||
port: port,
|
||||
publicPath: publicPath,
|
||||
noInfo: false
|
||||
},
|
||||
resolve: {
|
||||
extensions: ['', '.js', '.jsx'],
|
||||
alias: {
|
||||
actions: srcPath + '/actions/',
|
||||
components: srcPath + '/components/',
|
||||
sources: srcPath + '/sources/',
|
||||
stores: srcPath + '/stores/',
|
||||
styles: srcPath + '/styles/'
|
||||
}
|
||||
},
|
||||
module: {
|
||||
preLoaders: [
|
||||
{
|
||||
test: /\.(js|jsx)$/,
|
||||
include: path.join(__dirname, 'src'),
|
||||
loader: 'eslint-loader'
|
||||
}
|
||||
],
|
||||
loaders: [
|
||||
{
|
||||
test: /\.css$/,
|
||||
loader: 'style!css'
|
||||
},
|
||||
{
|
||||
test: /\.sass/,
|
||||
loader: 'style-loader!css-loader!sass-loader?outputStyle=expanded&indentedSyntax'
|
||||
},
|
||||
{
|
||||
test: /\.scss/,
|
||||
loader: 'style-loader!css-loader!sass-loader?outputStyle=expanded'
|
||||
},
|
||||
{
|
||||
test: /\.less/,
|
||||
loader: 'style-loader!css-loader!less-loader'
|
||||
},
|
||||
{
|
||||
test: /\.styl/,
|
||||
loader: 'style-loader!css-loader!stylus-loader'
|
||||
},
|
||||
{
|
||||
test: /\.(png|jpg|gif|woff|woff2)$/,
|
||||
loader: 'url-loader?limit=8192'
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
// Use config depending on environment
|
||||
switch(env) {
|
||||
case 'dist':
|
||||
config.entry = './src/components/run';
|
||||
config.cache = false;
|
||||
config.devtool = 'sourcemap';
|
||||
config.plugins = [
|
||||
new webpack.optimize.DedupePlugin(),
|
||||
new webpack.optimize.UglifyJsPlugin(),
|
||||
new webpack.optimize.OccurenceOrderPlugin(),
|
||||
new webpack.optimize.AggressiveMergingPlugin(),
|
||||
new webpack.NoErrorsPlugin()
|
||||
];
|
||||
|
||||
// Add needed loaders
|
||||
config.module.loaders.push({
|
||||
test: /\.(js|jsx)$/,
|
||||
loader: 'babel',
|
||||
include: path.join(__dirname, 'src')
|
||||
});
|
||||
break;
|
||||
|
||||
default:
|
||||
config.entry = [
|
||||
'webpack-dev-server/client?http://127.0.0.1:' + port,
|
||||
'webpack/hot/only-dev-server',
|
||||
'./src/components/run'
|
||||
];
|
||||
config.cache = true;
|
||||
config.devtool = 'eval';
|
||||
config.plugins = [
|
||||
new webpack.HotModuleReplacementPlugin(),
|
||||
new webpack.NoErrorsPlugin()
|
||||
];
|
||||
|
||||
// Add needed loaders
|
||||
config.module.loaders.push({
|
||||
test: /\.(js|jsx)$/,
|
||||
loader: 'react-hot!babel-loader',
|
||||
include: path.join(__dirname, 'src')
|
||||
});
|
||||
// Set the correct environment
|
||||
var env;
|
||||
if(args._.length > 0 && args._.indexOf('start') !== -1) {
|
||||
env = 'test';
|
||||
} else if (args.env) {
|
||||
env = args.env;
|
||||
} else {
|
||||
env = 'dev';
|
||||
}
|
||||
|
||||
module.exports = config;
|
||||
// Get available configurations
|
||||
var configs = {
|
||||
base: require(path.join(__dirname, 'cfg/base')),
|
||||
dev: require(path.join(__dirname, 'cfg/dev')),
|
||||
dist: require(path.join(__dirname, 'cfg/dist')),
|
||||
test: require(path.join(__dirname, 'cfg/test'))
|
||||
};
|
||||
|
||||
/**
|
||||
* Get an allowed environment
|
||||
* @param {String} env
|
||||
* @return {String}
|
||||
*/
|
||||
function getValidEnv(env) {
|
||||
var isValid = env && env.length > 0 && allowedEnvs.indexOf(env) !== -1;
|
||||
return isValid ? env : 'dev';
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the webpack configuration
|
||||
* @param {String} env Environment to use
|
||||
* @return {Object} Webpack config
|
||||
*/
|
||||
function buildConfig(env) {
|
||||
var usedEnv = getValidEnv(env);
|
||||
return configs[usedEnv];
|
||||
}
|
||||
|
||||
module.exports = buildConfig(env);
|
||||
|
||||
Reference in New Issue
Block a user