Skip to content

Commit 048121a

Browse files
authored
Merge pull request webpack#6140 from webpack/feature/add-entry-default-value
feat(defaults): default entry, output, mode
2 parents e9f1ad2 + 002f45a commit 048121a

6 files changed

Lines changed: 55 additions & 54 deletions

File tree

lib/WebpackOptionsDefaulter.js

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,22 @@
44
*/
55
"use strict";
66

7+
const path = require("path");
8+
79
const OptionsDefaulter = require("./OptionsDefaulter");
810
const Template = require("./Template");
911

12+
const isProductionLikeMode = options => {
13+
return options.mode === "production" || !options.mode;
14+
};
15+
1016
class WebpackOptionsDefaulter extends OptionsDefaulter {
1117
constructor() {
1218
super();
13-
this.set("devtool", "make", options => options.mode === "development" ? "eval" : false);
19+
20+
this.set("entry", "./src");
21+
22+
this.set("devtool", "make", options => (options.mode === "development" ? "eval" : false));
1423
this.set("cache", "make", options => options.mode === "development");
1524

1625
this.set("context", process.cwd());
@@ -33,18 +42,22 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
3342
this.set("module.unsafeCache", "make", options => !!options.cache);
3443
this.set("module.rules", []);
3544
this.set("module.defaultRules", [{
36-
type: "javascript/auto",
37-
resolve: {}
38-
}, {
39-
test: /\.mjs$/i,
40-
type: "javascript/esm"
41-
}, {
42-
test: /\.json$/i,
43-
type: "json",
44-
}, {
45-
test: /\.wasm$/i,
46-
type: "webassembly/experimental",
47-
}]);
45+
type: "javascript/auto",
46+
resolve: {}
47+
},
48+
{
49+
test: /\.mjs$/i,
50+
type: "javascript/esm"
51+
},
52+
{
53+
test: /\.json$/i,
54+
type: "json"
55+
},
56+
{
57+
test: /\.wasm$/i,
58+
type: "webassembly/experimental"
59+
}
60+
]);
4861

4962
this.set("output", "call", (value, options) => {
5063
if(typeof value === "string") {
@@ -57,8 +70,9 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
5770
return Object.assign({}, value);
5871
}
5972
});
73+
6074
this.set("output.filename", "[name].js");
61-
this.set("output.chunkFilename", "make", (options) => {
75+
this.set("output.chunkFilename", "make", options => {
6276
const filename = options.output.filename;
6377
const hasName = filename.indexOf("[name]") >= 0;
6478
const hasChunkHash = filename.indexOf("[chunkhash]") >= 0;
@@ -71,13 +85,13 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
7185
});
7286
this.set("output.webassemblyModuleFilename", "[modulehash].module.wasm");
7387
this.set("output.library", "");
74-
this.set("output.hotUpdateFunction", "make", (options) => {
88+
this.set("output.hotUpdateFunction", "make", options => {
7589
return Template.toIdentifier("webpackHotUpdate" + Template.toIdentifier(options.output.library));
7690
});
77-
this.set("output.jsonpFunction", "make", (options) => {
91+
this.set("output.jsonpFunction", "make", options => {
7892
return Template.toIdentifier("webpackJsonp" + Template.toIdentifier(options.output.library));
7993
});
80-
this.set("output.chunkCallbackName", "make", (options) => {
94+
this.set("output.chunkCallbackName", "make", options => {
8195
return Template.toIdentifier("webpackChunk" + Template.toIdentifier(options.output.library));
8296
});
8397
this.set("output.globalObject", "make", options => {
@@ -96,11 +110,11 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
96110
return "self";
97111
}
98112
});
99-
this.set("output.devtoolNamespace", "make", (options) => {
113+
this.set("output.devtoolNamespace", "make", options => {
100114
return options.output.library || "";
101115
});
102116
this.set("output.libraryTarget", "var");
103-
this.set("output.path", process.cwd());
117+
this.set("output.path", path.join(process.cwd(), "dist"));
104118
this.set("output.pathinfo", "make", options => options.mode === "development");
105119
this.set("output.sourceMapFilename", "[file].map[query]");
106120
this.set("output.hotUpdateChunkFilename", "[id].[hash].hot-update.js");
@@ -128,7 +142,7 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
128142
this.set("node.__filename", "mock");
129143
this.set("node.__dirname", "mock");
130144

131-
this.set("performance", "make", options => options.mode !== "production" ? false : undefined);
145+
this.set("performance", "make", options => (isProductionLikeMode(options) ? false : undefined));
132146
this.set("performance", "call", value => {
133147
if(typeof value === "boolean") {
134148
return value;
@@ -138,22 +152,22 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
138152
});
139153
this.set("performance.maxAssetSize", 250000);
140154
this.set("performance.maxEntrypointSize", 250000);
141-
this.set("performance.hints", "make", options => options.mode === "production" ? "warning" : false);
155+
this.set("performance.hints", "make", options => (isProductionLikeMode(options) ? "warning" : false));
142156

143157
this.set("optimization.removeAvailableModules", true);
144158
this.set("optimization.removeEmptyChunks", true);
145159
this.set("optimization.mergedDuplicateChunks", true);
146-
this.set("optimization.flagIncludedChunks", "make", options => options.mode === "production");
147-
this.set("optimization.occurrenceOrder", "make", options => options.mode === "production");
148-
this.set("optimization.sideEffects", "make", options => options.mode === "production");
160+
this.set("optimization.flagIncludedChunks", "make", options => isProductionLikeMode(options));
161+
this.set("optimization.occurrenceOrder", "make", options => isProductionLikeMode(options));
162+
this.set("optimization.sideEffects", "make", options => isProductionLikeMode(options));
149163
this.set("optimization.providedExports", true);
150-
this.set("optimization.usedExports", "make", options => options.mode === "production");
151-
this.set("optimization.concatenateModules", "make", options => options.mode === "production");
152-
this.set("optimization.noEmitOnErrors", "make", options => options.mode === "production");
164+
this.set("optimization.usedExports", "make", options => isProductionLikeMode(options));
165+
this.set("optimization.concatenateModules", "make", options => isProductionLikeMode(options));
166+
this.set("optimization.noEmitOnErrors", "make", options => isProductionLikeMode(options));
153167
this.set("optimization.namedModules", "make", options => options.mode === "development");
154168
this.set("optimization.namedChunks", "make", options => options.mode === "development");
155169
this.set("optimization.portableRecords", "make", options => !!(options.recordsInputPath || options.recordsOutputPath || options.recordsPath));
156-
this.set("optimization.minimize", "make", options => options.mode === "production");
170+
this.set("optimization.minimize", "make", options => isProductionLikeMode(options));
157171
this.set("optimization.minimizer", "make", options => [{
158172
apply: compiler => {
159173
// Lazy load the uglifyjs plugin
@@ -172,19 +186,15 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
172186
this.set("resolve.modules", ["node_modules"]);
173187
this.set("resolve.extensions", [".wasm", ".mjs", ".js", ".json"]);
174188
this.set("resolve.mainFiles", ["index"]);
175-
this.set("resolve.aliasFields", "make", (options) => {
176-
if(options.target === "web" || options.target === "webworker")
177-
return ["browser"];
178-
else
179-
return [];
189+
this.set("resolve.aliasFields", "make", options => {
190+
if(options.target === "web" || options.target === "webworker") return ["browser"];
191+
else return [];
180192
});
181-
this.set("resolve.mainFields", "make", (options) => {
182-
if(options.target === "web" || options.target === "webworker")
183-
return ["browser", "module", "main"];
184-
else
185-
return ["module", "main"];
193+
this.set("resolve.mainFields", "make", options => {
194+
if(options.target === "web" || options.target === "webworker") return ["browser", "module", "main"];
195+
else return ["module", "main"];
186196
});
187-
this.set("resolve.cacheWithContext", "make", (options) => {
197+
this.set("resolve.cacheWithContext", "make", options => {
188198
return Array.isArray(options.resolve.plugins) && options.resolve.plugins.length > 0;
189199
});
190200

@@ -193,7 +203,7 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
193203
this.set("resolveLoader.mainFields", ["loader", "main"]);
194204
this.set("resolveLoader.extensions", [".js", ".json"]);
195205
this.set("resolveLoader.mainFiles", ["index"]);
196-
this.set("resolveLoader.cacheWithContext", "make", (options) => {
206+
this.set("resolveLoader.cacheWithContext", "make", options => {
197207
return Array.isArray(options.resolveLoader.plugins) && options.resolveLoader.plugins.length > 0;
198208
});
199209
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"url-loader": "~0.5.0",
6363
"val-loader": "^1.0.2",
6464
"vm-browserify": "~0.0.0",
65-
"webpack-cli": "^1.5.2",
65+
"webpack-cli": "^1.5.3",
6666
"webpack-dev-middleware": "^1.9.0",
6767
"worker-loader": "^0.8.0",
6868
"xxhashjs": "^0.2.1"

schemas/WebpackOptions.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,8 +1735,5 @@
17351735
"type": "object"
17361736
}
17371737
},
1738-
"required": [
1739-
"entry"
1740-
],
17411738
"type": "object"
17421739
}

test/Compiler-caching.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ describe("Compiler (caching)", function() {
1212
this.timeout(15000);
1313

1414
function compile(entry, options, callback) {
15+
options.mode = "none";
1516
options = new WebpackOptionsDefaulter().process(options);
1617
options.cache = true;
1718
options.entry = entry;
19+
options.optimization.minimize = false;
1820
options.context = path.join(__dirname, "fixtures");
1921
options.output.path = "/";
2022
options.output.filename = "bundle.js";

test/Validation.test.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@ describe("Validation", () => {
1818
message: [
1919
" - configuration should be an object."
2020
]
21-
}, {
22-
name: "empty configuration",
23-
config: {},
24-
message: [
25-
" - configuration misses the property 'entry'.",
26-
" object { <key>: non-empty string | [non-empty string] } | non-empty string | [non-empty string] | function",
27-
" -> The entry point(s) of the compilation."
28-
]
2921
}, {
3022
name: "empty entry string",
3123
config: {
@@ -193,7 +185,7 @@ describe("Validation", () => {
193185
},
194186
message: [
195187
" - configuration has an unknown property 'postcss'. These properties are valid:",
196-
" object { mode?, amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry, externals?, " +
188+
" object { mode?, amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry?, externals?, " +
197189
"loader?, module?, name?, node?, output?, optimization?, parallelism?, performance?, plugins?, profile?, recordsInputPath?, " +
198190
"recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, stats?, target?, watch?, watchOptions? }",
199191
" For typos: please correct them.",

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5825,7 +5825,7 @@ webpack-addons@^1.1.4:
58255825
dependencies:
58265826
jscodeshift "^0.4.0"
58275827

5828-
webpack-cli@^1.5.2:
5828+
webpack-cli@^1.5.3:
58295829
version "1.5.3"
58305830
resolved "https://registry.yarnpkg.com/webpack-cli/-/webpack-cli-1.5.3.tgz#e49883b40249571ff79415196b3d76cab1f7272f"
58315831
dependencies:

0 commit comments

Comments
 (0)