-
-
Notifications
You must be signed in to change notification settings - Fork 217
/
index.js
153 lines (121 loc) · 3.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import {Buffer} from 'node:buffer';
import path from 'node:path';
import {vinylFile} from 'vinyl-file';
import revHash from 'rev-hash';
import {revPath} from 'rev-path';
import sortKeys from 'sort-keys';
import modifyFilename from 'modify-filename';
import Vinyl from 'vinyl';
import {gulpPlugin} from 'gulp-plugin-extras';
function relativePath(base, filePath) {
filePath = filePath.replaceAll('\\', '/');
base = base.replaceAll('\\', '/');
if (!filePath.startsWith(base)) {
return filePath;
}
const newPath = filePath.slice(base.length);
if (newPath[0] === '/') {
return newPath.slice(1);
}
return newPath;
}
function transformFilename(file) {
// Save the old path for later
file.revOrigPath = file.path;
file.revOrigBase = file.base;
file.revHash = revHash(file.contents);
file.path = modifyFilename(file.path, (filename, extension) => {
const extIndex = filename.lastIndexOf('.');
filename = extIndex === -1
? revPath(filename, file.revHash)
: revPath(filename.slice(0, extIndex), file.revHash) + filename.slice(extIndex);
return filename + extension;
});
}
const getManifestFile = async options => {
try {
return await vinylFile(options.path, options);
} catch (error) {
if (error.code === 'ENOENT') {
return new Vinyl(options);
}
throw error;
}
};
export default function gulpRev() {
const sourcemaps = [];
const pathMap = {};
return gulpPlugin('gulp-rev', file => {
// This is a sourcemap, hold until the end.
if (path.extname(file.path) === '.map') {
sourcemaps.push(file);
return;
}
const oldPath = file.path;
transformFilename(file);
pathMap[oldPath] = file.revHash;
return file;
}, {
async * onFinish() {
for (const file of sourcemaps) {
let reverseFilename;
// Attempt to parse the sourcemap's JSON to get the reverse filename
try {
reverseFilename = JSON.parse(file.contents.toString()).file;
} catch {}
if (!reverseFilename) {
reverseFilename = path.relative(path.dirname(file.path), path.basename(file.path, '.map'));
}
if (pathMap[reverseFilename]) {
// Save the old path for later
file.revOrigPath = file.path;
file.revOrigBase = file.base;
const hash = pathMap[reverseFilename];
file.path = revPath(file.path.replace(/\.map$/, ''), hash) + '.map';
} else {
transformFilename(file);
}
yield file;
}
},
});
}
gulpRev.manifest = (path_, options) => {
if (typeof path_ === 'string') {
path_ = {path: path_};
}
options = {
path: 'rev-manifest.json',
merge: false,
transformer: JSON,
...options,
...path_,
};
let manifest = {};
return gulpPlugin('gulp-rev', file => {
// Ignore all non-rev'd files
if (!file.path || !file.revOrigPath) {
return;
}
const revisionedFile = relativePath(path.resolve(file.cwd, file.base), path.resolve(file.cwd, file.path));
const originalFile = path.join(path.dirname(revisionedFile), path.basename(file.revOrigPath)).replaceAll('\\', '/');
manifest[originalFile] = revisionedFile;
}, {
async * onFinish() {
// No need to write a manifest file if there's nothing to manifest
if (Object.keys(manifest).length === 0) {
return;
}
const manifestFile = await getManifestFile(options);
if (options.merge && !manifestFile.isNull()) {
let oldManifest = {};
try {
oldManifest = options.transformer.parse(manifestFile.contents.toString());
} catch {}
manifest = Object.assign(oldManifest, manifest);
}
manifestFile.contents = Buffer.from(options.transformer.stringify(sortKeys(manifest), undefined, ' '));
yield manifestFile;
},
});
};