首先是webpack的配置
const path = require('path');
const HtmlwebpackPlugin = require('html-webpack-plugin');
const merge = require('webpack-merge');
const webpack = require('webpack');
const CleanPlugin = require('clean-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const pkg = require('./package.json');
const TARGET = process.env.npm_lifecycle_event;
const PATHS = {
app: path.join(__dirname, 'app'),
build: path.join(__dirname, 'build'),
style: path.join(__dirname, 'app/main.css'),
test: path.join(__dirname, 'tests')
};
const ENV = {
host: process.env.HOST || 'localhost',
port: process.env.PORT || 8080
};
process.env.BABEL_ENV = TARGET;
const common = {
entry: {
app: PATHS.app
},
resolve: {
extensions: ['', '.js', '.jsx']
},
output: {
path: PATHS.build,
filename: '[name].js'
},
module: {
loaders: [
{
test: /\.jsx?$/,
loaders: ['babel?cacheDirectory'],
include: PATHS.app
}
]
},
plugins: [
new HtmlwebpackPlugin({
template: 'node_modules/html-webpack-template/index.ejs',
title: 'Kanban app',
appMountId: 'app',
inject: false
})
]
};
if(TARGET === 'start' || !TARGET) {
module.exports = merge(common, {
entry: {
style: PATHS.style
},
devtool: 'eval-source-map',
devServer: {
historyApiFallback: true,
hot: true,
inline: true,
progress: true,
// display only errors to reduce the amount of output
stats: 'errors-only',
// parse host and port from env so this is easy
// to customize
host: ENV.host,
port: ENV.port
},
module: {
loaders: [
// Define development specific CSS setup
{
test: /\.css$/,
loaders: ['style', 'css'],
include: PATHS.app
}
]
},
plugins: [
new webpack.HotModuleReplacementPlugin()
]
});
}
if(TARGET === 'build' || TARGET === 'stats') {
module.exports = merge(common, {
entry: {
vendor: Object.keys(pkg.dependencies).filter(function(v) {
// Exclude alt-utils as it won't work with this setup
// due to the way the package has been designed
// (no package.json main).
return v !== 'alt-utils';
}),
style: PATHS.style
},
output: {
path: PATHS.build,
filename: '[name].[chunkhash].js',
chunkFilename: '[chunkhash].js'
},
module: {
loaders: [
// Extract CSS during build
{
test: /\.css$/,
loader: ExtractTextPlugin.extract('style', 'css'),
include: PATHS.app
}
]
},
plugins: [
new CleanPlugin([PATHS.build]),
// Output extracted CSS to a file
new ExtractTextPlugin('styles.[chunkhash].css'),
// Extract vendor and manifest files
new webpack.optimize.CommonsChunkPlugin({
names: ['vendor', 'manifest']
}),
// Setting DefinePlugin affects React library size!
new webpack.DefinePlugin({
'process.env.NODE_ENV': '"production"'
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
})
]
});
}
if(TARGET === 'test' || TARGET === 'tdd') {
module.exports = merge(common, {
devtool: 'inline-source-map',
resolve: {
alias: {
'app': PATHS.app
}
},
module: {
preLoaders: [
{
test: /\.jsx?$/,
loaders: ['isparta-instrumenter'],
include: PATHS.app
}
],
loaders: [
{
test: /\.jsx?$/,
loaders: ['babel?cacheDirectory'],
include: PATHS.test
}
]
}
});
}
然后是config.json的配置
{
"name": "kanban_app",
"version": "0.0.0",
"description": "Webpack demo app",
"main": "index.js",
"scripts": {
"deploy": "gh-pages -d build",
"stats": "webpack --profile --json > stats.json",
"build": "webpack",
"start": "webpack-dev-server",
"test": "karma start",
"tdd": "karma start --auto-watch --no-single-run"
},
"keywords": [
"webpack",
"demo"
],
"author": "",
"license": "MIT",
"devDependencies": {
"babel-core": "^6.7.7",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babel-preset-react-hmre": "^1.1.1",
"babel-preset-survivejs-kanban": "^0.3.3",
"clean-webpack-plugin": "^0.1.8",
"css-loader": "^0.23.1",
"extract-text-webpack-plugin": "^1.0.1",
"gh-pages": "^0.11.0",
"html-webpack-plugin": "^2.16.0",
"html-webpack-template": "^4.0.0",
"isparta-instrumenter-loader": "^1.0.0",
"karma": "^0.13.22",
"karma-coverage": "^0.5.5",
"karma-mocha": "^0.2.2",
"karma-phantomjs-launcher": "^1.0.0",
"karma-sourcemap-loader": "^0.3.7",
"karma-spec-reporter": "0.0.26",
"karma-webpack": "^1.7.0",
"mocha": "^2.4.5",
"phantomjs-polyfill": "0.0.2",
"phantomjs-prebuilt": "^2.1.7",
"redux-devtools": "^3.2.0",
"redux-devtools-dock-monitor": "^1.1.1",
"redux-devtools-log-monitor": "^1.0.11",
"style-loader": "^0.13.1",
"webpack": "^1.13.0",
"webpack-dev-server": "^1.14.1",
"webpack-merge": "^0.12.0"
},
"dependencies": {
"immutable": "^3.8.1",
"node-uuid": "^1.4.7",
"react": "^15.0.1",
"react-addons-update": "^15.0.1",
"react-dnd": "^2.1.4",
"react-dnd-html5-backend": "^2.1.2",
"react-dom": "^15.0.1",
"react-redux": "^4.4.5",
"redux": "^3.5.1"
}
}