2012-10-09 06:12:10 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2017-10-06 07:03:25 +08:00
|
|
|
"use strict";
|
2017-04-04 16:48:59 +08:00
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
2012-04-05 20:59:01 +08:00
|
|
|
|
2013-03-18 21:53:50 +08:00
|
|
|
function lessStrict(regExpStr) {
|
|
|
|
regExpStr = regExpStr
|
|
|
|
.replace(/node_modules/g, "(node_modules|~)")
|
2017-12-14 17:09:09 +08:00
|
|
|
.replace(/(\\\/|\\\\)/g, "[\\/\\\\]");
|
2013-03-18 21:53:50 +08:00
|
|
|
return regExpStr;
|
|
|
|
}
|
|
|
|
|
2019-11-19 21:10:28 +08:00
|
|
|
const runtimeModulesRegexp = /(\/\*{72}\/\n(?:\/(?:\*{6}|\*{72})\/.*\n)*\/\*{72}\/\n)/g;
|
2020-09-08 00:02:14 +08:00
|
|
|
const timeRegexp = / in \d+ ms/g;
|
2019-11-19 21:24:22 +08:00
|
|
|
const dataUrlRegexp = /("data:[^"]+")/g;
|
2016-09-07 17:46:30 +08:00
|
|
|
|
2017-12-14 17:09:09 +08:00
|
|
|
exports.replaceBase = (template) => {
|
2012-04-05 20:59:01 +08:00
|
|
|
|
2019-01-25 20:14:47 +08:00
|
|
|
const cwd = process.cwd();
|
2017-04-04 16:48:59 +08:00
|
|
|
let webpack = path.join(__dirname, "..");
|
|
|
|
let webpackParent = path.join(__dirname, "..", "..");
|
2019-01-25 20:14:47 +08:00
|
|
|
const cwdRegExpStr = lessStrict(cwd.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"));
|
|
|
|
const cwdRegExp = new RegExp(cwdRegExpStr, "g");
|
|
|
|
const cwdSlashRegExp = new RegExp(cwdRegExpStr + "[\\/\\\\]", "g");
|
2013-03-18 21:53:50 +08:00
|
|
|
webpack = lessStrict(webpack.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"));
|
|
|
|
webpack = new RegExp(webpack, "g");
|
|
|
|
webpackParent = lessStrict(webpackParent.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"));
|
|
|
|
webpackParent = new RegExp(webpackParent, "g");
|
2016-12-14 19:03:24 +08:00
|
|
|
|
2017-04-04 16:48:59 +08:00
|
|
|
return template
|
2016-09-07 17:46:30 +08:00
|
|
|
.replace(/\r\n/g, "\n")
|
2017-04-04 16:48:59 +08:00
|
|
|
.replace(/\r/g, "\n")
|
2020-12-11 17:29:32 +08:00
|
|
|
.replace(/\[webpack-cli\].+\n/g, "")
|
2019-01-25 20:14:47 +08:00
|
|
|
.replace(cwdSlashRegExp, "./")
|
|
|
|
.replace(cwdRegExp, ".")
|
2016-09-07 17:46:30 +08:00
|
|
|
.replace(webpack, "(webpack)")
|
|
|
|
.replace(webpackParent, "(webpack)/~")
|
2016-12-14 19:03:24 +08:00
|
|
|
.replace(timeRegexp, "")
|
2019-11-19 21:24:22 +08:00
|
|
|
.replace(dataUrlRegexp, function(match) {
|
|
|
|
if(match.length < 100) return match;
|
|
|
|
return match.slice(0, 50) + "..." + match.slice(-10);
|
|
|
|
})
|
2017-04-04 16:48:59 +08:00
|
|
|
.replace(/\.chunkhash\./g, ".[chunkhash].")
|
2018-12-19 21:05:17 +08:00
|
|
|
.replace(runtimeModulesRegexp, function(match, content) {
|
|
|
|
return "```\n\n<details><summary>"+
|
2019-11-19 21:10:28 +08:00
|
|
|
"<code>/* webpack runtime code */</code>"+
|
|
|
|
"</summary>\n\n``` js\n" + content + "```\n\n</details>\n\n``` js\n";
|
2016-09-07 17:46:30 +08:00
|
|
|
});
|
2017-12-14 17:09:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.needResults = (template, prefix) => {
|
2019-04-05 05:14:54 +08:00
|
|
|
const regExp = prefix ? new RegExp(`_\\{\\{${prefix}:`) : /_\{\{/;
|
2017-12-14 17:09:09 +08:00
|
|
|
return regExp.test(template);
|
|
|
|
};
|
2017-04-04 16:48:59 +08:00
|
|
|
|
2017-12-14 17:09:09 +08:00
|
|
|
exports.replaceResults = (template, baseDir, stdout, prefix) => {
|
2019-04-05 05:14:54 +08:00
|
|
|
const regexp = new RegExp("_\\{\\{" + (prefix ? prefix + ":" : "") + "([^:\\}]+)\\}\\}_", "g");
|
2017-04-04 16:48:59 +08:00
|
|
|
|
|
|
|
return template.replace(regexp, function(match) {
|
2022-03-14 05:54:18 +08:00
|
|
|
match = match.slice(3 + (prefix ? prefix.length + 1 : 0), -3);
|
2017-04-04 16:48:59 +08:00
|
|
|
if(match === "stdout")
|
|
|
|
return stdout;
|
2020-12-11 17:29:32 +08:00
|
|
|
try {
|
|
|
|
return fs.readFileSync(path.join(baseDir, match), "utf-8").replace(/[\r\n]*$/, "");
|
|
|
|
} catch(e) {
|
|
|
|
e.message += `\nwhile reading '${match}' in '${baseDir}`;
|
|
|
|
throw e;
|
|
|
|
}
|
2017-04-04 16:48:59 +08:00
|
|
|
});
|
2017-12-14 17:09:09 +08:00
|
|
|
};
|