-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathwebpack.config.js
More file actions
123 lines (105 loc) · 2.85 KB
/
webpack.config.js
File metadata and controls
123 lines (105 loc) · 2.85 KB
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
const path = require('path')
const webpack = require('webpack')
const autoprefixer = require('autoprefixer')
module.exports = {
entry: {
// 需要编译的入口文件
app: './src/index.js',
},
output: {
path: path.join(__dirname, '/build'),
// 输出文件名称规则,这里会生成 'app.js'
filename: '[name].js',
},
// 引用但不打包的文件
externals: { react: 'React', 'react-dom': 'ReactDOM' },
plugins: [
// webpack2 需要设置 LoaderOptionsPlugin 开启代码压缩
new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false,
}),
// Uglify的配置
new webpack.optimize.UglifyJsPlugin({
beautify: false,
comments: false,
compress: {
warnings: false,
drop_console: true,
collapse_vars: true,
},
}),
],
resolve: {
// 给src目录一个路径,避免出现'../../'这样的引入
alias: { _: path.resolve(__dirname, 'src') },
},
module: {
rules: [
{
test: /\.jsx?$/,
use: {
loader: 'babel-loader',
// 可以在这里配置babelrc,也可以在项目根目录加.babelrc文件
options: {
// false是不使用.babelrc文件
babelrc: false,
// webpack2 需要设置modules 为false
presets: [
['es2015', { modules: false }],
'react',
],
// babel的插件
plugins: [
'react-require',
'transform-object-rest-spread',
],
},
},
},
// 这是sass的配置,less配置和sass一样,把sass-loader换成less-loader即可
// webpack2 使用use来配置loader,并且不支持字符串形式的参数了,必须使用options
// loader的加载顺序是从后向前的,这里是 sass -> postcss -> css -> style
{
test: /\.scss$/,
use: [
{ loader: 'style-loader' },
{
loader: 'css-loader',
// 开启了CSS Module功能,避免类名冲突问题
options: {
modules: true,
localIdentName: '[name]-[local]',
},
},
{
loader: 'postcss-loader',
options: {
plugins() {
return [
autoprefixer,
]
},
},
},
{
loader: 'sass-loader',
},
],
},
// 当图片文件大于10KB时,复制文件到指定目录,小于10KB转为base64编码
{
test: /\.(png|jpg|jpeg|gif)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 10000,
name: './images/[name].[ext]',
},
},
],
},
],
},
}