UNPKG

4.25 kBJavaScriptView Raw
1var through = require( 'through2' );
2var watchify = require( 'watchify' );
3var trim = require( 'jq-trim' );
4
5var parseAsRegex = function parseAsRegex( regex ) {
6 if ( typeof regex === 'string' ) {
7 return new RegExp( regex );
8 }
9 return regex;
10};
11
12module.exports = function ( browserifyOpts, opts, argv ) {
13 browserifyOpts = browserifyOpts || { };
14 opts = opts || { };
15 var hash = require( 'hash-string' );
16
17 var xtend = require( 'xtend' );
18
19 var id = 'persistify_' + hash( process.cwd() + trim( opts.cacheId ) );
20 var depsCacheId = 'deps-cx-' + id;
21 var cacheDir = opts.cacheDir;
22
23 var flatCache = require( 'flat-cache' );
24 var fileEntryCache = require( 'file-entry-cache' );
25
26 if ( opts.recreate ) {
27 flatCache.clearCacheById( id, cacheDir );
28 flatCache.clearCacheById( depsCacheId, cacheDir );
29 }
30 // load the cache with id
31 var cache = flatCache.load( id, cacheDir );
32
33 // load the file entry cache with id, or create a new
34 // one if the previous one doesn't exist
35 var depsCacheFile = fileEntryCache.create( depsCacheId, cacheDir );
36
37 var ignoreCache = false;
38
39 // if the command was specified this can be used
40 // as the cache buster
41 if ( opts.command ) {
42 var configHashPersisted = cache.getKey( 'configHash' );
43 var hashOfConfig = hash( opts.command );
44
45 ignoreCache = configHashPersisted !== hashOfConfig;
46
47 if ( ignoreCache ) {
48 cache.setKey( 'configHash', hashOfConfig );
49 }
50 }
51
52 var defaultCache = {
53 cache: {},
54 packageCache: {}
55 };
56
57 var persistifyCache = ignoreCache ? defaultCache : (cache.getKey( 'persistifyArgs' ) || defaultCache);
58
59 browserifyOpts.cache = persistifyCache.cache;
60 browserifyOpts.packageCache = persistifyCache.packageCache;
61
62 var fromArgs = require( 'browserify/bin/args' );
63
64 var b = argv ? fromArgs( argv, browserifyOpts ) : require( 'browserify' )( browserifyOpts );
65
66 function normalizeCache( removeDeletedOnly ) {
67 var cachedFiles = Object.keys( browserifyOpts.cache );
68
69 var neverCache = opts.neverCache;
70 if ( neverCache ) {
71 if ( !Array.isArray( neverCache ) ) {
72 neverCache = [ neverCache ];
73 }
74 cachedFiles.forEach( function ( file ) {
75 for (var i = 0; i < neverCache.length; i++) {
76 var regex = parseAsRegex( neverCache[ 0 ] );
77
78 if ( file.match( regex ) ) {
79 b.emit( 'skip:cache', file );
80 delete browserifyOpts.cache[ file ]; //esfmt-ignore-line
81 break;
82 }
83 }
84 } );
85 }
86
87 var res = depsCacheFile.analyzeFiles( cachedFiles );
88
89 var changedFiles = res.changedFiles;
90 var notFoundFiles = res.notFoundFiles;
91
92 var changedOrNotFound = removeDeletedOnly ? notFoundFiles : changedFiles.concat( notFoundFiles );
93
94 if ( changedOrNotFound.length > 0 ) {
95 changedOrNotFound.forEach( function ( file ) {
96 delete browserifyOpts.cache[ file ]; //esfmt-ignore-line
97 } );
98 }
99
100 cache.setKey( 'persistifyArgs', { cache: browserifyOpts.cache, packageCache: browserifyOpts.packageCache } );
101 }
102
103 normalizeCache();
104
105 function collect() {
106 b.pipeline.get( 'deps' ).push( through.obj( function ( row, enc, next ) {
107 var file = row.expose ? b._expose[ row.id ] : row.file;
108 persistifyCache.cache[ file ] = {
109 source: row.source,
110 deps: xtend( { }, row.deps )
111 };
112 b.emit( 'file', file ); // attempt to make latest watchify to work with persistify
113 this.push( row );
114 next();
115 } ) );
116 }
117
118 if ( opts.watch ) {
119 b = watchify( b );
120 }
121
122 collect();
123 b.on( 'reset', collect );
124
125 var oldBundle = b.bundle;
126 b.bundle = function () {
127 var start = Date.now();
128 var stream;
129 try {
130 stream = oldBundle.apply( b, arguments );
131 stream.on( 'error', function ( err ) {
132 console.error( err ); // eslint-disable-line
133 } );
134 stream.on( 'end', function () {
135 setTimeout( function () {
136 normalizeCache( true /* remove deleted only*/ );
137 depsCacheFile.reconcile();
138 cache.save();
139 }, 0 );
140
141 var end = Date.now() - start;
142 b.emit( 'bundle:done', end );
143 } );
144 } catch (ex) {
145 console.error( ex ); // eslint-disable-line
146 }
147
148 return stream;
149 };
150
151 return b;
152};