-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
90 lines (79 loc) · 2.3 KB
/
index.js
File metadata and controls
90 lines (79 loc) · 2.3 KB
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
var amdCssBuilder = require( "builder-amd-css" );
function cssDependencies( data, which ) {
var result = [],
regexp = new RegExp( "\\/\\/>>\\s*css\\." + which + ":(.*)", "g" );
data.replace( regexp, function( garbage, input ) {
input = input.split( "," ).map( trim );
result.push.apply( result, input );
});
return result.map(function( cssDependency ) {
return "\"css!" + cssDependency.replace( /\.css$/i, "" ) + "\"";
});
}
function jsDependencies( data ) {
var match = data.match( /define\(\ ?\[([^\]]*?)\]/ );
if ( match === null ) {
return [];
}
return match[ 1 ].replace( /\/\/.+/g, "" ).split( "," ).map( trim );
}
/**
* transform( data, which )
*
* @data [String] File content.
*
* @which [String] The name of the css bundle selector.
*
* Parse the syntax (a) and transform it into the AMD definition (b).
*
* a: //>> css.<which>: cssFile1, cssFile2, ...
* define([ "foo", "bar" ], function() { ... });
*
* b: define([ "css!cssFile1", "css!cssFile2", "css!...", "foo", "bar" ]);
*/
function transform( data, which ) {
var dependencies = [];
dependencies.push.apply( dependencies, cssDependencies( data, which ) );
dependencies.push.apply( dependencies, jsDependencies( data ) );
return "define([" + dependencies.join( ", " ) + "]);";
}
// Helper: trim.
function trim( string ) {
return string.trim();
}
/**
* transformFiles( files, which )
*
* @files [Object]
*
* @which [String] The name of the css bundle selector.
*
* Transform the content of each file according to `transform()`.
* The original files Object is preserved intact.
*/
function transformFiles( files, which ) {
var transformedFiles = {};
Object.keys( files ).forEach(function( path ) {
var data;
if ( /\.js$/.test( path ) ) {
data = files[ path ].toString( "utf-8" );
transformedFiles[ path ] = transform( data, which );
} else if ( /\.css$/.test( path ) ) {
transformedFiles[ path ] = files[ path ].toString( "utf-8" );
} else {
transformedFiles[ path ] = files[ path ];
}
});
return transformedFiles;
}
/**
* jqueryCssBuilder( files, which, config, callback )
*/
module.exports = function( files, which, config, callback ) {
try {
var transformedFiles = transformFiles( files, which );
amdCssBuilder( transformedFiles, config, callback );
} catch( error ) {
callback( error );
}
};