diff --git a/.github/contributing.md b/.github/contributing.md index fd2cc5f..0b1bbcb 100644 --- a/.github/contributing.md +++ b/.github/contributing.md @@ -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. - - - diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6a82b18..346585c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/index.js b/index.js index 37231a5..a1bfee0 100644 --- a/index.js +++ b/index.js @@ -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; @@ -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; @@ -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_}; } @@ -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; diff --git a/package.json b/package.json index 23ebc7b..b01f60e 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "type": "module", "exports": "./index.js", "engines": { - "node": ">=16" + "node": ">=18" }, "scripts": { "test": "xo && ava" @@ -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"