Giter Club home page Giter Club logo

gulp-rev's Introduction

gulp-rev

Static asset revisioning by appending content hash to filenames unicorn.cssunicorn-d41d8cd98f.css

This project is feature complete. PRs adding new features will not be accepted.

Make sure to set the files to never expire for this to have an effect.

Install

npm install --save-dev gulp-rev

Usage

import gulp from 'gulp';
import rev from 'gulp-rev';

export default () => (
	gulp.src('src/*.css')
		.pipe(rev())
		.pipe(gulp.dest('dist'))
);

API

rev()

rev.manifest(path?, options?)

path

Type: string
Default: 'rev-manifest.json'

Manifest file path.

options

Type: object

base

Type: string
Default: process.cwd()

Override the base of the manifest file.

cwd

Type: string
Default: process.cwd()

Override the current working directory of the manifest file.

merge

Type: boolean
Default: false

Merge existing manifest file.

transformer

Type: object
Default: JSON

An object with parse and stringify methods. This can be used to provide a custom transformer instead of the default JSON for the manifest file.

Original path

Original file paths are stored at file.revOrigPath. This could come in handy for things like rewriting references to the assets.

Asset hash

The hash of each rev'd file is stored at file.revHash. You can use this for customizing the file renaming, or for building different manifest formats.

Asset manifest

import gulp from 'gulp';
import rev from 'gulp-rev';

export default () => (
	// By default, Gulp would pick `assets/css` as the base,
	// so we need to set it explicitly:
	gulp.src(['assets/css/*.css', 'assets/js/*.js'], {base: 'assets'})
		.pipe(gulp.dest('build/assets'))  // Copy original assets to build dir
		.pipe(rev())
		.pipe(gulp.dest('build/assets'))  // Write rev'd assets to build dir
		.pipe(rev.manifest())
		.pipe(gulp.dest('build/assets'))  // Write manifest to build dir
);

An asset manifest, mapping the original paths to the revisioned paths, will be written to build/assets/rev-manifest.json:

{
	"css/unicorn.css": "css/unicorn-d41d8cd98f.css",
	"js/unicorn.js": "js/unicorn-273c2c123f.js"
}

By default, rev-manifest.json will be replaced as a whole. To merge with an existing manifest, pass merge: true and the output destination (as base) to rev.manifest():

import gulp from 'gulp';
import rev from 'gulp-rev';

export default () => (
	// By default, Gulp would pick `assets/css` as the base,
	// so we need to set it explicitly:
	gulp.src(['assets/css/*.css', 'assets/js/*.js'], {base: 'assets'})
		.pipe(gulp.dest('build/assets'))
		.pipe(rev())
		.pipe(gulp.dest('build/assets'))
		.pipe(rev.manifest({
			base: 'build/assets',
			merge: true // Merge with the existing manifest if one exists
		}))
		.pipe(gulp.dest('build/assets'))
);

You can optionally call rev.manifest('manifest.json') to give it a different path or filename.

Sourcemaps and gulp-concat

Because of the way gulp-concat handles file paths, you may need to set cwd and path manually on your gulp-concat instance to get everything to work correctly:

import gulp from 'gulp';
import rev from 'gulp-rev';
import sourcemaps from 'gulp-sourcemaps';
import concat from 'gulp-concat';

export default () => (
	gulp.src('src/*.js')
		.pipe(sourcemaps.init())
		.pipe(concat({path: 'bundle.js', cwd: ''}))
		.pipe(rev())
		.pipe(sourcemaps.write('.'))
		.pipe(gulp.dest('dist'))
);

Different hash for unchanged files

Since the order of streams are not guaranteed, some plugins such as gulp-concat can cause the final file's content and hash to change. To avoid generating a new hash for unchanged source files, you can:

Streaming

This plugin does not support streaming. If you have files from a streaming source, such as Browserify, you should use gulp-buffer before gulp-rev in your pipeline:

import gulp from 'gulp';
import browserify from 'browserify';
import source from 'vinyl-source-stream';
import buffer from 'gulp-buffer';
import rev from 'gulp-rev';

export default () => (
	browserify('src/index.js')
		.bundle({debug: true})
		.pipe(source('index.min.js'))
		.pipe(buffer())
		.pipe(rev())
		.pipe(gulp.dest('dist'))
);

Integration

For more info on how to integrate gulp-rev into your app, have a look at the integration guide.

Use gulp-rev in combination with one or more of

It may be useful - and necessary - to use gulp-rev with other packages to complete the task.

gulp-rev's People

Contributors

adjavaherian avatar alexandre-abrioux avatar benbieler avatar bobthecow avatar borisceranic avatar briangonzalez avatar c0 avatar callumacrae avatar ebakan avatar leocaseiro avatar marwelln avatar mikaelbr avatar mstrutt avatar pioug avatar richienb avatar schneiderl avatar sebdeckers avatar shaunwarman avatar shonny-ua avatar sindresorhus avatar skube avatar steadicat avatar tanato avatar thedancingcode avatar uberspeck avatar undozen avatar wayneashleyberry avatar wvmf avatar xhmikosr avatar zhouzi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gulp-rev's Issues

Writing to/modifying existing manifest file fails

As described in this section of the README

var gulp = require('gulp');
var rev = require('gulp-rev');

gulp.task('default', function () {
    // by default, gulp would pick `assets/css` as the base,
    // so we need to set it explicitly:
    return gulp.src([
        'assets/css/*.css',
        'assets/js/*.js'
    ], {base: 'assets'})
        .pipe(gulp.dest('build/assets'))
        .pipe(rev())
        .pipe(gulp.dest('build/assets'))

        // Add rev-manifest.json as a new src to prevent rev'ing rev-manifest.json
        .pipe(gulp.src('build/assets/rev-manifest.json', {base: 'assets'}))
        .pipe(rev.manifest())             // applies only changes to the manifest
        .pipe(gulp.dest('build/assets'));
});

Opening a second gulp.src gives me the following error, which I can't seem to work around. Any ideas?

stream.js:94
      throw er; // Unhandled stream error in pipe.
            ^
Error: write after end

Rev-manifest.json not being generated

Originally reported by @tingGui in #58.

rev() is functioning as expected, but rev-manifest.json is not being created by rev.manifest(). Looks like the source is being built by RequireJS.

His code:

gulp.task('rjs', function() {
  rjs({
    baseUrl: basePath,
    mainConfigFile: basePath + 'build.js',
    removeCombined: true,
    name: 'rjs.build.main',
    out: 'libs.min.js'
  })
    .pipe(uglify())
    .pipe(rev())
    .pipe(gulp.dest('web/static/compiled/scripts'))
    .pipe(rev.manifest())
    .pipe(rename('rev-manifest.json'))
    .pipe(gulp.dest('web/static/compiled'));
});

Help with gulp-rev and stylus

This configuration:

gulp.task('style', function () {
  gulp.src('assets/css/site.styl')
    .pipe(stylus({
      use: assetPath,
      include: ['assets/css', 'vendor/assets/css']
    }))
    .pipe(rev())
    .pipe(rev.manifest())
    .pipe(gulp.dest('public/assets'));
});

doesn't seem to work. It outputs the manifest, but doesn't render the files themselves. Any ideas?

rev.forceWrite()

Thoughts on adding a rev.forceWrite() method to the plugin that can be used in a situation like gulp-watch (a never ending stream) to force the writing/merging of a manifest? This is useful in situations where rev is used to hash file names, but in development uses something you dont want to do a full 20 second compile when you only changed one file from the previous version.

File not change ,but hash changed.

Why the hash changed but file not updated ? Anyone can help me? Thanks
gulp.src('web/static/allLib/scripts/*/.js')
.pipe(concat('app.js'))
.pipe(rename({
suffix: '.min'
}))
.pipe(uglify({
mangle: false,
outSourceMap: false
}))
.pipe(rev())
.pipe(gulp.dest('web/static/compiled/scripts'))
.pipe(rev.manifest())
.pipe(rename('revd-manifest.json'))
.pipe(gulp.dest('web/static/rev'));

Rev'd file is written but the manifest isn't updated while watching

Hello,
I have a problem while running a watch-task. The following is my task to build images and their manifest:

assetLinkups = './app/config';

gulp.task('images', function(done) {
    runSequence('build-single-images', 'manifest-images', function() {
        return done();
    });
});

gulp.task('build-single-images', function() {
    var stream = gulp.src(appFiles.images)
        .pipe($.imagemin({ optimizationLevel: 7, progressive: true, interlaced: true }))
        .pipe(gulp.dest(paths.images.destTemp));

    return stream;
});

gulp.task('manifest-images', function() {
    var stream = gulp.src([paths.images.destTemp + '/**/*.{png,jpg,jpeg,gif,svg,ico}'])
        .pipe(rev())
        .pipe(gulp.dest(paths.images.dest))
        .pipe($.size({ showFiles: true }))
        .pipe(rev.manifest('assets-linkup-images.json'))
        .pipe(gulp.dest(assetLinkups));

    return stream;
});

And this is my watch-task:

gulp.task('watch', ['default'], function() {
    // watch images
    $.watch([ FILES TO WATCH FOR ])
        .pipe($.imagemin({ optimizationLevel: 7, progressive: true, interlaced: true }))
        .pipe(gulp.dest(paths.images.destTemp))
        .pipe(rev())
        .pipe(gulp.dest(paths.images.dest))
        .pipe($.size({ showFiles: true }))
        .pipe(rev.manifest('assets-linkup-images.json', { base: assetLinkups, merge: true }))
        .pipe(gulp.dest(assetLinkups));
});

So, when I start watching, first all images are built. After that I add a new image to a watched path. The image will be added and it gets a hash, but the key-value-pair isn't written to the manifest file.

Is there an error in the above code?

Greetings,
Lars

The wrong path in manifest.json on Windows

On Windows when generating the file manifest.json, wrong path are generated:

{
  "res\\styles\\main.css": "res\\styles\\main-8aaa81e2.css",
  "res\\styles\\main.ie.css": "res\\styles\\main.ie-6b801154.css",
  "res\\lib\\vendor\\jquery.js": "res\\lib\\vendor\\jquery-e5581654.js",
  "res\\lib\\vendor\\modernizr.js": "res\\lib\\vendor\\modernizr-3fcfe0bd.js",
  "res\\lib\\main.js": "res\\lib\\main-9ccf2f35.js",
}

Change the format of manifest.json?

Right now it's a mapping between old names and new names:

{
  "/appA.js": "/appA-82109b43.js",
  "/appB.js": "/appB-c6276861.js",
  "/appC.js": "/appC-a2dda226.js"
}

Can we change it into a mapping between the name and the hash instead? Namely:

{
  "appA": "82109b43",
  "appB": "c6276861",
  "appC": "a2dda226"
}

This makes it far more flexible to use. For example appA, appB, etc. are actually some Angular module names and I'm feeding these to some Rails helper - say include_angular_module appA. I don't wanna use include_angular_module "/appA.js" and I don't wanna do some tedious string manipulation either. And what really matters in the values is the hash part IMHO.

Update hash if child assets changed

Say I've got app.css and image.jpg, then run gulp rev. Now I have app-a1234567.css and image-b1234567.jpg. I run gulp-rev-css-url to update the references in my css file. Cool. Everything can be cached forever.

But what happens when I change image.jpg a couple months later? It'll get a new hash. gulp-rev-css-url will update the reference again in app-a1234567.css, but won't update the hash since nothing in the css source file changed. If someone's got a cached version of the css file, they won't see the new one since the file name didn't change. Their cached version will point to the old image, which is now out of date or non-existent.

Thoughts or plans on addressing this problem? I'm aware of the gulp-rev-all fork that addresses this, but I need a manifest file, and that doesn't provide one at the moment.

Speeding up gulp rev task

When run gulp-rev task, it generates a new hash for every output file provided. I'm a big fan of rails asset pipeline. I wonder if it could be possible to check which files has been changed and generate hash for only those leaving the unmodified files left intact having hash from previous gulp run.

So if a user run gulp rev task again without modifying any file, it will not generate a new hashed file and use the previous one.

With every new hashed file, I tend to lose my caching on user side even if that file is not changed but rev task associated a new hash to it, browser will load it again. It would be useful if you can make this happen and generate hash for only changed files.

timestamps in manifest file would skipping unchanged files.

Hey,

sprockets adds some more information to the manifest file:

