Skip to content
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

Fix path resolution relative to target file #43

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ gulp.task('default', function () {
});
```

**TIP: for a proper import inlining and url rebase, make sure you set the proper `base` for the input files.**
**TIP: above example will create file `out/styles/bundle.css`, using `out/` as the new base directory.**

## API

Expand Down
52 changes: 22 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,85 +8,79 @@ var parseImport = require('parse-import');
var reworkUrl = require('rework-plugin-url');
var defaults = require('lodash.defaults');

module.exports = function(destFile, options) {
module.exports = function (destFile, options) {
var buffer = [];
var firstFile, commonBase;
var destDir = path.dirname(destFile);
var hasFiles = false;
var destDir = path.dirname(path.resolve(destFile));
var urlImportRules = [];
options = defaults({}, options, {
inlineImports: true,
rebaseUrls: true,
includePaths: []
});

return through.obj(function(file, enc, cb) {
return through.obj(function (file, enc, cb) {
var processedCss;

if (file.isStream()) {
this.emit('error', new gutil.PluginError('gulp-concat-css', 'Streaming not supported'));
return cb();
}

if(!firstFile) {
firstFile = file;
commonBase = file.base;
}
hasFiles = true;

function urlPlugin(file) {
return reworkUrl(function(url) {
if(isUrl(url) || isDataURI(url) || path.extname(url) === '.css' || path.resolve(url) === url) {
return reworkUrl(function (url) {
if (isUrl(url) || isDataURI(url) || path.extname(url) === '.css' || path.resolve(url) === url) {
return url;
}

var resourceAbsUrl = path.relative(commonBase, path.resolve(path.dirname(file), url));
resourceAbsUrl = path.relative(destDir, resourceAbsUrl);
var resourceAbsUrl = path.relative(destDir, path.resolve(path.dirname(file), url));
//not all systems use forward slash as path separator
//this is required by urls.
if(path.sep === '\\'){
if (path.sep === '\\') {
//replace with forward slash
resourceAbsUrl = resourceAbsUrl.replace(/\\/g, '/');
}
return resourceAbsUrl;
});
}


function collectImportUrls(styles) {
var outRules = [];
styles.rules.forEach(function(rule) {
if(rule.type !== 'import') {
styles.rules.forEach(function (rule) {
if (rule.type !== 'import') {
return outRules.push(rule);
}

var importData = parseImport('@import ' + rule.import + ';');
var importPath = importData && importData[0].path;
if(isUrl(importPath) || !options.inlineImports) {
if (isUrl(importPath) || !options.inlineImports) {
return urlImportRules.push(rule);
}
return outRules.push(rule);
});
styles.rules = outRules;
}


function processNestedImport(contents) {
var rew = rework(contents,{source:this.source});//find the css file has syntax errors
if(options.rebaseUrls) {
var rew = rework(contents, { source: this.source });//find the css file has syntax errors
if (options.rebaseUrls) {
rew = rew.use(urlPlugin(this.source));
}
rew = rew.use(collectImportUrls);
return rew.toString();
}

try {
processedCss = rework(String(file.contents||""),{source:file.path});//find the css file has syntax errors
if(options.rebaseUrls) {
processedCss = rework(String(file.contents || ""), { source: file.path });//find the css file has syntax errors
if (options.rebaseUrls) {
processedCss = processedCss.use(urlPlugin(file.path));
}

processedCss = processedCss.use(collectImportUrls);

if(options.inlineImports) {
if (options.inlineImports) {
processedCss = processedCss.use(reworkImport({
path: [
'.',
Expand All @@ -98,26 +92,24 @@ module.exports = function(destFile, options) {
}

processedCss = processedCss.toString();
} catch(err) {
} catch (err) {
this.emit('error', new gutil.PluginError('gulp-concat-css', err));
return cb();
}

buffer.push(processedCss);
cb();
}, function(cb) {
if(!firstFile) {
}, function (cb) {
if (!hasFiles) {
return cb();
}

var contents = urlImportRules.map(function(rule) {
var contents = urlImportRules.map(function (rule) {
return '@import ' + rule.import + ';';
}).concat(buffer).join(gutil.linefeed);

var concatenatedFile = new gutil.File({
base: firstFile.base,
cwd: firstFile.cwd,
path: path.join(firstFile.base, destFile),
path: path.resolve(destFile),
contents: new Buffer(contents)
});
this.push(concatenatedFile);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"mocha": "^2.1.0"
},
"scripts": {
"test": "node_modules/mocha/bin/mocha test/*.js --reporter spec"
"test": "mocha test/*.js --reporter spec"
},
"keywords": [
"gulpplugin",
Expand Down