Skip to content

Commit

Permalink
refactor: remove unnecessary plugin options
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The `prefix`, `modifyUnreved`, and modifyReved options
have been removed.
  • Loading branch information
thomasvantuycom committed Sep 28, 2023
1 parent 6ad9de7 commit 3cc22fc
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 113 deletions.
12 changes: 0 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,6 @@ Type: `Buffer` (e.g., `fs.readFileSync()`)

Read JSON manifests written out by `rev`. Allows replacing filenames that were revisioned prior to the current task.

##### prefix

Type: `String`

Add a prefix to each replacement.

##### modifyUnreved, modifyReved

Type: `Function`

Modify the name of the unreved/reved files before using them. The function receives the unreved/reved filename as the first argument, and the [Vinyl](https://github.com/gulpjs/vinyl#instance-properties) object of the current file as the optional second argument.

## License

MIT © [James K Nelson](http://jamesknelson.com), [Thomas Vantuycom](https://github.com/TheDancingCode)
34 changes: 2 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,8 @@ function relativePath(from, to) {
return path.relative(from, to).replace(/\\/g, '/');
}

function prefixPath(path, prefix) {
if (path.startsWith('/') && prefix.endsWith('/')) {
return `${prefix}${path.slice(1)}`;
}

if (!path.startsWith('/') && !prefix.endsWith('/')) {
return `${prefix}/${path}`;
}

return `${prefix}${path}`;
}

module.exports = function (options = {}) {
let renames = [];
const renames = [];
const cache = [];

return new Transform({
Expand Down Expand Up @@ -57,29 +45,11 @@ module.exports = function (options = {}) {
}
}

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);
}
const newContents = replace(contents, renames);

if (newContents !== contents) {
file.contents = Buffer.from(newContents);
Expand Down
69 changes: 0 additions & 69 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,75 +78,6 @@ test('reads and replaces reved filenames from a manifest', async t => {
t.true(contents.includes('image-d41d8cd98f.png'));
});

test('allows prefixing reved filenames', async t => {
t.plan(2);

const stream = revRewrite({
prefix: 'https://www.example.com/',
manifest: createManifest()
});
const data = pEvent(stream, 'data');

stream.end(createFile('index.html', htmlFileBody));

const file = await data;
const contents = file.contents.toString();
t.true(contents.includes('https://www.example.com/css/style-81a53f7d04.css'));
t.true(contents.includes('https://www.example.com/image-d41d8cd98f.png'));
});

test('allows modifying unreved filenames', async t => {
t.plan(3);

const modifyUnreved = (unreved, file) => file.extname === '.html' ? `/${unreved}` : `${unreved}`;

const stream = revRewrite({
modifyUnreved,
manifest: createManifest()
});
const data = pEvent.multiple(stream, 'data', {count: 2});

stream.write(createFile('style.css', cssFileBody));
stream.end(createFile('index.html', htmlFileBody));

const files = await data;
for (const file of files) {
const contents = file.contents.toString();
if (file.extname === '.html') {
t.true(contents.includes('css/style-81a53f7d04.css'));
t.false(contents.includes('image-d41d8cd98f.png'));
} else {
t.true(contents.includes('image-d41d8cd98f.png'));
}
}
});

test('allows modifying reved filenames', async t => {
t.plan(3);

const modifyReved = (reved, file) => file.extname === '.html' ? `assets/${reved}` : `../${reved}`;

const stream = revRewrite({
modifyReved,
manifest: createManifest()
});
const data = pEvent.multiple(stream, 'data', {count: 2});

stream.write(createFile('style.css', cssFileBody));
stream.end(createFile('index.html', htmlFileBody));

const files = await data;
for (const file of files) {
const contents = file.contents.toString();
if (file.extname === '.html') {
t.true(contents.includes('assets/css/style-81a53f7d04.css'));
t.true(contents.includes('assets/image-d41d8cd98f.png'));
} else {
t.true(contents.includes('../image-d41d8cd98f.png'));
}
}
});

test('does not replace false positives', async t => {
t.plan(1);

Expand Down

0 comments on commit 3cc22fc

Please sign in to comment.