Skip to content

Commit 5538e15

Browse files
authored
Merge pull request webpack#5572 from TheLarkInn/feature/template_cleanup
feat(template): cleanup template a bit, add a test for toPath
2 parents 616ce45 + 7a47bd8 commit 5538e15

2 files changed

Lines changed: 30 additions & 17 deletions

File tree

lib/Template.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +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;
14+
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;
17+
const PATH_NAME_NORMALIZE_REPLACE_REGEX = /[^a-zA-Z0-9_!§$()=\-^°]+/g;
18+
const MATCH_PADDED_HYPHENS_REPLACE_REGEX = /^-|-$/g;
1319

1420
module.exports = class Template extends Tapable {
1521
constructor(outputOptions) {
@@ -18,17 +24,17 @@ module.exports = class Template extends Tapable {
1824
}
1925

2026
static getFunctionContent(fn) {
21-
return fn.toString().replace(/^function\s?\(\)\s?\{\n?|\n?\}$/g, "").replace(/^\t/mg, "");
27+
return fn.toString().replace(FUNCTION_CONTENT_REGEX, "").replace(INDENT_MULTILINE_REGEX, "");
2228
}
2329

2430
static toIdentifier(str) {
2531
if(typeof str !== "string") return "";
26-
return str.replace(/^[^a-zA-Z$_]/, "_").replace(/[^a-zA-Z0-9$_]/g, "_");
32+
return str.replace(IDENTIFIER_NAME_REPLACE_REGEX, "_").replace(IDENTIFIER_ALPHA_NUMERIC_NAME_REPLACE_REGEX, "_");
2733
}
2834

2935
static toPath(str) {
3036
if(typeof str !== "string") return "";
31-
return str.replace(/[^a-zA-Z0-9_!§$()=\-^°]+/g, "-").replace(/^-|-$/, "");
37+
return str.replace(PATH_NAME_NORMALIZE_REPLACE_REGEX, "-").replace(MATCH_PADDED_HYPHENS_REPLACE_REGEX, "");
3238
}
3339

3440
// map number to a single character a-z, A-Z or <_ + number> if number is too big
@@ -144,23 +150,27 @@ module.exports = class Template extends Tapable {
144150
} else {
145151
// Render an object
146152
source.add("{\n");
147-
allModules.sort(function(a, b) {
148-
var aId = a.id + "";
149-
var bId = b.id + "";
150-
if(aId < bId) return -1;
151-
if(aId > bId) return 1;
152-
return 0;
153-
}).forEach(function(module, idx) {
154-
if(idx !== 0) source.add(",\n");
155-
source.add("\n/***/ " + JSON.stringify(module.id) + ":\n");
156-
source.add(module.source);
157-
});
153+
allModules
154+
.sort(stringifyIdSortPredicate)
155+
.forEach(function(module, idx) {
156+
if(idx !== 0) source.add(",\n");
157+
source.add(`\n/***/ ${JSON.stringify(module.id)}:\n`);
158+
source.add(module.source);
159+
});
158160
source.add("\n\n" + prefix + "}");
159161
}
160162
return source;
161163
}
162164
};
163165

166+
function stringifyIdSortPredicate(a, b) {
167+
var aId = a.id + "";
168+
var bId = b.id + "";
169+
if(aId < bId) return -1;
170+
if(aId > bId) return 1;
171+
return 0;
172+
}
173+
164174
function moduleIdIsNumber(module) {
165175
return typeof module.id === "number";
166176
}

test/Template.test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
const should = require("should");
44

5-
const template = require("../lib/Template");
5+
const Template = require("../lib/Template");
66

77
describe("Template", () => {
88
it("should generate valid identifiers", () =>
9-
template.toIdentifier("0abc-def9").should.equal("_abc_def9"));
9+
Template.toIdentifier("0abc-def9").should.equal("_abc_def9"));
1010
it("should generate valid number identifiers", () => {
1111
const items = [];
1212
let item;
1313
for(let i = 0; i < 80; i += 1) {
14-
item = template.numberToIdentifer(i);
14+
item = Template.numberToIdentifer(i);
1515
if(item === "") {
1616
throw new Error("empty number identifier");
1717
} else if(items.indexOf(item) > -1) {
@@ -21,4 +21,7 @@ describe("Template", () => {
2121
}
2222
}
2323
});
24+
it("should generate sanitized path identifiers", () => {
25+
Template.toPath("path/to-sdfas/sadfome$$.js").should.equal("path-to-sdfas-sadfome$$-js");
26+
});
2427
});

0 commit comments

Comments
 (0)