Skip to content

Commit 0ad07f6

Browse files
committed
Require Node.js 8 and Gulp 4
1 parent 16d7622 commit 0ad07f6

File tree

9 files changed

+102
-106
lines changed

9 files changed

+102
-106
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: node_js
22
node_js:
3+
- '12'
34
- '10'
45
- '8'
5-
- '6'

index.js

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const modifyFilename = require('modify-filename');
99
const Vinyl = require('vinyl');
1010
const PluginError = require('plugin-error');
1111

12-
function relPath(base, filePath) {
12+
function relativePath(base, filePath) {
1313
filePath = filePath.replace(/\\/g, '/');
1414
base = base.replace(/\\/g, '/');
1515

@@ -43,43 +43,47 @@ function transformFilename(file) {
4343
});
4444
}
4545

46-
const getManifestFile = opts => vinylFile.read(opts.path, opts).catch(error => {
47-
if (error.code === 'ENOENT') {
48-
return new Vinyl(opts);
49-
}
46+
const getManifestFile = async options => {
47+
try {
48+
return await vinylFile.read(options.path, options);
49+
} catch (error) {
50+
if (error.code === 'ENOENT') {
51+
return new Vinyl(options);
52+
}
5053

51-
throw error;
52-
});
54+
throw error;
55+
}
56+
};
5357

5458
const plugin = () => {
5559
const sourcemaps = [];
5660
const pathMap = {};
5761

58-
return through.obj((file, enc, cb) => {
62+
return through.obj((file, encoding, callback) => {
5963
if (file.isNull()) {
60-
cb(null, file);
64+
callback(null, file);
6165
return;
6266
}
6367

6468
if (file.isStream()) {
65-
cb(new PluginError('gulp-rev', 'Streaming not supported'));
69+
callback(new PluginError('gulp-rev', 'Streaming not supported'));
6670
return;
6771
}
6872

6973
// This is a sourcemap, hold until the end
7074
if (path.extname(file.path) === '.map') {
7175
sourcemaps.push(file);
72-
cb();
76+
callback();
7377
return;
7478
}
7579

7680
const oldPath = file.path;
7781
transformFilename(file);
7882
pathMap[oldPath] = file.revHash;
7983

80-
cb(null, file);
81-
}, function (cb) {
82-
sourcemaps.forEach(file => {
84+
callback(null, file);
85+
}, function (callback) {
86+
for (const file of sourcemaps) {
8387
let reverseFilename;
8488

8589
// Attempt to parse the sourcemap's JSON to get the reverse filename
@@ -103,60 +107,68 @@ const plugin = () => {
103107
}
104108

105109
this.push(file);
106-
});
110+
}
107111

108-
cb();
112+
callback();
109113
});
110114
};
111115

112-
plugin.manifest = (pth, opts) => {
113-
if (typeof pth === 'string') {
114-
pth = {path: pth};
116+
plugin.manifest = (path_, options) => {
117+
if (typeof path_ === 'string') {
118+
path_ = {path: path_};
115119
}
116120

117-
opts = Object.assign({
121+
options = {
118122
path: 'rev-manifest.json',
119123
merge: false,
120-
transformer: JSON
121-
}, opts, pth);
124+
transformer: JSON,
125+
...options,
126+
...path_
127+
};
122128

123129
let manifest = {};
124130

125-
return through.obj((file, enc, cb) => {
131+
return through.obj((file, encoding, callback) => {
126132
// Ignore all non-rev'd files
127133
if (!file.path || !file.revOrigPath) {
128-
cb();
134+
callback();
129135
return;
130136
}
131137

132-
const revisionedFile = relPath(path.resolve(file.cwd, file.base), path.resolve(file.cwd, file.path));
138+
const revisionedFile = relativePath(path.resolve(file.cwd, file.base), path.resolve(file.cwd, file.path));
133139
const originalFile = path.join(path.dirname(revisionedFile), path.basename(file.revOrigPath)).replace(/\\/g, '/');
134140

135141
manifest[originalFile] = revisionedFile;
136142

137-
cb();
138-
}, function (cb) {
143+
callback();
144+
}, function (callback) {
139145
// No need to write a manifest file if there's nothing to manifest
140146
if (Object.keys(manifest).length === 0) {
141-
cb();
147+
callback();
142148
return;
143149
}
144150

145-
getManifestFile(opts).then(manifestFile => {
146-
if (opts.merge && !manifestFile.isNull()) {
147-
let oldManifest = {};
151+
(async () => {
152+
try {
153+
const manifestFile = await getManifestFile(options);
148154

149-
try {
150-
oldManifest = opts.transformer.parse(manifestFile.contents.toString());
151-
} catch (_) {}
155+
if (options.merge && !manifestFile.isNull()) {
156+
let oldManifest = {};
152157

153-
manifest = Object.assign(oldManifest, manifest);
154-
}
158+
try {
159+
oldManifest = options.transformer.parse(manifestFile.contents.toString());
160+
} catch (_) {}
155161

156-
manifestFile.contents = Buffer.from(opts.transformer.stringify(sortKeys(manifest), null, ' '));
157-
this.push(manifestFile);
158-
cb();
159-
}).catch(cb);
162+
manifest = Object.assign(oldManifest, manifest);
163+
}
164+
165+
manifestFile.contents = Buffer.from(options.transformer.stringify(sortKeys(manifest), undefined, ' '));
166+
this.push(manifestFile);
167+
callback();
168+
} catch (error) {
169+
callback(error);
170+
}
171+
})();
160172
});
161173
};
162174

integration.md

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ For our examples, we'll assume the following `rev-manifest.json`:
1010
"js/lib.js": "js/lib-6d94673e3d.js",
1111
"css/app.css": "css/app-a4ae3dfa4d.css"
1212
}
13-
````
13+
```
1414

1515

1616
## Approach #1 - Generate index.html during build
1717

1818
One approach to working with `rev-manifest.json` is to use a templating language, such as [handlebars](http://handlebarsjs.com), to generate an `index.html` file which contained your fingerprinted files embedded into the page.
1919

20-
The idea is to read in your app's `rev-manifest.json`, and use the non-fingerprinted path to read in the fingerprinted path and inject it into the page. Note, this approach requires the `'compile index.html'` task to be run as part of your build process.
20+
The idea is to read in your app's `rev-manifest.json`, and use the non-fingerprinted path to read in the fingerprinted path and inject it into the page. Note, this approach requires the `'compile-index-html'` task to be run as part of your build process.
2121

2222
#### `index.hbs`
2323

@@ -38,32 +38,30 @@ The idea is to read in your app's `rev-manifest.json`, and use the non-fingerpri
3838
#### `gulpfile.js`
3939

4040
```js
41-
var fs = require('fs');
42-
var gulp = require('gulp');
43-
var handlebars = require('gulp-compile-handlebars');
44-
var rename = require('gulp-rename');
41+
const fs = require('fs');
42+
const gulp = require('gulp');
43+
const handlebars = require('gulp-compile-handlebars');
44+
const rename = require('gulp-rename');
4545

46-
// create a handlebars helper to look up
46+
// Create a handlebars helper to look up
4747
// fingerprinted asset by non-fingerprinted name
48-
var handlebarOpts = {
48+
const handlebarOpts = {
4949
helpers: {
50-
assetPath: function (path, context) {
51-
return ['/assets', context.data.root[path]].join('/');
52-
}
50+
assetPath: (path, context) => ['/assets', context.data.root[path]].join('/')
5351
}
5452
};
5553

56-
gulp.task('compile index.html', function () {
57-
// read in our manifest file
58-
var manifest = JSON.parse(fs.readFileSync('path/to/rev-manifest', 'utf8'));
54+
exports['compile-index-html'] = () => {
55+
// Read in our manifest file
56+
const manifest = JSON.parse(fs.readFileSync('path/to/rev-manifest', 'utf8'));
5957

60-
// read in our handlebars template, compile it using
58+
// Read in our handlebars template, compile it using
6159
// our manifest, and output it to index.html
6260
return gulp.src('index.hbs')
6361
.pipe(handlebars(manifest, handlebarOpts))
6462
.pipe(rename('index.html'))
6563
.pipe(gulp.dest('public'));
66-
});
64+
};
6765
```
6866

6967

@@ -74,20 +72,20 @@ Another approach would be to make a AJAX request to get the manifest JSON blob,
7472
For example, if you wanted to include your JavaScript files into the page:
7573

7674
```js
77-
$.getJSON('/path/to/rev-manifest.json', function (manifest) {
78-
var s = document.getElementsByTagName('script')[0];
75+
$.getJSON('/path/to/rev-manifest.json', manifest => {
76+
const s = document.getElementsByTagName('script')[0];
7977

80-
var assetPath = function (src) {
81-
src = 'js/' + src + '.js'
78+
const assetPath = src => {
79+
src = `js/${src}.js`;
8280
return ['/assets', manifest[src]].join('/');
8381
};
8482

85-
['lib', 'app'].forEach(function (src) {
86-
var el = document.createElement('script');
87-
el.async = true;
88-
el.src = assetPath(src);
89-
s.parentNode.insertBefore(el, s);
90-
});
83+
for (const src of ['lib', 'app']) {
84+
const element = document.createElement('script');
85+
element.async = true;
86+
element.src = assetPath(src);
87+
s.parentNode.insertBefore(element, s);
88+
}
9189
})
9290
```
9391

@@ -102,7 +100,7 @@ If the file is not present in the manifest it will return the original filename.
102100

103101
```php
104102
/**
105-
* @param string $filename
103+
* @param string $filename
106104
* @return string
107105
*/
108106
function asset_path($filename) {

package.json

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"url": "sindresorhus.com"
1111
},
1212
"engines": {
13-
"node": ">=6"
13+
"node": ">=8"
1414
},
1515
"scripts": {
1616
"test": "xo && ava"
@@ -36,16 +36,19 @@
3636
"dependencies": {
3737
"modify-filename": "^1.1.0",
3838
"plugin-error": "^1.0.1",
39-
"rev-hash": "^2.0.0",
39+
"rev-hash": "^3.0.0",
4040
"rev-path": "^2.0.0",
41-
"sort-keys": "^2.0.0",
42-
"through2": "^2.0.0",
41+
"sort-keys": "^4.0.0",
42+
"through2": "^3.0.1",
4343
"vinyl": "^2.1.0",
4444
"vinyl-file": "^3.0.0"
4545
},
4646
"devDependencies": {
47-
"ava": "^0.25.0",
48-
"p-event": "^2.1.0",
49-
"xo": "^0.23.0"
47+
"ava": "^2.3.0",
48+
"p-event": "^4.1.0",
49+
"xo": "^0.24.0"
50+
},
51+
"peerDependencies": {
52+
"gulp": ">=4"
5053
}
5154
}

0 commit comments

Comments
 (0)