Skip to content

Commit

Permalink
Merge pull request #7819 from webpack/refactor/template_es6
Browse files Browse the repository at this point in the history
Convert Template to ES6
  • Loading branch information
sokra committed Jul 30, 2018
2 parents 482ff20 + 3992ba3 commit 61ce434
Showing 1 changed file with 44 additions and 43 deletions.
87 changes: 44 additions & 43 deletions lib/Template.js
Expand Up @@ -27,7 +27,7 @@ const MATCH_PADDED_HYPHENS_REPLACE_REGEX = /^-|-$/g;
/**
* @typedef {Object} HasId
* @property {number | string} id
* */
*/

/**
* @typedef {function(Module, number): boolean} ModuleFilterPredicate
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand All @@ -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 &&
Expand All @@ -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, {
Expand All @@ -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;
}
Expand Down

0 comments on commit 61ce434

Please sign in to comment.