Skip to content

Commit b864603

Browse files
aemTheLarkInn
authored andcommitted
refactor(es6): upgrade (Webpack)OptionsDefaulter to ES6 (webpack#3671)
* refactor(es6): refactor (Webpack)OptiionsDefaulter to ES6 classes
1 parent 49a3172 commit b864603

File tree

2 files changed

+131
-129
lines changed

2 files changed

+131
-129
lines changed

lib/OptionsDefaulter.js

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
function OptionsDefaulter() {
6-
this.defaults = {};
7-
this.config = {};
8-
}
9-
module.exports = OptionsDefaulter;
5+
"use strict";
106

117
function getProperty(obj, name) {
128
name = name.split(".");
@@ -27,44 +23,49 @@ function setProperty(obj, name, value) {
2723
obj[name.pop()] = value;
2824
}
2925

30-
OptionsDefaulter.prototype.process = function(options) {
31-
var addItemTo = function addItemTo(list) {
32-
return function(item) {
33-
list.push(item);
34-
}
26+
class OptionsDefaulter {
27+
constructor() {
28+
this.defaults = {}
29+
this.config = {};
3530
}
3631

37-
for(var name in this.defaults) {
38-
switch(this.config[name]) {
39-
case undefined:
40-
if(getProperty(options, name) === undefined)
41-
setProperty(options, name, this.defaults[name]);
42-
break;
43-
case "call":
44-
setProperty(options, name, this.defaults[name].call(this, getProperty(options, name), options), options);
45-
break;
46-
case "make":
47-
if(getProperty(options, name) === undefined)
48-
setProperty(options, name, this.defaults[name].call(this, options), options);
49-
break;
50-
case "append":
51-
var oldValue = getProperty(options, name);
52-
if(!Array.isArray(oldValue)) oldValue = [];
53-
this.defaults[name].forEach(addItemTo(oldValue));
54-
setProperty(options, name, oldValue);
55-
break;
56-
default:
57-
throw new Error("OptionsDefaulter cannot process " + this.config[name]);
32+
process(options) {
33+
for(let name in this.defaults) {
34+
switch(this.config[name]) {
35+
case undefined:
36+
if(getProperty(options, name) === undefined)
37+
setProperty(options, name, this.defaults[name]);
38+
break;
39+
case "call":
40+
setProperty(options, name, this.defaults[name].call(this, getProperty(options, name), options), options);
41+
break;
42+
case "make":
43+
if(getProperty(options, name) === undefined)
44+
setProperty(options, name, this.defaults[name].call(this, options), options);
45+
break;
46+
case "append":
47+
{
48+
let oldValue = getProperty(options, name);
49+
if(!Array.isArray(oldValue)) oldValue = [];
50+
oldValue.push.apply(oldValue, this.defaults[name]);
51+
setProperty(options, name, oldValue);
52+
break;
53+
}
54+
default:
55+
throw new Error("OptionsDefaulter cannot process " + this.config[name]);
56+
}
5857
}
5958
}
60-
};
6159

62-
OptionsDefaulter.prototype.set = function(name, config, def) {
63-
if(arguments.length === 3) {
64-
this.defaults[name] = def;
65-
this.config[name] = config;
66-
} else {
67-
this.defaults[name] = config;
68-
delete this.config[name];
60+
set(name, config, def) {
61+
if(arguments.length === 3) {
62+
this.defaults[name] = def;
63+
this.config[name] = config;
64+
} else {
65+
this.defaults[name] = config;
66+
delete this.config[name];
67+
}
6968
}
70-
};
69+
}
70+
71+
module.exports = OptionsDefaulter;

lib/WebpackOptionsDefaulter.js

Lines changed: 90 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -2,102 +2,103 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
var OptionsDefaulter = require("./OptionsDefaulter");
6-
var Template = require("./Template");
5+
"use strict";
76

8-
function WebpackOptionsDefaulter() {
9-
OptionsDefaulter.call(this);
10-
this.set("devtool", false);
11-
this.set("cache", true);
7+
const OptionsDefaulter = require("./OptionsDefaulter");
8+
const Template = require("./Template");
129

13-
this.set("context", process.cwd());
14-
this.set("target", "web");
10+
class WebpackOptionsDefaulter extends OptionsDefaulter {
11+
constructor() {
12+
super();
13+
this.set("devtool", false);
14+
this.set("cache", true);
1515

16-
this.set("module.unknownContextRequest", ".");
17-
this.set("module.unknownContextRegExp", false);
18-
this.set("module.unknownContextRecursive", true);
19-
this.set("module.unknownContextCritical", true);
20-
this.set("module.exprContextRequest", ".");
21-
this.set("module.exprContextRegExp", false);
22-
this.set("module.exprContextRecursive", true);
23-
this.set("module.exprContextCritical", true);
24-
this.set("module.wrappedContextRegExp", /.*/);
25-
this.set("module.wrappedContextRecursive", true);
26-
this.set("module.wrappedContextCritical", false);
16+
this.set("context", process.cwd());
17+
this.set("target", "web");
2718

28-
this.set("module.unsafeCache", true);
19+
this.set("module.unknownContextRequest", ".");
20+
this.set("module.unknownContextRegExp", false);
21+
this.set("module.unknownContextRecursive", true);
22+
this.set("module.unknownContextCritical", true);
23+
this.set("module.exprContextRequest", ".");
24+
this.set("module.exprContextRegExp", false);
25+
this.set("module.exprContextRecursive", true);
26+
this.set("module.exprContextCritical", true);
27+
this.set("module.wrappedContextRegExp", /.*/);
28+
this.set("module.wrappedContextRecursive", true);
29+
this.set("module.wrappedContextCritical", false);
2930

30-
this.set("output", "call", function(value, options) {
31-
if(typeof value === "string") {
32-
return {
33-
filename: value
34-
};
35-
} else if(typeof value !== "object") {
36-
return {};
37-
} else {
38-
return value;
39-
}
40-
});
41-
this.set("output.filename", "[name].js");
42-
this.set("output.chunkFilename", "make", function(options) {
43-
var filename = options.output.filename;
44-
return filename.indexOf("[name]") >= 0 ? filename.replace("[name]", "[id]") : "[id]." + filename;
45-
});
46-
this.set("output.library", "");
47-
this.set("output.hotUpdateFunction", "make", function(options) {
48-
return Template.toIdentifier("webpackHotUpdate" + options.output.library);
49-
});
50-
this.set("output.jsonpFunction", "make", function(options) {
51-
return Template.toIdentifier("webpackJsonp" + options.output.library);
52-
});
53-
this.set("output.libraryTarget", "var");
54-
this.set("output.path", process.cwd());
55-
this.set("output.sourceMapFilename", "[file].map[query]");
56-
this.set("output.hotUpdateChunkFilename", "[id].[hash].hot-update.js");
57-
this.set("output.hotUpdateMainFilename", "[hash].hot-update.json");
58-
this.set("output.crossOriginLoading", false);
59-
this.set("output.hashFunction", "md5");
60-
this.set("output.hashDigest", "hex");
61-
this.set("output.hashDigestLength", 20);
62-
this.set("output.devtoolLineToLine", false);
63-
this.set("output.strictModuleExceptionHandling", false);
31+
this.set("module.unsafeCache", true);
6432

65-
this.set("node", {});
66-
this.set("node.console", false);
67-
this.set("node.process", true);
68-
this.set("node.global", true);
69-
this.set("node.Buffer", true);
70-
this.set("node.setImmediate", true);
71-
this.set("node.__filename", "mock");
72-
this.set("node.__dirname", "mock");
33+
this.set("output", "call", (value, options) => {
34+
if(typeof value === "string") {
35+
return {
36+
filename: value
37+
};
38+
} else if(typeof value !== "object") {
39+
return {};
40+
} else {
41+
return value;
42+
}
43+
});
44+
this.set("output.filename", "[name].js");
45+
this.set("output.chunkFilename", "make", (options) => {
46+
let filename = options.output.filename;
47+
return filename.indexOf("[name]") >= 0 ? filename.replace("[name]", "[id]") : "[id]." + filename;
48+
});
49+
this.set("output.library", "");
50+
this.set("output.hotUpdateFunction", "make", (options) => {
51+
return Template.toIdentifier("webpackHotUpdate" + options.output.library)
52+
});
53+
this.set("output.jsonpFunction", "make", (options) => {
54+
return Template.toIdentifier("webpackJsonp" + options.output.library)
55+
});
56+
this.set("output.libraryTarget", "var");
57+
this.set("output.path", process.cwd());
58+
this.set("output.sourceMapFilename", "[file].map[query]");
59+
this.set("output.hotUpdateChunkFilename", "[id].[hash].hot-update.js");
60+
this.set("output.hotUpdateMainFilename", "[hash].hot-update.json");
61+
this.set("output.crossOriginLoading", false);
62+
this.set("output.hashFunction", "md5");
63+
this.set("output.hashDigest", "hex");
64+
this.set("output.hashDigestLength", 20);
65+
this.set("output.devtoolLineToLine", false);
66+
this.set("output.strictModuleExceptionHandling", false);
7367

74-
this.set("performance.maxAssetSize", 250000);
75-
this.set("performance.maxEntrypointSize", 250000);
76-
this.set("performance.hints", false);
68+
this.set("node", {});
69+
this.set("node.console", false);
70+
this.set("node.process", true);
71+
this.set("node.global", true);
72+
this.set("node.Buffer", true);
73+
this.set("node.setImmediate", true);
74+
this.set("node.__filename", "mock");
75+
this.set("node.__dirname", "mock");
7776

78-
this.set("resolve", {});
79-
this.set("resolve.unsafeCache", true);
80-
this.set("resolve.modules", ["node_modules"]);
81-
this.set("resolve.extensions", [".js", ".json"]);
82-
this.set("resolve.aliasFields", "make", function(options) {
83-
if(options.target === "web" || options.target === "webworker")
84-
return ["browser"];
85-
else
86-
return [];
87-
});
88-
this.set("resolve.mainFields", "make", function(options) {
89-
if(options.target === "web" || options.target === "webworker")
90-
return ["browser", "module", "main"];
91-
else
92-
return ["module", "main"];
93-
});
94-
this.set("resolveLoader", {});
95-
this.set("resolveLoader.unsafeCache", true);
96-
this.set("resolveLoader.mainFields", ["loader", "main"]);
97-
this.set("resolveLoader.extensions", [".js", ".json"]);
98-
}
99-
module.exports = WebpackOptionsDefaulter;
77+
this.set("performance.maxAssetSize", 250000);
78+
this.set("performance.maxEntrypointSize", 250000);
79+
this.set("performance.hints", false);
10080

101-
WebpackOptionsDefaulter.prototype = Object.create(OptionsDefaulter.prototype);
81+
this.set("resolve", {});
82+
this.set("resolve.unsafeCache", true);
83+
this.set("resolve.modules", ["node_modules"]);
84+
this.set("resolve.extensions", [".js", ".json"]);
85+
this.set("resolve.aliasFields", "make", (options) => {
86+
if(options.target === "web" || options.target === "webworker")
87+
return ["browser"];
88+
else
89+
return [];
90+
});
91+
this.set("resolve.mainFields", "make", (options) => {
92+
if(options.target === "web" || options.target === "webworker")
93+
return ["browser", "module", "main"];
94+
else
95+
return ["module", "main"];
96+
});
97+
this.set("resolveLoader", {});
98+
this.set("resolveLoader.unsafeCache", true);
99+
this.set("resolveLoader.mainFields", ["loader", "main"]);
100+
this.set("resolveLoader.extensions", [".js", ".json"]);
101+
}
102+
}
102103

103-
WebpackOptionsDefaulter.prototype.constructor = WebpackOptionsDefaulter;
104+
module.exports = WebpackOptionsDefaulter;

0 commit comments

Comments
 (0)