一个完整的例子
webpack.config.js
const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = {
entry: {
main: './index.js',
vendor: ['react', 'react-dom']
},
output: {
filename: 'static/js/[name].js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/',
},
context: path.resolve(__dirname, 'src'),
module: {
rules: [{
test: /.js$/,
use: ['babel-loader'],
exclude: /node_modules/
},
{
test: /.css$/,
use: ExtractTextPlugin.extract(['css-loader', 'postcss-loader'])
},
{
test: /.(png|jpg|gif|svg)$/,
use: ['file-loader?name=static/images/[name].[ext]']
}
],
},
devtool: 'source-map',
plugins: [
new webpack.optimize.CommonsChunkPlugin({
names: ['vendor']
}),
new ExtractTextPlugin('static/css/[name].css'),
new HtmlWebpackPlugin({
template: path.resolve(__dirname, 'public/index.html'),
filename: 'index.html'
}),
new CopyWebpackPlugin([{
from: path.resolve(__dirname, 'public/favicon.ico')
}])
],
performance: {
hints: false
}
}
if (process.env.NODE_ENV === 'production') {
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
warnings: false
}
})
])
}
postcss.config.js
module.exports = {
plugins: [
require("postcss-cssnext")()
]
}
文件目录如下
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…