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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change manifest option to accept Buffer #53

Merged
merged 2 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,29 @@ Only [LTS and current releases](https://github.com/nodejs/Release#release-schedu

## Usage

The most common use pattern consists of two steps:
```js
const { src, dest } = require('gulp');
const rev = require('gulp-rev');
const revRewrite = require('gulp-rev-rewrite');

function revision() {
return src('dist/**/*.{css,js}')
.pipe(rev())
.pipe(src('dist/**/*.html'))
.pipe(revRewrite())
.pipe(dest('dist'));
}

exports.default = revision;
```

Alternatively:

1. Revision your assets and create an asset manifest.
2. Collect the revisioned paths from the manifest and rewrite references to them

```js
const { readFileSync } = require('fs');
const { src, dest, series } = require('gulp');
const rev = require('gulp-rev');
const revRewrite = require('gulp-rev-rewrite');
Expand All @@ -35,7 +52,7 @@ function revision() {

// Step 2
function rewrite() {
const manifest = src('dist/assets/rev-manifest.json');
const manifest = readFileSync('dist/assets/rev-manifest.json');

return src('dist/**/*.html')
.pipe(revRewrite({ manifest }))
Expand All @@ -45,24 +62,6 @@ function rewrite() {
exports.default = series(revision, rewrite);
```

Alternatively, you can combine both steps.

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

function revision() {
return src('dist/**/*.{css,js}')
.pipe(rev())
.pipe(src('dist/**/*.html'))
.pipe(revRewrite())
.pipe(dest('dist'));
}

exports.default = revision;
```

## API

### revRewrite([options])
Expand All @@ -73,7 +72,7 @@ Type: `Object`

##### manifest

Type: `Stream` (e.g., `gulp.src()`)
Type: `Buffer` (e.g., `fs.readFileSync()`)

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

Expand Down
16 changes: 6 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,15 @@ module.exports = function (options = {}) {
const stream = this;

if (options.manifest) {
// Collect renames from rev-manifest.
options.manifest.on('data', file => {
const manifest = JSON.parse(file.contents.toString());
const manifest = JSON.parse(options.manifest.toString());

for (const [unreved, reved] of Object.entries(manifest)) {
renames.push({unreved, reved});
}
});
options.manifest.on('end', replaceContents);
} else {
replaceContents();
for (const [unreved, reved] of Object.entries(manifest)) {
renames.push({unreved, reved});
}
}

replaceContents();

function replaceContents() {
if (options.prefix) {
renames = renames.map(entry => {
Expand Down
10 changes: 0 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"@ava/babel": "^1.0.0",
"ava": "^3.0.0",
"gulp-rev": "^9.0.0",
"into-stream": "^6.0.0",
"p-event": "^4.1.0",
"semantic-release": "^17.0.0",
"vinyl": "^2.1.0",
Expand Down
15 changes: 5 additions & 10 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import test from 'ava';
import Vinyl from 'vinyl';
import pEvent from 'p-event';
import rev from 'gulp-rev';
import intoStream from 'into-stream';
import revRewrite from '..';

const htmlFileBody =
Expand All @@ -17,15 +16,11 @@ const createFile = (path, contents, encoding = 'utf8') =>
});

const createManifest = () => {
return intoStream.object(
createFile(
'rev-manifest.json',
JSON.stringify({
'image.png': 'image-d41d8cd98f.png',
'css/style.css': 'css/style-81a53f7d04.css'
})
)
);
const manifest = {
'image.png': 'image-d41d8cd98f.png',
'css/style.css': 'css/style-81a53f7d04.css'
};
return Buffer.from(JSON.stringify(manifest, null, 4));
};

test('identifies and replaces reved filenames in the stream', async t => {
Expand Down