webpack-typescript-builder
Version:
Webpack config builder for typescript, styles (css and sass), fonts and images.
83 lines (82 loc) • 3.46 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = __importDefault(require("chalk"));
const path = __importStar(require("path"));
function printResults(err, multiStats, printAssets = true) {
process.stdout.write("\n");
if (printAssets) {
printFiles(multiStats.stats[0], "Client");
printFiles(multiStats.stats[1], "Server");
}
printErrors(multiStats.stats[1]);
const warnings = printWarnings(multiStats.stats[1]);
printSummary(multiStats.stats[1], warnings, "Build");
process.stdout.write("\n");
}
exports.printResults = printResults;
function printErrors(stats) {
const issues = stats.compilation.errors;
for (const issue of issues) {
process.stdout.write(chalk_1.default.red(issue.message));
process.stdout.write("\n");
}
}
function printWarnings(stats) {
const issues = stats.compilation.warnings;
let warningCount = 0;
for (const issue of issues) {
if (!issue.warning) {
continue;
}
const allWarnings = issue.warning.toString();
const warnings = allWarnings
.replace("Error: ", chalk_1.default.grey(""))
.replace(/\[(\d+), (\d+)\]: (.+)/g, (_substring, start, end, message) => {
warningCount++;
const location = chalk_1.default.bold.cyanBright(`${issue.module.resource}(${start},${end})`);
const entry = chalk_1.default.grey("[wtb]");
return chalk_1.default.yellow(`${entry} WARNING in ${location}\n ${message}`);
});
process.stdout.write(warnings);
}
return warningCount;
}
function printFiles(stats, name) {
const out = stats.compilation.outputOptions.path;
const target = stats.compilation.outputOptions.libraryTarget;
process.stdout.write(`${name} (${target}):\n`);
const assets = Object.keys(stats.compilation.assets).filter(_ => !_.endsWith("d.ts"));
for (const asset of assets) {
const webpackAsset = stats.compilation.assets[asset];
const size = webpackAsset.size
? webpackAsset.size()
: webpackAsset.children
? webpackAsset.children[0]._value.length
: webpackAsset._value.length;
const strSize = (size / 1024).toFixed(2);
process.stdout.write(` ${chalk_1.default.green.italic(path.join(out, asset))} (${strSize} Kb)\n`);
}
process.stdout.write("\n");
}
function printSummary(stats, warningsCount, name) {
process.stdout.write(`========== ${chalk_1.default.bold(name)}: `);
printColoredStats(warningsCount, "Warning", chalk_1.default.yellow);
process.stdout.write(", ");
printColoredStats(stats.compilation.errors.length, "Error");
process.stdout.write(" ========== \n");
}
function printColoredStats(stat, name, errorChulk = chalk_1.default.red) {
const type = stat > 0 ? errorChulk : chalk_1.default.green;
process.stdout.write(type(`${stat} ${name}`));
process.stdout.write(type(stat === 1 ? "" : "s"));
}