webpack description

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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const path = require('path');

module.exports = {
entry: "./app/entry",
// array : 两个入口文件,最后合并输出为一个文件。
entry: ["./app/entry1", "./app/entry2"],
// object: 两个入口文件,最后输出两个文件。两个文件输出hash时(filename: "[hash].js",),会提示错误,因为会重名。所以多个输出应该(filename: "[name]-[hash].js",)
entry: {
a: "./app/entry-a",
b: ["./app/entry-b1", "./app/entry-b2"]
},

// 一般来说入口文件都是一个。

output: {

// 输出路径
path: path.resolve(__dirname, "dist"),

/**
* 文件名三种输出方式
* 1. 指定名称
* 2. 和入口文件名相同
* 3. 哈希值输出
*/
filename: "bundle.js",
// 本次webpack打包的hash值。
filename: "[hash].js",
filename: "[name].js",
// 每一个chunk的hash,产生多个输出文件时的文件都带有hash,并且只有文件改变后打包hash才改变。不能与--hot热更新一起使用。可以用hash。
filename: "[chunkhash].js",

// 该路径会指定webpackDevServer的监听路径,引用时可能是:<link href="/assets/spinner.gif" />
// 指定一个路径,一般是上线环境的路径。例如:http://cheatlys.info/
publicPath: "/assets/",

// the filename template for additional chunks
chunkFilename: "[id].js",
chunkFilename: "[chunkhash].js",

// 编写输出文件的SourceMap选项时使用。
sourceMapFilename: "[file].map",
sourceMapFilename: "sourcemaps/[file].map"

},

module: {

rules: [
// 配置各类加载器。
{
test: /\.jsx?$/,
// 各类解析加载器作用的文件。
include: [
path.resolve(__dirname, "app")
],
// 不许要作用的文件。
exclude: [
path.resolve(__dirname, "app/demo-files")
],
// 各类加载器
loader: "babel-loader",

// 选项配置
options: {
presets: ["es2015"]
}

},

{
test: /\.html$/,

use: [
// 进行多个配置
"htmllint-loader",
{
loader: "html-loader",
options: {
/* ... */
}
}
]
}
],
// 无需解析的内容
noParse: [
/special-library\.js$/
]
},

resolve: {
// 用来解析模块请求,例如设置请求的别名,但不会解析加载器

modules: [
"node_modules",
path.resolve(__dirname, "app")
],
// directories where to look for modules

extensions: [".js", ".json", ".jsx", ".css"],
// 设置别名,可以用别名代替长路径
alias: {

// 别名即可代表"module" -> "new-module" 也可代表 "module/path/file" -> "new-module/path/file"
"module": "new-module",

// // $限定了别名,只能代表 "only-module" -> "new-module", 而不能代表 "module/path/file" -> "new-module/path/file"
"only-module$": "new-module",

},

alias: [
{
name: "module",

alias: "new-module",

onlyModule: true
// if true only "module" is aliased
// if false "module/inner/path" is also aliased
}
]
},

// 源文件遍历,方便调试。
devtool: "source-map", // enum

// 指定webpack根目录(绝对路径)
context: __dirname,

// 打包时的一些信息展示,样式。
stats: "errors-only",
stats: {
assets: true,
colors: true,
errors: true,
errorDetails: true,
hash: true,
// ...
},

// 开发环境的web服务器,基于express搭建
devServer: {
proxy: {
'/api': 'http://localhost:3000'
},
// 服务器启动,监听的路径设置
contentBase: path.join(__dirname, 'public'),
// 压缩代码
compress: true,
historyApiFallback: true,
// 模块热替换,依赖于HotModuleReplacementPlugin
hot: true,
https: false,
noInfo: true
},

// 各类插件的使用
plugins: [
// ...
],

resolveLoader: { /* same as resolve */ }
// separate resolve options for loaders

parallelism: 1, // number
// limit the number of parallel processed modules

profile: true, // boolean
// capture timing information

bail: true, //boolean
// fail out on the first error instead of tolerating it.

cache: false, // boolean
// disable/enable caching

watch: true, // boolean
// enables watching

watchOptions: {
aggregateTimeout: 1000, // in ms
// aggregates multiple changes to a single rebuild

poll: true,
poll: 500, // intervall in ms
// enables polling mode for watching
// must be used on filesystems that doesn't notify on change
// i. e. nfs shares
},

node: {
// Polyfills and mocks to run Node.js-
// environment code in non-Node environments.

console: false, // boolean | "mock"
global: true, // boolean | "mock"
process: true, // boolean
__filename: "mock", // boolean | "mock"
__dirname: "mock", // boolean | "mock"
Buffer: true, // boolean | "mock"
setImmediate: true // boolean | "mock" | "empty"
}

}

概念

loader 和 plugin

  • loader,它是一个转换器,将A文件进行编译成B文件,比如:将A.less转换为A.css,单纯的文件转换过程;多数 loader 使用了函数的编程的 compose(组合),所以 loader 加载顺序是从右往左;多数 loader 使用 AST 来实现文件转换

  • plugin 是一个扩展器,处理loader之后的内容;不直接操作文件,基于事件机制工作,会监听webpack打包过程中的某些节点,执行任务

返回
顶部