Skip to content

Commit

Permalink
Require Node.js 16 and move to ESM (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
pioug committed Dec 12, 2022
1 parent 63b601b commit 1a9b1e3
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 147 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/main.yml
Expand Up @@ -10,13 +10,11 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 8
- 18
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
89 changes: 39 additions & 50 deletions index.js
@@ -1,13 +1,13 @@
'use strict';
const path = require('path');
const through = require('through2');
const vinylFile = require('vinyl-file');
const revHash = require('rev-hash');
const revPath = require('rev-path');
const sortKeys = require('sort-keys');
const modifyFilename = require('modify-filename');
const Vinyl = require('vinyl');
const PluginError = require('plugin-error');
import {Buffer} from 'node:buffer';
import path from 'node:path';
import transformStream from 'easy-transform-stream';
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 PluginError from 'plugin-error';

function relativePath(base, filePath) {
filePath = filePath.replace(/\\/g, '/');
Expand Down Expand Up @@ -35,17 +35,17 @@ function transformFilename(file) {
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);
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.read(options.path, options);
return await vinylFile(options.path, options);
} catch (error) {
if (error.code === 'ENOENT') {
return new Vinyl(options);
Expand All @@ -59,37 +59,36 @@ const plugin = () => {
const sourcemaps = [];
const pathMap = {};

return through.obj((file, encoding, callback) => {
return transformStream({objectMode: true}, file => {
if (file.isNull()) {
callback(null, file);
return;
return file;
}

if (file.isStream()) {
callback(new PluginError('gulp-rev', 'Streaming not supported'));
return;
throw new PluginError('gulp-rev', 'Streaming not supported');
}

// This is a sourcemap, hold until the end
if (path.extname(file.path) === '.map') {
sourcemaps.push(file);
callback();
return;
}

const oldPath = file.path;
transformFilename(file);
pathMap[oldPath] = file.revHash;

callback(null, file);
}, function (callback) {
return file;
}, () => {
const files = [];

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 (_) {}
} catch {}

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

this.push(file);
files.push(file);
}

callback();
return files;
});
};

Expand All @@ -123,53 +122,43 @@ plugin.manifest = (path_, options) => {
merge: false,
transformer: JSON,
...options,
...path_
...path_,
};

let manifest = {};

return through.obj((file, encoding, callback) => {
return transformStream({objectMode: true}, file => {
// Ignore all non-rev'd files
if (!file.path || !file.revOrigPath) {
callback();
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)).replace(/\\/g, '/');

manifest[originalFile] = revisionedFile;

callback();
}, function (callback) {
}, async function * () {
// No need to write a manifest file if there's nothing to manifest
if (Object.keys(manifest).length === 0) {
callback();
return;
}

(async () => {
try {
const manifestFile = await getManifestFile(options);
const manifestFile = await getManifestFile(options);

if (options.merge && !manifestFile.isNull()) {
let oldManifest = {};
if (options.merge && !manifestFile.isNull()) {
let oldManifest = {};

try {
oldManifest = options.transformer.parse(manifestFile.contents.toString());
} catch (_) {}
try {
oldManifest = options.transformer.parse(manifestFile.contents.toString());
} catch {}

manifest = Object.assign(oldManifest, manifest);
}

manifest = Object.assign(oldManifest, manifest);
}
manifestFile.contents = Buffer.from(options.transformer.stringify(sortKeys(manifest), undefined, ' '));

manifestFile.contents = Buffer.from(options.transformer.stringify(sortKeys(manifest), undefined, ' '));
this.push(manifestFile);
callback();
} catch (error) {
callback(error);
}
})();
yield manifestFile;
});
};

module.exports = plugin;
export default plugin;
27 changes: 15 additions & 12 deletions package.json
Expand Up @@ -4,13 +4,16 @@
"description": "Static asset revisioning by appending content hash to filenames: unicorn.css => unicorn-d41d8cd98f.css",
"license": "MIT",
"repository": "sindresorhus/gulp-rev",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=16"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -34,19 +37,19 @@
"assets"
],
"dependencies": {
"modify-filename": "^1.1.0",
"plugin-error": "^1.0.1",
"rev-hash": "^3.0.0",
"rev-path": "^2.0.0",
"sort-keys": "^4.0.0",
"through2": "^3.0.1",
"vinyl": "^2.1.0",
"vinyl-file": "^3.0.0"
"easy-transform-stream": "^1.0.0",
"modify-filename": "^2.0.0",
"plugin-error": "^2.0.1",
"rev-hash": "^4.0.0",
"rev-path": "^3.0.0",
"sort-keys": "^5.0.0",
"vinyl": "^3.0.0",
"vinyl-file": "^5.0.0"
},
"devDependencies": {
"ava": "^2.3.0",
"p-event": "^4.1.0",
"xo": "^0.24.0"
"ava": "^5.1.0",
"p-event": "^5.0.1",
"xo": "^0.53.1"
},
"peerDependencies": {
"gulp": ">=4"
Expand Down
40 changes: 20 additions & 20 deletions readme.md
Expand Up @@ -16,10 +16,10 @@ $ npm install --save-dev gulp-rev
## Usage

```js
const gulp = require('gulp');
const rev = require('gulp-rev');
import gulp from 'gulp';
import rev from 'gulp-rev';

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

```js
const gulp = require('gulp');
const rev = require('gulp-rev');
import gulp from 'gulp';
import rev from 'gulp-rev';

exports.default = () => (
export default () => (
// By default, Gulp would pick `assets/css` as the base,
// so we need to set it explicitly:
gulp.src(['assets/css/*.css', 'assets/js/*.js'], {base: 'assets'})
Expand All @@ -110,10 +110,10 @@ An asset manifest, mapping the original paths to the revisioned paths, will be w
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()`:

```js
const gulp = require('gulp');
const rev = require('gulp-rev');
import gulp from 'gulp';
import rev from 'gulp-rev';

exports.default = () => (
export default () => (
// By default, Gulp would pick `assets/css` as the base,
// so we need to set it explicitly:
gulp.src(['assets/css/*.css', 'assets/js/*.js'], {base: 'assets'})
Expand All @@ -135,12 +135,12 @@ You can optionally call `rev.manifest('manifest.json')` to give it a different p
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:

```js
const gulp = require('gulp');
const rev = require('gulp-rev');
const sourcemaps = require('gulp-sourcemaps');
const concat = require('gulp-concat');
import gulp from 'gulp';
import rev from 'gulp-rev';
import sourcemaps from 'gulp-sourcemaps';
import concat from 'gulp-concat';

exports.default = () => (
export default () => (
gulp.src('src/*.js')
.pipe(sourcemaps.init())
.pipe(concat({path: 'bundle.js', cwd: ''}))
Expand All @@ -163,13 +163,13 @@ Since the order of streams are not guaranteed, some plugins such as `gulp-concat
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:

```js
const gulp = require('gulp');
const browserify = require('browserify');
const source = require('vinyl-source-stream');
const buffer = require('gulp-buffer');
const rev = require('gulp-rev');
import gulp from 'gulp';
import browserify from 'browserify';
import source from 'vinyl-source-stream';
import buffer from 'gulp-buffer';
import rev from 'gulp-rev';

exports.default = () => (
export default () => (
browserify('src/index.js')
.bundle({debug: true})
.pipe(source('index.min.js'))
Expand Down
5 changes: 3 additions & 2 deletions test/_helper.js
@@ -1,3 +1,4 @@
import {Buffer} from 'node:buffer';
import Vinyl from 'vinyl';

export default function createFile({
Expand All @@ -8,13 +9,13 @@ export default function createFile({
revName,
cwd,
base,
contents = ''
contents = '',
}) {
const file = new Vinyl({
path,
cwd,
base,
contents: Buffer.from(contents)
contents: Buffer.from(contents),
});
file.revOrigPath = revOrigPath;
file.revOrigBase = revOrigBase;
Expand Down

0 comments on commit 1a9b1e3

Please sign in to comment.