Skip to content

Commit

Permalink
refactor: replace through2 with native streams
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasvantuycom committed Jul 25, 2023
1 parent 01d6697 commit 6ad9de7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 53 deletions.
104 changes: 53 additions & 51 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const path = require('path');
const PluginError = require('plugin-error');
const through = require('through2');
const {Transform} = require('stream');

const replace = require('./lib/replace.js');

Expand All @@ -26,66 +26,68 @@ module.exports = function (options = {}) {
let renames = [];
const cache = [];

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

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

// Collect renames from reved files.
if (file.revOrigPath) {
renames.push({
unreved: relativePath(file.revOrigBase, file.revOrigPath),
reved: relativePath(file.base, file.path)
});
}
// Collect renames from reved files.
if (file.revOrigPath) {
renames.push({
unreved: relativePath(file.revOrigBase, file.revOrigPath),
reved: relativePath(file.base, file.path)
});
}

cache.push(file);
cache.push(file);

callback();
}, function (callback) {
if (options.manifest) {
const manifest = JSON.parse(options.manifest.toString());
callback();
}, flush(callback) {
if (options.manifest) {
const manifest = JSON.parse(options.manifest.toString());

for (const [unreved, reved] of Object.entries(manifest)) {
renames.push({unreved, reved});
for (const [unreved, reved] of Object.entries(manifest)) {
renames.push({unreved, reved});
}
}
}

if (options.prefix) {
renames = renames.map(entry => {
entry.reved = prefixPath(entry.reved, options.prefix);
return entry;
});
}

// Once we have a full list of renames, search/replace in the cached
// files and push them through.
for (const file of cache) {
const modifiedRenames = renames.map(entry => {
const {unreved, reved} = entry;
const modifiedUnreved = options.modifyUnreved ? options.modifyUnreved(unreved, file) : unreved;
const modifiedReved = options.modifyReved ? options.modifyReved(reved, file) : reved;
return {unreved: modifiedUnreved, reved: modifiedReved};
});

const contents = file.contents.toString();
let newContents = replace(contents, modifiedRenames);

if (options.prefix) {
newContents = newContents.split('/' + options.prefix).join(options.prefix);
renames = renames.map(entry => {
entry.reved = prefixPath(entry.reved, options.prefix);
return entry;
});
}

if (newContents !== contents) {
file.contents = Buffer.from(newContents);
}
// Once we have a full list of renames, search/replace in the cached
// files and push them through.
for (const file of cache) {
const modifiedRenames = renames.map(entry => {
const {unreved, reved} = entry;
const modifiedUnreved = options.modifyUnreved ? options.modifyUnreved(unreved, file) : unreved;
const modifiedReved = options.modifyReved ? options.modifyReved(reved, file) : reved;
return {unreved: modifiedUnreved, reved: modifiedReved};
});

this.push(file);
}
const contents = file.contents.toString();
let newContents = replace(contents, modifiedRenames);

if (options.prefix) {
newContents = newContents.split('/' + options.prefix).join(options.prefix);
}

if (newContents !== contents) {
file.contents = Buffer.from(newContents);
}

this.push(file);
}

callback();
});
callback();
}});
};
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
"license": "MIT",
"dependencies": {
"lodash.escaperegexp": "^4.1.2",
"plugin-error": "^2.0.0",
"through2": "^4.0.2"
"plugin-error": "^2.0.0"
},
"devDependencies": {
"@ava/babel": "^2.0.0",
Expand Down

0 comments on commit 6ad9de7

Please sign in to comment.