Skip to content

Commit 1a9b1e3

Browse files
authored
Require Node.js 16 and move to ESM (#267)
1 parent 63b601b commit 1a9b1e3

File tree

7 files changed

+160
-147
lines changed

7 files changed

+160
-147
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13-
- 14
14-
- 12
15-
- 10
16-
- 8
13+
- 18
14+
- 16
1715
steps:
18-
- uses: actions/checkout@v2
19-
- uses: actions/setup-node@v1
16+
- uses: actions/checkout@v3
17+
- uses: actions/setup-node@v3
2018
with:
2119
node-version: ${{ matrix.node-version }}
2220
- run: npm install

index.js

Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
'use strict';
2-
const path = require('path');
3-
const through = require('through2');
4-
const vinylFile = require('vinyl-file');
5-
const revHash = require('rev-hash');
6-
const revPath = require('rev-path');
7-
const sortKeys = require('sort-keys');
8-
const modifyFilename = require('modify-filename');
9-
const Vinyl = require('vinyl');
10-
const PluginError = require('plugin-error');
1+
import {Buffer} from 'node:buffer';
2+
import path from 'node:path';
3+
import transformStream from 'easy-transform-stream';
4+
import {vinylFile} from 'vinyl-file';
5+
import revHash from 'rev-hash';
6+
import {revPath} from 'rev-path';
7+
import sortKeys from 'sort-keys';
8+
import modifyFilename from 'modify-filename';
9+
import Vinyl from 'vinyl';
10+
import PluginError from 'plugin-error';
1111

1212
function relativePath(base, filePath) {
1313
filePath = filePath.replace(/\\/g, '/');
@@ -35,17 +35,17 @@ function transformFilename(file) {
3535
file.path = modifyFilename(file.path, (filename, extension) => {
3636
const extIndex = filename.lastIndexOf('.');
3737

38-
filename = extIndex === -1 ?
39-
revPath(filename, file.revHash) :
40-
revPath(filename.slice(0, extIndex), file.revHash) + filename.slice(extIndex);
38+
filename = extIndex === -1
39+
? revPath(filename, file.revHash)
40+
: revPath(filename.slice(0, extIndex), file.revHash) + filename.slice(extIndex);
4141

4242
return filename + extension;
4343
});
4444
}
4545

4646
const getManifestFile = async options => {
4747
try {
48-
return await vinylFile.read(options.path, options);
48+
return await vinylFile(options.path, options);
4949
} catch (error) {
5050
if (error.code === 'ENOENT') {
5151
return new Vinyl(options);
@@ -59,37 +59,36 @@ const plugin = () => {
5959
const sourcemaps = [];
6060
const pathMap = {};
6161

62-
return through.obj((file, encoding, callback) => {
62+
return transformStream({objectMode: true}, file => {
6363
if (file.isNull()) {
64-
callback(null, file);
65-
return;
64+
return file;
6665
}
6766

6867
if (file.isStream()) {
69-
callback(new PluginError('gulp-rev', 'Streaming not supported'));
70-
return;
68+
throw new PluginError('gulp-rev', 'Streaming not supported');
7169
}
7270

7371
// This is a sourcemap, hold until the end
7472
if (path.extname(file.path) === '.map') {
7573
sourcemaps.push(file);
76-
callback();
7774
return;
7875
}
7976

8077
const oldPath = file.path;
8178
transformFilename(file);
8279
pathMap[oldPath] = file.revHash;
8380

84-
callback(null, file);
85-
}, function (callback) {
81+
return file;
82+
}, () => {
83+
const files = [];
84+
8685
for (const file of sourcemaps) {
8786
let reverseFilename;
8887

8988
// Attempt to parse the sourcemap's JSON to get the reverse filename
9089
try {
9190
reverseFilename = JSON.parse(file.contents.toString()).file;
92-
} catch (_) {}
91+
} catch {}
9392

9493
if (!reverseFilename) {
9594
reverseFilename = path.relative(path.dirname(file.path), path.basename(file.path, '.map'));
@@ -106,10 +105,10 @@ const plugin = () => {
106105
transformFilename(file);
107106
}
108107

109-
this.push(file);
108+
files.push(file);
110109
}
111110

112-
callback();
111+
return files;
113112
});
114113
};
115114

@@ -123,53 +122,43 @@ plugin.manifest = (path_, options) => {
123122
merge: false,
124123
transformer: JSON,
125124
...options,
126-
...path_
125+
...path_,
127126
};
128127

129128
let manifest = {};
130129

131-
return through.obj((file, encoding, callback) => {
130+
return transformStream({objectMode: true}, file => {
132131
// Ignore all non-rev'd files
133132
if (!file.path || !file.revOrigPath) {
134-
callback();
135133
return;
136134
}
137135

138136
const revisionedFile = relativePath(path.resolve(file.cwd, file.base), path.resolve(file.cwd, file.path));
139137
const originalFile = path.join(path.dirname(revisionedFile), path.basename(file.revOrigPath)).replace(/\\/g, '/');
140138

141139
manifest[originalFile] = revisionedFile;
142-
143-
callback();
144-
}, function (callback) {
140+
}, async function * () {
145141
// No need to write a manifest file if there's nothing to manifest
146142
if (Object.keys(manifest).length === 0) {
147-
callback();
148143
return;
149144
}
150145

151-
(async () => {
152-
try {
153-
const manifestFile = await getManifestFile(options);
146+
const manifestFile = await getManifestFile(options);
154147

155-
if (options.merge && !manifestFile.isNull()) {
156-
let oldManifest = {};
148+
if (options.merge && !manifestFile.isNull()) {
149+
let oldManifest = {};
157150

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

162-
manifest = Object.assign(oldManifest, manifest);
163-
}
158+
manifestFile.contents = Buffer.from(options.transformer.stringify(sortKeys(manifest), undefined, ' '));
164159

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-
})();
160+
yield manifestFile;
172161
});
173162
};
174163

175-
module.exports = plugin;
164+
export default plugin;

package.json

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
"description": "Static asset revisioning by appending content hash to filenames: unicorn.css => unicorn-d41d8cd98f.css",
55
"license": "MIT",
66
"repository": "sindresorhus/gulp-rev",
7+
"funding": "https://github.com/sponsors/sindresorhus",
78
"author": {
89
"name": "Sindre Sorhus",
910
"email": "[email protected]",
1011
"url": "sindresorhus.com"
1112
},
13+
"type": "module",
14+
"exports": "./index.js",
1215
"engines": {
13-
"node": ">=8"
16+
"node": ">=16"
1417
},
1518
"scripts": {
1619
"test": "xo && ava"
@@ -34,19 +37,19 @@
3437
"assets"
3538
],
3639
"dependencies": {
37-
"modify-filename": "^1.1.0",
38-
"plugin-error": "^1.0.1",
39-
"rev-hash": "^3.0.0",
40-
"rev-path": "^2.0.0",
41-
"sort-keys": "^4.0.0",
42-
"through2": "^3.0.1",
43-
"vinyl": "^2.1.0",
44-
"vinyl-file": "^3.0.0"
40+
"easy-transform-stream": "^1.0.0",
41+
"modify-filename": "^2.0.0",
42+
"plugin-error": "^2.0.1",
43+
"rev-hash": "^4.0.0",
44+
"rev-path": "^3.0.0",
45+
"sort-keys": "^5.0.0",
46+
"vinyl": "^3.0.0",
47+
"vinyl-file": "^5.0.0"
4548
},
4649
"devDependencies": {
47-
"ava": "^2.3.0",
48-
"p-event": "^4.1.0",
49-
"xo": "^0.24.0"
50+
"ava": "^5.1.0",
51+
"p-event": "^5.0.1",
52+
"xo": "^0.53.1"
5053
},
5154
"peerDependencies": {
5255
"gulp": ">=4"

readme.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ $ npm install --save-dev gulp-rev
1616
## Usage
1717

1818
```js
19-
const gulp = require('gulp');
20-
const rev = require('gulp-rev');
19+
import gulp from 'gulp';
20+
import rev from 'gulp-rev';
2121

22-
exports.default = () => (
22+
export default () => (
2323
gulp.src('src/*.css')
2424
.pipe(rev())
2525
.pipe(gulp.dest('dist'))
@@ -83,10 +83,10 @@ The hash of each rev'd file is stored at `file.revHash`. You can use this for cu
8383
### Asset manifest
8484

8585
```js
86-
const gulp = require('gulp');
87-
const rev = require('gulp-rev');
86+
import gulp from 'gulp';
87+
import rev from 'gulp-rev';
8888

89-
exports.default = () => (
89+
export default () => (
9090
// By default, Gulp would pick `assets/css` as the base,
9191
// so we need to set it explicitly:
9292
gulp.src(['assets/css/*.css', 'assets/js/*.js'], {base: 'assets'})
@@ -110,10 +110,10 @@ An asset manifest, mapping the original paths to the revisioned paths, will be w
110110
By default, `rev-manifest.json` will be replaced as a whole. To merge with an existing manifest, pass `merge: true` and the output destination (as `base`) to `rev.manifest()`:
111111

112112
```js
113-
const gulp = require('gulp');
114-
const rev = require('gulp-rev');
113+
import gulp from 'gulp';
114+
import rev from 'gulp-rev';
115115

116-
exports.default = () => (
116+
export default () => (
117117
// By default, Gulp would pick `assets/css` as the base,
118118
// so we need to set it explicitly:
119119
gulp.src(['assets/css/*.css', 'assets/js/*.js'], {base: 'assets'})
@@ -135,12 +135,12 @@ You can optionally call `rev.manifest('manifest.json')` to give it a different p
135135
Because of the way `gulp-concat` handles file paths, you may need to set `cwd` and `path` manually on your `gulp-concat` instance to get everything to work correctly:
136136

137137
```js
138-
const gulp = require('gulp');
139-
const rev = require('gulp-rev');
140-
const sourcemaps = require('gulp-sourcemaps');
141-
const concat = require('gulp-concat');
138+
import gulp from 'gulp';
139+
import rev from 'gulp-rev';
140+
import sourcemaps from 'gulp-sourcemaps';
141+
import concat from 'gulp-concat';
142142

143-
exports.default = () => (
143+
export default () => (
144144
gulp.src('src/*.js')
145145
.pipe(sourcemaps.init())
146146
.pipe(concat({path: 'bundle.js', cwd: ''}))
@@ -163,13 +163,13 @@ Since the order of streams are not guaranteed, some plugins such as `gulp-concat
163163
This plugin does not support streaming. If you have files from a streaming source, such as Browserify, you should use [`gulp-buffer`](https://github.com/jeromew/gulp-buffer) before `gulp-rev` in your pipeline:
164164

165165
```js
166-
const gulp = require('gulp');
167-
const browserify = require('browserify');
168-
const source = require('vinyl-source-stream');
169-
const buffer = require('gulp-buffer');
170-
const rev = require('gulp-rev');
166+
import gulp from 'gulp';
167+
import browserify from 'browserify';
168+
import source from 'vinyl-source-stream';
169+
import buffer from 'gulp-buffer';
170+
import rev from 'gulp-rev';
171171

172-
exports.default = () => (
172+
export default () => (
173173
browserify('src/index.js')
174174
.bundle({debug: true})
175175
.pipe(source('index.min.js'))

test/_helper.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {Buffer} from 'node:buffer';
12
import Vinyl from 'vinyl';
23

34
export default function createFile({
@@ -8,13 +9,13 @@ export default function createFile({
89
revName,
910
cwd,
1011
base,
11-
contents = ''
12+
contents = '',
1213
}) {
1314
const file = new Vinyl({
1415
path,
1516
cwd,
1617
base,
17-
contents: Buffer.from(contents)
18+
contents: Buffer.from(contents),
1819
});
1920
file.revOrigPath = revOrigPath;
2021
file.revOrigBase = revOrigBase;

0 commit comments

Comments
 (0)