@@ -6,56 +6,74 @@ var applySourceMap = require('vinyl-sourcemaps-apply');
66var objectAssign = require ( 'object-assign' ) ;
77var replaceExt = require ( 'replace-ext' ) ;
88var babel = require ( 'babel-core' ) ;
9+ var lru = require ( 'lru-cache' ) ;
10+ var crypto = require ( 'crypto' ) ;
911
1012function replaceExtension ( fp ) {
1113 return path . extname ( fp ) ? replaceExt ( fp , '.js' ) : fp ;
1214}
1315
14- module . exports = function ( opts ) {
15- opts = opts || { } ;
16-
17- return through . obj ( function ( file , enc , cb ) {
18- if ( file . isNull ( ) ) {
19- cb ( null , file ) ;
20- return ;
21- }
22-
23- if ( file . isStream ( ) ) {
24- cb ( new gutil . PluginError ( 'gulp-babel' , 'Streaming not supported' ) ) ;
25- return ;
26- }
27-
28- try {
29- var fileOpts = objectAssign ( { } , opts , {
30- filename : file . path ,
31- filenameRelative : file . relative ,
32- sourceMap : Boolean ( file . sourceMap ) ,
33- sourceFileName : file . relative ,
34- sourceMapTarget : file . relative
35- } ) ;
36-
37- var res = babel . transform ( file . contents . toString ( ) , fileOpts ) ;
38-
39- if ( file . sourceMap && res . map ) {
40- res . map . file = replaceExtension ( res . map . file ) ;
41- applySourceMap ( file , res . map ) ;
16+ module . exports = function ( lruOptions ) {
17+ lruOptions = lruOptions || 500 ;
18+
19+ var cache = lru ( lruOptions ) ;
20+
21+ return function ( opts ) {
22+ opts = opts || { } ;
23+
24+ return through . obj ( function ( file , enc , cb ) {
25+ if ( file . isNull ( ) ) {
26+ cb ( null , file ) ;
27+ return ;
4228 }
4329
44- if ( ! res . ignored ) {
45- file . contents = new Buffer ( res . code ) ;
46- file . path = replaceExtension ( file . path ) ;
30+ if ( file . isStream ( ) ) {
31+ cb ( new gutil . PluginError ( 'gulp-babel' , 'Streaming not supported' ) ) ;
32+ return ;
4733 }
4834
49- file . babel = res . metadata ;
35+ try {
36+ var fileOpts = objectAssign ( { } , opts , {
37+ filename : file . path ,
38+ filenameRelative : file . relative ,
39+ sourceMap : Boolean ( file . sourceMap ) ,
40+ sourceFileName : file . relative ,
41+ sourceMapTarget : file . relative
42+ } ) ;
43+
44+ var resultHash = crypto . createHash ( 'sha1' ) . update ( file . contents . toString ( ) ) . digest ( 'hex' ) ;
45+
46+ var res ;
47+
48+ res = cache . get ( resultHash ) ;
5049
51- this . push ( file ) ;
52- } catch ( err ) {
53- this . emit ( 'error' , new gutil . PluginError ( 'gulp-babel' , err , {
54- fileName : file . path ,
55- showProperties : false
56- } ) ) ;
57- }
50+ if ( ! res ) {
51+ res = babel . transform ( file . contents . toString ( ) , fileOpts ) ;
52+
53+ cache . set ( resultHash , res ) ;
54+ }
55+
56+ if ( file . sourceMap && res . map ) {
57+ res . map . file = replaceExtension ( res . map . file ) ;
58+ applySourceMap ( file , res . map ) ;
59+ }
60+
61+ if ( ! res . ignored ) {
62+ file . contents = new Buffer ( res . code ) ;
63+ file . path = replaceExtension ( file . path ) ;
64+ }
65+
66+ file . babel = res . metadata ;
67+
68+ this . push ( file ) ;
69+ } catch ( err ) {
70+ this . emit ( 'error' , new gutil . PluginError ( 'gulp-babel' , err , {
71+ fileName : file . path ,
72+ showProperties : false
73+ } ) ) ;
74+ }
5875
59- cb ( ) ;
60- } ) ;
76+ cb ( ) ;
77+ } ) ;
78+ } ;
6179} ;
0 commit comments