cbst 1.0.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -16
- package/dist/CacheBuster.d.ts +4 -4
- package/dist/CacheBuster.js +36 -23
- package/package.json +7 -6
package/README.md
CHANGED
|
@@ -31,37 +31,37 @@ This diff demonstrates how versioning is applied to a simple website:
|
|
|
31
31
|
<html>
|
|
32
32
|
<head>
|
|
33
33
|
<title>Hello, World!</title>
|
|
34
|
-
- <link rel="stylesheet" href="/
|
|
35
|
-
+ <link rel="stylesheet" href="/
|
|
34
|
+
- <link rel="stylesheet" href="/css/main.css">
|
|
35
|
+
+ <link rel="stylesheet" href="/css/main.v-ed1e118152.css">
|
|
36
36
|
</head>
|
|
37
37
|
<body>
|
|
38
38
|
<h1>Hello, World!</h1>
|
|
39
39
|
<p>
|
|
40
|
-
- <img id="welcome" src="/
|
|
41
|
-
+ <img id="welcome" src="/
|
|
40
|
+
- <img id="welcome" src="/img/loading.svg">
|
|
41
|
+
+ <img id="welcome" src="/img/loading.v-834ab3df39.svg">
|
|
42
42
|
</p>
|
|
43
43
|
<script type="module">
|
|
44
|
-
- import { main } from './
|
|
45
|
-
+ import { main } from './
|
|
44
|
+
- import { main } from './js/main.js';
|
|
45
|
+
+ import { main } from './js/main.v-f39e95e656.js';
|
|
46
46
|
|
|
47
47
|
setTimeout(main, 1000);
|
|
48
48
|
</script>
|
|
49
49
|
</body>
|
|
50
50
|
</html>
|
|
51
51
|
|
|
52
|
-
# /
|
|
52
|
+
# /js/main.js -> /js/main.v-f39e95e656.js
|
|
53
53
|
|
|
54
54
|
- import { hello } from './hello.js';
|
|
55
|
-
+ import { hello } from './hello.93724d33b5.js';
|
|
55
|
+
+ import { hello } from './hello.v-93724d33b5.js';
|
|
56
56
|
|
|
57
57
|
export function main() {
|
|
58
58
|
hello('world');
|
|
59
|
-
- document.getElementById('welcome').src = '/
|
|
60
|
-
+ document.getElementById('welcome').src = '/
|
|
59
|
+
- document.getElementById('welcome').src = '/img/welcome.svg';
|
|
60
|
+
+ document.getElementById('welcome').src = '/img/welcome.v-afe45bb832.svg';
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
# /
|
|
64
|
-
# /
|
|
63
|
+
# /css/main.css -> /css/main.v-ed1e118152.css
|
|
64
|
+
# /js/hello.js -> /js/hello.v-93724d33b5.js
|
|
65
65
|
# etc.
|
|
66
66
|
```
|
|
67
67
|
|
|
@@ -84,8 +84,8 @@ The configuration file (JSON) supports the following properties:
|
|
|
84
84
|
- References found in matching source files will be still be rewritten.
|
|
85
85
|
- Defaults to `["*.html"]`.
|
|
86
86
|
- `metadata`
|
|
87
|
-
- Metadata filename.
|
|
88
|
-
-
|
|
87
|
+
- Metadata filename (JSON).
|
|
88
|
+
- If omitted, no metadata file is written.
|
|
89
89
|
- `hashLength`
|
|
90
90
|
- Length of hashes in versioned filenames.
|
|
91
91
|
- Defaults to `10`.
|
|
@@ -121,6 +121,10 @@ browsers and proxies to cache them indefinitely. For example:
|
|
|
121
121
|
Cache-Control: public, max-age=31536000, immutable
|
|
122
122
|
```
|
|
123
123
|
|
|
124
|
+
Version tags generated by `cbst` are prefixed with `v-`. You may configure your
|
|
125
|
+
web server to deliver files containing `.v-` in their name with the above
|
|
126
|
+
immutable cache control header.
|
|
127
|
+
|
|
124
128
|
**Dynamic files** (those listed in `dynamic`) will not be versioned, but any
|
|
125
129
|
references detected inside these files will be rewritten if necessary. For
|
|
126
130
|
example, `*.html` files are usually be marked dynamic because they are directly
|
|
@@ -143,13 +147,13 @@ See also
|
|
|
143
147
|
|
|
144
148
|
## Discussion
|
|
145
149
|
|
|
146
|
-
> I can't use my favorite libraries from
|
|
150
|
+
> I can't use my favorite libraries from npm with this because they require
|
|
147
151
|
> Node.js resolution and/or bundling.
|
|
148
152
|
|
|
149
153
|
Chicken and egg problem here. The industry got used to bundlers and started
|
|
150
154
|
building libraries for the web assuming that everyone used a bundler.
|
|
151
155
|
|
|
152
|
-
Ask library authors to ship standard
|
|
156
|
+
Ask library authors to ship standard ES modules. That will make all our lives
|
|
153
157
|
easier.
|
|
154
158
|
|
|
155
159
|
> Isn't bundling still more performant for larger websites?
|
package/dist/CacheBuster.d.ts
CHANGED
|
@@ -24,7 +24,7 @@ export interface CacheBusterConfig {
|
|
|
24
24
|
dynamic?: string[];
|
|
25
25
|
/**
|
|
26
26
|
* Metadata filename.
|
|
27
|
-
*
|
|
27
|
+
* If omitted, no metadata file is written.
|
|
28
28
|
*/
|
|
29
29
|
metadata?: string;
|
|
30
30
|
/**
|
|
@@ -65,9 +65,9 @@ export declare class CacheBuster extends EventEmitter {
|
|
|
65
65
|
protected extensionRx: RegExp;
|
|
66
66
|
constructor(options: CacheBusterOptions);
|
|
67
67
|
run(): Promise<void>;
|
|
68
|
-
writeMetadata(): Promise<void>;
|
|
69
|
-
handleDir(dir: string): Promise<
|
|
70
|
-
handleFile(file: string): Promise<
|
|
68
|
+
writeMetadata(file: string): Promise<void>;
|
|
69
|
+
handleDir(dir: string): Promise<void>;
|
|
70
|
+
handleFile(file: string): Promise<void>;
|
|
71
71
|
transformFile(file: string): Promise<string | Buffer>;
|
|
72
72
|
hashFile(file: string): Promise<string | undefined>;
|
|
73
73
|
hashFileReferences(file: string, exclude: string[]): Promise<string[]>;
|
package/dist/CacheBuster.js
CHANGED
|
@@ -11,7 +11,7 @@ export class CacheBuster extends EventEmitter {
|
|
|
11
11
|
source: ['*.html', '*.css', '*.js', '*.svg', '*.json'],
|
|
12
12
|
html: ['*.html'],
|
|
13
13
|
dynamic: ['*.html'],
|
|
14
|
-
metadata: '
|
|
14
|
+
metadata: '',
|
|
15
15
|
hashLength: 10,
|
|
16
16
|
};
|
|
17
17
|
inputDir;
|
|
@@ -29,7 +29,7 @@ export class CacheBuster extends EventEmitter {
|
|
|
29
29
|
map = new Map();
|
|
30
30
|
unresolved = new Map();
|
|
31
31
|
referenceRx = /((")(([^"\s]+)\.([a-zA-Z0-9]+))")|((')(([^'\s]+)\.([a-zA-Z0-9]+))')|# sourceMappingURL=(([^\s]+)\.([a-zA-Z0-9]+))/g;
|
|
32
|
-
externalReferenceRx = /^([a-zA-Z][a-zA-Z0-9
|
|
32
|
+
externalReferenceRx = /^([a-zA-Z][a-zA-Z0-9+.-]*:|\/\/)/;
|
|
33
33
|
baseRx = /<base[^<>]+href\s*=\s*"([^"]+)"[^<>]*>/i;
|
|
34
34
|
extensionRx = /(?<=\.)[^.]+$/;
|
|
35
35
|
constructor(options) {
|
|
@@ -46,14 +46,17 @@ export class CacheBuster extends EventEmitter {
|
|
|
46
46
|
}
|
|
47
47
|
async run() {
|
|
48
48
|
await this.handleDir('.');
|
|
49
|
-
|
|
49
|
+
if (this.metadata) {
|
|
50
|
+
await this.writeMetadata(this.metadata);
|
|
51
|
+
}
|
|
50
52
|
}
|
|
51
|
-
async writeMetadata() {
|
|
52
|
-
await this.writeFile(
|
|
53
|
+
async writeMetadata(file) {
|
|
54
|
+
await this.writeFile(file, JSON.stringify({ map: Object.fromEntries(this.map.entries()) }));
|
|
53
55
|
}
|
|
54
56
|
async handleDir(dir) {
|
|
55
|
-
if (this.excludeGlob.match(dir))
|
|
56
|
-
return
|
|
57
|
+
if (this.excludeGlob.match(dir)) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
57
60
|
const entries = await this.readDir(dir);
|
|
58
61
|
entries.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
59
62
|
for (const entry of entries) {
|
|
@@ -66,8 +69,9 @@ export class CacheBuster extends EventEmitter {
|
|
|
66
69
|
}
|
|
67
70
|
}
|
|
68
71
|
async handleFile(file) {
|
|
69
|
-
if (this.excludeGlob.match(file))
|
|
70
|
-
return
|
|
72
|
+
if (this.excludeGlob.match(file)) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
71
75
|
const output = await this.transformFile(file);
|
|
72
76
|
const hash = await this.hashFileCache.get(file);
|
|
73
77
|
const outputFile = hash
|
|
@@ -96,28 +100,32 @@ export class CacheBuster extends EventEmitter {
|
|
|
96
100
|
const reference = groups[2] || groups[7] || groups[10];
|
|
97
101
|
const filename = groups[3] || groups[8] || groups[11];
|
|
98
102
|
const extension = groups[4] || groups[9] || groups[12];
|
|
99
|
-
if (!this.isResolvableReference(reference))
|
|
103
|
+
if (!this.isResolvableReference(reference)) {
|
|
100
104
|
return match;
|
|
105
|
+
}
|
|
101
106
|
const hash = hashes[i++];
|
|
102
107
|
if (!hash)
|
|
103
108
|
return match;
|
|
109
|
+
const version = this.formatVersion(hash);
|
|
104
110
|
if (groups[10]) {
|
|
105
|
-
return `# sourceMappingURL=${filename}.${
|
|
111
|
+
return `# sourceMappingURL=${filename}.${version}.${extension}`;
|
|
106
112
|
}
|
|
107
|
-
return `${quote}${filename}.${
|
|
113
|
+
return `${quote}${filename}.${version}.${extension}${quote}`;
|
|
108
114
|
});
|
|
109
115
|
}
|
|
110
116
|
async hashFile(file) {
|
|
111
|
-
if (this.dynamicGlob.match(file))
|
|
117
|
+
if (this.dynamicGlob.match(file)) {
|
|
112
118
|
return undefined;
|
|
119
|
+
}
|
|
113
120
|
return this.hash([
|
|
114
121
|
await this.hashRawFileCache.get(file),
|
|
115
122
|
...(await this.hashFileReferences(file, [])),
|
|
116
123
|
]);
|
|
117
124
|
}
|
|
118
125
|
async hashFileReferences(file, exclude) {
|
|
119
|
-
if (exclude.includes(file))
|
|
126
|
+
if (exclude.includes(file)) {
|
|
120
127
|
return [];
|
|
128
|
+
}
|
|
121
129
|
const references = await this.getReferencesCache.get(file);
|
|
122
130
|
const direct = await Promise.all(references.map((it) => this.hashRawFileCache.get(it).catch((err) => {
|
|
123
131
|
this.handleReferenceError(err, file, it);
|
|
@@ -139,15 +147,17 @@ export class CacheBuster extends EventEmitter {
|
|
|
139
147
|
unresolved = new Set();
|
|
140
148
|
this.unresolved.set(source, unresolved);
|
|
141
149
|
}
|
|
142
|
-
if (unresolved.has(reference))
|
|
150
|
+
if (unresolved.has(reference)) {
|
|
143
151
|
return;
|
|
152
|
+
}
|
|
144
153
|
unresolved.add(reference);
|
|
145
154
|
this.emit('error', new ReferenceError(error, source, reference));
|
|
146
155
|
}
|
|
147
|
-
//
|
|
156
|
+
// References
|
|
148
157
|
async getReferences(file) {
|
|
149
|
-
if (!this.sourceGlob.match(file))
|
|
158
|
+
if (!this.sourceGlob.match(file)) {
|
|
150
159
|
return [];
|
|
160
|
+
}
|
|
151
161
|
const buffer = await this.readFileCache.get(file);
|
|
152
162
|
const source = buffer.toString();
|
|
153
163
|
const base = await this.getBase(file);
|
|
@@ -167,8 +177,9 @@ export class CacheBuster extends EventEmitter {
|
|
|
167
177
|
return !!reference.match(this.externalReferenceRx);
|
|
168
178
|
}
|
|
169
179
|
resolveReference(base, reference) {
|
|
170
|
-
if (reference[0] === '/')
|
|
180
|
+
if (reference[0] === '/') {
|
|
171
181
|
return reference.slice(1);
|
|
182
|
+
}
|
|
172
183
|
return path.join(base[0] === '/' ? base.slice(1) : base, reference);
|
|
173
184
|
}
|
|
174
185
|
async getBase(file) {
|
|
@@ -176,22 +187,24 @@ export class CacheBuster extends EventEmitter {
|
|
|
176
187
|
const buffer = await this.readFileCache.get(file);
|
|
177
188
|
const source = buffer.toString();
|
|
178
189
|
const match = source.match(this.baseRx);
|
|
179
|
-
if (match)
|
|
190
|
+
if (match) {
|
|
180
191
|
return match[1];
|
|
192
|
+
}
|
|
181
193
|
}
|
|
182
194
|
return path.dirname(file);
|
|
183
195
|
}
|
|
184
|
-
//
|
|
196
|
+
// Hashing
|
|
185
197
|
formatVersion(hash) {
|
|
186
|
-
return hash.slice(0, this.hashLength)
|
|
198
|
+
return `v-${hash.slice(0, this.hashLength)}`;
|
|
187
199
|
}
|
|
188
200
|
hash(inputs) {
|
|
189
201
|
const h = createHash('sha1');
|
|
190
|
-
for (const input of inputs)
|
|
202
|
+
for (const input of inputs) {
|
|
191
203
|
h.update(input);
|
|
204
|
+
}
|
|
192
205
|
return h.digest('hex');
|
|
193
206
|
}
|
|
194
|
-
//
|
|
207
|
+
// I/O
|
|
195
208
|
async readFile(file) {
|
|
196
209
|
return fs.readFile(this.safePath(this.inputDir, file));
|
|
197
210
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cbst",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "A cache buster that versions a website's files for immutable cache control",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"versioning",
|
|
@@ -30,19 +30,20 @@
|
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
32
|
"build": "tsc --project tsconfig.build.json",
|
|
33
|
-
"clean": "rm -rf coverage dist test/fixtures/out",
|
|
33
|
+
"clean": "rm -rf coverage dist test/fixtures/all/out test/fixtures/example/out",
|
|
34
34
|
"lint": "eslint .",
|
|
35
35
|
"format": "prettier --write .",
|
|
36
36
|
"format-check": "prettier --check .",
|
|
37
37
|
"test": "node --import tsx --test test/*.test.ts test/*/*.test.ts"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
+
"@eslint/js": "^9.9.1",
|
|
41
|
+
"@types/eslint__js": "^8.42.3",
|
|
40
42
|
"@types/node": "^20.14.8",
|
|
41
|
-
"
|
|
42
|
-
"@typescript-eslint/parser": "^7.13.1",
|
|
43
|
-
"eslint": "^8.15.0",
|
|
43
|
+
"eslint": "^9.9.1",
|
|
44
44
|
"prettier": "^3.3.2",
|
|
45
45
|
"tsx": "^4.16.2",
|
|
46
|
-
"typescript": "^5.0.4"
|
|
46
|
+
"typescript": "^5.0.4",
|
|
47
|
+
"typescript-eslint": "^8.3.0"
|
|
47
48
|
}
|
|
48
49
|
}
|