Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dependencies and move to ESM #267

Merged
merged 12 commits into from Dec 12, 2022
9 changes: 4 additions & 5 deletions .github/workflows/main.yml
Expand Up @@ -10,13 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
- 18
- 16
- 14
- 12
- 10
- 8
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": ">=14"
},
"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
54 changes: 27 additions & 27 deletions readme.md
Expand Up @@ -16,13 +16,13 @@ $ npm install --save-dev gulp-rev
## Usage

```js
const gulp = require('gulp');
const rev = require('gulp-rev');
import {src, dest} from 'gulp';
import rev from 'gulp-rev';

exports.default = () => (
gulp.src('src/*.css')
export default () => (
src('src/*.css')
.pipe(rev())
.pipe(gulp.dest('dist'))
.pipe(dest('dist'))
);
```

Expand Down Expand Up @@ -83,18 +83,18 @@ 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 {src, dest} from 'gulp';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to keep gulp a default import.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

鉃★笍 39e09fa

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'})
.pipe(gulp.dest('build/assets')) // Copy original assets to build dir
src(['assets/css/*.css', 'assets/js/*.js'], {base: 'assets'})
.pipe(dest('build/assets')) // Copy original assets to build dir
.pipe(rev())
.pipe(gulp.dest('build/assets')) // Write rev'd assets to build dir
.pipe(dest('build/assets')) // Write rev'd assets to build dir
.pipe(rev.manifest())
.pipe(gulp.dest('build/assets')) // Write manifest to build dir
.pipe(dest('build/assets')) // Write manifest to build dir
);
```

Expand All @@ -110,21 +110,21 @@ 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 {src, dest} 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'})
.pipe(gulp.dest('build/assets'))
src(['assets/css/*.css', 'assets/js/*.js'], {base: 'assets'})
.pipe(dest('build/assets'))
.pipe(rev())
.pipe(gulp.dest('build/assets'))
.pipe(dest('build/assets'))
.pipe(rev.manifest({
base: 'build/assets',
merge: true // Merge with the existing manifest if one exists
}))
.pipe(gulp.dest('build/assets'))
.pipe(dest('build/assets'))
);
```

Expand All @@ -136,18 +136,18 @@ Because of the way `gulp-concat` handles file paths, you may need to set `cwd` a

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

exports.default = () => (
gulp.src('src/*.js')
exports.default = async () => {
const {default: rev} = await import('gulp-rev');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why dynamic import?

Copy link
Contributor Author

@pioug pioug Dec 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

鉃★笍 1d6480d

My mistake, I thought that we couldn't import the other packages of the examples using ES import

return gulp.src('src/*.js')
.pipe(sourcemaps.init())
.pipe(concat({path: 'bundle.js', cwd: ''}))
.pipe(rev())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('dist'))
);
};
```

## Different hash for unchanged files
Expand All @@ -167,16 +167,16 @@ const gulp = require('gulp');
const browserify = require('browserify');
const source = require('vinyl-source-stream');
const buffer = require('gulp-buffer');
const rev = require('gulp-rev');

exports.default = () => (
browserify('src/index.js')
exports.default = async () => {
const {default: rev} = await import('gulp-rev');
return browserify('src/index.js')
.bundle({debug: true})
.pipe(source('index.min.js'))
.pipe(buffer())
.pipe(rev())
.pipe(gulp.dest('dist'))
);
};
```

## Integration
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