1
1
import {Buffer} from 'node:buffer';
2
2
import path from 'node:path';
3
- import transformStream from 'easy-transform-stream';
4
3
import {vinylFile} from 'vinyl-file';
5
4
import revHash from 'rev-hash';
6
5
import {revPath} from 'rev-path';
7
6
import sortKeys from 'sort-keys';
8
7
import modifyFilename from 'modify-filename';
9
8
import Vinyl from 'vinyl';
10
- import PluginError from 'plugin-error ';
9
+ import {gulpPlugin} from 'gulp- plugin-extras ';
11
10
12
11
function relativePath(base, filePath) {
13
- filePath = filePath.replace(/\\/g , '/');
14
- base = base.replace(/\\/g , '/');
12
+ filePath = filePath.replaceAll('\\' , '/');
13
+ base = base.replaceAll('\\' , '/');
15
14
16
15
if (!filePath.startsWith(base)) {
17
16
return filePath;
@@ -55,20 +54,12 @@ const getManifestFile = async options => {
55
54
}
56
55
};
57
56
58
- const plugin = () => {
57
+ export default function gulpRev() {
59
58
const sourcemaps = [];
60
59
const pathMap = {};
61
60
62
- return transformStream({objectMode: true}, file => {
63
- if (file.isNull()) {
64
- return file;
65
- }
66
-
67
- if (file.isStream()) {
68
- throw new PluginError('gulp-rev', 'Streaming not supported');
69
- }
70
-
71
- // This is a sourcemap, hold until the end
61
+ return gulpPlugin('gulp-rev', file => {
62
+ // This is a sourcemap, hold until the end.
72
63
if (path.extname(file.path) === '.map') {
73
64
sourcemaps.push(file);
74
65
return;
@@ -79,40 +70,38 @@ const plugin = () => {
79
70
pathMap[oldPath] = file.revHash;
80
71
81
72
return file;
82
- }, () => {
83
- const files = [];
84
-
85
- for (const file of sourcemaps) {
86
- let reverseFilename;
87
-
88
- // Attempt to parse the sourcemap's JSON to get the reverse filename
89
- try {
90
- reverseFilename = JSON.parse(file.contents.toString()).file;
91
- } catch {}
92
-
93
- if (!reverseFilename) {
94
- reverseFilename = path.relative(path.dirname(file.path), path.basename(file.path, '.map'));
73
+ }, {
74
+ async * onFinish() {
75
+ for (const file of sourcemaps) {
76
+ let reverseFilename;
77
+
78
+ // Attempt to parse the sourcemap's JSON to get the reverse filename
79
+ try {
80
+ reverseFilename = JSON.parse(file.contents.toString()).file;
81
+ } catch {}
82
+
83
+ if (!reverseFilename) {
84
+ reverseFilename = path.relative(path.dirname(file.path), path.basename(file.path, '.map'));
85
+ }
86
+
87
+ if (pathMap[reverseFilename]) {
88
+ // Save the old path for later
89
+ file.revOrigPath = file.path;
90
+ file.revOrigBase = file.base;
91
+
92
+ const hash = pathMap[reverseFilename];
93
+ file.path = revPath(file.path.replace(/\.map$/, ''), hash) + '.map';
94
+ } else {
95
+ transformFilename(file);
96
+ }
97
+
98
+ yield file;
95
99
}
96
-
97
- if (pathMap[reverseFilename]) {
98
- // Save the old path for later
99
- file.revOrigPath = file.path;
100
- file.revOrigBase = file.base;
101
-
102
- const hash = pathMap[reverseFilename];
103
- file.path = revPath(file.path.replace(/\.map$/, ''), hash) + '.map';
104
- } else {
105
- transformFilename(file);
106
- }
107
-
108
- files.push(file);
109
- }
110
-
111
- return files;
100
+ },
112
101
});
113
- };
102
+ }
114
103
115
- plugin .manifest = (path_, options) => {
104
+ gulpRev .manifest = (path_, options) => {
116
105
if (typeof path_ === 'string') {
117
106
path_ = {path: path_};
118
107
}
@@ -127,38 +116,38 @@ plugin.manifest = (path_, options) => {
127
116
128
117
let manifest = {};
129
118
130
- return transformStream({objectMode: true} , file => {
119
+ return gulpPlugin('gulp-rev' , file => {
131
120
// Ignore all non-rev'd files
132
121
if (!file.path || !file.revOrigPath) {
133
122
return;
134
123
}
135
124
136
125
const revisionedFile = relativePath(path.resolve(file.cwd, file.base), path.resolve(file.cwd, file.path));
137
- const originalFile = path.join(path.dirname(revisionedFile), path.basename(file.revOrigPath)).replace(/\\/g , '/');
126
+ const originalFile = path.join(path.dirname(revisionedFile), path.basename(file.revOrigPath)).replaceAll('\\' , '/');
138
127
139
128
manifest[originalFile] = revisionedFile;
140
- }, async function * () {
141
- // No need to write a manifest file if there's nothing to manifest
142
- if (Object.keys(manifest).length === 0) {
143
- return;
144
- }
129
+ }, {
130
+ async * onFinish() {
131
+ // No need to write a manifest file if there's nothing to manifest
132
+ if (Object.keys(manifest).length === 0) {
133
+ return;
134
+ }
145
135
146
- const manifestFile = await getManifestFile(options);
136
+ const manifestFile = await getManifestFile(options);
147
137
148
- if (options.merge && !manifestFile.isNull()) {
149
- let oldManifest = {};
138
+ if (options.merge && !manifestFile.isNull()) {
139
+ let oldManifest = {};
150
140
151
- try {
152
- oldManifest = options.transformer.parse(manifestFile.contents.toString());
153
- } catch {}
141
+ try {
142
+ oldManifest = options.transformer.parse(manifestFile.contents.toString());
143
+ } catch {}
154
144
155
- manifest = Object.assign(oldManifest, manifest);
156
- }
145
+ manifest = Object.assign(oldManifest, manifest);
146
+ }
157
147
158
- manifestFile.contents = Buffer.from(options.transformer.stringify(sortKeys(manifest), undefined, ' '));
148
+ manifestFile.contents = Buffer.from(options.transformer.stringify(sortKeys(manifest), undefined, ' '));
159
149
160
- yield manifestFile;
150
+ yield manifestFile;
151
+ },
161
152
});
162
153
};
163
-
164
- export default plugin;
0 commit comments