gulp-css-rebase
Advanced tools
Comparing version 1.0.0 to 2.0.0
95
index.js
@@ -9,44 +9,75 @@ 'use strict'; | ||
var fs = require('fs'); | ||
var merge = require('merge'); | ||
var md5 = require('md5'); | ||
var fileExists = require('file-exists'); | ||
module.exports = function(filePath, publicPrefix){ | ||
return through.obj(function(file, enc, cb) { | ||
if (file.isNull()) { | ||
return; | ||
} | ||
module.exports = function (opt) { | ||
var options = merge({ | ||
output: null, | ||
exclude: [], | ||
overwrite: false | ||
}, opt); | ||
if (file.isStream()){ | ||
return this.emit('error', PluginError('gulp-css-rebase', 'Streaming not supported')); | ||
} | ||
return through.obj(function (file, enc, cb) { | ||
if (file.isNull()) { | ||
return; | ||
} | ||
var adjusted = adjust(file); | ||
file.contents = new Buffer(adjusted); | ||
if (file.isStream()) { | ||
return this.emit('error', PluginError('gulp-css-rebase', 'Streaming not supported')); | ||
} | ||
cb(null, file); | ||
}); | ||
var adjusted = adjust(file); | ||
file.contents = new Buffer(adjusted); | ||
function adjust(file){ | ||
var css = file.contents.toString(); | ||
cb(null, file); | ||
}); | ||
return rework(css) | ||
.use(url(function(url) { | ||
if(!/^(data|\/|\w+:\/\/)/.test(url)){ | ||
var assetPath = path.join(path.dirname(file.path), url); | ||
var assetFolder = path.basename(path.dirname(assetPath)) | ||
var newPath = path.join(filePath, assetFolder, path.basename(assetPath)).replace(/[\#|\?].*$/, ''); | ||
function adjust(file) { | ||
var css = file.contents.toString(); | ||
mkpath(path.dirname(newPath), function(err) { | ||
if (err) { | ||
throw err; | ||
} | ||
return rework(css) | ||
.use(url(function (url) { | ||
if (!/^(data|\/|\w+:\/\/)/.test(url)) { | ||
var assetPath = path.join(path.dirname(file.path), url); | ||
var assetFolder = md5(path.relative(process.cwd(), path.dirname(assetPath))); | ||
var IsExclude = false; | ||
fs.createReadStream(assetPath.replace(/[\#|\?].*$/, '')).pipe(fs.createWriteStream(newPath)); | ||
}); | ||
for (var index in options.exclude) { | ||
if (options.exclude[index] === assetPath.substr(0, options.exclude[index].length)) { | ||
IsExclude = true; | ||
break; | ||
} | ||
} | ||
url = path.normalize(path.join(publicPrefix, assetFolder, path.basename(assetPath))).replace(/\\/g, '/'); | ||
} | ||
var newPath = !IsExclude | ||
? path.normalize(path.join(options.output, assetFolder, path.basename(assetPath))) | ||
: path.normalize(assetPath) | ||
; | ||
return url; | ||
})) | ||
.toString(); | ||
} | ||
if ( | ||
(!IsExclude && !fileExists(newPath)) | ||
|| | ||
(!IsExclude && options.overwrite) | ||
) { | ||
mkpath(path.dirname(newPath), function (err) { | ||
if (err) { | ||
throw err; | ||
} | ||
fs | ||
.createReadStream(assetPath.replace(/[\#|\?].*$/, '')) | ||
.pipe(fs.createWriteStream(newPath.replace(/[\#|\?].*$/, ''))) | ||
; | ||
}); | ||
} | ||
url = path.relative(options.output, newPath); | ||
} | ||
return url; | ||
})) | ||
.toString(); | ||
} | ||
}; |
{ | ||
"name": "gulp-css-rebase", | ||
"version": "1.0.0", | ||
"description": "", | ||
"version": "2.0.0", | ||
"description": "Rewrite url references in CSS and copyed CSS assets", | ||
"main": "index.js", | ||
"dependencies": { | ||
"file-exists": "^2.0.0", | ||
"fs": "0.0.1-security", | ||
"md5": "^2.2.1", | ||
"merge": "^1.2.0", | ||
"mkpath": "^1.0.0", | ||
@@ -31,6 +34,8 @@ "path": "^0.12.7", | ||
"gulp", | ||
"gulpplugin", | ||
"css", | ||
"url", | ||
"rebase" | ||
"rebase", | ||
"rewrite" | ||
] | ||
} |
# gulp-css-rebase | ||
A [gulp](http://gulpjs.com/) plugin that allows rewriting url references in CSS | ||
## Install | ||
@@ -15,7 +17,13 @@ | ||
var webPath = '.', outputPath = 'dest'; | ||
var outputPath = 'dest'; | ||
gulp.task('default', () => | ||
gulp.src('src/*.css') | ||
.pipe(rebase(outputPath, webPath)) | ||
.pipe(rebase({ | ||
output: outputPath, | ||
exclude: [ | ||
'path/to/web/dir' | ||
], | ||
overwrite: false | ||
})) | ||
.pipe(gulp.dest(outputPath)) | ||
@@ -25,1 +33,5 @@ ); | ||
### Options | ||
- `output` - The target directory for the processed CSS. Paths are rewritten relatively to that directory. | ||
- `exclude` - Paths are rewritten relatively to original path. | ||
- `overwrite` - Overwrite files |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4280
67
36
9
+ Addedfile-exists@^2.0.0
+ Addedmd5@^2.2.1
+ Addedmerge@^1.2.0
+ Addedcharenc@0.0.2(transitive)
+ Addedcrypt@0.0.2(transitive)
+ Addedfile-exists@2.0.0(transitive)
+ Addedis-buffer@1.1.6(transitive)
+ Addedmd5@2.3.0(transitive)
+ Addedmerge@1.2.1(transitive)