-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
194 lines (164 loc) · 7.04 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* NodeJS dependencies
*/
var fs = require('fs');
var fse = require('fs-extra');
var path = require('path');
var regexEscape = require('regex-escape');
/**
* Vendor dependencies
*/
var resolve = require('resolve');
var cssp = require('cssp');
var traverse = require('traverse');
/**
* Mark file recursively for @import statements
* @param {String} file Absolute path to .css file
* @param {Array} imports Array of files marked (used for recursion)
* @param {Boolean} isEntry 'true' if this is the entry CSS file, else 'false'
* @return
*/
function mapImports(file, imports, isEntry) {
var src = undefined;
var tree = undefined;
var base = path.dirname(file);
if (path.parse(file).ext === '.css' && fs.existsSync(file)) {
try {
src = fs.readFileSync(file, 'utf8');
tree = cssp.parse(src);
} catch (e) {
//TODO: handle error
}
} else {
//TODO: handle error
}
if (isEntry) {
imports.push(file);
}
traverse(tree).forEach(function (node) {
var self = this;
if (node !== 'atrules') {
return;
}
node = this.parent.node;
// ignore non import
if (node[1][0] !== 'atkeyword' && node[1][1][0] !== 'ident' &&
node[1][1][1] !== 'import') {
return;
}
// ignore non string imports
if (node[3][0] !== 'string') {
return;
}
// remove quotes from imported name
var name = node[3][1].replace(/["']/g, '');
var cssFilePath = path.resolve(base, name);
if (path.parse(cssFilePath).ext === '.css' && fs.existsSync(cssFilePath)) {
if (imports.indexOf(cssFilePath) <= -1) {
imports.push(cssFilePath);
// recursively mark all imported css files for processing
mapImports(cssFilePath, imports, false);
}
}
});
return imports;
};
function processImports(imports, cssDir, vendorDir) {
var fullSrc = '';
var processCount = 0;
imports.forEach(function (file) {
var src = fs.readFileSync(file, 'utf8');
var base = path.dirname(file);
var tree = cssp.parse(src);
var relativePath = path.relative(cssDir, file);
traverse(tree).forEach(function (node) {
var self = this;
// Remove @import statements.
if (node === 'atrules') {
var node = this.parent.node;
if (node[1][0] === 'atkeyword' && node[1][1][0] === 'ident' &&
node[1][1][1] === 'import') {
if (node[3][0] === 'string') {
self.parent.node.splice(0, self.parent.node.length);
}
}
}
// Process resource references
if (node === 'uri') {
var uriNode = this.parent.node;
var uriFile = uriNode[1][1].replace(/["']/g, '');
// Ignore URLs.
if (uriFile.indexOf(':') <= -1 || uriFile.substr(0, 2) === '//') {
// Convert directory based URIs ('images/hello.png' => './images/hello.png')
if (uriFile.substr(0, 3) !== '../' || uriFile.substr(0, 2) !== './') {
uriFile = './' + uriFile;
}
var uriParts = [];
var uriFilePath = uriFile;
var uriQuery = '';
// Fix to exclude query parameters from file names.
var uriContainsQ = uriFile.indexOf('?');
var uriContainsH = uriFile.indexOf('#');
// URI does contains either '?' or '#'
if (uriContainsQ !== -1 || uriContainsH !== -1) {
// URI contains both '?' and '#'
if (uriContainsQ !== -1 && uriContainsH !== -1) {
// Check if '#' occurs before '?'
if (uriContainsQ < uriContainsH) {
uriParts = uriFile.split('?');
uriFilePath = uriParts[0];
if (uriParts.length > 1) {
uriQuery = '?' + uriParts.slice(1, uriParts.length + 1).join('');
}
// '#' occurs before '?'
} else {
uriParts = uriFile.split('#');
uriFilePath = uriParts[0];
if (uriParts.length > 1) {
uriQuery = '#' + uriParts.slice(1, uriParts.length + 1).join('');
}
}
} else if (uriContainsQ !== -1) {
//URI contains only '?
uriParts = uriFile.split('?');
uriFilePath = uriParts[0];
if (uriParts.length > 1) {
uriQuery = '?' + uriParts.slice(1, uriParts.length + 1).join('');
}
} else {
//URI contains only '#'
uriParts = uriFile.split('#');
uriFilePath = uriParts[0];
if (uriParts.length > 1) {
uriQuery = '#' + uriParts.slice(1, uriParts.length + 1).join('');
}
}
}
// Leave absolute paths alone.
if (!path.isAbsolute(uriFilePath)) {
var copyFrom = path.resolve(base, uriFilePath);
// Check if the resource actually exists.
if (fs.existsSync(copyFrom)) {
var flatUriPath = uriFilePath.replace(new RegExp(regexEscape('../'), 'g'), '').replace(new RegExp(regexEscape('./'), 'g'), '');
var copyTo = path.resolve(vendorDir, flatUriPath);
fse.copySync(copyFrom, copyTo);
var relativeUriPath = path.relative(cssDir, vendorDir) + '/' + flatUriPath + uriQuery;
self.parent.node[1][1] = '"./' + relativeUriPath + '"';
} else {
console.warn("!!WARN!! [css-assets]: A referenced resource was not found! ['" + uriFile + "' in " + file + "]");
}
}
}
}
});
fullSrc = fullSrc + '\n/* css-assets: "' + relativePath + '" */\n' + cssp.translate(tree).trim() + '\n';
processCount++;
});
fse.outputFileSync(cssDir + '/vendor.css', fullSrc);
return processCount;
}
// file should be /full/path/to/file.css
module.exports = function (file, cssDir, vendorDir) {
var cssImports = mapImports(file, [], true);
return processImports(cssImports, cssDir, vendorDir);
};