Skip to content

Commit

Permalink
Require Node.js 18
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 3, 2023
1 parent a243f4d commit ea7c490
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 78 deletions.
3 changes: 0 additions & 3 deletions .github/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,3 @@
- Commit naming convention: `{ IssueID } { Issue Type (e.g. Enhancement, Bug, etc.) } short description`
- Run `npm test`
- Run `xo --fix` if you have any [coding style](https://github.com/sindresorhus/xo) issues. Most of them will be fixed for you.



6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ jobs:
fail-fast: false
matrix:
node-version:
- 20
- 18
- 16
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
119 changes: 54 additions & 65 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
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';
import {gulpPlugin} from 'gulp-plugin-extras';

function relativePath(base, filePath) {
filePath = filePath.replace(/\\/g, '/');
base = base.replace(/\\/g, '/');
filePath = filePath.replaceAll('\\', '/');
base = base.replaceAll('\\', '/');

if (!filePath.startsWith(base)) {
return filePath;
Expand Down Expand Up @@ -55,20 +54,12 @@ const getManifestFile = async options => {
}
};

const plugin = () => {
export default function gulpRev() {
const sourcemaps = [];
const pathMap = {};

return transformStream({objectMode: true}, file => {
if (file.isNull()) {
return file;
}

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

// This is a sourcemap, hold until the end
return gulpPlugin('gulp-rev', file => {
// This is a sourcemap, hold until the end.
if (path.extname(file.path) === '.map') {
sourcemaps.push(file);
return;
Expand All @@ -79,40 +70,38 @@ const plugin = () => {
pathMap[oldPath] = file.revHash;

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 {}

if (!reverseFilename) {
reverseFilename = path.relative(path.dirname(file.path), path.basename(file.path, '.map'));
}, {
async * onFinish() {
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 {}

if (!reverseFilename) {
reverseFilename = path.relative(path.dirname(file.path), path.basename(file.path, '.map'));
}

if (pathMap[reverseFilename]) {
// Save the old path for later
file.revOrigPath = file.path;
file.revOrigBase = file.base;

const hash = pathMap[reverseFilename];
file.path = revPath(file.path.replace(/\.map$/, ''), hash) + '.map';
} else {
transformFilename(file);
}

yield file;
}

if (pathMap[reverseFilename]) {
// Save the old path for later
file.revOrigPath = file.path;
file.revOrigBase = file.base;

const hash = pathMap[reverseFilename];
file.path = revPath(file.path.replace(/\.map$/, ''), hash) + '.map';
} else {
transformFilename(file);
}

files.push(file);
}

return files;
},
});
};
}

plugin.manifest = (path_, options) => {
gulpRev.manifest = (path_, options) => {
if (typeof path_ === 'string') {
path_ = {path: path_};
}
Expand All @@ -127,38 +116,38 @@ plugin.manifest = (path_, options) => {

let manifest = {};

return transformStream({objectMode: true}, file => {
return gulpPlugin('gulp-rev', file => {
// Ignore all non-rev'd files
if (!file.path || !file.revOrigPath) {
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, '/');
const originalFile = path.join(path.dirname(revisionedFile), path.basename(file.revOrigPath)).replaceAll('\\', '/');

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

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, ' '));

yield manifestFile;
yield manifestFile;
},
});
};

export default plugin;
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=16"
"node": ">=18"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -37,19 +37,18 @@
"assets"
],
"dependencies": {
"easy-transform-stream": "^1.0.0",
"gulp-plugin-extras": "^0.3.0",
"modify-filename": "^2.0.0",
"plugin-error": "^2.0.1",
"rev-hash": "^4.0.0",
"rev-hash": "^4.1.0",
"rev-path": "^3.0.0",
"sort-keys": "^5.0.0",
"vinyl": "^3.0.0",
"vinyl-file": "^5.0.0"
},
"devDependencies": {
"ava": "^5.1.0",
"p-event": "^5.0.1",
"xo": "^0.53.1"
"ava": "^5.3.1",
"p-event": "^6.0.0",
"xo": "^0.56.0"
},
"peerDependencies": {
"gulp": ">=4"
Expand Down

0 comments on commit ea7c490

Please sign in to comment.