@@ -40,7 +40,7 @@ module.exports = function (grunt) {
40
40
} ,
41
41
42
42
// Tags Regular Expressions
43
- regexTagStartTemplate = "<!--\\s*%parseTag%:(\\w+)\\s*(inline)?\\s*(optional)?\\s*(recursive)?\\s*(noprocess)?\\s*(defer)? \\s*(async)? \\s*([^\\s]*) \\s*-->" , // <!-- build:{type} [ inline] [ optional] [ recursive] {name} --> {} required [] optional
43
+ regexTagStartTemplate = "<!--\\s*%parseTag%:(\\w+)\\s*(inline)?\\s*(optional)?\\s*(recursive)?\\s*(noprocess)?\\s*([^ \\s]*) \\s*(?:\\[(.*)\\])? \\s*-->" , // <!-- build:{type} ( inline) ( optional) ( recursive) {name} [attributes...] --> {} required () optional
44
44
regexTagEndTemplate = "<!--\\s*\\/%parseTag%\\s*-->" , // <!-- /build -->
45
45
regexTagStart = "" ,
46
46
regexTagEnd = "" ,
@@ -68,9 +68,8 @@ module.exports = function (grunt) {
68
68
optional : ! ! tagStart [ 3 ] ,
69
69
recursive : ! ! tagStart [ 4 ] ,
70
70
noprocess : ! ! tagStart [ 5 ] ,
71
- defer : ! ! tagStart [ 6 ] ,
72
- async : ! ! tagStart [ 7 ] ,
73
- name : tagStart [ 8 ] ,
71
+ name : tagStart [ 6 ] ,
72
+ attributes : tagStart [ 7 ] ,
74
73
lines : [ ]
75
74
} ;
76
75
tags . push ( last ) ;
@@ -122,16 +121,16 @@ module.exports = function (grunt) {
122
121
}
123
122
else {
124
123
// if paths are named, just take values
125
- files = _ . values ( src ) ;
124
+ files = _ . values ( src ) ;
126
125
}
127
126
}
128
127
129
128
if ( ! Array . isArray ( files ) ) {
130
129
files = [ files ] ;
131
- }
130
+ }
132
131
133
132
return params . processPath ( files , params , opt ) ;
134
- }
133
+ }
135
134
}
136
135
function validateBlockAlways ( tag ) {
137
136
return true ;
@@ -146,46 +145,59 @@ module.exports = function (grunt) {
146
145
147
146
//#region Processors Methods
148
147
149
- function createTemplateData ( options , extend ) {
148
+ function createTemplateData ( options , src , attrs ) {
149
+ var extend = { } ;
150
+
151
+ if ( src ) {
152
+ extend . src = src ;
153
+ }
154
+
155
+ if ( attrs ) {
156
+ extend . attributes = attrs ;
157
+ }
158
+
150
159
return {
151
- data : extend ? _ . extend ( { } , options . data , extend ) : options . data
160
+ data : _ . extend ( { } , options . data , extend )
152
161
} ;
153
162
}
154
- function processTemplate ( template , options , extend ) {
155
- return grunt . template . process ( template , createTemplateData ( options , extend ) ) ;
163
+ function processTemplate ( template , options , src , attrs ) {
164
+ return grunt . template . process ( template , createTemplateData ( options , src , attrs ) ) ;
156
165
}
157
- function processHtmlTagTemplate ( options , extend ) {
158
- var template = templates [ options . type + ( options . inline ? "-inline" : "" ) ] ;
159
- if ( ! options . inline ) {
160
- var result = template . replace ( "<%= src %>" , extend . src ) ;
166
+ function createAttributes ( options , src ) {
167
+ var attrs = options . attributes || "" ;
161
168
162
- if ( options . type === "style" && path . extname ( extend . src ) === ".less" ) {
163
- result = result . replace ( 'rel="stylesheet"' , 'rel="stylesheet/less"' ) ;
169
+ if ( options . type === "script" ) {
170
+ attrs = 'type="text/javascript" ' + attrs ;
171
+ }
172
+ else if ( options . type === "style" && ! options . inline ) {
173
+ if ( path . extname ( src ) === ".less" ) {
174
+ attrs = 'type="text/css" rel="stylesheet/less" ' + attrs ;
164
175
}
165
-
166
- if ( options . type === "script" ) {
167
- if ( options . async ) {
168
- result = result . replace ( ">" , " async>" ) ;
169
- }
170
- else if ( options . defer ) {
171
- result = result . replace ( ">" , " defer>" ) ;
172
- }
176
+ else {
177
+ attrs = 'type="text/css" rel="stylesheet" ' + attrs ;
173
178
}
174
-
175
- return result ;
176
179
}
177
- else if ( options . noprocess ) {
178
- return template . replace ( "<%= src %>" , extend . src ) ;
180
+
181
+ return attrs . trim ( ) ;
182
+ }
183
+ function processHtmlTagTemplate ( options , src ) {
184
+ var template = templates [ options . type + ( options . inline ? "-inline" : "" ) ] ,
185
+ attrs = createAttributes ( options , src ) ;
186
+
187
+ if ( ! options . inline || options . noprocess ) {
188
+ return template
189
+ . replace ( "<%= src %>" , src )
190
+ . replace ( "<%= attributes %>" , attrs ) ;
179
191
}
180
192
else {
181
- return processTemplate ( template , options , extend ) ;
193
+ return processTemplate ( template , options , src , attrs ) ;
182
194
}
183
195
}
184
196
185
197
function processHtmlTag ( options ) {
186
198
if ( options . inline ) {
187
199
var content = options . files . map ( grunt . file . read ) . join ( EOL ) ;
188
- return processHtmlTagTemplate ( options , { src : content } ) ;
200
+ return processHtmlTagTemplate ( options , content ) ;
189
201
}
190
202
else {
191
203
return options . files . map ( function ( f ) {
@@ -196,8 +208,8 @@ module.exports = function (grunt) {
196
208
if ( options . prefix ) {
197
209
url = URL . resolve ( options . prefix . replace ( / \\ / g, '/' ) , url ) ;
198
210
}
199
-
200
- return processHtmlTagTemplate ( options , { src : url } ) ;
211
+
212
+ return processHtmlTagTemplate ( options , url ) ;
201
213
} ) . join ( EOL ) ;
202
214
}
203
215
}
@@ -208,10 +220,10 @@ module.exports = function (grunt) {
208
220
209
221
var
210
222
templates = {
211
- 'script' : '<script type="text/javascript" src="<%= src %>"></script>' ,
212
- 'script-inline' : '<script type="text/javascript" ><%= src %></script>' ,
213
- 'style' : '<link type="text/css" rel="stylesheet" href="<%= src %>" />' ,
214
- 'style-inline' : '<style><%= src %></style>'
223
+ 'script' : '<script <%= attributes %> src="<%= src %>"></script>' ,
224
+ 'script-inline' : '<script <%= attributes %> ><%= src %></script>' ,
225
+ 'style' : '<link <%= attributes %> href="<%= src %>" />' ,
226
+ 'style-inline' : '<style <%= attributes %> ><%= src %></style>'
215
227
} ,
216
228
validators = {
217
229
script : validateBlockWithName ,
@@ -273,45 +285,45 @@ module.exports = function (grunt) {
273
285
var tags = getBuildTags ( content ) ,
274
286
config = grunt . config ( ) ;
275
287
276
- tags . forEach ( function ( tag ) {
277
- var raw = tag . lines . join ( EOL ) ,
278
- result = "" ,
279
- tagFiles = validators . validate ( tag , params ) ;
280
-
281
- if ( tagFiles ) {
282
- var options = _ . extend ( { } , tag , {
283
- data : _ . extend ( { } , config , params . data ) ,
284
- files : tagFiles ,
285
- dest : dest ,
286
- prefix : params . prefix ,
288
+ tags . forEach ( function ( tag ) {
289
+ var raw = tag . lines . join ( EOL ) ,
290
+ result = "" ,
291
+ tagFiles = validators . validate ( tag , params ) ;
292
+
293
+ if ( tagFiles ) {
294
+ var options = _ . extend ( { } , tag , {
295
+ data : _ . extend ( { } , config , params . data ) ,
296
+ files : tagFiles ,
297
+ dest : dest ,
298
+ prefix : params . prefix ,
287
299
relative : params . relative ,
288
300
params : params
289
- } ) ;
301
+ } ) ;
290
302
291
- result = processors . transform ( options ) ;
292
- }
303
+ result = processors . transform ( options ) ;
304
+ }
293
305
else if ( tagFiles === false ) {
294
306
grunt . log . warn ( "Unknown tag detected: '" + tag . type + "'" ) ;
295
307
296
308
if ( ! params . allowUnknownTags ) {
297
309
grunt . fail . warn ( "Use 'parseTag' or 'allowUnknownTags' options to avoid this issue" ) ;
298
310
}
299
311
}
300
- else if ( tag . optional ) {
312
+ else if ( tag . optional ) {
301
313
if ( params . logOptionals ) {
302
314
grunt . log . warn ( "Tag with type: '" + tag . type + "' and name: '" + tag . name + "' is not configured in your Gruntfile.js but is set optional, deleting block !" ) ;
303
315
}
304
- }
305
- else {
306
- grunt . fail . warn ( "Tag with type '" + tag . type + "' and name: '" + tag . name + "' is not configured in your Gruntfile.js !" ) ;
307
- }
316
+ }
317
+ else {
318
+ grunt . fail . warn ( "Tag with type '" + tag . type + "' and name: '" + tag . name + "' is not configured in your Gruntfile.js !" ) ;
319
+ }
308
320
309
- content = content . replace ( raw , function ( ) { return result } ) ;
310
- } ) ;
321
+ content = content . replace ( raw , function ( ) { return result } ) ;
322
+ } ) ;
311
323
312
- if ( params . beautify ) {
313
- content = beautify . html ( content , _ . isObject ( params . beautify ) ? params . beautify : { } ) ;
314
- }
324
+ if ( params . beautify ) {
325
+ content = beautify . html ( content , _ . isObject ( params . beautify ) ? params . beautify : { } ) ;
326
+ }
315
327
316
328
return content ;
317
329
}
0 commit comments