mirror of https://github.com/webpack/webpack.git
Merge pull request #7819 from webpack/refactor/template_es6
Convert Template to ES6
This commit is contained in:
commit
61ce43443c
|
|
@ -27,7 +27,7 @@ const MATCH_PADDED_HYPHENS_REPLACE_REGEX = /^-|-$/g;
|
|||
/**
|
||||
* @typedef {Object} HasId
|
||||
* @property {number | string} id
|
||||
* */
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {function(Module, number): boolean} ModuleFilterPredicate
|
||||
|
|
@ -39,8 +39,8 @@ const MATCH_PADDED_HYPHENS_REPLACE_REGEX = /^-|-$/g;
|
|||
* @returns {-1|0|1} the sort value
|
||||
*/
|
||||
const stringifyIdSortPredicate = (a, b) => {
|
||||
var aId = a.id + "";
|
||||
var bId = b.id + "";
|
||||
const aId = a.id + "";
|
||||
const bId = b.id + "";
|
||||
if (aId < bId) return -1;
|
||||
if (aId > bId) return 1;
|
||||
return 0;
|
||||
|
|
@ -59,6 +59,7 @@ class Template {
|
|||
.replace(INDENT_MULTILINE_REGEX, "")
|
||||
.replace(LINE_SEPARATOR_REGEX, "\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} str the string converted to identifier
|
||||
* @returns {string} created identifier
|
||||
|
|
@ -128,31 +129,28 @@ class Template {
|
|||
|
||||
/**
|
||||
*
|
||||
* @param {string | string[]} str string to convert to identity
|
||||
* @param {string | string[]} s string to convert to identity
|
||||
* @returns {string} converted identity
|
||||
*/
|
||||
static indent(str) {
|
||||
if (Array.isArray(str)) {
|
||||
return str.map(Template.indent).join("\n");
|
||||
static indent(s) {
|
||||
if (Array.isArray(s)) {
|
||||
return s.map(Template.indent).join("\n");
|
||||
} else {
|
||||
str = str.trimRight();
|
||||
const str = s.trimRight();
|
||||
if (!str) return "";
|
||||
var ind = str[0] === "\n" ? "" : "\t";
|
||||
const ind = str[0] === "\n" ? "" : "\t";
|
||||
return ind + str.replace(/\n([^\n])/g, "\n\t$1");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string|string[]} str string to create prefix for
|
||||
* @param {string|string[]} s string to create prefix for
|
||||
* @param {string} prefix prefix to compose
|
||||
* @returns {string} returns new prefix string
|
||||
*/
|
||||
static prefix(str, prefix) {
|
||||
if (Array.isArray(str)) {
|
||||
str = str.join("\n");
|
||||
}
|
||||
str = str.trim();
|
||||
static prefix(s, prefix) {
|
||||
const str = Template.asString(s).trim();
|
||||
if (!str) return "";
|
||||
const ind = str[0] === "\n" ? "" : prefix;
|
||||
return ind + str.replace(/\n([^\n])/g, "\n" + prefix + "$1");
|
||||
|
|
@ -181,8 +179,8 @@ class Template {
|
|||
* or false if not every module has a number based id
|
||||
*/
|
||||
static getModulesArrayBounds(modules) {
|
||||
var maxId = -Infinity;
|
||||
var minId = Infinity;
|
||||
let maxId = -Infinity;
|
||||
let minId = Infinity;
|
||||
for (const module of modules) {
|
||||
if (typeof module.id !== "number") return false;
|
||||
if (maxId < module.id) maxId = /** @type {number} */ (module.id);
|
||||
|
|
@ -192,15 +190,11 @@ class Template {
|
|||
// add minId x ',' instead of 'Array(minId).concat(…)'
|
||||
minId = 0;
|
||||
}
|
||||
var objectOverhead = modules
|
||||
.map(module => {
|
||||
var idLength = (module.id + "").length;
|
||||
return idLength + 2;
|
||||
})
|
||||
.reduce((a, b) => {
|
||||
return a + b;
|
||||
}, -1);
|
||||
var arrayOverhead = minId === 0 ? maxId : 16 + ("" + minId).length + maxId;
|
||||
const objectOverhead = modules
|
||||
.map(module => (module.id + "").length + 2)
|
||||
.reduce((a, b) => a + b, -1);
|
||||
const arrayOverhead =
|
||||
minId === 0 ? maxId : 16 + ("" + minId).length + maxId;
|
||||
return arrayOverhead < objectOverhead ? [minId, maxId] : false;
|
||||
}
|
||||
|
||||
|
|
@ -217,13 +211,13 @@ class Template {
|
|||
filterFn,
|
||||
moduleTemplate,
|
||||
dependencyTemplates,
|
||||
prefix
|
||||
prefix = ""
|
||||
) {
|
||||
if (!prefix) prefix = "";
|
||||
var source = new ConcatSource();
|
||||
const source = new ConcatSource();
|
||||
const modules = chunk.getModules().filter(filterFn);
|
||||
let removedModules;
|
||||
if (chunk instanceof HotUpdateChunk) {
|
||||
var removedModules = chunk.removedModules;
|
||||
removedModules = chunk.removedModules;
|
||||
}
|
||||
if (
|
||||
modules.length === 0 &&
|
||||
|
|
@ -233,7 +227,7 @@ class Template {
|
|||
return source;
|
||||
}
|
||||
/** @type {{id: string|number, source: Source|string}[]} */
|
||||
var allModules = modules.map(module => {
|
||||
const allModules = modules.map(module => {
|
||||
return {
|
||||
id: module.id,
|
||||
source: moduleTemplate.render(module, dependencyTemplates, {
|
||||
|
|
@ -249,38 +243,45 @@ class Template {
|
|||
});
|
||||
}
|
||||
}
|
||||
var bounds = Template.getModulesArrayBounds(allModules);
|
||||
|
||||
const bounds = Template.getModulesArrayBounds(allModules);
|
||||
if (bounds) {
|
||||
// Render a spare array
|
||||
var minId = bounds[0];
|
||||
var maxId = bounds[1];
|
||||
if (minId !== 0) source.add("Array(" + minId + ").concat(");
|
||||
const minId = bounds[0];
|
||||
const maxId = bounds[1];
|
||||
if (minId !== 0) {
|
||||
source.add(`Array(${minId}).concat(`);
|
||||
}
|
||||
source.add("[\n");
|
||||
const modules = new Map();
|
||||
for (const module of allModules) {
|
||||
modules.set(module.id, module);
|
||||
}
|
||||
for (var idx = minId; idx <= maxId; idx++) {
|
||||
var module = modules.get(idx);
|
||||
if (idx !== minId) source.add(",\n");
|
||||
source.add("/* " + idx + " */");
|
||||
for (let idx = minId; idx <= maxId; idx++) {
|
||||
const module = modules.get(idx);
|
||||
if (idx !== minId) {
|
||||
source.add(",\n");
|
||||
}
|
||||
source.add(`/* ${idx} */`);
|
||||
if (module) {
|
||||
source.add("\n");
|
||||
source.add(module.source);
|
||||
}
|
||||
}
|
||||
source.add("\n" + prefix + "]");
|
||||
if (minId !== 0) source.add(")");
|
||||
if (minId !== 0) {
|
||||
source.add(")");
|
||||
}
|
||||
} else {
|
||||
// Render an object
|
||||
source.add("{\n");
|
||||
allModules.sort(stringifyIdSortPredicate).forEach((module, idx) => {
|
||||
if (idx !== 0) source.add(",\n");
|
||||
if (idx !== 0) {
|
||||
source.add(",\n");
|
||||
}
|
||||
source.add(`\n/***/ ${JSON.stringify(module.id)}:\n`);
|
||||
source.add(module.source);
|
||||
});
|
||||
source.add("\n\n" + prefix + "}");
|
||||
source.add(`\n\n${prefix}}`);
|
||||
}
|
||||
return source;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue