Skip to content

Commit 86aa18d

Browse files
committed
refactor(template): add type support for Template base class
1 parent e7c8fa4 commit 86aa18d

1 file changed

Lines changed: 89 additions & 5 deletions

File tree

‎lib/Template.js‎

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
"use strict";
5+
/** @typedef {typeof import("./Module")} Module */
6+
/** @typedef {typeof import("./Chunk")} Chunk */
7+
/** @typedef {typeof import("./ModuleTemplate")} ModuleTemplate */
8+
/** @typedef {import("webpack-sources").ConcatSource} ConcatSource */
69

710
const { ConcatSource } = require("webpack-sources");
811

@@ -18,6 +21,20 @@ const COMMENT_END_REGEX = /\*\//g;
1821
const PATH_NAME_NORMALIZE_REPLACE_REGEX = /[^a-zA-Z0-9_!§$()=\-^°]+/g;
1922
const MATCH_PADDED_HYPHENS_REPLACE_REGEX = /^-|-$/g;
2023

24+
/**
25+
* @typedef {Object} HasId
26+
* @property {number} id
27+
* */
28+
29+
/**
30+
* @typedef {(m: Module, idx: number) => boolean} ModuleFilterPredicate
31+
*/
32+
33+
/**
34+
* @param {HasId} a first id object to be sorted
35+
* @param {HasId} b second id object to be sorted against
36+
* @return {-1|0|1} the sort value
37+
*/
2138
const stringifyIdSortPredicate = (a, b) => {
2239
var aId = a.id + "";
2340
var bId = b.id + "";
@@ -26,36 +43,63 @@ const stringifyIdSortPredicate = (a, b) => {
2643
return 0;
2744
};
2845

46+
//TODO: Change type to Module when import works
47+
//https://github.com/Microsoft/TypeScript/issues/23375
48+
/**
49+
* @param {HasId} module the module to compare against
50+
* @return {boolean} return true if module.id is equal to type "number"
51+
*/
2952
const moduleIdIsNumber = module => {
3053
return typeof module.id === "number";
3154
};
3255

33-
module.exports = class Template {
56+
class Template {
57+
/**
58+
*
59+
* @param {Function} fn - a runtime function (.runtime.js) "template"
60+
* @return {string} the updated and normalized function string
61+
*/
3462
static getFunctionContent(fn) {
3563
return fn
3664
.toString()
3765
.replace(FUNCTION_CONTENT_REGEX, "")
3866
.replace(INDENT_MULTILINE_REGEX, "")
3967
.replace(LINE_SEPARATOR_REGEX, "\n");
4068
}
41-
69+
/**
70+
* @param {string} str the string converted to identifier
71+
* @return {string} created identifier
72+
*/
4273
static toIdentifier(str) {
4374
if (typeof str !== "string") return "";
4475
return str
4576
.replace(IDENTIFIER_NAME_REPLACE_REGEX, "_$1")
4677
.replace(IDENTIFIER_ALPHA_NUMERIC_NAME_REPLACE_REGEX, "_");
4778
}
48-
79+
/**
80+
*
81+
* @param {string} str string to be converted to commented in bundle code
82+
* @return {string} returns a commented version of string
83+
*/
4984
static toComment(str) {
5085
if (!str) return "";
5186
return `/*! ${str.replace(COMMENT_END_REGEX, "* /")} */`;
5287
}
5388

89+
/**
90+
*
91+
* @param {string} str string to be converted to "normal comment"
92+
* @return {string} returns a commented version of string
93+
*/
5494
static toNormalComment(str) {
5595
if (!str) return "";
5696
return `/* ${str.replace(COMMENT_END_REGEX, "* /")} */`;
5797
}
5898

99+
/**
100+
* @param {string} str string path to be normalized
101+
* @return {string} normalized bundle-safe path
102+
*/
59103
static toPath(str) {
60104
if (typeof str !== "string") return "";
61105
return str
@@ -64,6 +108,11 @@ module.exports = class Template {
64108
}
65109

66110
// map number to a single character a-z, A-Z or <_ + number> if number is too big
111+
/**
112+
*
113+
* @param {number} n number to convert to ident
114+
* @return {string} returns single character ident
115+
*/
67116
static numberToIdentifer(n) {
68117
// lower case
69118
if (n < DELTA_A_TO_Z)
@@ -81,6 +130,11 @@ module.exports = class Template {
81130
);
82131
}
83132

133+
/**
134+
*
135+
* @param {string} str string to convert to identity
136+
* @return {string} converted identity
137+
*/
84138
static indent(str) {
85139
if (Array.isArray(str)) {
86140
return str.map(Template.indent).join("\n");
@@ -92,6 +146,12 @@ module.exports = class Template {
92146
}
93147
}
94148

149+
/**
150+
*
151+
* @param {string|string[]} str string to create prefix for
152+
* @param {string} prefix prefix to compose
153+
* @return {string} returns new prefix string
154+
*/
95155
static prefix(str, prefix) {
96156
if (Array.isArray(str)) {
97157
str = str.join("\n");
@@ -102,13 +162,24 @@ module.exports = class Template {
102162
return ind + str.replace(/\n([^\n])/g, "\n" + prefix + "$1");
103163
}
104164

165+
/**
166+
*
167+
* @param {string|string[]} str string or string collection
168+
* @return {string} returns a single string from array
169+
*/
105170
static asString(str) {
106171
if (Array.isArray(str)) {
107172
return str.join("\n");
108173
}
109174
return str;
110175
}
111176

177+
/**
178+
*
179+
* @param {HasId[]} modules - a collection of modules to get array bounds for
180+
* @return {[number, number] | false} returns the upper and lower array bounds
181+
* or false if not every module has a number based id
182+
*/
112183
static getModulesArrayBounds(modules) {
113184
if (!modules.every(moduleIdIsNumber)) return false;
114185
var maxId = -Infinity;
@@ -133,6 +204,17 @@ module.exports = class Template {
133204
return arrayOverhead < objectOverhead ? [minId, maxId] : false;
134205
}
135206

207+
//TODO: replace any for Chunk and ModuleTemplate types instead.
208+
//https://github.com/Microsoft/TypeScript/issues/23375
209+
/**
210+
*
211+
* @param {any} chunk chunk whose modules will be rendered
212+
* @param {ModuleFilterPredicate} filterFn function used to filter modules from chunk to render
213+
* @param {any} moduleTemplate ModuleTemplate instance used to render modules
214+
* @param {any | any[]} dependencyTemplates templates needed for each module to render dependencies
215+
* @param {string} prefix applying prefix strings
216+
* @return {ConcatSource} rendered chunk modules in a Source object
217+
*/
136218
static renderChunkModules(
137219
chunk,
138220
filterFn,
@@ -202,4 +284,6 @@ module.exports = class Template {
202284
}
203285
return source;
204286
}
205-
};
287+
}
288+
289+
module.exports = Template;

0 commit comments

Comments
 (0)