-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Expand file tree
/
Copy pathtemplate-common.js
More file actions
114 lines (103 loc) · 3.19 KB
/
Copy pathtemplate-common.js
File metadata and controls
114 lines (103 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const fs = require("fs");
const path = require("path");
/**
* @param {string} regExpStr reg exp string
* @returns {string} less strict string regexp
*/
function lessStrict(regExpStr) {
regExpStr = regExpStr
.replace(/node_modules/g, "(node_modules|~)")
.replace(/\\\/|\\\\/g, "[\\/\\\\]");
return regExpStr;
}
const runtimeModulesRegexp =
/(\/\*{72}\/\n(?:\/(?:\*{6}|\*{72})\/.*\n)*\/\*{72}\/\n)/g;
const timeRegexp = / in \d+ ms/g;
const dataUrlRegexp = /("data:[^"]+")/g;
/**
* @param {string} template template
* @param {string} prefix prefix
* @returns {boolean} true when need results, otherwise false
*/
const needResults = (template, prefix) => {
const regExp = prefix ? new RegExp(`_\\{\\{${prefix}:`) : /_\{\{/;
return regExp.test(template);
};
/**
* @param {string} template template
* @returns {string} template with base replacements
*/
const replaceBase = (template) => {
const cwd = process.cwd();
let webpack = path.join(__dirname, "..");
let webpackParent = path.join(__dirname, "..", "..");
const cwdRegExpStr = lessStrict(
cwd.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")
);
const cwdRegExp = new RegExp(cwdRegExpStr, "g");
const cwdSlashRegExp = new RegExp(`${cwdRegExpStr}[\\/\\\\]`, "g");
webpack = lessStrict(webpack.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"));
webpackParent = lessStrict(
webpackParent.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")
);
const webpackRegExp = new RegExp(webpack, "g");
const webpackParentRegExp = new RegExp(webpackParent, "g");
return template
.replace(/\r\n/g, "\n")
.replace(/\r/g, "\n")
.replace(/\[webpack-cli\].+\n/g, "")
.replace(cwdSlashRegExp, "./")
.replace(cwdRegExp, ".")
.replace(webpackRegExp, "(webpack)")
.replace(webpackParentRegExp, "(webpack)/~")
.replace(timeRegexp, "")
.replace(dataUrlRegexp, (match) => {
if (match.length < 100) return match;
return `${match.slice(0, 50)}...${match.slice(-10)}`;
})
.replace(/\.chunkhash\./g, ".[chunkhash].")
.replace(
runtimeModulesRegexp,
/**
* @param {string} match match
* @param {string} content content
* @returns {string} new content
*/
(match, content) =>
"```\n\n<details><summary>" +
"<code>/* webpack runtime code */</code>" +
`</summary>\n\n\`\`\` js\n${content}\`\`\`\n\n</details>\n\n\`\`\` js\n`
);
};
/**
* @param {string} template template
* @param {string} baseDir base dir
* @param {string} stdout stdout
* @param {string} prefix prefix
* @returns {string} template with replacements
*/
const replaceResults = (template, baseDir, stdout, prefix) => {
const regexp = new RegExp(
`_\\{\\{${prefix ? `${prefix}:` : ""}([^:\\}]+)\\}\\}_`,
"g"
);
return template.replace(regexp, (match) => {
match = match.slice(3 + (prefix ? prefix.length + 1 : 0), -3);
if (match === "stdout") return stdout;
try {
return fs
.readFileSync(path.join(baseDir, match), "utf8")
.replace(/[\r\n]*$/, "");
} catch (err) {
/** @type {Error} */
(err).message += `\nwhile reading '${match}' in '${baseDir}`;
throw err;
}
});
};
module.exports = { needResults, replaceBase, replaceResults };