mirror of https://github.com/webpack/webpack.git
Merge pull request #5572 from TheLarkInn/feature/template_cleanup
feat(template): cleanup template a bit, add a test for toPath
This commit is contained in:
commit
5538e157d5
|
|
@ -10,6 +10,12 @@ const ConcatSource = require("webpack-sources").ConcatSource;
|
|||
const START_LOWERCASE_ALPHABET_CODE = "a".charCodeAt(0);
|
||||
const START_UPPERCASE_ALPHABET_CODE = "A".charCodeAt(0);
|
||||
const DELTA_A_TO_Z = "z".charCodeAt(0) - START_LOWERCASE_ALPHABET_CODE + 1;
|
||||
const FUNCTION_CONTENT_REGEX = /^function\s?\(\)\s?\{\n?|\n?\}$/g;
|
||||
const INDENT_MULTILINE_REGEX = /^\t/mg;
|
||||
const IDENTIFIER_NAME_REPLACE_REGEX = /^[^a-zA-Z$_]/;
|
||||
const IDENTIFIER_ALPHA_NUMERIC_NAME_REPLACE_REGEX = /[^a-zA-Z0-9$_]/g;
|
||||
const PATH_NAME_NORMALIZE_REPLACE_REGEX = /[^a-zA-Z0-9_!§$()=\-^°]+/g;
|
||||
const MATCH_PADDED_HYPHENS_REPLACE_REGEX = /^-|-$/g;
|
||||
|
||||
module.exports = class Template extends Tapable {
|
||||
constructor(outputOptions) {
|
||||
|
|
@ -18,17 +24,17 @@ module.exports = class Template extends Tapable {
|
|||
}
|
||||
|
||||
static getFunctionContent(fn) {
|
||||
return fn.toString().replace(/^function\s?\(\)\s?\{\n?|\n?\}$/g, "").replace(/^\t/mg, "");
|
||||
return fn.toString().replace(FUNCTION_CONTENT_REGEX, "").replace(INDENT_MULTILINE_REGEX, "");
|
||||
}
|
||||
|
||||
static toIdentifier(str) {
|
||||
if(typeof str !== "string") return "";
|
||||
return str.replace(/^[^a-zA-Z$_]/, "_").replace(/[^a-zA-Z0-9$_]/g, "_");
|
||||
return str.replace(IDENTIFIER_NAME_REPLACE_REGEX, "_").replace(IDENTIFIER_ALPHA_NUMERIC_NAME_REPLACE_REGEX, "_");
|
||||
}
|
||||
|
||||
static toPath(str) {
|
||||
if(typeof str !== "string") return "";
|
||||
return str.replace(/[^a-zA-Z0-9_!§$()=\-^°]+/g, "-").replace(/^-|-$/, "");
|
||||
return str.replace(PATH_NAME_NORMALIZE_REPLACE_REGEX, "-").replace(MATCH_PADDED_HYPHENS_REPLACE_REGEX, "");
|
||||
}
|
||||
|
||||
// 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 {
|
|||
} 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);
|
||||
});
|
||||
allModules
|
||||
.sort(stringifyIdSortPredicate)
|
||||
.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;
|
||||
}
|
||||
};
|
||||
|
||||
function stringifyIdSortPredicate(a, b) {
|
||||
var aId = a.id + "";
|
||||
var bId = b.id + "";
|
||||
if(aId < bId) return -1;
|
||||
if(aId > bId) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
function moduleIdIsNumber(module) {
|
||||
return typeof module.id === "number";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,16 +2,16 @@
|
|||
|
||||
const should = require("should");
|
||||
|
||||
const template = require("../lib/Template");
|
||||
const Template = require("../lib/Template");
|
||||
|
||||
describe("Template", () => {
|
||||
it("should generate valid identifiers", () =>
|
||||
template.toIdentifier("0abc-def9").should.equal("_abc_def9"));
|
||||
Template.toIdentifier("0abc-def9").should.equal("_abc_def9"));
|
||||
it("should generate valid number identifiers", () => {
|
||||
const items = [];
|
||||
let item;
|
||||
for(let i = 0; i < 80; i += 1) {
|
||||
item = template.numberToIdentifer(i);
|
||||
item = Template.numberToIdentifer(i);
|
||||
if(item === "") {
|
||||
throw new Error("empty number identifier");
|
||||
} else if(items.indexOf(item) > -1) {
|
||||
|
|
@ -21,4 +21,7 @@ describe("Template", () => {
|
|||
}
|
||||
}
|
||||
});
|
||||
it("should generate sanitized path identifiers", () => {
|
||||
Template.toPath("path/to-sdfas/sadfome$$.js").should.equal("path-to-sdfas-sadfome$$-js");
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue