1- var path = require ( 'path' )
21var fs = require ( 'graceful-fs' )
32var crypto = require ( 'crypto' )
43var mm = require ( 'minimatch' )
5- var extensions = require ( './binary-extensions.json' ) . extensions
4+ var isBinaryFile = require ( 'isbinaryfile' )
65
76var log = require ( './logger' ) . create ( 'preprocess' )
87
@@ -12,10 +11,30 @@ var sha1 = function (data) {
1211 return hash . digest ( 'hex' )
1312}
1413
15- var isBinary = Object . create ( null )
16- extensions . forEach ( function ( extension ) {
17- isBinary [ '.' + extension ] = true
18- } )
14+ var createNextProcessor = function ( preprocessors , file , done ) {
15+ return function nextPreprocessor ( error , content ) {
16+ // normalize B-C
17+ if ( arguments . length === 1 && typeof error === 'string' ) {
18+ content = error
19+ error = null
20+ }
21+
22+ if ( error ) {
23+ file . content = null
24+ file . contentPath = null
25+ return done ( error )
26+ }
27+
28+ if ( ! preprocessors . length ) {
29+ file . contentPath = null
30+ file . content = content
31+ file . sha = sha1 ( content )
32+ return done ( )
33+ }
34+
35+ preprocessors . shift ( ) ( content , file , nextPreprocessor )
36+ }
37+ }
1938
2039var createPreprocessor = function ( config , basePath , injector ) {
2140 var alreadyDisplayedWarnings = { }
@@ -51,64 +70,49 @@ var createPreprocessor = function (config, basePath, injector) {
5170
5271 return function preprocess ( file , done ) {
5372 patterns = Object . keys ( config )
54- var thisFileIsBinary = isBinary [ path . extname ( file . originalPath ) . toLowerCase ( ) ]
55- var preprocessors = [ ]
56-
57- var nextPreprocessor = function ( error , content ) {
58- // normalize B-C
59- if ( arguments . length === 1 && typeof error === 'string' ) {
60- content = error
61- error = null
62- }
63-
64- if ( error ) {
65- file . content = null
66- file . contentPath = null
67- return done ( error )
68- }
6973
70- if ( ! preprocessors . length ) {
71- file . contentPath = null
72- file . content = content
73- file . sha = sha1 ( content )
74- return done ( )
74+ return fs . readFile ( file . originalPath , function ( err , buffer ) {
75+ if ( err ) {
76+ throw err
7577 }
7678
77- preprocessors . shift ( ) ( content , file , nextPreprocessor )
78- }
79-
80- for ( var i = 0 ; i < patterns . length ; i ++ ) {
81- if ( mm ( file . originalPath , patterns [ i ] , { dot : true } ) ) {
82- if ( thisFileIsBinary ) {
83- log . warn ( 'Ignoring preprocessing (%s) %s because it is a binary file.' ,
84- config [ patterns [ i ] ] . join ( ', ' ) , file . originalPath )
85- } else {
86- config [ patterns [ i ] ] . forEach ( function ( name ) {
87- var p = instances [ name ]
88- if ( p == null ) {
89- p = instantiatePreprocessor ( name )
90- }
79+ isBinaryFile ( buffer , buffer . length , function ( err , thisFileIsBinary ) {
80+ if ( err ) {
81+ throw err
82+ }
9183
92- if ( p == null ) {
93- if ( ! alreadyDisplayedWarnings [ name ] ) {
94- alreadyDisplayedWarnings [ name ] = true
95- log . warn ( 'Failed to instantiate preprocessor %s' , name )
96- }
97- return
84+ var preprocessors = [ ]
85+ var nextPreprocessor = createNextProcessor ( preprocessors , file , done )
86+
87+ for ( var i = 0 ; i < patterns . length ; i ++ ) {
88+ if ( mm ( file . originalPath , patterns [ i ] , { dot : true } ) ) {
89+ if ( thisFileIsBinary ) {
90+ log . warn ( 'Ignoring preprocessing (%s) %s because it is a binary file.' ,
91+ config [ patterns [ i ] ] . join ( ', ' ) , file . originalPath )
92+ } else {
93+ config [ patterns [ i ] ] . forEach ( function ( name ) {
94+ var p = instances [ name ]
95+ if ( p == null ) {
96+ p = instantiatePreprocessor ( name )
97+ }
98+
99+ if ( p == null ) {
100+ if ( ! alreadyDisplayedWarnings [ name ] ) {
101+ alreadyDisplayedWarnings [ name ] = true
102+ log . warn ( 'Failed to instantiate preprocessor %s' , name )
103+ }
104+ return
105+ }
106+
107+ instances [ name ] = p
108+ preprocessors . push ( p )
109+ } )
98110 }
99-
100- instances [ name ] = p
101- preprocessors . push ( p )
102- } )
111+ }
103112 }
104- }
105- }
106113
107- return fs . readFile ( file . originalPath , function ( err , buffer ) {
108- if ( err ) {
109- throw err
110- }
111- nextPreprocessor ( null , thisFileIsBinary ? buffer : buffer . toString ( ) )
114+ nextPreprocessor ( null , thisFileIsBinary ? buffer : buffer . toString ( ) )
115+ } )
112116 } )
113117 }
114118}
0 commit comments