Skip to content

Commit 6c761fa

Browse files
committed
Release v1.1.0
#1
1 parent f7a9470 commit 6c761fa

File tree

12 files changed

+63
-21
lines changed

12 files changed

+63
-21
lines changed

dist/rev-web-assets.d.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
//! rev-web-assets v1.0.0 ~~ https://github.com/center-key/rev-web-assets ~~ MIT License
1+
//! rev-web-assets v1.1.0 ~~ https://github.com/center-key/rev-web-assets ~~ MIT License
22

33
export type Settings = {
44
cd: string | null;
5+
force: boolean;
56
metaContentBase: string | null;
67
saveManifest: boolean;
78
};
@@ -17,6 +18,8 @@ export type ManifestDetail = {
1718
hashedFilename: string | null;
1819
destFolder: string;
1920
destPath: string | null;
21+
usedIn: string[] | null;
22+
references: number | null;
2023
};
2124
export type Manifest = ManifestDetail[];
2225
export type Results = {
@@ -28,24 +31,13 @@ export type Results = {
2831
};
2932
declare const revWebAssets: {
3033
readFolderRecursive(folder: string): string[];
31-
manifest(source: string, target: string): {
32-
origin: string;
33-
filename: string;
34-
canonicalFolder: string;
35-
canonical: string;
36-
isHtml: boolean;
37-
isCss: boolean;
38-
hash: null;
39-
hashedFilename: null;
40-
destFolder: string;
41-
destPath: null;
42-
}[];
34+
manifest(source: string, target: string): ManifestDetail[];
4335
hashFilename(filename: string, hash: string | null): string;
44-
calcAssetHash(detail: ManifestDetail): void;
45-
hashAssetPath(manifest: Manifest, detail: ManifestDetail, settings: Settings): (matched: string, pre: string, uri: string, post: string) => string;
36+
calcAssetHash(detail: ManifestDetail): ManifestDetail;
37+
hashAssetPath(manifest: ManifestDetail[], detail: ManifestDetail, settings: Settings): (matched: string, pre: string, uri: string, post: string) => string;
4638
processHtml(manifest: ManifestDetail[], settings: Settings): void;
4739
processCss(manifest: ManifestDetail[], settings: Settings): void;
48-
copyAssets(manifest: Manifest): void;
40+
copyAssets(manifest: ManifestDetail[]): void;
4941
revision(sourceFolder: string, targetFolder: string, options?: Options): Results;
5042
};
5143
export { revWebAssets };

dist/rev-web-assets.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! rev-web-assets v1.0.0 ~~ https://github.com/center-key/rev-web-assets ~~ MIT License
1+
//! rev-web-assets v1.1.0 ~~ https://github.com/center-key/rev-web-assets ~~ MIT License
22

33
import crypto from 'crypto';
44
import fs from 'fs';
@@ -36,6 +36,8 @@ const revWebAssets = {
3636
hashedFilename: null,
3737
destFolder: destFolder,
3838
destPath: null,
39+
usedIn: isHtml ? null : [],
40+
references: isHtml ? null : 0,
3941
};
4042
};
4143
const manifest = files.map(process);
@@ -51,16 +53,21 @@ const revWebAssets = {
5153
const hash = crypto.createHash('md5').update(contents).digest('hex');
5254
detail.hash = hash.substring(0, hashLen);
5355
detail.hashedFilename = revWebAssets.hashFilename(detail.filename, detail.hash);
56+
return detail;
5457
},
5558
hashAssetPath(manifest, detail, settings) {
56-
return (matched, pre, uri, post) => {
59+
const replacer = (matched, pre, uri, post) => {
5760
const ext = path.extname(uri);
5861
const doNotHash = uri.includes(':') || ['.html', '.htm', '.php'].includes(ext) || ext.length < 2;
5962
const canonicalPath = detail.canonicalFolder ? detail.canonicalFolder + '/' : '';
6063
const canonical = slash(path.normalize(canonicalPath + uri));
6164
const assetDetail = doNotHash ? null : manifest.find(detail => detail.canonical === canonical);
6265
if (assetDetail && !assetDetail.hash)
6366
revWebAssets.calcAssetHash(assetDetail);
67+
if (assetDetail)
68+
assetDetail.references++;
69+
if (assetDetail && !assetDetail.usedIn.includes(detail.canonical))
70+
assetDetail.usedIn.push(detail.canonical);
6471
const hashedUri = () => {
6572
const hashed = revWebAssets.hashFilename(uri, assetDetail.hash);
6673
const noBase = !settings.metaContentBase || !pre.startsWith('<meta');
@@ -69,6 +76,7 @@ const revWebAssets = {
6976
};
7077
return (assetDetail === null || assetDetail === void 0 ? void 0 : assetDetail.hash) ? pre + hashedUri() + post : matched;
7178
};
79+
return replacer;
7280
},
7381
processHtml(manifest, settings) {
7482
const hrefPattern = /(<[a-z]{1,4}\s.*href=['"]?)([^"'>\s]*)(['"]?[^<]*>)/ig;
@@ -111,6 +119,7 @@ const revWebAssets = {
111119
revision(sourceFolder, targetFolder, options) {
112120
const defaults = {
113121
cd: null,
122+
force: false,
114123
metaContentBase: null,
115124
saveManifest: false,
116125
};
@@ -134,7 +143,11 @@ const revWebAssets = {
134143
const manifest = revWebAssets.manifest(source, target);
135144
revWebAssets.processHtml(manifest, settings);
136145
revWebAssets.processCss(manifest, settings);
146+
const hashUnusedAsset = (detail) => !detail.hash && !detail.isHtml && revWebAssets.calcAssetHash(detail);
147+
if (settings.force)
148+
manifest.forEach(hashUnusedAsset);
137149
revWebAssets.copyAssets(manifest);
150+
manifest.forEach(detail => detail.usedIn && detail.usedIn.sort());
138151
const manifestPath = path.join(target, 'manifest.json');
139152
if (settings.saveManifest)
140153
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, ' ') + '\n');

dist/rev-web-assets.umd.cjs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! rev-web-assets v1.0.0 ~~ https://github.com/center-key/rev-web-assets ~~ MIT License
1+
//! rev-web-assets v1.1.0 ~~ https://github.com/center-key/rev-web-assets ~~ MIT License
22

33
var __importDefault = (this && this.__importDefault) || function (mod) {
44
return (mod && mod.__esModule) ? mod : { "default": mod };
@@ -51,6 +51,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5151
hashedFilename: null,
5252
destFolder: destFolder,
5353
destPath: null,
54+
usedIn: isHtml ? null : [],
55+
references: isHtml ? null : 0,
5456
};
5557
};
5658
const manifest = files.map(process);
@@ -66,16 +68,21 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
6668
const hash = crypto_1.default.createHash('md5').update(contents).digest('hex');
6769
detail.hash = hash.substring(0, hashLen);
6870
detail.hashedFilename = revWebAssets.hashFilename(detail.filename, detail.hash);
71+
return detail;
6972
},
7073
hashAssetPath(manifest, detail, settings) {
71-
return (matched, pre, uri, post) => {
74+
const replacer = (matched, pre, uri, post) => {
7275
const ext = path_1.default.extname(uri);
7376
const doNotHash = uri.includes(':') || ['.html', '.htm', '.php'].includes(ext) || ext.length < 2;
7477
const canonicalPath = detail.canonicalFolder ? detail.canonicalFolder + '/' : '';
7578
const canonical = (0, slash_1.default)(path_1.default.normalize(canonicalPath + uri));
7679
const assetDetail = doNotHash ? null : manifest.find(detail => detail.canonical === canonical);
7780
if (assetDetail && !assetDetail.hash)
7881
revWebAssets.calcAssetHash(assetDetail);
82+
if (assetDetail)
83+
assetDetail.references++;
84+
if (assetDetail && !assetDetail.usedIn.includes(detail.canonical))
85+
assetDetail.usedIn.push(detail.canonical);
7986
const hashedUri = () => {
8087
const hashed = revWebAssets.hashFilename(uri, assetDetail.hash);
8188
const noBase = !settings.metaContentBase || !pre.startsWith('<meta');
@@ -84,6 +91,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
8491
};
8592
return (assetDetail === null || assetDetail === void 0 ? void 0 : assetDetail.hash) ? pre + hashedUri() + post : matched;
8693
};
94+
return replacer;
8795
},
8896
processHtml(manifest, settings) {
8997
const hrefPattern = /(<[a-z]{1,4}\s.*href=['"]?)([^"'>\s]*)(['"]?[^<]*>)/ig;
@@ -126,6 +134,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
126134
revision(sourceFolder, targetFolder, options) {
127135
const defaults = {
128136
cd: null,
137+
force: false,
129138
metaContentBase: null,
130139
saveManifest: false,
131140
};
@@ -149,7 +158,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
149158
const manifest = revWebAssets.manifest(source, target);
150159
revWebAssets.processHtml(manifest, settings);
151160
revWebAssets.processCss(manifest, settings);
161+
const hashUnusedAsset = (detail) => !detail.hash && !detail.isHtml && revWebAssets.calcAssetHash(detail);
162+
if (settings.force)
163+
manifest.forEach(hashUnusedAsset);
152164
revWebAssets.copyAssets(manifest);
165+
manifest.forEach(detail => detail.usedIn && detail.usedIn.sort());
153166
const manifestPath = path_1.default.join(target, 'manifest.json');
154167
if (settings.saveManifest)
155168
fs_1.default.writeFileSync(manifestPath, JSON.stringify(manifest, null, ' ') + '\n');

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rev-web-assets",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "Revision web asset filenames with cache busting content hash fingerprints",
55
"license": "MIT",
66
"type": "module",

spec/fixtures/source/mock1.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,8 @@ <h3>Images</h3>
3737
<p>Cover 2A</p>
3838
<p>Cover 2B</p>
3939
</article>
40+
<footer>
41+
<a href=https://github.com/center-key/rev-web-assets>github.com/center-key/rev-web-assets</a>
42+
</footer>
4043
</body>
4144
</html>

spec/fixtures/source/mock1.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,8 @@
3737
<p>Cover 2A</p>
3838
<p>Cover 2B</p>
3939
</article>
40+
<footer>
41+
<a href=https://github.com/center-key/rev-web-assets>github.com/center-key/rev-web-assets</a>
42+
</footer>
4043
</body>
4144
</html>

spec/fixtures/source/subfolder/mock2.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,8 @@ <h3>Images</h3>
3737
<p>Cover 2A</p>
3838
<p>Cover 2B</p>
3939
</article>
40+
<footer>
41+
<a href=https://github.com/center-key/rev-web-assets>github.com/center-key/rev-web-assets</a>
42+
</footer>
4043
</body>
4144
</html>

spec/fixtures/source/subfolder/mock2.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,8 @@
3737
<p>Cover 2A</p>
3838
<p>Cover 2B</p>
3939
</article>
40+
<footer>
41+
<a href=https://github.com/center-key/rev-web-assets>github.com/center-key/rev-web-assets</a>
42+
</footer>
4043
</body>
4144
</html>

spec/fixtures/target/mock1.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,8 @@ <h3>Images</h3>
3737
<p>Cover 2A</p>
3838
<p>Cover 2B</p>
3939
</article>
40+
<footer>
41+
<a href=https://github.com/center-key/rev-web-assets>github.com/center-key/rev-web-assets</a>
42+
</footer>
4043
</body>
4144
</html>

spec/fixtures/target/mock1.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,8 @@
3737
<p>Cover 2A</p>
3838
<p>Cover 2B</p>
3939
</article>
40+
<footer>
41+
<a href=https://github.com/center-key/rev-web-assets>github.com/center-key/rev-web-assets</a>
42+
</footer>
4043
</body>
4144
</html>

0 commit comments

Comments
 (0)