limit data url module name in stats printer

This commit is contained in:
Ivan Kopeykin 2021-12-01 00:26:17 +03:00
parent f07a3b8232
commit fb4cb893e5
6 changed files with 92 additions and 10 deletions

View File

@ -9,6 +9,8 @@
/** @typedef {import("./StatsPrinter")} StatsPrinter */
/** @typedef {import("./StatsPrinter").StatsPrinterContext} StatsPrinterContext */
const DATA_URI_CONTENT_LENGTH = 16;
const plural = (n, singular, plural) => (n === 1 ? singular : plural);
/**
@ -26,6 +28,23 @@ const printSizes = (sizes, { formatSize = n => `${n}` }) => {
}
};
const getResourceName = resource => {
const dataUri = /^data:[^,]+,/.exec(resource);
if (!dataUri) return resource;
const len = dataUri[0].length + DATA_URI_CONTENT_LENGTH;
if (resource.length < len) return resource;
return `${resource.slice(
0,
Math.min(resource.length - /* '..'.length */ 2, len)
)}..`;
};
const getModuleName = name => {
const [, prefix, resource] = /^(.*!)?([^!]*)$/.exec(name);
return [prefix, getResourceName(resource)];
};
const mapLines = (str, fn) => str.split("\n").map(fn).join("\n");
/**
@ -282,8 +301,8 @@ const SIMPLE_PRINTERS = {
"module.id": (id, { formatModuleId }) =>
isValidId(id) ? formatModuleId(id) : undefined,
"module.name": (name, { bold }) => {
const [, prefix, resource] = /^(.*!)?([^!]*)$/.exec(name);
return (prefix || "") + bold(resource);
const [prefix, resource] = getModuleName(name);
return `${prefix || ""}${bold(resource || "")}`;
},
"module.identifier": identifier => undefined,
"module.layer": (layer, { formatLayer }) =>
@ -388,7 +407,8 @@ const SIMPLE_PRINTERS = {
"moduleIssuer.profile.total": (value, { formatTime }) => formatTime(value),
"moduleReason.type": type => type,
"moduleReason.userRequest": (userRequest, { cyan }) => cyan(userRequest),
"moduleReason.userRequest": (userRequest, { cyan }) =>
cyan(getResourceName(userRequest)),
"moduleReason.moduleId": (moduleId, { formatModuleId }) =>
isValidId(moduleId) ? formatModuleId(moduleId) : undefined,
"moduleReason.module": (module, { magenta }) => magenta(module),

View File

@ -123,17 +123,54 @@ chunk (runtime: main) c5861419d7f3f6ea6c19.js 899 bytes [rendered]
webpack x.x.x compiled successfully in X ms"
`;
exports[`StatsTestCases should print correct stats for all-stats 1`] = `
"PublicPath: auto
asset bundle.js 3.47 KiB {main} [emitted] (name: main)
Entrypoint main 3.47 KiB = bundle.js
chunk {main} (runtime: main) bundle.js (main) 154 bytes (javascript) 274 bytes (runtime) [entry] [rendered]
> ./index.js main
./index.js 82 bytes {main} [depth 0] [built] [code generated]
[no exports]
[used exports unknown]
entry ./index.js main
data:text/plain;base64,szsaAAdsadasdfaf.. 72.2 bytes {main} [depth 1] [dependent] [built] [code generated]
[no exports]
[used exports unknown]
harmony side effect evaluation data:text/plain;base64,szsaAAdsadasdfaf.. [./index.js] 1:0-81
webpack/runtime/make namespace object 274 bytes {main} [code generated]
[no exports]
[used exports unknown]
./index.js 82 bytes {main} [depth 0] [built] [code generated]
[no exports]
[used exports unknown]
entry ./index.js main
data:text/plain;base64,szsaAAdsadasdfaf.. 72.2 bytes {main} [depth 1] [built] [code generated]
[no exports]
[used exports unknown]
harmony side effect evaluation data:text/plain;base64,szsaAAdsadasdfaf.. [./index.js] 1:0-81
webpack/runtime/make namespace object 274 bytes {main} [code generated]
[no exports]
[used exports unknown]
1970-04-20 12:42:42: webpack x.x.x compiled successfully in X ms (d0d97703a88fdf5418be)"
`;
exports[`StatsTestCases should print correct stats for asset 1`] = `
"asset 89a353e9c515885abd8e.png 14.6 KiB [emitted] [immutable] [from: images/file.png] (auxiliary name: main)
asset bundle.js 12.4 KiB [emitted] (name: main)
asset bundle.js 13.1 KiB [emitted] (name: main)
asset static/file.html 12 bytes [emitted] [from: static/file.html] (auxiliary name: main)
runtime modules 1.06 KiB 2 modules
asset modules 8.9 KiB (javascript) 14.6 KiB (asset)
./images/file.png 42 bytes (javascript) 14.6 KiB (asset) [built] [code generated]
./images/file.svg 915 bytes [built] [code generated]
./images/file.jpg 7.92 KiB [built] [code generated]
./static/file.html 42 bytes (javascript) 12 bytes (asset) [built] [code generated]
./index.js 150 bytes [built] [code generated]
asset modules 9.07 KiB (javascript) 14.6 KiB (asset)
modules by path ./ 8.9 KiB (javascript) 14.6 KiB (asset)
./images/file.png 42 bytes (javascript) 14.6 KiB (asset) [built] [code generated]
./images/file.svg 915 bytes [built] [code generated]
./images/file.jpg 7.92 KiB [built] [code generated]
./static/file.html 42 bytes (javascript) 12 bytes (asset) [built] [code generated]
modules by path data:text/ 172 bytes
data:text/plain;base64,szsaAAdsadasdfaf.. 72.2 bytes [built] [code generated]
data:text/plain,asd= 41.4 bytes [built] [code generated]
data:text/plain,XXXXXXXXXXXXXXX.. 58.8 bytes [built] [code generated]
./index.js 339 bytes [built] [code generated]
webpack x.x.x compiled successfully in X ms"
`;

View File

@ -0,0 +1 @@
import text1 from "data:text/plain;base64,szsaAAdsadasdfafasfasAADas123aasdasd=="

View File

@ -0,0 +1,17 @@
/** @type {import("../../../").Configuration} */
module.exports = {
mode: "development",
entry: "./index.js",
output: {
filename: "bundle.js"
},
module: {
rules: [
{
mimetype: "text/plain",
type: "asset"
}
]
},
stats: { all: true }
};

View File

@ -2,3 +2,6 @@ import png from "./images/file.png";
import svg from "./images/file.svg";
import jpg from "./images/file.jpg";
import html from "./static/file.html";
import text1 from "data:text/plain;base64,szsaAAdsadasdfafasfasAADas123aasdasd=="
import text2 from "data:text/plain,asd="
import text3 from "data:text/plain,XXXXXXXXXXXXXXXXX" // 17 chars

View File

@ -14,6 +14,10 @@ module.exports = {
generator: {
filename: "static/[name][ext]"
}
},
{
mimetype: "text/plain",
type: "asset"
}
]
},