webpack 常用配置
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
// 提取编译后js中的css
const ExtractTextPlugin = require("extract-text-webpack-plugin");
// 压缩js
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const webpack = require('webpack');
module.exports = {
entry: {
index: './src/index.js'
},
output: {
filename: "[name].js",
path: path.resolve(__dirname, 'dist')
},
devtool: 'source-map',
devServer: {
contentBase: './dist'
},
plugins: [
new CleanWebpackPlugin(['dist']),
// new UglifyJSPlugin(),
new HtmlWebpackPlugin({
title: '首页'
}),
// 提取公共模块
new webpack.optimize.CommonsChunkPlugin({
name: 'common'
}),
new ExtractTextPlugin({
filename: '[name].css',
ignoreOrder: true,
})
],
module: {
rules: [
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
use: {
loader: 'css-loader',
options: {
modules: true,
sourceMap: true
},
},
fallback : 'style-loader',
})
},
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
enforce: 'pre',
use: {
loader: 'babel-loader'
}
},
{
test: /\.(png|svg|jpg|gif|bmp)$/,
use: 'file-loader',
exclude: '/(static)/'
}
]
}
};