1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const webpack = require('webpack');// 热加载需要的 webpack
let ExtractTextPlugin = require("extract-text-webpack-plugin");
// the path(s) that should be cleaned
let pathsToClean = [
'dist',
'static'
]
// the clean options to use
let cleanOptiOns= {
root: '/static',
// exclude: ['*.html'],
verbose: true,
dry: false
}
let cOnfig= {
/*
entry: {
index: './src/index.js',
vendor: [
'lodash'
]
},
*/
entry: {},
// Rule.loader is a shortcut to Rule.use: [ { loader } ]
// Rule.options and Rule.query are shortcuts to Rule.use: [ { options } ]
module: {
rules: [
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{ // 解析less文件,把less解析成浏览器可以识别的css语言
test: /.less$/,
use: ['style-loader', 'css-loader', 'less-loader']
},
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
loaders: [
'react-hot-loader', // react 热替换
'babel-loader' // .babelrc 具体配置文件
]
},
{
test:/\.jsx?$/,
exclude:/node_modules/,
/*
loader:'babel-loader',
query:{
presets:['env', 'react', 'stage-0']
}
*/
loader:'babel-loader'
}
]
},
plugins: [
// new CleanWebpackPlugin(['static']),
new CleanWebpackPlugin(pathsToClean, cleanOptions),
new webpack.HotModuleReplacementPlugin(), // 热加载的插件,使用缓存时请注释
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor'
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'runtime'
}),
new ExtractTextPlugin({
filename: 'styles/[name].css'
}),
new webpack.ProvidePlugin({ // 将 $ 变量挂载到 window 下面,可以在项目中直接使用 $ 不用再引用
$:"jquery",
jQuery:"jquery",
"window.jQuery":"jquery"
})
],
// 通过 output.filename 和 output.path 属性,来告诉 webpack bundle(捆)的名称,以及我们想要生成(emit)到哪里
output: {
filename: '[name].bundle.js',
// filename: '[name].[chunkhash].js',
path: path.resolve(__dirname, 'static')
},
externals: {
}
};
// 页面入口处理
let entry = {
index: './src/index.js',
vendor: [
'jquery',
'lodash'
]
};
config.entry = entry;
module.exports = config; |