1
1
module . exports = function ( grunt ) {
2
- var blocks = require ( 'blocks' ) ;
2
+ var esprima = require ( 'esprima' ) ;
3
+ var escodegen = require ( 'escodegen' ) ;
4
+ var estrvarse = require ( 'estraverse' ) ;
5
+ var definedModuleNames = { } ;
3
6
var requirejsConfig = { } ;
4
7
var requirejsOptions = {
5
8
baseUrl : 'src' ,
@@ -8,7 +11,7 @@ module.exports = function (grunt) {
8
11
optimize : 'none' ,
9
12
skipSemiColonInsertion : true ,
10
13
onBuildWrite : function ( name , path , contents ) {
11
- var rdefineEnd = / \} \) ; [ ^ } \w ] * $ / ;
14
+ var rdefineEnd = / \} \) ; [ ^ } \w ] * $ / ;
12
15
13
16
if ( / .\/ v a r \/ / . test ( path ) ) {
14
17
contents = contents
@@ -17,6 +20,46 @@ module.exports = function (grunt) {
17
20
18
21
} else {
19
22
contents = contents
23
+ . replace ( / \/ \* \s * E x c l u d e S t a r t \s * \* \/ [ \w \W ] * ?\/ \* \s * E x c l u d e E n d \s * \* \/ / ig, '' )
24
+ . replace ( / \/ \/ \s * B u i l d E x c l u d e \n \r ? [ \w \W ] * ?\n \r ? / ig, '' ) ;
25
+ var ast = esprima . parse ( contents , {
26
+ tokens : true ,
27
+ comment : true ,
28
+ range : true
29
+ } ) ;
30
+
31
+ estrvarse . attachComments ( ast , ast . comments , ast . tokens ) ;
32
+
33
+ if ( ast . body [ 0 ] . expression . callee . name == 'define' ) {
34
+ var moduleExpression = findModuleExpressionArgument ( ast . body [ 0 ] . expression . arguments ) ;
35
+ if ( ! moduleExpression || ! moduleExpression . body . body [ 0 ] || moduleExpression . body . body [ 0 ] . type == 'ReturnStatement' ) {
36
+ // Null out empty define statements e.g. define(['./query/ready', '...'])
37
+ // and expresions without an expression or only an return statement e.g. define([], function () { return blocks; })
38
+ contents = '' ;
39
+ } else {
40
+ var moduleName ;
41
+ try {
42
+ moduleName = findModuleExportIdentifier ( moduleExpression . body . body ) || / \/ ( \w + ) .? j ? s ? $ / . exec ( name ) [ 1 ] ;
43
+ } catch ( e ) { }
44
+ if ( moduleName && definedModuleNames [ moduleName ] && definedModuleNames [ moduleName ] != path ) {
45
+ grunt . fail . warn ( '[NamingConflict]: Module ' + path + ' tried to define ' + moduleName + ' which is already defined by ' + definedModuleNames [ moduleName ] + ' !' ) ;
46
+ } else if ( moduleName ) {
47
+ definedModuleNames [ moduleName ] = path ;
48
+ }
49
+ ast = wrapModuleAst ( moduleExpression , moduleName ) ;
50
+ contents = escodegen . generate ( ast , {
51
+ format : {
52
+ indent : {
53
+ style : ' ' ,
54
+ base : 0 ,
55
+ adjustMultilineComment : true
56
+ }
57
+ } ,
58
+ comment : true
59
+ } ) ;
60
+ }
61
+ }
62
+ /* contents = contents
20
63
.replace(/\s*return\s+[^\}]+(\ }\);[^\w\ }]*)$/, '$1')
21
64
// Multiple exports
22
65
.replace(/\s*exports\.\w+\s*=\s*\w+;/g, '');
@@ -29,19 +72,52 @@ module.exports = function (grunt) {
29
72
// Remove anything wrapped with
30
73
// /* ExcludeStart */ /* ExcludeEnd */
31
74
// or a single line directly after a // BuildExclude comment
32
- contents = contents
33
- . replace ( / \/ \* \s * E x c l u d e S t a r t \s * \* \/ [ \w \W ] * ?\/ \* \s * E x c l u d e E n d \s * \* \/ / ig, '' )
34
- . replace ( / \/ \/ \s * B u i l d E x c l u d e \n \r ? [ \w \W ] * ?\n \r ? / ig, '' ) ;
35
75
36
76
// Remove empty definitions
37
- contents = contents
38
- . replace ( / d e f i n e \( \[ [ ^ \] ] + \] \) [ \W \n ] + $ / , '' ) ;
77
+ /* contents = contents
78
+ .replace(/define\(\[[^\]]+\]\)[\W\n]+$/, '');*/
79
+
39
80
}
40
81
41
82
return contents ;
42
83
}
43
84
} ;
44
85
86
+ function findModuleExportIdentifier ( module ) {
87
+ for ( var i = module . length - 1 ; i >= 0 ; i -- ) {
88
+ var expression = module [ i ] ;
89
+ if ( expression . type == 'ReturnStatement' ) {
90
+ return expression . argument . name ;
91
+ }
92
+ }
93
+ throw new Error ( 'No return statement' ) ;
94
+ }
95
+
96
+ function findModuleExpressionArgument ( args ) {
97
+ for ( var i in args ) {
98
+ var arg = args [ i ] ;
99
+ if ( arg . type == 'FunctionExpression' ) {
100
+ return arg ;
101
+ }
102
+ }
103
+ }
104
+
105
+
106
+ function wrapModuleAst ( node , exportName ) {
107
+ var wrapedModule ;
108
+ var bodyNode ;
109
+ if ( exportName ) {
110
+ wrapedModule = esprima . parse ( 'var ' + exportName + ' = (function () { })();' ) ;
111
+ bodyNode = wrapedModule . body [ 0 ] . declarations [ 0 ] . init . callee . body ;
112
+ } else {
113
+ wrapedModule = esprima . parse ( '(function () { })();' ) ;
114
+ bodyNode = wrapedModule . body [ 0 ] . expression . callee . body ;
115
+ }
116
+ // insert body of the original "define"-function to the
117
+ bodyNode . body = node . body . body ;
118
+ return wrapedModule ;
119
+ }
120
+
45
121
var names = [ 'query' , 'mvc' , 'node' ] ;
46
122
names . forEach ( function ( name ) {
47
123
grunt . config . set ( 'name' , name ) ;
@@ -54,7 +130,7 @@ module.exports = function (grunt) {
54
130
grunt . registerTask ( 'build' , function ( ) {
55
131
var tasks = [ ] ;
56
132
for ( var i = 0 ; i < arguments . length ; i ++ ) {
57
- tasks . push ( 'requirejs:' + arguments [ i ] )
133
+ tasks . push ( 'requirejs:' + arguments [ i ] ) ;
58
134
}
59
135
grunt . task . run ( tasks . length ? tasks : 'requirejs' ) ;
60
136
} ) ;
0 commit comments