-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
335 lines (287 loc) · 8.58 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* Shameless adapted from [Eyeglass](https://github.com/sass-eyeglass/eyeglass)
* because I wanted a general-use import-once importer for Node
**/
'use strict';
var fs = require('fs'),
yaml = require('js-yaml'),
path = require('path');
/**
* All imports use the forward slash as a directory
* delimeter. This function converts to the filesystem's
* delimeter if it uses an alternate.
**/
var makeFsPath = function makeFsPath(importPath) {
var fsPath = importPath;
if (path.sep !== '/') {
fsPath = fsPath.replace(/\//, path.sep);
}
return fsPath;
};
/**
* Determines if a file should be imported or not
**/
var importOnce = function importOnce(data, done) {
if (this._importOnceCache[data.file]) {
done({
'contents': '',
'filename': 'already-imported:' + data.file
});
}
else {
this._importOnceCache[data.file] = true;
done(data);
}
};
/**
* Sass imports are usually in an abstract form in that
* they leave off the partial prefix and the suffix.
* This code creates the possible extensions, whether it is a partial
* and whether it is a directory index file having those
* same possible variations. If the import contains an extension,
* then it is left alone.
*
**/
var getFileNames = function getFileNames(abstractName) {
var names = [],
directory,
basename;
if ([ '.scss', '.sass' ].indexOf(path.extname(abstractName)) !== -1) {
directory = path.dirname(abstractName);
basename = path.basename(abstractName);
[ '', '_' ].forEach(function(prefix) {
names.push(path.join(directory, prefix + basename));
});
}
else if (path.extname(abstractName)) {
names.push(abstractName);
}
else {
directory = path.dirname(abstractName);
basename = path.basename(abstractName);
// Standard File Names
[ '', '_' ].forEach(function(prefix) {
[ '.scss', '.sass' ].forEach(function(ext) {
names.push(path.join(directory, prefix + basename + ext));
});
});
// Index Files
if (this.options.importOnce.index) {
[ '', '_' ].forEach(function(prefix) {
[ '.scss', '.sass' ].forEach(function(ext) {
names.push(path.join(abstractName, prefix + 'index' + ext));
});
});
}
// CSS Files
if (this.options.importOnce.css) {
names.push(abstractName + '.css');
}
}
return names;
};
/**
* Build list of potential Bower imports
**/
var getBowerNames = function getBowerNames(uri) {
var gfn = getFileNames.bind(this);
var bowerrc = path.resolve(process.cwd(), '.bowerrc'),
bowerPath = 'bower_components',
core = uri.split('/')[0],
paths = [],
results = [];
uri = makeFsPath(uri);
if (fs.existsSync(bowerrc)) {
bowerrc = JSON.parse(fs.readFileSync(bowerrc, 'utf-8'));
if (bowerrc.directory) {
bowerPath = bowerrc.directory;
}
}
// Resolve the path to the Bower repository;
bowerPath = path.resolve(process.cwd(), bowerPath);
// Basic, add import path-esque
paths.push(path.resolve(bowerPath, uri));
// For those projects that were Ruby gems and are now distributed through Bower
if (core !== '..' && core !== '.') {
paths.push(path.resolve(bowerPath, core, uri));
paths.push(path.resolve(bowerPath, core, 'stylesheets', uri));
paths.push(path.resolve(bowerPath, core + '-sass', 'stylesheets', uri));
paths.push(path.resolve(bowerPath, 'sass-' + core, 'stylesheets', uri));
paths.push(path.resolve(bowerPath, core, 'sass', uri));
paths.push(path.resolve(bowerPath, core + '-sass', 'sass', uri));
paths.push(path.resolve(bowerPath, 'sass-' + core, 'sass', uri));
if (this.options.importOnce.css) {
paths.push(path.resolve(bowerPath, core, 'dist', uri));
paths.push(path.resolve(bowerPath, core, 'dist', 'css', uri));
}
}
// Get the file names for all of the paths!
paths.forEach(function(pathName) {
results = results.concat(gfn(pathName));
});
return results;
};
/**
* getIn
**/
var getIncludePaths = function getIncludePaths(uri) {
// From https://github.com/sass/node-sass/issues/762#issuecomment-80580955
var arr = this.options.includePaths.split(path.delimiter),
gfn = getFileNames.bind(this),
paths = [];
arr.forEach(function(includePath) {
paths = paths.concat(gfn(path.resolve(process.cwd(), includePath, uri)));
});
return paths;
};
/**
* Parse JSON into Sass
**/
var parseJSON = function parseJSON(data, filename) {
var fileReturn = '$' + path.basename(filename).replace(path.extname(filename), '') + ':',
colors;
data = data.toString();
if ([ '.yml', '.yaml' ].indexOf(path.extname(filename)) !== -1) {
data = yaml.safeLoad(data);
data = JSON.stringify(data);
}
data = data.replace(/\{/g, '(');
data = data.replace(/\[/g, '(');
data = data.replace(/\}/g, ')');
data = data.replace(/\]/g, ')');
fileReturn += data;
if (fileReturn.substr(fileReturn.length - 1) === '\n') {
fileReturn = fileReturn.slice(0, -1);
}
fileReturn += ';';
//////////////////////////////
// Hex colors
//////////////////////////////
colors = fileReturn.match(/"(#([0-9a-f]{3}){1,2})"/g);
if (colors) {
colors.forEach(function (color) {
fileReturn = fileReturn.replace(color, color.slice(1, -1));
});
}
//////////////////////////////
// RGB/A Colors
//////////////////////////////
colors = fileReturn.match(/"(rgb|rgba)\((\d{1,3}), (\d{1,3}), (\d{1,3})\)"/g);
if (colors) {
colors.forEach(function (color) {
fileReturn = fileReturn.replace(color, color.slice(1, -1));
});
}
//////////////////////////////
// HSL/A Colors
//////////////////////////////
//////////////////////////////
// RGB/A Colors
//////////////////////////////
colors = fileReturn.match(/"(hsl|hsla)\((\d{1,3}), (\d{1,3}), (\d{1,3})\)"/g);
if (colors) {
colors.forEach(function (color) {
fileReturn = fileReturn.replace(color, color.slice(1, -1));
});
}
return new Buffer(fileReturn);
};
/**
* Asynchronously walks the file list until a match is found. If
* no matches are found, calls the callback with an error
**/
var readFirstFile = function readFirstFile(uri, filenames, css, cb, examinedFiles) {
var filename = filenames.shift();
examinedFiles = examinedFiles || [];
examinedFiles.push(filename);
fs.readFile(filename, function(err, data) {
if (err) {
if (filenames.length) {
readFirstFile(uri, filenames, css, cb, examinedFiles);
}
else {
cb(new Error('Could not import `' + uri + '` from any of the following locations:\n ' + examinedFiles.join('\n ')));
}
}
else {
if ([ '.js', '.json', '.yml', '.yaml' ].indexOf(path.extname(filename)) !== -1) {
data = parseJSON(data, filename);
}
cb(null, {
'contents': data.toString(),
'file': filename
});
}
});
};
// This is a bootstrap function for calling readFirstFile.
var readAbstractFile = function readAbstractFile(uri, abstractName, cb) {
var gfn = getFileNames.bind(this),
gip = getIncludePaths.bind(this),
gbn = getBowerNames.bind(this),
css = this.options.importOnce.css;
var files = gfn(abstractName);
if (this.options.includePaths) {
files = files.concat(gip(uri));
}
if (this.options.importOnce.bower) {
files = files.concat(gbn(uri));
}
readFirstFile(uri, files, css, cb);
};
/**
* Import the goodies!
**/
var importer = function importer(uri, prev, done) {
var isRealFile = fs.existsSync(prev),
io = importOnce.bind(this),
raf = readAbstractFile.bind(this),
file;
// Ensure options are available
if (!this.options.importOnce) {
this.options.importOnce = {};
}
// Set default index import
if (!this.options.importOnce.index) {
this.options.importOnce.index = false;
}
// Set default bower import
if (!this.options.importOnce.bower) {
this.options.importOnce.bower = false;
}
// Set default css import
if (!this.options.importOnce.css) {
this.options.importOnce.css = false;
}
// Create an import cache if it doesn't exist
if (!this._importOnceCache) {
this._importOnceCache = {};
}
if (isRealFile) {
file = path.resolve(path.dirname(prev), makeFsPath(uri));
raf(uri, file, function (err, data) {
if (err) {
console.log(err.toString());
done({});
}
else {
io(data, done);
}
});
}
else {
raf(uri, process.cwd(), function (err, data) {
if (err) {
console.log(err.toString());
done({});
}
else {
io(data, done);
}
});
}
};
/**
* Exports the importer
**/
module.exports = importer;