# 二.options
配置合并
new WebpackOptionsDefaulter().process(options)
1
WebpackOptionsDefaulter.js
class WebpackOptionsDefaulter extends OptionsDefaulter {
constructor() {
super();
this.set("devtool", false);
this.set("cache", true);
this.set("context", process.cwd());
this.set("target", "web");
this.set("module.unknownContextRequest", ".");
this.set("module.unknownContextRegExp", false);
this.set("module.unknownContextRecursive", true);
this.set("module.unknownContextCritical", true);
this.set("module.exprContextRequest", ".");
this.set("module.exprContextRegExp", false);
this.set("module.exprContextRecursive", true);
this.set("module.exprContextCritical", true);
this.set("module.wrappedContextRegExp", /.*/);
this.set("module.wrappedContextRecursive", true);
this.set("module.wrappedContextCritical", false);
this.set("module.strictExportPresence", false);
this.set("module.unsafeCache", true);
this.set("output", "call", (value, options) => {
if(typeof value === "string") {
return {
filename: value
};
} else if(typeof value !== "object") {
return {};
} else {
return value;
}
});
this.set("output.filename", "[name].js");
this.set("output.chunkFilename", "make", (options) => {
const filename = options.output.filename;
return filename.indexOf("[name]") >= 0 ? filename.replace("[name]", "[id]") : "[id]." + filename;
});
this.set("output.library", "");
this.set("output.hotUpdateFunction", "make", (options) => {
return Template.toIdentifier("webpackHotUpdate" + options.output.library);
});
this.set("output.jsonpFunction", "make", (options) => {
return Template.toIdentifier("webpackJsonp" + options.output.library);
});
this.set("output.libraryTarget", "var");
this.set("output.path", process.cwd());
this.set("output.sourceMapFilename", "[file].map[query]");
this.set("output.hotUpdateChunkFilename", "[id].[hash].hot-update.js");
this.set("output.hotUpdateMainFilename", "[hash].hot-update.json");
this.set("output.crossOriginLoading", false);
this.set("output.chunkLoadTimeout", 120000);
this.set("output.hashFunction", "md5");
this.set("output.hashDigest", "hex");
this.set("output.hashDigestLength", 20);
this.set("output.devtoolLineToLine", false);
this.set("output.strictModuleExceptionHandling", false);
this.set("node", {});
this.set("node.console", false);
this.set("node.process", true);
this.set("node.global", true);
this.set("node.Buffer", true);
this.set("node.setImmediate", true);
this.set("node.__filename", "mock");
this.set("node.__dirname", "mock");
this.set("performance.maxAssetSize", 250000);
this.set("performance.maxEntrypointSize", 250000);
this.set("performance.hints", false);
this.set("resolve", {});
this.set("resolve.unsafeCache", true);
this.set("resolve.modules", ["node_modules"]);
this.set("resolve.extensions", [".js", ".json"]);
this.set("resolve.aliasFields", "make", (options) => {
if(options.target === "web" || options.target === "webworker")
return ["browser"];
else
return [];
});
this.set("resolve.mainFields", "make", (options) => {
if(options.target === "web" || options.target === "webworker")
return ["browser", "module", "main"];
else
return ["module", "main"];
});
this.set("resolveLoader", {});
this.set("resolveLoader.unsafeCache", true);
this.set("resolveLoader.mainFields", ["loader", "main"]);
this.set("resolveLoader.extensions", [".js", ".json"]);
}
}
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
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
class OptionsDefaulter {
constructor() {
this.defaults = {};
this.config = {};
}
process(options) {
for(let name in this.defaults) {
switch(this.config[name]) {
case undefined:
if(getProperty(options, name) === undefined)
setProperty(options, name, this.defaults[name]);
break;
case "call":
setProperty(options, name, this.defaults[name].call(this, getProperty(options, name), options), options);
break;
case "make":
if(getProperty(options, name) === undefined)
setProperty(options, name, this.defaults[name].call(this, options), options);
break;
case "append":
{
let oldValue = getProperty(options, name);
if(!Array.isArray(oldValue)) oldValue = [];
oldValue.push.apply(oldValue, this.defaults[name]);
setProperty(options, name, oldValue);
break;
}
default:
throw new Error("OptionsDefaulter cannot process " + this.config[name]);
}
}
}
set(name, config, def) {
if(arguments.length === 3) {
this.defaults[name] = def;
this.config[name] = config;
} else {
this.defaults[name] = config;
delete this.config[name];
}
}
}
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
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
class Template extends Tapable {
constructor(outputOptions) {
super();
this.outputOptions = outputOptions || {};
}
static getFunctionContent(fn) {
return fn.toString().replace(/^function\s?\(\)\s?\{\n?|\n?\}$/g, "").replace(/^\t/mg, "");
}
static toIdentifier(str) {
if(typeof str !== "string") return "";
return str.replace(/^[^a-zA-Z$_]/, "_").replace(/[^a-zA-Z0-9$_]/g, "_");
}
static toPath(str) {
if(typeof str !== "string") return "";
return str.replace(/[^a-zA-Z0-9_!§$()=\-\^°]+/g, "-").replace(/^-|-$/, "");
}
// map number to a single character a-z, A-Z or <_ + number> if number is too big
static numberToIdentifer(n) {
// lower case
if(n < DELTA_A_TO_Z) return String.fromCharCode(START_LOWERCASE_ALPHABET_CODE + n);
// upper case
n -= DELTA_A_TO_Z;
if(n < DELTA_A_TO_Z) return String.fromCharCode(START_UPPERCASE_ALPHABET_CODE + n);
// fall back to _ + number
n -= DELTA_A_TO_Z;
return "_" + n;
}
indent(str) {
if(Array.isArray(str)) {
return str.map(this.indent.bind(this)).join("\n");
} else {
str = str.trimRight();
if(!str) return "";
var ind = (str[0] === "\n" ? "" : "\t");
return ind + str.replace(/\n([^\n])/g, "\n\t$1");
}
}
prefix(str, prefix) {
if(Array.isArray(str)) {
str = str.join("\n");
}
str = str.trim();
if(!str) return "";
const ind = (str[0] === "\n" ? "" : prefix);
return ind + str.replace(/\n([^\n])/g, "\n" + prefix + "$1");
}
asString(str) {
if(Array.isArray(str)) {
return str.join("\n");
}
return str;
}
getModulesArrayBounds(modules) {
if(!modules.every(moduleIdIsNumber))
return false;
var maxId = -Infinity;
var minId = Infinity;
modules.forEach(function(module) {
if(maxId < module.id) maxId = module.id;
if(minId > module.id) minId = module.id;
});
if(minId < 16 + ("" + minId).length) {
// add minId x ',' instead of 'Array(minId).concat(...)'
minId = 0;
}
var objectOverhead = modules.map(function(module) {
var idLength = (module.id + "").length;
return idLength + 2;
}).reduce(function(a, b) {
return a + b;
}, -1);
var arrayOverhead = minId === 0 ? maxId : 16 + ("" + minId).length + maxId;
return arrayOverhead < objectOverhead ? [minId, maxId] : false;
}
renderChunkModules(chunk, moduleTemplate, dependencyTemplates, prefix) {
if(!prefix) prefix = "";
var source = new ConcatSource();
if(chunk.modules.length === 0) {
source.add("[]");
return source;
}
var removedModules = chunk.removedModules;
var allModules = chunk.modules.map(function(module) {
return {
id: module.id,
source: moduleTemplate.render(module, dependencyTemplates, chunk)
};
});
if(removedModules && removedModules.length > 0) {
removedModules.forEach(function(id) {
allModules.push({
id: id,
source: "false"
});
});
}
var bounds = this.getModulesArrayBounds(chunk.modules);
if(bounds) {
// Render a spare array
var minId = bounds[0];
var maxId = bounds[1];
if(minId !== 0) source.add("Array(" + minId + ").concat(");
source.add("[\n");
var modules = {};
allModules.forEach(function(module) {
modules[module.id] = module;
});
for(var idx = minId; idx <= maxId; idx++) {
var module = modules[idx];
if(idx !== minId) source.add(",\n");
source.add("/* " + idx + " */");
if(module) {
source.add("\n");
source.add(module.source);
}
}
source.add("\n" + prefix + "]");
if(minId !== 0) source.add(")");
} else {
// Render an object
source.add("{\n");
allModules.sort(function(a, b) {
var aId = a.id + "";
var bId = b.id + "";
if(aId < bId) return -1;
if(aId > bId) return 1;
return 0;
}).forEach(function(module, idx) {
if(idx !== 0) source.add(",\n");
source.add("\n/***/ " + JSON.stringify(module.id) + ":\n");
source.add(module.source);
});
source.add("\n\n" + prefix + "}");
}
return source;
}
};
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
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
← 一.webpack 三.compiler →