1010
1111var fs = require ( 'fs' ) ;
1212var path = require ( 'path' ) ;
13- var _ = require ( 'lodash' ) ;
1413var chalk = require ( 'chalk' ) ;
1514
1615var globalDependenciesSorted ;
1716var ignorePath ;
1817var fileTypes ;
1918
20- var fileTypesDefault = {
21- html : {
22- block : / ( ( [ \t ] * ) < ! - - \s * b o w e r : * ( \S * ) \s * - - > ) ( \n | \r | .) * ?( < ! - - \s * e n d b o w e r \s * - - > ) / gi,
23- detect : {
24- js : / < s c r i p t .* s r c = [ ' " ] ( .+ ) [ ' " ] > / gi,
25- css : / < l i n k .* h r e f = [ ' " ] ( .+ ) [ ' " ] / gi
26- } ,
27- replace : {
28- js : '<script src="{{filePath}}"></script>' ,
29- css : '<link rel="stylesheet" href="{{filePath}}" />'
30- }
31- } ,
32-
33- jade : {
34- block : / ( ( [ \t ] * ) \/ \/ \s * b o w e r : * ( \S * ) ) ( \n | \r | .) * ?( \/ \/ \s * e n d b o w e r ) / gi,
35- detect : {
36- js : / s c r i p t \( .* s r c = [ ' " ] ( .+ ) [ ' " ] > / gi,
37- css : / l i n k \( h r e f = [ ' " ] ( .+ ) [ ' " ] / gi
38- } ,
39- replace : {
40- js : 'script(src=\'{{filePath}}\')' ,
41- css : 'link(rel=\'stylesheet\', href=\'{{filePath}}\')'
42- }
43- } ,
44-
45- sass : {
46- block : / ( ( [ \t ] * ) \/ \/ \s * b o w e r : * ( \S * ) ) ( \n | \r | .) * ?( \/ \/ \s * e n d b o w e r ) / gi,
47- detect : {
48- css : / @ i m p o r t \s [ ' " ] ( .+ ) [ ' " ] / gi,
49- sass : / @ i m p o r t \s [ ' " ] ( .+ ) [ ' " ] / gi,
50- scss : / @ i m p o r t \s [ ' " ] ( .+ ) [ ' " ] / gi
51- } ,
52- replace : {
53- css : '@import {{filePath}}' ,
54- sass : '@import {{filePath}}' ,
55- scss : '@import {{filePath}}'
56- }
57- } ,
58-
59- scss : {
60- block : / ( ( [ \t ] * ) \/ \/ \s * b o w e r : * ( \S * ) ) ( \n | \r | .) * ?( \/ \/ \s * e n d b o w e r ) / gi,
61- detect : {
62- css : / @ i m p o r t \s [ ' " ] ( .+ ) [ ' " ] / gi,
63- sass : / @ i m p o r t \s [ ' " ] ( .+ ) [ ' " ] / gi,
64- scss : / @ i m p o r t \s [ ' " ] ( .+ ) [ ' " ] / gi
65- } ,
66- replace : {
67- css : '@import "{{filePath}}";' ,
68- sass : '@import "{{filePath}}";' ,
69- scss : '@import "{{filePath}}";'
70- }
71- } ,
72-
73- yaml : {
74- block : / ( ( [ \t ] * ) # \s * b o w e r : * ( \S * ) ) ( \n | \r | .) * ?( # \s * e n d b o w e r ) / gi,
75- detect : {
76- js : / - \s ( .+ ) / gi,
77- css : / - \s ( .+ ) / gi
78- } ,
79- replace : {
80- js : '- {{filePath}}' ,
81- css : '- {{filePath}}'
82- }
83- }
84- } ;
85-
86- fileTypesDefault . yml = fileTypesDefault . yaml ;
87- fileTypesDefault . htm = fileTypesDefault . html ;
88- fileTypesDefault [ 'default' ] = fileTypesDefault . html ;
8919
9020/**
9121 * Find references already on the page, not in a Bower block.
@@ -154,11 +84,16 @@ var injectScripts = function (filePath) {
15484 var fileType = fileTypes [ fileExt ] || fileTypes [ 'default' ] ;
15585 var returnType = / \r \n / . test ( contents ) ? '\r\n' : '\n' ;
15686
157- fs . writeFileSync ( filePath , contents . replace (
87+ var newContents = contents . replace (
15888 fileType . block ,
15989 replaceIncludes ( filePath , fileType , returnType )
160- ) ) ;
161- console . log ( 'File ' + chalk . cyan ( filePath ) + ' modified.' ) ;
90+ ) ;
91+
92+ if ( contents !== newContents ) {
93+ fs . writeFileSync ( filePath , newContents ) ;
94+
95+ console . log ( chalk . cyan ( filePath ) + ' modified.' ) ;
96+ }
16297} ;
16398
16499
@@ -184,19 +119,7 @@ module.exports = function inject(config) {
184119
185120 globalDependenciesSorted = config . get ( 'global-dependencies-sorted' ) ;
186121 ignorePath = config . get ( 'ignore-path' ) ;
187- fileTypes = _ . clone ( fileTypesDefault , true ) ;
188-
189- _ ( config . get ( 'file-types' ) ) . each ( function ( fileTypeConfig , fileType ) {
190- fileTypes [ fileType ] = fileTypes [ fileType ] || { } ;
191- _ . each ( fileTypeConfig , function ( config , configKey ) {
192- if ( _ . isPlainObject ( fileTypes [ fileType ] [ configKey ] ) ) {
193- fileTypes [ fileType ] [ configKey ] =
194- _ . assign ( fileTypes [ fileType ] [ configKey ] , config ) ;
195- } else {
196- fileTypes [ fileType ] [ configKey ] = config ;
197- }
198- } ) ;
199- } ) ;
122+ fileTypes = config . get ( 'file-types' ) ;
200123
201124 if ( stream . src ) {
202125 config . set ( 'stream' , {
0 commit comments