Skip to content

Commit c1f80bb

Browse files
committed
v2.0.0
1 parent 58b3eb7 commit c1f80bb

File tree

3 files changed

+85
-37
lines changed

3 files changed

+85
-37
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# gulp-css-rebase
22

3+
A [gulp](http://gulpjs.com/) plugin that allows rewriting url references in CSS
4+
35
## Install
46

57
```
@@ -12,12 +14,22 @@ $ npm install --save-dev gulp-css-rebase
1214
const gulp = require('gulp');
1315
const rebase = require('gulp-css-rebase');
1416

15-
var webPath = '.', outputPath = 'dest';
17+
var outputPath = 'dest';
1618

1719
gulp.task('default', () =>
1820
gulp.src('src/*.css')
19-
.pipe(rebase(outputPath, webPath))
21+
.pipe(rebase({
22+
output: outputPath,
23+
exclude: [
24+
'path/to/web/dir'
25+
],
26+
overwrite: false
27+
}))
2028
.pipe(gulp.dest(outputPath))
2129
);
2230
```
2331

32+
### Options
33+
- `output` - The target directory for the processed CSS. Paths are rewritten relatively to that directory.
34+
- `exclude` - Paths are rewritten relatively to original path.
35+
- `overwrite` - Overwrite files

index.js

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,77 @@ var url = require('rework-plugin-url');
66
var through = require('through2');
77
var mkpath = require('mkpath');
88
var fs = require('fs');
9+
var merge = require('merge');
10+
var md5 = require('md5');
11+
var fileExists = require('file-exists');
912

10-
module.exports = function(filePath, publicPrefix){
11-
return through.obj(function(file, enc, cb) {
12-
if (file.isNull()) {
13-
return;
14-
}
13+
module.exports = function (opt) {
14+
var options = merge({
15+
output: null,
16+
exclude: [],
17+
overwrite: false
18+
}, opt);
1519

16-
if (file.isStream()){
17-
return this.emit('error', PluginError('gulp-css-rebase', 'Streaming not supported'));
18-
}
20+
return through.obj(function (file, enc, cb) {
21+
if (file.isNull()) {
22+
return;
23+
}
1924

20-
var adjusted = adjust(file);
21-
file.contents = new Buffer(adjusted);
25+
if (file.isStream()) {
26+
return this.emit('error', PluginError('gulp-css-rebase', 'Streaming not supported'));
27+
}
2228

23-
cb(null, file);
24-
});
29+
var adjusted = adjust(file);
30+
file.contents = new Buffer(adjusted);
2531

26-
function adjust(file){
27-
var css = file.contents.toString();
32+
cb(null, file);
33+
});
2834

29-
return rework(css)
30-
.use(url(function(url) {
31-
if(!/^(data|\/|\w+:\/\/)/.test(url)){
32-
var assetPath = path.join(path.dirname(file.path), url);
33-
var assetFolder = path.basename(path.dirname(assetPath))
34-
var newPath = path.join(filePath, assetFolder, path.basename(assetPath)).replace(/[\#|\?].*$/, '');
35+
function adjust(file) {
36+
var css = file.contents.toString();
3537

36-
mkpath(path.dirname(newPath), function(err) {
37-
if (err) {
38-
throw err;
39-
}
38+
return rework(css)
39+
.use(url(function (url) {
40+
if (!/^(data|\/|\w+:\/\/)/.test(url)) {
41+
var assetPath = path.join(path.dirname(file.path), url);
42+
var assetFolder = md5(path.relative(process.cwd(), path.dirname(assetPath)));
43+
var IsExclude = false;
4044

41-
fs.createReadStream(assetPath.replace(/[\#|\?].*$/, '')).pipe(fs.createWriteStream(newPath));
42-
});
45+
for (var index in options.exclude) {
46+
if (options.exclude[index] === assetPath.substr(0, options.exclude[index].length)) {
47+
IsExclude = true;
48+
break;
49+
}
50+
}
4351

44-
url = path.normalize(path.join(publicPrefix, assetFolder, path.basename(assetPath))).replace(/\\/g, '/');
45-
}
52+
var newPath = !IsExclude
53+
? path.normalize(path.join(options.output, assetFolder, path.basename(assetPath)))
54+
: path.normalize(assetPath)
55+
;
4656

47-
return url;
48-
}))
49-
.toString();
50-
}
57+
if (
58+
(!IsExclude && !fileExists(newPath))
59+
||
60+
(!IsExclude && options.overwrite)
61+
) {
62+
mkpath(path.dirname(newPath), function (err) {
63+
if (err) {
64+
throw err;
65+
}
66+
67+
fs
68+
.createReadStream(assetPath.replace(/[\#|\?].*$/, ''))
69+
.pipe(fs.createWriteStream(newPath.replace(/[\#|\?].*$/, '')))
70+
;
71+
});
72+
73+
}
74+
75+
url = path.relative(options.output, newPath);
76+
}
77+
78+
return url;
79+
}))
80+
.toString();
81+
}
5182
};

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
{
22
"name": "gulp-css-rebase",
3-
"version": "1.0.0",
4-
"description": "",
3+
"version": "2.0.0",
4+
"description": "Rewrite url references in CSS and copyed CSS assets",
55
"main": "index.js",
66
"dependencies": {
7+
"file-exists": "^2.0.0",
78
"fs": "0.0.1-security",
9+
"md5": "^2.2.1",
10+
"merge": "^1.2.0",
811
"mkpath": "^1.0.0",
912
"path": "^0.12.7",
1013
"rework": "^1.0.1",
@@ -28,8 +31,10 @@
2831
"keywords": [
2932
"url-rebase",
3033
"gulp",
34+
"gulpplugin",
3135
"css",
3236
"url",
33-
"rebase"
37+
"rebase",
38+
"rewrite"
3439
]
3540
}

0 commit comments

Comments
 (0)