Skip to content

Commit 756bf2f

Browse files
committed
Add filename extension to 404 warning message
1 parent fff981a commit 756bf2f

File tree

3 files changed

+40
-26
lines changed

3 files changed

+40
-26
lines changed

spec/fixtures/target/manifest.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,14 @@
141141
"references": null,
142142
"skipped": false,
143143
"missing": [
144-
"<img src=graphics/missing-asset1.png alt=bogus>",
145-
"<img src=graphics/missing-asset2.jpg alt=bogus class=extraneous-whitespace>"
144+
{
145+
"ext": ".png",
146+
"line": "<img src=graphics/missing-asset1.png alt=bogus>"
147+
},
148+
{
149+
"ext": ".jpg",
150+
"line": "<img src=graphics/missing-asset2.jpg alt=bogus class=extraneous-whitespace>"
151+
}
146152
]
147153
},
148154
{
@@ -229,7 +235,10 @@
229235
"references": 4,
230236
"skipped": false,
231237
"missing": [
232-
"url(wrong-folder/mock.jpg)"
238+
{
239+
"ext": ".jpg",
240+
"line": "url(wrong-folder/mock.jpg)"
241+
}
233242
]
234243
},
235244
{

src/rev-web-assets.ts

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,26 @@ export type Settings = {
1717
saveManifest: boolean, //output the list of files to manifest.json in the target folder
1818
skip: string | null, //do not revision (hash) asset files with paths containing given string.
1919
};
20+
export type MissingAsset = {
21+
ext: string, //filename extension of the asset file not found
22+
line: string, //html line referencing the asset file not found
23+
};
2024
export type ManifestDetail = {
21-
origin: string, //source path of asset file
22-
filename: string, //source filename of asset file
23-
canonical: string, //normalized path used to lookup asset in manifest
24-
canonicalFolder: string, //directory of the normalized path of the asset file
25-
isHtml: boolean, //asset file is HTML
26-
isCss: boolean, //asset file is CSS
27-
bytes: number | null, //asset file size
28-
hash: string | null, //eight-digit cache busting hex humber that changes if the asset changes
29-
hashedFilename: string | null, //filename of the asset with hash inserted before the file extension
30-
destFolder: string, //directory of the target asset
31-
destPath: string | null, //folder and filename of the target asset
32-
usedIn: string[] | null, //files that references the asset
33-
references: number | null, //number of times the asset is referenced
34-
skipped: boolean, //asset file is configured to not be hashed
35-
missing: string[] | null, //html lines of asset files referenced but not found
25+
origin: string, //source path of asset file
26+
filename: string, //source filename of asset file
27+
canonical: string, //normalized path used to lookup asset in manifest
28+
canonicalFolder: string, //directory of the normalized path of the asset file
29+
isHtml: boolean, //asset file is HTML
30+
isCss: boolean, //asset file is CSS
31+
bytes: number | null, //asset file size
32+
hash: string | null, //eight-digit cache busting hex humber that changes if the asset changes
33+
hashedFilename: string | null, //filename of the asset with hash inserted before the file extension
34+
destFolder: string, //directory of the target asset
35+
destPath: string | null, //folder and filename of the target asset
36+
usedIn: string[] | null, //files that references the asset
37+
references: number | null, //number of times the asset is referenced
38+
skipped: boolean, //asset file is configured to not be hashed
39+
missing: MissingAsset[] | null, //asset files referenced but not found (404)
3640
};
3741
export type Manifest = ManifestDetail[]; //list of assets
3842
export type Results = {
@@ -122,6 +126,7 @@ const revWebAssets = {
122126
const replacer = (matched: string, pre: string, url: string, post: string): string => {
123127
// Example matched broken into 3 parts:
124128
// '<img src=logo.png alt=Logo>' ==> '<img src=', 'logo.png', ' alt=Logo>'
129+
const line = matched.replace(/\s+/g, ' ');
125130
const uri = url.replace(/[#?].*/, ''); //strip off trailing query string and hash fragment
126131
const ext = path.extname(uri);
127132
const doNotHash = uri.includes(':') || webPages.includes(ext) || ext.length < 2;
@@ -137,7 +142,7 @@ const revWebAssets = {
137142
if (assetDetail && !assetDetail.usedIn!.includes(detail.canonical))
138143
assetDetail.usedIn!.push(detail.canonical);
139144
if (!doNotHash && !skipAsset && !assetDetail)
140-
detail.missing!.push(matched.replace(/\s+/g, ' '));
145+
detail.missing!.push({ ext, line });
141146
const trailingSlashes = /\/*$/;
142147
const metaContentBase = settings.metaContentBase?.replace(trailingSlashes, '/');
143148
const absoluteUrl = () =>
@@ -259,15 +264,15 @@ const revWebAssets = {
259264
const arrow = { big: chalk.gray.bold(' ⟹ '), little: chalk.gray.bold('→') };
260265
const infoColor = results.count ? chalk.white : chalk.red.bold;
261266
const info = infoColor(`(files: ${results.count}, ${results.duration}ms)`);
262-
const warning = chalk.red.bold('missing asset in');
263267
log(name, source, arrow.big, target, info);
264268
const logDetail = (detail: ManifestDetail) => {
265-
const origin = chalk.white(detail.origin.substring(results.source.length + 1));
266-
const dest = chalk.green(detail.destPath!.substring(results.target.length + 1));
267-
const file = chalk.blue.bold(detail.origin);
269+
const origin = chalk.white(detail.origin.substring(results.source.length + 1));
270+
const dest = chalk.green(detail.destPath!.substring(results.target.length + 1));
271+
const file = chalk.blue.bold(detail.origin);
272+
const warning = (ext: string) => chalk.red.bold(`missing ${ext} asset in`);
268273
log(name, origin, arrow.little, dest);
269-
const logMissingAsset = (assetLine: string) =>
270-
log(name, warning, file, arrow.little, chalk.green(assetLine));
274+
const logMissingAsset = (missing: MissingAsset) =>
275+
log(name, warning(missing.ext), file, arrow.little, chalk.green(missing.line));
271276
if (!settings.hide404s && detail.missing)
272277
detail.missing.forEach(logMissingAsset);
273278
};

task-runner.sh.command

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ buildProject() {
8787

8888
devNote() {
8989
echo "To test out local code changes:"
90-
echo " $ npm run pretest && node bin/cli.js spec/fixtures/source spec/fixtures/target"
90+
echo " $ npm run pretest && node bin/cli.js spec/fixtures/source spec/fixtures/target --manifest"
9191
echo
9292
}
9393

0 commit comments

Comments
 (0)