-
Notifications
You must be signed in to change notification settings - Fork 232
/
webpack.config.js
126 lines (121 loc) · 3.76 KB
/
webpack.config.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin'); //css单独打包
var HtmlWebpackPlugin = require('html-webpack-plugin'); //生成html
var publicPath = '/dist/'; //服务器路径
var path = __dirname + '/dist/';
var plugins = [];
if (process.argv.indexOf('-p') > -1) { //生产环境
plugins.push(new webpack.DefinePlugin({ //编译成生产版本
'process.env': {
NODE_ENV: JSON.stringify('production')
}
}));
publicPath = '/react-cnode/dist/';
path = __dirname + '/react-cnode/dist/';
}
plugins.push(new ExtractTextPlugin({
filename:'[name].css'
})); //css单独打包
plugins.push(new HtmlWebpackPlugin({ //根据模板插入css/js等生成最终HTML
filename: '../index.html', //生成的html存放路径,相对于 path
template: './src/template/index.html', //html模板路径
hash: true, //为静态资源生成hash值
}));
module.exports = {
entry: {
app: './src/App', //编译的入口文件
},
output: {
publicPath, //编译好的文件,在服务器的路径
path, //编译到当前目录
filename: '[name].js' //编译后的文件名字
},
module: {
rules: [
{
test: /\.js$/,
exclude: /^node_modules$/,
use: [
{
loader: "babel-loader",
options: {
presets: ['es2015'],
compact: 'false',
plugins: ['syntax-dynamic-import']
}
}
]
}, {
test: /\.css$/,
exclude: /^node_modules$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [
{ loader: 'css-loader', options: { importLoaders: 1 } },
'postcss-loader'
]
})
// loader: ExtractTextPlugin.extract('style-loader', 'css-loader!autoprefixer-loader')
}, {
test: /\.less/,
exclude: /^node_modules$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [
{ loader: 'css-loader', options: { importLoaders: 1 } },
'postcss-loader',
{
loader: 'less-loader'
}
]
})
// loader: ExtractTextPlugin.extract('style-loader', 'css-loader!autoprefixer-loader!less-loader')
}, {
test: /\.(eot|woff|svg|ttf|woff2|gif|appcache)(\?|$)/,
exclude: /^node_modules$/,
use: [
{
loader: "file-loader",
options: {
name: '[name].[ext]'
}
}
]
// loader: 'file-loader?name=[name].[ext]'
}, {
test: /\.(png|jpg)$/,
exclude: /^node_modules$/,
use: [
{
loader: "url-loader",
options: {
limit: 20000,
name: '[name].[ext]'
}
}
]
// loader: 'url?limit=20000&name=[name].[ext]' //注意后面那个limit的参数,当你图片大小小于这个限制的时候,会自动启用base64编码图片
}, {
test: /\.jsx$/,
exclude: /^node_modules$/,
use: [
{
loader: "babel-loader",
options: {
presets: [
"es2015",
"react"
],
plugins: ['syntax-dynamic-import']
}
}
]
// loaders: ['jsx', 'babel?presets[]=es2015,presets[]=react']
}
]
},
plugins,
resolve: {
extensions: ['.js', '.jsx'], //后缀名自动补全
}
};