Skip to content

Added convertToAbsolute option to accommodate css relocation without image relocation #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,28 @@ production. I want this final file for the css above:
}
```

## Using the 'convertToAbsolute' option

This option will prefix the rebased relative URL with a forward slash to convert it into an absolute path. This is to accommodate situations where the CSS files are being transferred into another directory (such as a temp directory for generated files) but the images aren't being relocated.

By default the option is turned off.

### Example

```javascript
var gulp = require('gulp');
var rebaseUrls = require('gulp-css-rebase-urls');

gulp.task('default', function () {
gulp.src('css/**/*.css')
.pipe(rebaseUrls({
convertToAbsolute: true,
}))
.pipe(concat('style.css')) // <-- just an example
.pipe(gulp.dest('./build/'));
});
```



Pull requests and use cases welcome.
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ var rebaseUrls = function(css, options) {
if (process.platform === 'win32') {
p = p.replace(/\\/g, '/');
}

if (options.convertToAbsolute) {
p = "/" + p;
}
return p;
}))
.toString();
Expand All @@ -30,11 +32,13 @@ var rebaseUrls = function(css, options) {
module.exports = function(options) {
options = options || {};
var root = options.root || '.';
var convertToAbsolute = options.convertToAbsolute || false;

return through.obj(function(file, enc, cb) {
var css = rebaseUrls(file.contents.toString(), {
currentDir: path.dirname(file.path),
root: path.join(file.cwd, root)
root: path.join(file.cwd, root),
convertToAbsolute: convertToAbsolute
});

file.contents = new Buffer(css);
Expand Down