1
1
/* jshint node: true */
2
2
var markdown = require ( 'node-markdown' ) . Markdown ;
3
+ var fs = require ( 'fs' ) ;
3
4
4
5
module . exports = function ( grunt ) {
5
6
@@ -29,6 +30,9 @@ module.exports = function(grunt) {
29
30
modules : 'angular.module("ui.bootstrap", [<%= srcModules %>]);' ,
30
31
tplmodules : 'angular.module("ui.bootstrap.tpls", [<%= tplModules %>]);' ,
31
32
all : 'angular.module("ui.bootstrap", ["ui.bootstrap.tpls", <%= srcModules %>]);' ,
33
+ cssInclude : '' ,
34
+ cssFileBanner : '/* Include this file in your html if you are using the CSP mode. */\n\n' ,
35
+ cssFileDest : '<%= dist %>/<%= filename %>-<%= pkg.version %>-csp.css' ,
32
36
banner : [ '/*' ,
33
37
' * <%= pkg.name %>' ,
34
38
' * <%= pkg.homepage %>\n' ,
@@ -54,14 +58,16 @@ module.exports = function(grunt) {
54
58
concat : {
55
59
dist : {
56
60
options : {
57
- banner : '<%= meta.banner %><%= meta.modules %>\n'
61
+ banner : '<%= meta.banner %><%= meta.modules %>\n' ,
62
+ footer : '<%= meta.cssInclude %>'
58
63
} ,
59
64
src : [ ] , //src filled in by build task
60
65
dest : '<%= dist %>/<%= filename %>-<%= pkg.version %>.js'
61
66
} ,
62
67
dist_tpls : {
63
68
options : {
64
- banner : '<%= meta.banner %><%= meta.all %>\n<%= meta.tplmodules %>\n'
69
+ banner : '<%= meta.banner %><%= meta.all %>\n<%= meta.tplmodules %>\n' ,
70
+ footer : '<%= meta.cssInclude %>'
65
71
} ,
66
72
src : [ ] , //src filled in by build task
67
73
dest : '<%= dist %>/<%= filename %>-tpls-<%= pkg.version %>.js'
@@ -246,6 +252,7 @@ module.exports = function(grunt) {
246
252
moduleName : enquote ( 'ui.bootstrap.' + name ) ,
247
253
displayName : ucwords ( breakup ( name , ' ' ) ) ,
248
254
srcFiles : grunt . file . expand ( 'src/' + name + '/*.js' ) ,
255
+ cssFiles : grunt . file . expand ( 'src/' + name + '/*.css' ) ,
249
256
tplFiles : grunt . file . expand ( 'template/' + name + '/*.html' ) ,
250
257
tpljsFiles : grunt . file . expand ( 'template/' + name + '/*.html.js' ) ,
251
258
tplModules : grunt . file . expand ( 'template/' + name + '/*.html' ) . map ( enquote ) ,
@@ -259,6 +266,17 @@ module.exports = function(grunt) {
259
266
. map ( grunt . file . read ) . join ( '\n' )
260
267
}
261
268
} ;
269
+
270
+ var styles = {
271
+ css : [ ] ,
272
+ js : [ ]
273
+ } ;
274
+ module . cssFiles . forEach ( processCSS . bind ( null , styles , true ) ) ;
275
+ if ( styles . css . length ) {
276
+ module . css = styles . css . join ( '\n' ) ;
277
+ module . cssJs = styles . js . join ( '\n' ) ;
278
+ }
279
+
262
280
module . dependencies . forEach ( findModule ) ;
263
281
grunt . config ( 'modules' , grunt . config ( 'modules' ) . concat ( module ) ) ;
264
282
}
@@ -322,6 +340,17 @@ module.exports = function(grunt) {
322
340
} )
323
341
) ;
324
342
343
+ var cssStrings = _ . flatten ( _ . compact ( _ . pluck ( modules , 'css' ) ) ) ;
344
+ var cssJsStrings = _ . flatten ( _ . compact ( _ . pluck ( modules , 'cssJs' ) ) ) ;
345
+ if ( cssStrings . length ) {
346
+ grunt . config ( 'meta.cssInclude' , cssJsStrings . join ( '\n' ) ) ;
347
+
348
+ grunt . file . write ( grunt . config ( 'meta.cssFileDest' ) , grunt . config ( 'meta.cssFileBanner' ) +
349
+ cssStrings . join ( '\n' ) ) ;
350
+
351
+ grunt . log . writeln ( 'File ' + grunt . config ( 'meta.cssFileDest' ) + ' created' ) ;
352
+ }
353
+
325
354
var moduleFileMapping = _ . clone ( modules , true ) ;
326
355
moduleFileMapping . forEach ( function ( module ) {
327
356
delete module . docs ;
@@ -374,9 +403,40 @@ module.exports = function(grunt) {
374
403
var genRawFilesJs = require ( './misc/raw-files-generator' ) ;
375
404
376
405
genRawFilesJs ( grunt , jsFilename , _ . flatten ( grunt . config ( 'concat.dist_tpls.src' ) ) ,
377
- grunt . config ( 'meta.banner' ) ) ;
406
+ grunt . config ( 'meta.banner' ) , grunt . config ( 'meta.cssFileBanner' ) ) ;
378
407
} ) ;
379
408
409
+ /**
410
+ * Logic from AngularJS
411
+ * https://github.com/angular/angular.js/blob/36831eccd1da37c089f2141a2c073a6db69f3e1d/lib/grunt/utils.js#L121-L145
412
+ */
413
+ function processCSS ( state , minify , file ) {
414
+ /* jshint quotmark: false */
415
+ var css = fs . readFileSync ( file ) . toString ( ) ,
416
+ js ;
417
+ state . css . push ( css ) ;
418
+
419
+ if ( minify ) {
420
+ css = css
421
+ . replace ( / \r ? \n / g, '' )
422
+ . replace ( / \/ \* .* ?\* \/ / g, '' )
423
+ . replace ( / : \s + / g, ':' )
424
+ . replace ( / \s * \{ \s * / g, '{' )
425
+ . replace ( / \s * \} \s * / g, '}' )
426
+ . replace ( / \s * \, \s * / g, ',' )
427
+ . replace ( / \s * \; \s * / g, ';' ) ;
428
+ }
429
+ //escape for js
430
+ css = css
431
+ . replace ( / \\ / g, '\\\\' )
432
+ . replace ( / ' / g, "\\'" )
433
+ . replace ( / \r ? \n / g, '\\n' ) ;
434
+ js = "!angular.$$csp() && angular.element(document).find('head').prepend('<style type=\"text/css\">" + css + "</style>');" ;
435
+ state . js . push ( js ) ;
436
+
437
+ return state ;
438
+ }
439
+
380
440
function setVersion ( type , suffix ) {
381
441
var file = 'package.json' ;
382
442
var VERSION_REGEX = / ( [ \' | \" ] v e r s i o n [ \' | \" ] [ ] * : [ ] * [ \' | \" ] ) ( [ \d | . ] * ) ( - \w + ) * ( [ \' | \" ] ) / ;
0 commit comments