-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
136 lines (116 loc) · 3.71 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'use strict';
var PluginError = require('plugin-error');
var Vinyl = require('vinyl');
var path = require('path');
var rework = require('rework');
var reworkImport = require('rework-import');
var through = require('through2');
var parseImport = require('parse-import');
var reworkUrl = require('rework-plugin-url');
var defaults = require('lodash.defaults');
module.exports = function(destFile, options) {
var buffer = [];
var firstFile, commonBase;
var destDir = path.dirname(destFile);
var urlImportRules = [];
options = defaults({}, options, {
inlineImports: true,
rebaseUrls: true,
includePaths: [],
commonBase: null
});
return through.obj(function(file, enc, cb) {
var processedCss;
if (file.isStream()) {
this.emit('error', new PluginError('gulp-concat-css', 'Streaming not supported'));
return cb();
}
if(!firstFile) {
firstFile = file;
commonBase = options.commonBase || file.base;
}
function urlPlugin(file) {
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);
//not all systems use forward slash as path separator
//this is required by urls.
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') {
return outRules.push(rule);
}
var importData = parseImport('@import ' + rule.import + ';');
var importPath = importData && importData[0].path;
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) {
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 = processedCss.use(urlPlugin(file.path));
}
processedCss = processedCss.use(collectImportUrls);
if(options.inlineImports) {
processedCss = processedCss.use(reworkImport({
path: [
'.',
path.dirname(file.path)
].concat(options.includePaths),
transform: processNestedImport
}))
.toString();
}
processedCss = processedCss.toString();
} catch(err) {
this.emit('error', new PluginError('gulp-concat-css', err));
return cb();
}
buffer.push(processedCss);
cb();
}, function(cb) {
if(!firstFile) {
return cb();
}
var contents = urlImportRules.map(function(rule) {
return '@import ' + rule.import + ';';
}).concat(buffer).join('\n');
var concatenatedFile = new Vinyl({
base: firstFile.base,
cwd: firstFile.cwd,
path: path.join(firstFile.base, destFile),
contents: new Buffer(contents)
});
this.push(concatenatedFile);
cb();
});
};
function isUrl(url) {
return (/^([\w]+:)?\/\/./).test(url);
}
function isDataURI(url) {
return url && url.indexOf('data:') === 0;
}