This repository has been archived by the owner on Dec 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
539 lines (422 loc) · 18.1 KB
/
main.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*jshint laxcomma:true */
!function() {
// Node deps
var path = require( 'path' );
// NPM deps
var glob = require( 'glob' )
, cssmin = require( 'ycssmin' ).cssmin
;
// Local deps
var log = require( './lib/logger' )
, fsh = require( './lib/fsh' )
;
// Matches single and double quotes
var rQuotes = /['"]/g;
// Captures the css asset path (no absolutes, bounding quotes, or spaces)
var rAssetURLs = /url\(\s*(?:['"])?(?!data|http:|https:|ftp:|\/\/|\/)([^'"\)\s]+)/g;
// Captures the `@import` path and media condition
var rImport = /(?!.*\*\/)@import\s*(?:url\()?\s*["']([^'"]*)['"]\s*\)?\s*(.*?);/;
// Global version of `rImport`
var rImportGlobal = new RegExp( rImport.toString().slice( 1, rImport.toString().length - 1 ), [ 'g' ] );
// Logs error and exits
function error( msg ) {
msg = '\x1B[31m' + '[Error] ' + msg + '\x1B[39m';
console.log( msg );
process.exit(1);
}
/**
* CSSCat
*
* @module CSSCat
* @constructor
*/
function CSSCat( config ) {
this.options = {
dir: '',
files: [],
exclude: /^\.|\/\.|node_modules/,
ignore: [],
optimize: true,
log: true,
debug: false
/* Potential Hooks
concatenate: function( content ) {
// alter content
return content
},
optimize: function ( content ) {
// optimize content
return content
}
*/
}
/*
files = {
data: {
'absolute/path/to/parent/file.css': {
skip: false,
imports: {
'absolute/path/to/parent/file-1.css': {
statement: '@import url( \'dependency/file.css\' ) screen and ( min-width: 100px );',
path: 'dependency/file.css',
rule: '@import url( \'dependency/file.css\' );',
condition: 'screen and ( min-width: 100px )'
}
}
}
},
graph: {
'absolute/path/to/parent/file-1.css': [
'absolute/path/to/dependency/file-4.css',
'absolute/path/to/dependency/file-3.css'
]
},
order: [
'absolute/path/to/parent/file-1.css',
'absolute/path/to/parent/file-2.css',
'absolute/path/to/parent/file-3.css',
'absolute/path/to/parent/file-4.css',
]
}
*/
this.files = {
data: {},
graph: {},
order: []
}
/*
TODO: Potential return values
- A `test` option for accessing private members.
- A `config` method for substituting internal methods (RequireJS needs Rhino support).
*/
}
CSSCat.prototype = {
/**
* Configure options and files.
*
* @method init
*
* @param config {Object}
* @param dir {String} CSS directory path.
* @param debug {Boolean} Log debugging info.
*/
init: function( config ) {
this.config( config );
this.createFileData();
this.process();
return {
data: this.files.data,
order: this.files.order
};
},
/**
* Description needed
*
* @method createFileData
*/
config: function( config ) {
// Extend options
for ( var key in this.options ) {
if ( key in config ) this.options[ key ] = config[ key ];
}
// Set up logger options
log.config( {
debug: this.options.debug,
level: this.options.log
});
if ( !this.options.dir && !this.options.files.length ) {
error( 'No `dir` or `files` option defined. CSScat requires at least one of the options be defined.' );
}
if ( this.options.dir ) {
this.options.dir = path.resolve( this.options.dir );
if ( !fsh.exists( this.options.dir ) ) error( 'Directory could not be found at: ' + this.options.dir );
}
log.info( 'CSSCat options extended.' );
log.debug( this.options, 'Options' );
},
/**
* Description needed
*
* @method createFileData
*/
createFileData: function() {
var list;
// Get the list of css files
list = this.options.files;
if ( !list.length ) {
log.info( 'Getting list of CSS files.' );
list = fsh.glob( '**/*.css', {
dir: this.options.dir,
exclude: this.options.exclude
});
}
if ( !list ) error( 'Could not find any css files in given directory.' );
log.debug( list, 'File Listing' );
// Build the various data structures
log.info( 'Creating the file data/graph objects:' );
this.files = this.buildFileObject( list );
log.debug( this.files.data, 'File Data' );
log.debug( this.files.graph, 'Dependency Graph' );
log.info( 'Adding files to the processing list (ordered by dependency):' );
this.files.order = this.sortByDependency( this.files.graph );
},
/**
* Description needed
*
* @method process
*/
process: function() {
var files = this.files.order
, withOptimization = this.options.optimize ? 'with' : 'without'
;
log.info( 'Handling imports with media conditions:' );
files.forEach( this.buildMediaBlock, this );
log.info( 'Concatenating files with their imports (' + withOptimization + ' optimization):' );
files.forEach( this.concatenate, this );
log.success( '\
{\n\
(*)\n\
J J\n\
CSSCat did her business without errors!');
},
/**
* Builds a files object and dependency graph.
*
* // data structure
* {
* 'absolute/path/to/parent/file.css': {
* skip: false,
* imports: {
* 'absolute/path/to/dependency/file.css': {
* statement: '@import url( \'dependency/file.css\' ) screen and ( min-width: 100px );',
* path: 'dependency/file.css',
* rule: '@import url( \'dependency/file.css\' );',
* condition: 'screen and ( min-width: 100px )'
* }
* }
* }
* }
*
* // graph structure
* {
* 'absolute/path/to/parent/file.css': [
* 'absolute/path/to/dependency/dep-1.css',
* 'absolute/path/to/dependency/dep-2.css'
* ]
* }
*
* @method buildFileObject
*
* @param list {Array} An array of file paths.
*
* @return {Object} An object that contains the `data` and `graph` objects.
*/
buildFileObject: function( list ) {
var file
, absPath
, matches
, curFile
, curMatch
, isIgnored
, colonIndex
, fileContents
, absPathImport
, origPathImport
, firstSlashIndex
, data = {}
, graph = {}
;
list.forEach( function( file ) {
absPath = this.options.dir ? path.join( this.options.dir, file ) : path.resolve( file );
fileContents = fsh.read( absPath );
if ( !fileContents ) error( 'File does not exist: "' + absPath + '"' );
graph[ absPath ] = [];
curFile = data[ absPath ] = {};
curFile.skip = false;
var matches = fileContents.match( rImportGlobal );
if ( matches ) {
curFile.imports = {};
matches.forEach( function( theMatch ) {
curMatch = theMatch.match( rImport );
origPathImport = curMatch[1].replace( rQuotes, '' );
absPathImport = path.resolve( path.dirname( absPath ), origPathImport ).replace( /\\/g, '/' );
firstSlashIndex = origPathImport.indexOf( '/' );
colonIndex = origPathImport.indexOf( ':' );
isIgnored = this.options.ignore.indexOf( origPathImport ) !== -1 ? true : false;
// Skip files that start with '/' or have a protocol.
if ( isIgnored || origPathImport.charAt( 0 ) === '/' || ( colonIndex !== -1 && colonIndex < firstSlashIndex ) ) {
curFile.skip = true;
absPathImport = origPathImport;
}
graph[ absPath ].push( absPathImport );
curFile.imports[ absPathImport ] = {
statement: curMatch[0],
path: curMatch[1],
rule: [ "@import '", curMatch[1], "';" ].join(''),
condition: curMatch[2]
}
}, this );
}
if ( curFile.skip ) {
log.warn( ' [warning] The file "' + file + '" has dependencies that are unresolvable or configured to be ignored.' );
}
else log.minor( ' [parsed] ' + absPath );
}, this );
return {
data: data,
graph: graph
}
},
/**
* Create a flat list of files ordered by their dependencies.
*
* @param graph {Object} Each file path as a key with its value an array of files it dependeds on.
*/
sortByDependency: function( graph ) {
var sorted = []
, visited = {}
, self = this
, count = 0
;
var checkSkippedDeps = function( imports ) {
var fileObj
, toSkip = false
;
if ( !imports ) return toSkip;
Object.keys( imports ).forEach( function( fileName ) {
fileObj = self.files.data[ fileName ];
if ( !fileObj ) toSkip = true;
else if ( fileObj && fileObj.imports ) {
toSkip = checkSkippedDeps( self.files.data[ fileName ].imports );
}
else if ( self.files.data[ fileName ].skip ) toSkip = true;
});
return toSkip;
};
var visit = function( name, ancestors ) {
var toSkipFile = false
, parentFile = self.files.data[ name ]
, imports = parentFile.imports
;
if ( visited[ name ] ) return;
if ( !Array.isArray( ancestors ) ) ancestors = [];
ancestors.push( name );
visited[ name ] = true;
// Skip the file if it can't be processed.
if ( parentFile.skip ) return;
toSkipFile = checkSkippedDeps( imports );
if ( toSkipFile ) return parentFile.skip = true;
graph[ name ].forEach( function( dep ) {
// If already in ancestors, a closed chain exists.
if ( ancestors.indexOf( dep ) >= 0 ) {
error( 'Circular dependency found: "' + dep + '" is required by "' + name + '"( ' + ancestors.join( ' -> ' ) + ' )' );
}
visit( dep, ancestors.slice( 0 ) );
});
sorted.push( name );
log.major( ' [' + ++count + '] ' + name );
}
Object.keys( graph ).forEach( visit );
return sorted;
},
/**
* Wraps `@media` blocks around each `@import` dependency that uses a media query.
*
* @param thePath {String} The absolute path to the file. Maps to the key in the `files.data` object.
*/
buildMediaBlock: function( thePath ) {
var content
, mediaBlock
, importFile
, mediaImports = []
, fileData = this.files.data[ thePath ]
;
// Make sure there are `@import` statements
if ( fileData.skip || !fileData.imports ) return log.minor( ' [none] ' + thePath );
content = fsh.read( thePath );
for ( var file in fileData.imports ) {
importFile = fileData.imports[ file ];
if ( importFile.condition ) mediaImports.push( file );
}
// Make sure the `@import` statements have media conditions
if ( !mediaImports.length ) return log.minor( ' [none] ' + thePath );
mediaImports.forEach( function( file ) {
importFile = fileData.imports[ file ];
// Create the `@media` block
mediaBlock = [
'@media ', importFile.condition, ' {\n',
importFile.rule, '\n',
'}'
].join('');
// Update the content
content = content.replace( importFile.statement, mediaBlock );
}, this );
// Write the new content to the file
fsh.write( thePath, content );
log.major( ' [meow] ' + thePath );
},
/**
* Find and replace all `@import` statements with content.
*
* @method concatenate
*/
concatenate: function( thePath ) {
var content
, pattern
, importContent
, opts = this.options
, fileData = this.files.data[ thePath ]
;
// Skip if no `@import` statements and no optimization needed
if ( fileData.skip || ( !opts.optimize && !fileData.imports ) ) return log.minor( ' [none] ' + thePath );
content = fsh.read( thePath );
// Make sure there are `@import` statements
if ( fileData.imports ) {
for ( var importPath in fileData.imports ) {
pattern = rImport;
importContent = fsh.read( importPath );
// Fix asset paths
importContent = this.resolvePaths( thePath, importPath, importContent );
// Update the content
content = content.replace( pattern, importContent );
}
}
if ( this.options.optimize ) content = cssmin( content );
// Write the new content to the file
fsh.write( thePath, content );
log.major( ' [meow] ' + thePath );
},
/**
* Resolve asset paths to the parent that imports them.
*
* @method resolvePaths
*
* @param parentPath {String} Path to file that contains the `@import` statement.
* @param childPath {String} Path to file being imported.
* @param content {String} Content of file being imported.
*/
resolvePaths: function( parentPath, childPath, content ) {
var absAssetPath
, relAssetPath
, self = this
;
content = content.replace( rAssetURLs, function ( match, assetPath ) {
absAssetPath = path.resolve( childPath, assetPath );
relAssetPath = path.relative( parentPath, absAssetPath );
return match.replace( assetPath, relAssetPath ).replace( /\\/g, '/' );
});
return content;
}
}
var api = new CSSCat();
module.exports = {
init: function ( config ) {
var api = new CSSCat();
return api.init( config )
},
test: {
rAssetURLs: rAssetURLs,
rImportGlobal: rImportGlobal
}
}
}();