Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gulp-rev-rewrite

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gulp-rev-rewrite - npm Package Compare versions

Comparing version 1.1.4 to 2.0.0

104

index.js
'use strict';
const path = require('path');
const {relative} = require('path');
const PluginError = require('plugin-error');

@@ -9,17 +9,29 @@ const through = require('through2');

module.exports = function (options) {
function relativePath(from, to) {
return relative(from, to).replace(/\\/g, '/');
}
function prefixPath(path, prefix) {
if (path.startsWith('/') && prefix.endsWith('/')) {
return `${prefix}${path.substr(1)}`;
}
if (!path.startsWith('/') && !prefix.endsWith('/')) {
return `${prefix}/${path}`;
}
return `${prefix}${path}`;
}
module.exports = function (options = {}) {
let renames = [];
const cache = [];
options = Object.assign({canonicalUris: true, replaceInExtensions: ['.js', '.css', '.html', '.hbs']}, options);
return through.obj(function (file, enc, cb) {
return through.obj((file, encoding, callback) => {
if (file.isNull()) {
this.push(file);
return cb();
return callback(null, file);
}
if (file.isStream()) {
this.emit('error', new PluginError('gulp-rev-rewrite', 'Streaming not supported'));
return cb();
return callback(new PluginError('gulp-rev-rewrite', 'Streaming not supported'));
}

@@ -30,29 +42,21 @@

renames.push({
unreved: fmtPath(file.revOrigBase, file.revOrigPath),
reved: fmtPath(file.base, file.path)
unreved: relativePath(file.revOrigBase, file.revOrigPath),
reved: relativePath(file.base, file.path)
});
}
if (options.replaceInExtensions.includes(path.extname(file.path))) {
// File should be searched for replaces
cache.push(file);
} else {
// Nothing to do with this file
this.push(file);
}
cache.push(file);
cb();
}, function (cb) {
callback();
}, function (callback) {
const stream = this;
if (options.manifest) {
// Read manifest file for the list of renames.
// Collect renames from rev-manifest.
options.manifest.on('data', file => {
const manifest = JSON.parse(file.contents.toString());
Object.keys(manifest).forEach(srcFile => {
renames.push({
unreved: canonicalizeUri(srcFile),
reved: canonicalizeUri(manifest[srcFile])
});
});
for (const [unreved, reved] of Object.entries(manifest)) {
renames.push({unreved, reved});
}
});

@@ -65,7 +69,8 @@ options.manifest.on('end', replaceContents);

function replaceContents() {
renames = renames.map(entry => {
const {unreved} = entry;
const reved = options.prefix ? prefixPath(entry.reved, options.prefix) : entry.reved;
return {unreved, reved};
});
if (options.prefix) {
renames = renames.map(entry => {
entry.reved = prefixPath(entry.reved, options.prefix);
return entry;
});
}

@@ -76,5 +81,6 @@ // Once we have a full list of renames, search/replace in the cached

const modifiedRenames = renames.map(entry => {
const unreved = options.modifyUnreved ? options.modifyUnreved(entry.unreved, file) : entry.unreved;
const reved = options.modifyReved ? options.modifyReved(entry.reved, file) : entry.reved;
return {unreved, reved};
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};
});

@@ -93,31 +99,5 @@

cb();
callback();
}
});
function fmtPath(base, filePath) {
const newPath = path.relative(base, filePath);
return canonicalizeUri(newPath);
}
function canonicalizeUri(filePath) {
if (options.canonicalUris) {
filePath = filePath.replace(/\\/g, '/');
}
return filePath;
}
function prefixPath(filePath, prefix) {
if (filePath.startsWith('/') && prefix.endsWith('/')) {
return `${prefix}${filePath.substr(1)}`;
}
if (!filePath.startsWith('/') && !prefix.endsWith('/')) {
return `${prefix}/${filePath}`;
}
return `${prefix}${filePath}`;
}
};
{
"name": "gulp-rev-rewrite",
"version": "1.1.4",
"version": "2.0.0",
"description": "Rewrite occurences of filenames which have been renamed by gulp-rev",

@@ -32,3 +32,3 @@ "main": "index.js",

"engines": {
"node": ">=6"
"node": ">=8"
},

@@ -42,6 +42,6 @@ "license": "MIT",

"devDependencies": {
"ava": "^1.1.0",
"event-stream": "3.3.4",
"ava": "^2.0.0",
"gulp-rev": "^9.0.0",
"p-event": "^2.0.0",
"into-stream": "^5.1.0",
"p-event": "^4.1.0",
"semantic-release": "^15.3.1",

@@ -48,0 +48,0 @@ "vinyl": "^2.1.0",

# gulp-rev-rewrite [![Build Status](https://travis-ci.org/TheDancingCode/gulp-rev-rewrite.svg?branch=master)](https://travis-ci.org/TheDancingCode/gulp-rev-rewrite) [![npm](https://img.shields.io/npm/v/gulp-rev-rewrite.svg)](https://www.npmjs.com/package/gulp-rev-rewrite) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release) [![Greenkeeper badge](https://badges.greenkeeper.io/TheDancingCode/gulp-rev-rewrite.svg)](https://greenkeeper.io/)
> Rewrite occurrences of filenames which have been renamed by gulp-rev
> Rewrite references to assets revisioned by `gulp-rev`

@@ -17,45 +17,53 @@ **This plugin is an improved and maintained fork of [gulp-rev-replace](https://github.com/jamesknelson/gulp-rev-replace).**

Pipe through a stream with both the revved files and the files containing references to them.
The most common use pattern consists of two steps:
1. Revision your assets and create an asset manifest.
2. Collect the revisioned paths from the manifest and rewrite references to them
```js
const gulp = require('gulp');
const filter = require('gulp-filter');
const { src, dest, series } = require('gulp');
const rev = require('gulp-rev');
const revRewrite = require('gulp-rev-rewrite');
gulp.task('rev', () => {
const assetFilter = filter(['**/*', '!**/index.html'], { restore: true });
// Step 1
function revision() {
return src('dist/assets/**/*.{css,js}')
.pipe(rev())
.pipe(dest('dist/assets'))
.pipe(rev.manifest())
.pipe(dest('dist/assets'));
}
return gulp.src('src/**')
.pipe(assetFilter)
.pipe(rev()) // Rename all files except index.html
.pipe(assetFilter.restore)
.pipe(revRewrite()) // Substitute in new filenames
.pipe(gulp.dest('dist'));
});
// Step 2
function rewrite() {
const manifest = src('dist/assets/rev-manifest.json');
return src('dist/**/*.html')
.pipe(revRewrite({ manifest }))
.pipe(dest('dist'));
}
exports.default = series(revision, rewrite);
```
It is also possible to collect the revisioned filenames from JSON manifests written out by `gulp-rev`. This allows for replacing filenames that were revved prior to the current task.
Alternatively, you can combine both steps with the use of `gulp-filter`.
```js
const { src, dest } = require('gulp');
const filter = require('gulp-filter');
const rev = require('gulp-rev');
const revRewrite = require('gulp-rev-rewrite');
const revDelete = require('gulp-rev-delete-original');
gulp.task('revision', ['dist:css', 'dist:js'], () => {
return gulp.src('dist/**/*.{css,js}')
.pipe(rev())
.pipe(revDelete()) // Remove the unrevved files
.pipe(gulp.dest('dist'))
.pipe(rev.manifest())
.pipe(gulp.dest('dist'));
});
function revision() {
const assetFilter = filter(['**', '!**/*.html'], { restore: true });
gulp.task('revRewrite', ['revision'], function() {
const manifest = gulp.src('dist/rev-manifest.json');
return src('dist/**')
.pipe(assetFilter)
.pipe(rev()) // Rename all files except html files
.pipe(assetFilter.restore)
.pipe(revRewrite())
.pipe(dest('dist'));
}
return gulp.src('dist/index.html')
.pipe(revRewrite({ manifest }))
.pipe(gulp.dest('dist'));
});
exports.default = revision;
```

@@ -71,16 +79,8 @@

##### canonicalUris
##### manifest
Type: `Boolean`<br>
Default: `true`
Type: `Stream` (e.g., `gulp.src()`)
Use canonical URIs when replacing filePaths, i.e. use a forward slash (`/`) as the path segment seperator.
Read JSON manifests written out by `rev`. Allows replacing filenames that were revisioned prior to the current task.
##### replaceInExtensions
Type: `Array`<br>
Default: `['.js', '.css', '.html', '.hbs']`
Only substitute in new filenames in files of these types.
##### prefix

@@ -92,9 +92,2 @@

##### manifest
Type: `Stream` (e.g., `gulp.src()`)
Read JSON manifests written out by `rev`. Allows replacing filenames that were
revved prior to the current task.
##### modifyUnreved, modifyReved

@@ -117,2 +110,4 @@

```js
const { src, dest } = require('gulp');
function replaceJsIfMap(filename) {

@@ -125,9 +120,13 @@ if (filename.includes('.map')) {

return gulp.src('dist/**/*.js')
.pipe(revRewrite({
manifest: manifest,
modifyUnreved: replaceJsIfMap,
modifyReved: replaceJsIfMap
}))
.pipe(gulp.dest('dist'));
function rewrite() {
return src('dist/**/*.js')
.pipe(revRewrite({
manifest: src('rev-manifest.json'),
modifyUnreved: replaceJsIfMap,
modifyReved: replaceJsIfMap
}))
.pipe(dest('dist'));
}
exports.default = rewrite;
```

@@ -134,0 +133,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc