Skip to content

Commit

Permalink
add support to replace url in JS
Browse files Browse the repository at this point in the history
  • Loading branch information
galkinrost committed Aug 20, 2014
1 parent 4ab03ae commit 7621cb0
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
5 changes: 5 additions & 0 deletions expected/rev-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"images/dummy.jpg": "images/dummy-7051c65f.jpg",
"scripts/script.js": "scripts/script-57cb6b72.js",
"styles/styles.css": "styles/styles-5f3db2f0.css"
}
3 changes: 3 additions & 0 deletions expected/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';
var img = new Image();
img.src = '/images/dummy-7051c65f.jpg';
7 changes: 7 additions & 0 deletions expected/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.background-image {
background-image: url('../images/dummy-7051c65f.jpg');
}

.background-image-2 {
background-image: url('../images/dummy-7051c65f.jpg');
}
3 changes: 3 additions & 0 deletions fixtures/scripts/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';
var img = new Image();
img.src = '/images/dummy.jpg';
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var gutil = require('gulp-util');
var path = require('path');

module.exports = function override() {
var cssRegExp = /\.css$/;
var allowedPathRegExp = /\.(css|js)$/;

function relPath(base, filePath) {
if (filePath.indexOf(base) !== 0) {
Expand Down Expand Up @@ -35,7 +35,7 @@ module.exports = function override() {
}, function (cb) {
var self = this;
f.forEach(function (_f) {
if (cssRegExp.test(_f.file.revOrigPath) && _f.file.contents) {
if ((allowedPathRegExp.test(_f.file.revOrigPath) ) && _f.file.contents) {
var contents = _f.file.contents.toString();
f.forEach(function (__f) {
contents = contents.replace(new RegExp(__f.origPath, 'g'), __f.hashedPath);
Expand Down
24 changes: 17 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,36 @@ var fs = require('fs');
var gulp = require('gulp');
var fse = require('fs-extra');
var override = require('./index');
var expect = require('chai').expect;

describe('gulp-rev-css-url', function () {
beforeEach(function (done) {
fse.remove('./results', done);
})

it('Should override urls in css', function (done) {
var expectedCSS = '.background-image {\n background-image: url(\'../images/dummy-7051c65f.jpg\');\n}\n\n.background-image-2 {\n background-image: url(\'../images/dummy-7051c65f.jpg\');\n}';
it('Should override urls in css and js', function (done) {
var expectedCSS = fs.readFileSync('./expected/styles.css', 'utf-8'),
expectedJs = fs.readFileSync('./expected/script.js', 'utf-8'),
expectedManifest = require('./expected/rev-manifest.json', 'utf-8');
gulp.src('./fixtures/**/*')
.pipe(rev())
.pipe(override())
.pipe(gulp.dest('./results/'))
.pipe(rev.manifest())
.pipe(gulp.dest('./results/'))
.on('end', function () {
var css = fs.readFileSync('./results/styles/styles-5f3db2f0.css', 'utf-8');
css.should.be.equal(expectedCSS);
var manifest = require('./results/rev-manifest.json', 'utf-8');
manifest['images/dummy.jpg'].should.be.equal('images/dummy-7051c65f.jpg');
manifest['styles/styles.css'].should.be.equal('styles/styles-5f3db2f0.css');
// load results
var css = fs.readFileSync('./results/styles/styles-5f3db2f0.css', 'utf-8'),
js = fs.readFileSync('./results/scripts/script-57cb6b72.js', 'utf-8'),
manifest = require('./results/rev-manifest.json', 'utf-8');

// check files' content
expect(css).to.equal(expectedCSS);
expect(js).to.equal(expectedJs);

// check manifest
expect(manifest).to.deep.equal(expectedManifest);

done();
});
});
Expand Down

0 comments on commit 7621cb0

Please sign in to comment.