{
   "files":{
      "application-723d1be6cc741a3aabb1cec24276d681.js":{
         "logical_path":"application.js",
         "mtime":"2013-07-26T22:55:03-07:00",
         "size":302506,
         "digest":"723d1be6cc741a3aabb1cec24276d681"
      },
   "assets":{
      "application.js":"application-723d1be6cc741a3aabb1cec24276d681.js",
   }
}

which should allow to check if the file was changed since last rev() run.

Good idea?

Can't append to rev-manifest.json

I've followed the guidelines but I'm unable to get a task to append to rev-manifest.json rather than replace it.

I run my css task first which generates the file hashes using gulp-rev and writes both the files and the manifest to disk in the ./dist/assets/ directory. All working correctly with the manifest generated. I then run my JS task and pass in the source rev-manifest.json file as the source:

.pipe(gulp.src('./dist/assets/rev-manifest.json')) // Pass in the current manifest so it's added to rather than replaced

Then run rev.manifest() and write to disk:

.pipe(rev()) // Hash the filenames
.pipe(gulp.dest('./dist/assets'))  // Write rev'd assets to build dir
.pipe(gulp.src('./dist/assets/rev-manifest.json')) // Pass in the current manifest so it's added to rather than replaced
.pipe(rev.manifest()) // Append hashed files to the current manifest
.pipe(gulp.dest('./dist/assets')) // Write manifest to build dir

Unfortunately the console returns an error however:

Finished 'js' after 39 ms

stream.js:94
      throw er; // Unhandled stream error in pipe.
            ^`
...

If I comment out the pipe where I pass in the source reference then it all works but overwrites the rev-manifest.json file losing the entries in their previously for my CSS files.

I'm using "gulp-rev": "^1.1.0",

Does anyone else have this problem or is there something I'm forgetting to do?

Mapping original files to rev'd files?

I'm exploring using gulp (and this plugin) and I'm trying to work out how to update my html to reflect the rev'd filenames.

In Grunt, I'd typically use grunt-usemin to do this which performs the updates by referencing the grunt.filerev.summary object that grunt-filerev outputs. I also occasionally use the grunt-filerev-assets module which dumps the summary object to a json file so I can consume it in my node app.

A similar approach doesn't seem trivial since the actual destination output doesn't occur until a later gulp.dest() call.

Any thoughts on a good approach to this?

Get hashname

Hi there,

I am using your plugin and it works really well.
Is there any way to get the filenames after 'rev()'? Or is it impossible because of the gulp architecture itself?
That would help me a lot.

Thanks

manifest have a trailing slash

Hi Guys, seems like the recent PR is incomplete

{
  "min.js": "/min-577f0c45.js"
}

I'm getting a trailing slash in osx, double forward slash \\ in windows

Error implementing changes added by PR#51

Hi all, I'm having trouble successfully replicating the functionality added with #51.

If I follow the example in the readme exactly:

var gulp = require('gulp');
var rev  = require('gulp-rev');
gulp.task('default', function(){
    return gulp.src(['assets/css/*.css', 'assets/js/*.js'], {base: 'assets'})
        .pipe(gulp.dest('build/assets'))
        .pipe(rev())
        .pipe(gulp.dest('build/assets'))
        .pipe(gulp.src('build/assets/rev-manifest.json', {base: 'assets'}))
        .pipe(rev.manifest())
        .pipe(gulp.dest('build/assets'));
});

I get this error:

stream.js:94
      throw er; // Unhandled stream error in pipe.
            ^
Error: write after end

Maybe it has to do with the gulp.src call mid-stream?

If it helps, I have style.css in assets/css/ and scripts.js in assets/js, and if I look in build/assets after the error, css/ has style.css and style-hash.css, while js/ only has scripts.js. This would seem to indicate the error is somewhere between rev'ing those assets, but when I remove the code specific to generating rev-manifest.json both types of assets are rev'd properly.

Manifest return absolute paths in Windows

Hello, I tried to use gulp-rev on a Windows machine but the manifest was full of absolute paths, both in keys and values.

At first I tried this solution reported in #54 but with no luck. Then I pulled down the package slash to convert the absolute path obtained and it showed good keys in the manifest but the values were still absolute paths.

So I added gulp-slash and piped it before calling the manifest and now I have proper paths!

It is possible to add something like that built-in for Windows Users?

Here what I needed to do:

var gulp  = require('gulp'),
rev       = require('gulp-rev'),
gulpslash = require('gulp-slash'),
slash     = require('slash'),
path      = require('path');

var abspath = slash(path.join(process.cwd(), 'public/assets'));

gulp.task('rev', function() {
    return gulp.src([
        'public/assets/scripts/*.js',
        'public/assets/styles/*.css'
        ], { base: abspath})
    .pipe(rev())
    .pipe(gulp.dest('public/assets'))
    .pipe(gulpslash())
    .pipe(rev.manifest())
    .pipe(gulp.dest('public/assets'));
});

Using rev-manifest.json to update image urls in css/scss

I'm working on an asset pipeline replacement and am trying to figure out a clean way to replace image urls in a css or scss file.

I have a gulp task to run images through gulp-imagemin, fingerprint them, and then places the fingerprinted file in a dist directory. At the same time I'm creating a rev-manifest.json, which gets renamed to image-manifest.json.

// Images Dist
gulp.task('imagesDist', ['images'], function () {
  return gulp.src(['app/images/**/*'], {base: path.resolve('app')})
    .pipe(imagemin()) // Optimize
    .pipe(rev())
    .pipe(gulp.dest('dist'))
    .pipe(size())
    .pipe(rev.manifest())
    .pipe(rename('image-manifest.json'))
    .pipe(gulp.dest('dist'));
});

I have a separate task for compiling my scss to css for distribution. How can I use the image-manifest.json to update any images used in my css? Is there a better approach to handle this?

Absolute paths are included when using multiple directory sources

When using sources from multiple directories, the absolute path remains. I attempted to set base as an option to gulp.src, but that did not work either.

In this scenario, javascript is stored under public/assets and images in public/images. The manifest is stored in public/assets/rev-manifest.json.

I also attempted to fix it in #47, but the path to the asset remains.

Simple example:

//gulpfile.js
var gulp       = require('gulp');
var rev        = require('gulp-rev');

var src = [
  'public/assets/*.js',
  'public/images/*.png',
];

var manifest = function () {
  gulp.src(src)
    .pipe(rev())
    .pipe(gulp.dest('public/assets'))
    .pipe(rev.manifest())
    .pipe(gulp.dest('public/assets'));
};

gulp.task('default', manifest);
// public/assets/rev-manifest.json
{
  "foo.js": "foo-d41d8cd9.js",
  "foo.png": "/Users/full/path/to/public/assets/foo-d41d8cd9.png"
}

Asset manifest not closed before task has ended

I think I have a case here where the gulp task ends before the asset manifest is really written. I found this out that the next, subsequent gulp task still refers to an older asset manifest.

Here is the gulp task I am using:

gulp.task('stylus', ['imagemin', 'clean:css'], function() {
    var stylus       = require('gulp-stylus'),
        nib          = require('nib'),
        fingerprint  = require('gulp-fingerprint'),
        autoprefixer = require('gulp-autoprefixer'),
        minifycss    = require('gulp-minify-css'),
        cssBase64    = require('gulp-css-base64')

    gulp.src(paths.stylus)
        .pipe(stylus({
            use:    [nib()],
            errors: true,
            define: {
                'video-width':  settings.server.video.size.record.width,
                'video-height': settings.server.video.size.record.height
            }
        }))
        // write image revisions into css
        .pipe(fingerprint(require(path.join(settings.server.paths.rev, 'img', 'rev-manifest.json')), {
            regex: /url\("\/img\/([a-z-_.\d]*)"\)/gim
            //, verbose: true
        }))
        .pipe(cssBase64({
            baseDir: path.relative(process.cwd(), settings.server.paths.www)
        }))
        // https://github.com/ai/autoprefixer#browsers
        .pipe(autoprefixer(
            'last 3 versions',
            '> 5%',
            'Explorer 9',
            'ios 7', 'android 4'
        ))
        // just keep an unminified one for local development only
        .pipe(gulpif(settings.LOCAL, gulp.dest(paths.css)))
        .pipe(minifycss())
        .pipe(rename({suffix: '.min'}))
        .pipe(rev())
        .pipe(gulp.dest(paths.css))
        .pipe(rev.manifest())
        .pipe(gulp.dest(path.join(paths.rev, 'css')))
})

Any clues?

Always create rev-manifest.json even if no -rev

Is it possible to create rev-manifest.json with:

{
  "style.css": "style.css"
}

even if you don't want to rev your files? It allows me in backend to always read rev-manifest.json and to include what I need/want from frontend.

How to make gulp-rev work smoothly with gulp-usemin

Hi guys,

I am moving from grunt to gulp. At grunt, I can easily integrate grunt rev and grunt usemin but I can't do the same in gulp. My expectation is that:

  • When I do gulp-rev, it will override old asset files with new asset files. For now, it just create new.
  • After gulp-rev, I do gulp-usemin, it will detect all asset files in *.html, *.css, *.js and change them into rev name.

Please help me out. Thanks!

Integration: rev-manifest.json not working with gulp.watch()

If you gulp.watch() your css styles and trigger the gulp-rev task multiple times, the changed rev-manifest.json will not be updated.

That's beacuse the require statement is cached:
var manifest = require('path/to/rev-manifest');

I changed it to

var manifest = JSON.parse(fs.readFileSync('./build/rev-manifest.json', "utf8"));

Now there a no problems with gulp.watch() and gulp-rev

firstFile is null when after gulp stream is done

plugin.manifest = function () {
    var manifest  = {};
    var firstFile = null;

    return through.obj(function (file, enc, cb) {
        // ignore all non-rev'd files
                console.log(file) // is exists and goes in the if condition
        if (file.path && file.revOrigPath) {
            firstFile = firstFile || file;
            manifest[relPath(firstFile.revOrigBase, file.revOrigPath)] = relPath(firstFile.base, file.path);
        }

        cb();
    }, function (cb) {
                console.log(firstFile) // null
        if (firstFile) { 
            this.push(new gutil.File({
                cwd: firstFile.cwd,
                base: firstFile.base,
                path: path.join(firstFile.base, 'rev-manifest.json'),
                contents: new Buffer(JSON.stringify(manifest, null, '  '))
            }));
        }

        cb();
    });
};

Merge is broken on 3.0.0

I just updated the appendExisting (#65 (comment)) option to merge (https://github.com/sindresorhus/gulp-rev/releases/tag/v3.0.0) and now the manifest file is not being updated :(

My gulpfile.js


var publicPath = 'public/'
gulp.task('js', function() {
  return gulp.src(scriptsPath + 'main.js', { base: path.join(process.cwd(), 'public/app') } )
    .pipe(browserify({
      insertGlobals: true,
      debug: true
    }))
    // Bundle to a single file
    .pipe(concat('scripts/app.js'))
    .pipe(uglify())
    .pipe(rev())
    .pipe(gulp.dest(publicPath))
    .pipe(rev.manifest({
      base: publicPath,
      merge: true
    }))
    .pipe(gulp.dest(publicPath))
});

The existing rev-manifest.json file is

{
  "styles/main.css": "styles/main-23ea264f.min.css"
}

The app.js is correctly minified and rev'd but the manifest file is not updated.

why prepend hash and use "." as separator?

curious about the reasoning behind it.

for me it makes more sense to do unicorn_098f6bcd.css instead of 098f6bcd.unicorn.css so files are always sorted in the same way (in case you need to compare folders, browse it manually, etc..)

PS: I'm sure many naive scripts/regexp expects the filename to contain a single . just before the extension.

Some path trouble with browserify

    var browserify = require('browserify')()

    browserify.transform({ global: true }, require('reactify') )
    browserify.add( __dirname + '/../../app/assets/full/browserify.js' )

    return browserify.bundle()
      .pipe( require('vinyl-source-stream')( 'browserify.js ) )
      .pipe( require('gulp-buffer')() )
      .pipe( require('gulp-rev')() )
      .pipe( gulp.dest('web/assets/full/scripts/') )
      .pipe( require('gulp-rev').manifest() )
      .pipe( gulp.dest('var/build/full/rev-manifest/scripts/') )
  }

results:

(Yes, I've hidden no-browserify compilation)

{
  "html5.js": "html5-831d9bca.js",
  "first.js": "first-a916a19c.js",
  "lib.js": "lib-14faf9b0.js",
  "/Users/kud/Projects/_playmedia/desktop/browserify.js": "browserify-da6e74e6.js",
  "main.js": "main-25f31d95.js"
}

As you can see, only browserify stuff isn't named correctly.

Question: Removing original files / Handling source maps

I'm using gulp-rev to append content hashes to every file in my dist directory (except .html files).

Revving works fine on all files, including the sourcemaps that were generated into dist as *.js.map and *.css.map in the tasks uglify and sass.

Before deploying the dist directory to staging or production servers, i need to remove all original files - including the original, un-revved sourcemaps. The answer posted in #50 works for everything that is not a sourcemap.

module.exports = function (gulp, config, $) {
    var dist = './dist';

    gulp.task('rev', [ 'sass', 'uglify', 'imagemin' ], function () {
        return gulp.src(dist + '/**/*.{js,css,jpg,png,svg}')
            .pipe($.sourcemaps.init({ loadMaps: true }))
            .pipe($.rev())
            .pipe($.sourcemaps.write('.'))
            .pipe(gulp.dest(dist))
            .pipe($.rev.manifest())
            .pipe(gulp.dest(dist));
    });
};

On a sidenote, this also generates a name-hash.type.map file for images - which is an undesired side-effect of my appoach to sourcemaps.

What would i have to do to rev all files in a directory (excluding *.html), keeping external sourcemaps working and removing all original files?

scripts-50e8cdb8-50e8cdb8-50e8cdb8-50e8cdb8.js

gulp-rev with gulp-usemin seems to get in some kind of loop:

Error: ENAMETOOLONG, open '/home/mkulesza/projects/ipl-frontend/dist/styles/vendor-837c2ec4-837c2ec4-837c2ec4-837c2ec4-837c2ec4-837c2ec4-837c2ec4-837c2ec4-837c2ec4-837c2ec4-837c2ec4-837c2ec4-837c2ec4-837c2ec4-837c2ec4-837c2ec4-9c3d37ce-9c3d37ce-9c3d37ce-9c3d37ce-9c3d37ce-9c3d37ce-9c3d37ce-9c3d37ce-9c3d37ce-9c3d37ce-9c3d37ce-9c3d37ce-9c3d37ce-9c3d37ce-9c3d37ce-9c3d37ce.css'


gulp.task('usemin:dist', ['templates:dist','styles:dist'], function(){
    var js = [$.ngAnnotate(), $.rev()];
    var css = [
            // rewrite paths from source to compiled directories
            $.minifyCss(),
            // FIX paths in CSS - AFTER MINIFICATION ( gulp error )
            $.replace('/bower_components/sass-bootstrap/fonts/','/fonts/'),
            $.replace('images/images/','images/'),
            $.replace('app/images/','images/'),
            $.rev(),
        ];

    return gulp.src(['./app/index.html'])
        .pipe($.replace('<!-- #templates -->', '<script src="scripts/templates.min.js"></script>'))
        .pipe($.usemin({
            // there apparently cannot be two pipes (builds) with same name
            ie: [$.rev()],
            jsvendor: [$.rev()],
            js: js,
            templates: js,
            cssvendor: css,
            css: css
        }))
        .pipe(gulp.dest('dist'));

    <!-- build:jsvendor /scripts/vendor.js -->
    <!-- bower:js -->
    <script src="bower_components/jquery/jquery.js"></script>-
    <script src="bower_components/es5-shim/es5-shim.js"></script>
...
    <!-- endbuild -->

What might be the problem?
Why now each pipeline must have unique id? Previously it worked with multiple "build:js" for example.

contents of file changes, but rev hash stays the same

Wondering if someone may be able to help shed some light on this for me. When I change the contents of my js files and run this build (below) the contents of the built files are indeed different, but they are always generated with the same rev name. Any idea why this could be happening?

Thanks in advance for any insight.

gulp.task("cleanjs", function(){

     return gulp.src([
        './lib/dist/js/*'
    ])
    .pipe(clean());

});

gulp.task("js", ["cleanjs"], function(){

      var files = ["app", "public"];

      _.each(files, function(el, i){

            browserify('./lib/js/' + el + '.js').bundle({
                        debug: true
                })
                .on("error", util.log)
                .pipe(source(el + '.min.js'))
                // .pipe(streamify(uglify()))
                .pipe(rev())
                .pipe(gulp.dest("./lib/dist/js"))
                .pipe(rev.manifest())
                .pipe(rename(el + ".manifest.json"))
                .pipe(gulp.dest("./lib/dist/js"));

      });

});

manifest writes absolute path, and question about consuming it.

okay guys would you mind telling us how to consume the manifest.json I'll plan to rewrite the assets via gulp-cheerio.

Also if you write to disk twice

        .pipe( rev() )
        .pipe( gulp.dest("dist/js") )
        .pipe( rev.manifest() )
        .pipe( gulp.dest("dist/js") )

the manifest file uses an absolute path
It writes

{
  "/var/www/assets/css/unicorn.css": "unicorn-098f6bcd.css"
}

while if you remove one

        .pipe( rev() )
        .pipe( rev.manifest() )
        .pipe( gulp.dest("dist/js") )

It writes

{
  "unicorn.css": "unicorn-098f6bcd.css"
}

Have a manifest with different revision folders?

I have tried all the afternoon, before asking here, so I've done my homework before asking for help :)
I want to create a manifest file out of revisioned files. The problem is that the files are in different folders (one for CSS and one for JS files) and the revisioned files need to be stored in the same folder as the source files. This is the code I have written:

gulp.task('versioning', function() {
    gulp.src([config.out.css_folder + '*.css', config.out.js_folder + '*.js'])
        .pipe(plumber(config.plumber))
        .pipe(revision())
        .pipe(gulp.dest( __path_to_current_file_base__ ))
        .pipe(revision.manifest())
        .pipe(rename('my-manifest.json'))
        .pipe(gulp.dest(config.out.resources));
});

My point is that I need to declare the path of the destination file the same as the source file ("path_to_current_file_base "). I have tried with file.base, event.path, path.base, file.revOrigPath, __dirname but none of them is correct.
Does anybody have an idea on how to do it?

Absolute path in key of rev-manifest.json

Similar issue to #48, but I think this is different enough to warrant a separate issue. Feel free to close if not.

When the following are true:

  1. I don't copy an asset I want to gulp-rev to my destination dir before calling rev() on it
  2. I generate rev-manifest.json
  3. I explicitly set base to get a relative path in the value of rev-manifest.json

I end up with a rev-manifest.json that looks like this:

{
    "/super/long/absolute/path/scripts.js": "js/scripts-hash.js"
}

Simplified example of a gulp task to reproduce this:

var gulp = require('gulp');
var rev  = require('gulp-rev');

gulp.task('rev', function(){
  return gulp.src('source/js/scripts.js', {base: 'source'})
    .pipe(rev())
    .pipe(gulp.dest('dist'))
    .pipe(rev.manifest())
    .pipe(gulp.dest('dist'));
});

This puts scripts.js inside dist/js/, which is desirable for organization. It also includes js/ in the value in rev-manifest.json to accurately reflect this. The problem is that absolute path in the key which makes consuming the manifest problematic.

Interestingly, I've found that interacting with the source before calling rev() avoids the long path. For example, mixing in the gulp-rename trick I mentioned in #53:

var gulp   = require('gulp');
var rev    = require('gulp-rev');
var rename = require('gulp-rename');

gulp.task('rev', function(){
  return gulp.src('source/js/scripts.js', {base: 'source'})
    .pipe(rename({extname: ''})
    .pipe(rev())
    .pipe(rename({extname: '.js'})
    .pipe(gulp.dest('dist'))
    .pipe(rev.manifest())
    .pipe(gulp.dest('dist'));
});

Suddenly, the absolute path is gone. rev-manifest.json looks like this:

{
    "js/scripts": "js/scripts-hash.js"
}

Any idea what's causing this interaction? I know I could get around this by copying the original file over to /dist by starting gulp.task with a .pipe(gulp.dest('dist')), but I don't want the original file in /dist. I'd have to delete it later. Making a temporary copy of a file and deleting it later in the task doesn't seem very gulp-like.

Can anyone else reproduce based on the above, and if so, do you have any ideas why that absolute path is being generated? Is the cause in gulp itself or gulp-rev, or am I simply doing something wrong? I feel like there's probably a little detail I'm missing 😓

access to file.revHash

"The hash of each rev'd file is stored at file.revHash. You can use this for customizing the file renaming, or for building different manifest formats."

Where does one access this file.revHash? I don't see it being used in any examples. Been pounding my head on this for quite a while. I'd be happy to update the docs once I understand what's going on.

rev-manifest outputs absolute paths

When I run this:

gulp.task('build', ['clean'], function () {
  var js  = gulp.src('src/js/app.js');

  var css = gulp.src('src/css/app.styl')
    .pipe(stylus({ include: ['src/css' ] }));

  var img = gulp.src('src/img/*');

  var templates = gulp.src('src/views/**/*.jade')
    .pipe(jade());

  var types = {
    assets: function (input) {
      if ('.html' === path.extname(input.path)) return false;
      return true;
    }
  };

  return stream.merge(js, css, img, templates)
    .pipe(match(types.assets, rev()))
    .pipe(replace())
    .pipe(gulp.dest('build'))
    .pipe(rev.manifest())
    .pipe(gulp.dest('build'));
});

the images render in the manifest just fine. However, the CSS and JavaScripts output absolute paths, which doesn't work so well. Is there a way to override this?

Questions regarding the plugin

  1. When running concurrent tasks, much in the standard gulp flow, how does your plugin account for read/write locks when trying to access the manifest file? How are those multiple tasks reconciled so that they dont step on each other's toes?

I've tried it out and haven't seen a problem, but was still left wondering how it all "just worked".

  1. I noticed that gulp-rev now supports creating a manifest file directly. Any idea what, if anything, they're doing differently to other plugins like gulp-rev-all?

Question: How to deal with "." from filenames in rev-manifest.json elegantly

I'm running into a situation that leads me to believe I may be Doing It WrongTM, so I thought I'd see if anyone else has encountered something similar or has any ideas.

I'm trying to get my gulp workflow for Jade, Stylus, and JS files to play nicely with gulp-rev for static builds. Ideally, I would like to structure asset imports in Jade with variables that reference rev-manifest.json. Something like this:

script(src= app.js)

In my Jade task I pass rev-manifest.json as a data object, and ideally Jade would compile the above to something like this:

// rev-manifest.json
{
  "app.js": "app-d41d8cd9.js"
}
<!--compiled html-->
<script src="app-d41d8cd9.js"></script>

That doesn't work, though, because of dot notation. Jade tries to interpret .js as an object property of app. I'm not sure how people are getting around this when consuming rev-manifest.json--seems like having {file}.{ext} as the template for JSON strings would cause problems anywhere you try to use a js variable to refer to a JSON object.

My current hackish solution uses gulp-rename to remove the file extension before rev() and add it back before rev.manifest():

// simplified gulp task
// requires
var gulp   = require('gulp');
var rename = require('gulp-rename');
var rev    = require('gulp-rev');
// paths
var src  = 'app.js';
var dest = 'dist/js';
var data = 'dist/data';
// task
gulp.task('rev', function(){
  return gulp.src(src)
    .pipe(rename({extname: ''})
    .pipe(rev())
    .pipe(rename({extname: '.js'})
    .pipe(gulp.dest(dest))
    .pipe(rev.manifest())
    .pipe(gulp.dest(data));
});

With this set up, I can pass rev-manifest.json successfully. Ex:

// rev-manifest.json
{
  "app": "app-d41d8cd9.js"
}
// jade
script(src= app)

Is there a more elegant way to do this? Any insight appreciated!

Oddly-named sourcemap files from gulp-ruby-sass

I'm using gulp-ruby-sass to turn my SCSS files into a site.css file and a site.css.map file. Piping them through gulp-rev results in files like site-2ca2ed30.css and site.css-ce8373b0.map. Should the latter not be site-ce8373b0.css.map? ie, still have the .css.map extensions?

Here's my task:

return gulp.src(paths.css.src+'site.scss')
    .pipe(sass({bundleExec: true, style: 'compressed'}))
    .pipe(rev())
    .pipe(gulp.dest(paths.css.dest))
    // Generate the manifest file that maps original to new filename.
    .pipe(rev.manifest())
    // Instead of saving it, parse the object in the file's content and save
    // it for later.
    .pipe(gutil.buffer(function(err, files){
        addToRevisions(JSON.parse(files[0].contents.toString()));
    }));

Add support for streams

Currently I have to wrap gulp-rev with streamify to make it work for streams.

Otherwise you'd see that error message:

events.js:72
        throw er; // Unhandled 'error' event
              ^
[gulp] Error in plugin 'gulp-rev': Streaming not supported
    at Transform._transform (/home/michael.heuberger/binarykitchen/code/videomail.io/node_modules/gulp-rev/index.js:31:23)
    at Transform._read (/home/michael.heuberger/binarykitchen/code/videomail.io/node_modules/gulp-rev/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:184:10)
    at Transform._write (/home/michael.heuberger/binarykitchen/code/videomail.io/node_modules/gulp-rev/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:172:12)
    at doWrite (/home/michael.heuberger/binarykitchen/code/videomail.io/node_modules/gulp-rev/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:238:10)
    at writeOrBuffer (/home/michael.heuberger/binarykitchen/code/videomail.io/node_modules/gulp-rev/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:228:5)
    at Transform.Writable.write (/home/michael.heuberger/binarykitchen/code/videomail.io/node_modules/gulp-rev/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:195:11)
    at write (_stream_readable.js:582:24)
    at flow (_stream_readable.js:591:7)
    at Transform.pipeOnReadable (_stream_readable.js:623:5)
    at Transform.EventEmitter.emit (events.js:92:17)

Subfolder not being added to manifest

Hi!

I have this task

var publicPath = 'public/';
var scriptsPath = publicPath + 'app/scripts/',
gulp.task('browserify', function() {
  return gulp.src(scriptsPath + 'main.js', { base: path.join(process.cwd(), 'public/app') } )
    .pipe(browserify({
      insertGlobals: true,
      debug: true
    }))
    // Bundle to a single file
    .pipe(concat('app.js'))
    .pipe(rev())
    .pipe(gulp.dest(publicPath + 'scripts'))
    .pipe(rev.manifest())
    .pipe(gulp.dest(publicPath))
});

The manifest file is:

{
  "app.js": "app-286cbb15.js"
}

The "scripts" dir name is not being added to the file. Any idea why?

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.