Skip to content

Commit bafcdf1

Browse files
authored
Merge pull request webpack#5459 from webpack/refactor/cleanups
Small refactorings
2 parents f53c0ce + f83c913 commit bafcdf1

26 files changed

Lines changed: 83 additions & 86 deletions

‎lib/AmdMainTemplatePlugin.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class AmdMainTemplatePlugin {
2222
typeof m.request === "object" ? m.request.amd : m.request
2323
));
2424
const externalsArguments = externals.map((m) =>
25-
Template.toIdentifier(`__WEBPACK_EXTERNAL_MODULE_${m.id}__`)
25+
`__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(`${m.id}`)}__`
2626
).join(", ");
2727

2828
if(this.name) {

‎lib/BannerPlugin.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88
const ConcatSource = require("webpack-sources").ConcatSource;
99
const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
10+
const Template = require("./Template");
1011

1112
const wrapComment = (str) => {
12-
if(!str.includes("\n")) return `/*! ${str} */`;
13-
return `/*!\n * ${str.split("\n").join("\n * ")}\n */`;
13+
if(!str.includes("\n")) return Template.toComment(str);
14+
return `/*!\n * ${str.replace(/\*\//g, "* /").split("\n").join("\n * ")}\n */`;
1415
};
1516

1617
class BannerPlugin {

‎lib/Compilation.js‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -874,17 +874,22 @@ class Compilation extends Tapable {
874874
};
875875

876876
const iteratorDependency = d => {
877-
if(!d.module) {
877+
const ref = d.getReference();
878+
if(!ref) {
879+
return;
880+
}
881+
const refModule = ref.module;
882+
if(!refModule) {
878883
return;
879884
}
880885
if(d.weak) {
881886
return;
882887
}
883-
if(chunk.addModule(d.module)) {
884-
d.module.addChunk(chunk);
888+
if(chunk.addModule(refModule)) {
889+
refModule.addChunk(chunk);
885890
queue.push({
886-
block: d.module,
887-
module: d.module,
891+
block: refModule,
892+
module: refModule,
888893
chunk
889894
});
890895
}

‎lib/Dependency.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const compareLocations = require("./compareLocations");
88
class Dependency {
99
constructor() {
1010
this.module = null;
11+
this.weak = false;
1112
}
1213

1314
isEqualResource() {
@@ -19,6 +20,7 @@ class Dependency {
1920
if(!this.module) return null;
2021
return {
2122
module: this.module,
23+
weak: this.weak,
2224
importedNames: true, // true: full object, false: only sideeffects/no export, array of strings: the exports with this names
2325
};
2426
}

‎lib/ExternalModule.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class ExternalModule extends Module {
7070
}
7171

7272
getSourceForAmdOrUmdExternal(id, optional, request) {
73-
const externalVariable = Template.toIdentifier(`__WEBPACK_EXTERNAL_MODULE_${id}__`);
73+
const externalVariable = `__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(`${id}`)}__`;
7474
const missingModuleError = optional ? this.checkExternalVariable(externalVariable, request) : "";
7575
return `${missingModuleError}module.exports = ${externalVariable};`;
7676
}

‎lib/FlagInitialModulesAsUsedPlugin.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class FlagInitialModulesAsUsedPlugin {
1313
return;
1414
}
1515
chunk.forEachModule((module) => {
16+
module.used = true;
1617
module.usedExports = true;
1718
});
1819
});

‎lib/FunctionModuleTemplatePlugin.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"use strict";
66

77
const ConcatSource = require("webpack-sources").ConcatSource;
8+
const Template = require("./Template");
89

910
class FunctionModuleTemplatePlugin {
1011
apply(moduleTemplate) {
@@ -29,17 +30,17 @@ class FunctionModuleTemplatePlugin {
2930
source.add(" !*** " + req.replace(/\*\//g, "*_/") + " ***!\n");
3031
source.add(" \\****" + req.replace(/./g, "*") + "****/\n");
3132
if(Array.isArray(module.providedExports))
32-
source.add("/*! exports provided: " + module.providedExports.join(", ") + " */\n");
33+
source.add(Template.toComment("exports provided: " + module.providedExports.join(", ")) + "\n");
3334
else if(module.providedExports)
34-
source.add("/*! no static exports found */\n");
35+
source.add(Template.toComment("no static exports found") + "\n");
3536
if(Array.isArray(module.usedExports))
36-
source.add("/*! exports used: " + module.usedExports.join(", ") + " */\n");
37+
source.add(Template.toComment("exports used: " + module.usedExports.join(", ")) + "\n");
3738
else if(module.usedExports)
38-
source.add("/*! all exports used */\n");
39+
source.add(Template.toComment("all exports used") + "\n");
3940
if(module.optimizationBailout) {
4041
module.optimizationBailout.forEach(text => {
4142
if(typeof text === "function") text = text(this.requestShortener);
42-
source.add(`/*! ${text} */\n`);
43+
source.add(Template.toComment(`${text}`) + "\n");
4344
});
4445
}
4546
source.add(moduleSource);

‎lib/MultiModule.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"use strict";
66

77
const Module = require("./Module");
8+
const Template = require("./Template");
89
const RawSource = require("webpack-sources").RawSource;
910

1011
class MultiModule extends Module {
@@ -58,7 +59,7 @@ class MultiModule extends Module {
5859
str.push("module.exports = ");
5960
str.push("__webpack_require__(");
6061
if(outputOptions.pathinfo)
61-
str.push(`/*! ${dep.request} */`);
62+
str.push(Template.toComment(dep.request));
6263
str.push(`${JSON.stringify(dep.module.id)}`);
6364
str.push(")");
6465
} else {

‎lib/NormalModuleFactory.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class NormalModuleFactory extends Tapable {
7272
// Ignored
7373
if(!result) return callback();
7474

75-
let createdModule = this.applyPluginsBailResult("create-module", result);
75+
let createdModule = this.applyPluginsBailResult1("create-module", result);
7676
if(!createdModule) {
7777

7878
if(!result.request) {

‎lib/Template.js‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ const ConcatSource = require("webpack-sources").ConcatSource;
1010
const START_LOWERCASE_ALPHABET_CODE = "a".charCodeAt(0);
1111
const START_UPPERCASE_ALPHABET_CODE = "A".charCodeAt(0);
1212
const DELTA_A_TO_Z = "z".charCodeAt(0) - START_LOWERCASE_ALPHABET_CODE + 1;
13-
const FUNCTION_CONTENT_REGEX = /^function\s?\(\)\s?\{\n?|\n?\}$/g;
13+
const FUNCTION_CONTENT_REGEX = /^function\s?\(\)\s?\{\r?\n?|\r?\n?\}$/g;
1414
const INDENT_MULTILINE_REGEX = /^\t/mg;
15-
const IDENTIFIER_NAME_REPLACE_REGEX = /^[^a-zA-Z$_]/;
16-
const IDENTIFIER_ALPHA_NUMERIC_NAME_REPLACE_REGEX = /[^a-zA-Z0-9$_]/g;
15+
const LINE_SEPARATOR_REGEX = /\r?\n/g;
16+
const IDENTIFIER_NAME_REPLACE_REGEX = /^([^a-zA-Z$_])/;
17+
const IDENTIFIER_ALPHA_NUMERIC_NAME_REPLACE_REGEX = /[^a-zA-Z0-9$]+/g;
18+
const COMMENT_END_REGEX = /\*\//g;
1719
const PATH_NAME_NORMALIZE_REPLACE_REGEX = /[^a-zA-Z0-9_!§$()=\-^°]+/g;
1820
const MATCH_PADDED_HYPHENS_REPLACE_REGEX = /^-|-$/g;
1921

@@ -24,12 +26,22 @@ module.exports = class Template extends Tapable {
2426
}
2527

2628
static getFunctionContent(fn) {
27-
return fn.toString().replace(FUNCTION_CONTENT_REGEX, "").replace(INDENT_MULTILINE_REGEX, "");
29+
return fn.toString().replace(FUNCTION_CONTENT_REGEX, "").replace(INDENT_MULTILINE_REGEX, "").replace(LINE_SEPARATOR_REGEX, "\n");
2830
}
2931

3032
static toIdentifier(str) {
3133
if(typeof str !== "string") return "";
32-
return str.replace(IDENTIFIER_NAME_REPLACE_REGEX, "_").replace(IDENTIFIER_ALPHA_NUMERIC_NAME_REPLACE_REGEX, "_");
34+
return str.replace(IDENTIFIER_NAME_REPLACE_REGEX, "_$1").replace(IDENTIFIER_ALPHA_NUMERIC_NAME_REPLACE_REGEX, "_");
35+
}
36+
37+
static toComment(str) {
38+
if(!str) return "";
39+
return `/*! ${str.replace(COMMENT_END_REGEX, "* /")} */`;
40+
}
41+
42+
static toNormalComment(str) {
43+
if(!str) return "";
44+
return `/* ${str.replace(COMMENT_END_REGEX, "* /")} */`;
3345
}
3446

3547
static toPath(str) {

0 commit comments

Comments
 (0)