-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathwebpack.config.example.babel.js
142 lines (130 loc) · 3.66 KB
/
webpack.config.example.babel.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import path from 'path';
import webpack from 'webpack';
import HtmlwebpackPlugin from 'html-webpack-plugin';
const ip = 'localhost';
const port = 9090;
const hotDevServer = 'webpack/hot/dev-server';
// https://github.com/webpack/webpack-dev-server
const webpackDevServer = `webpack-dev-server/client?http://${ip}:${port}`;
const appPath = path.resolve(__dirname, 'examples');
let webpackConfig = {
// eslint 配置
eslint: {
emitError: true, // 验证失败,终止
configFile: '.eslintrc'
},
cache: true,
debug: true,
devtool: 'source-map', //生成 source map文件
stats: {
colors: true,
reasons: true
},
devServer: {
contentBase: './examples',
historyApiFallback: true,
hot: true,
stats: {
colors: true
},
watchOptions: {
aggregateTimeout: 300,
poll: 1000
},
quiet: false, // 设为true,不把任何信息输出到控制台
publicPath: '/'
},
resolve: {
root: [appPath], // 设置要加载模块根路径,该路径必须是绝对路径
//自动扩展文件后缀名
extensions: ['', '.js', '.jsx', '.css']
},
// 入口文件 让webpack用哪个文件作为项目的入口
entry: {
index: ['./examples/index.js', webpackDevServer, hotDevServer],
simple: ['./examples/simple.js', webpackDevServer, hotDevServer],
paging: ['./examples/paging.js', webpackDevServer, hotDevServer],
pullOption: ['./examples/pullOption.js', webpackDevServer, hotDevServer],
},
// 出口 让webpack把处理完成的文件放在哪里
output: {
path: path.resolve(__dirname, 'examples/dist'), //打包输出目录
filename: '[name].[hash].bundle.js', //文件名称
publicPath: './' //资源路径
},
module: {
// https://github.com/MoOx/eslint-loader
preLoaders: [{
test: /\.jsx?$/,
exclude: /node_modules.*/,
loader: 'eslint'
}],
loaders: [
{
test: /\.jsx?$/,
loader: 'babel', // 'babel-loader' is also a legal name to reference
exclude: /node_modules/,
cacheDirectory: true // 开启缓存
},
{
test: /\.css/,
loader: 'style-loader!css-loader!postcss-loader'
},
{
test: /\.scss/,
loader: 'style-loader!css-loader!postcss-loader!sass-loader?outputStyle=expanded'
},
// https://github.com/webpack/url-loader
{
test: /\.(png|jpg|gif|woff|woff2|svg)$/,
loader: 'url?limit=10000', // 10kb
query: {
mimetype: 'image/png'
}
}
]
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(), //
new webpack.HotModuleReplacementPlugin(), // 热部署替换模块
new webpack.NoErrorsPlugin() //
]
};
//创建 HtmlWebpackPlugin 的实例
// https://www.npmjs.com/package/html-webpack-plugin
const entry = webpackConfig.entry;
// 为 HtmlwebpackPlugin 设置配置项,与 entry 键对应,根据需要设置其参数值
const htmlwebpackPluginConfig = {
index: {
title: '例子列表'
},
simple: {
title: '简单例子'
},
paging: {
title: '分页加载与刷新'
},
pullOption: {
title: '禁止向上或向下滑动'
}
};
for (let key in entry) {
if (entry.hasOwnProperty(key) && key !== 'vendors') {
webpackConfig.plugins.push(
new HtmlwebpackPlugin({
title: htmlwebpackPluginConfig[key].title,
template: path.resolve(appPath, 'templates/layout.html'),
filename: `${key}.html`,
//chunks这个参数告诉插件要引用entry里面的哪几个入口
chunks: [key, 'vendors'],
//要把script插入到标签里
inject: 'body'
})
);
}
}
export default {
webpackConfig,
ip,
port
};