UNPKG

6.43 kBMarkdownView Raw
1[![NPM Version](http://img.shields.io/npm/v/persistify.svg?style=flat)](https://npmjs.org/package/persistify)
2
3# persistify
4`persistify` is a wrapper over watchify and browserify to make it easy to make incremental builds without having to rely on the `watch` mode for scenarios where a watch mode is not available. It reduces the build time from several seconds to milliseconds, without relying on a watch mode :)
5
6## Motivation
7Just wanted to have a good wrapper over browserify/watchify that allow me to make incremental builds even when not using the watch mode.
8
9**DISCLAIMER**: this is done persisting the watchify arguments to disk using the [flat-cache](https://npmjs.org/package/flat-cache) and [file-entry-cache](https://npmjs.org/package/file-entry-cache) modules. The best results happen when only a few files were modified. Worse case scenario is when all the files are modified. In average you should experience a very noticeable reduction. As always keep in mind that your mileage may vary.
10
11## TODO
12
13Add unit tests
14
15## Install
16
17```bash
18npm i -g persistify
19```
20
21## CLI options
22
23Apart from all the browserify and watchify options the following are also available:
24
25```bash
26Standard Options:
27
28 --outfile=FILE, -o FILE
29
30 This option is required. Write the browserify bundle to this file. If
31 the file contains the operators `|` or `>`, it will be treated as a
32 shell command, and the output will be piped to it (not available on
33 Windows).
34
35 --verbose, -v [default: false]
36
37 Show when a file was written and how long the bundling took (in
38 seconds).
39
40 --version
41
42 Show the persistify, watchify and browserify versions with their module paths.
43
44 --watch [default: false]
45
46 if true will use watchify instead of browserify
47
48 --recreate [default: false]
49
50 if set will recreate the cache. Useful when transforms and cached files refuse to cooperate
51
52 --never-cache, -n [default: null]
53
54 a string that will be converted to a regula expression. If a file matches the returned regExp
55 will never be saved in the cache. Even if the file is in the cache already it will be ignored.
56
57 More than one pattern to be ignored can be specified by repeating this option with other regex
58 value to ignore
59
60 --cache-id [default: null]
61
62 If you're running multiple persistify instances from the same directory, use this to
63 differentiate them.
64
65 --cache-dir [default: node_modules/flat-cache/.cache]
66
67 By default, the cache is saved to the default directory that flat-cache sets. This sets a custom directory for the cache files.
68
69```
70
71## Examples
72
73```bash
74# this will browserify src/foo.js and move it to dist/foo.js
75# the cache is constructed the first time the command is run so this might take a few
76# seconds depending on the complexity of the files you want to browserify
77persistify src/foo.js -o dist/foo.js
78
79# next builds will be benefited by the cache
80# noticeable reducing the building time
81persistify src/foo.js -o dist/foo.js
82
83# reconstruct the cache, this useful when a transform file has changed or
84# the cache just started to behave like a spoiled child
85persistify src/foo.js -o dist/foo.js --recreate
86
87# this will use the cache and watchify to provide faster startup times on watch mode
88persistify src/foo.js -o dist/foo.js --watch
89
90# this will just use the cache and use a transform
91# (all the parameters are just passed to browserify
92# so it should work with any transform)
93persistify src/foo.js -t babelify -o dist/foo.js --watch
94
95# this will just use the cache and use two transforms
96# but will never add to the cache any files that match the `m.less` extension
97# since those files can also require files and those files won't be cached
98# this is the safer way to prevent those changes to be skipped because of the cache
99persistify src/foo.js -t babelify -t simpless -o dist/foo.js -n '\.less$'
100```
101
102## As a node module
103
104```javascript
105var persistify = require( 'persistify' );
106
107var b = persistify( {
108 //browserify options here. e.g
109 // debug: true
110 }, { watch: true } );
111
112b.add( './demo/dep1.js' );
113
114b.on( 'bundle:done', function ( time ) {
115 console.log( 'time', time );
116} );
117
118b.on( 'error', function ( err ) {
119 console.log( 'error', err );
120} );
121
122function doBundle() {
123 b.bundle( function ( err, buff ) {
124 if ( err ) {
125 throw err;
126 }
127 require( 'fs' ).writeFileSync( './dist/bundle.js', buff.toString() );
128 } );
129
130}
131
132doBundle();
133
134b.on( 'update', function () {
135 doBundle();
136} );
137
138```
139
140## FAQ
141
142### My less files are not detected as changed when using a transformation like `lessify`. Why?
143
144Because those files are not loaded thru browserify so the cache will ignore them. use `-n, --never-cache` to specify certain files (or file types) to never be cached.
145
146**Example: Using the cli**
147
148```bash
149# the following will exclude files ending in `.less` from being kept in the cache
150persistify src/foo.js -t lessify -o dist/foo.js -n '\.less$'
151```
152
153**Example: Using the node api**
154```javascript
155var persistify = require( 'persistify' );
156
157var b = persistify( {
158 //browserify options here. e.g
159 // debug: true
160 }, {
161 neverCache: [/\.less$/] // using the node api
162 } );
163
164b.add( './demo/dep1.js' );
165
166// when a file is ignored from the cache
167// a skip:cache event is fired on the bundle instance
168b.on('skip:cache', function (file) {
169 console.log( 'skipping the cache for', file);
170});
171
172b.on( 'error', function ( err ) {
173 console.log( 'error', err );
174} );
175
176function doBundle() {
177 b.bundle( function ( err, buff ) {
178 if ( err ) {
179 throw err;
180 }
181 require( 'fs' ).writeFileSync( './dist/bundle.js', buff.toString() );
182 } );
183}
184
185doBundle();
186```
187
188### My build does not include the latest changes to my files! not detecting changed files?
189
190Mmm... that's weird, but the option `--recreate` should destroy the cache and create it again which most of the times should fix your issue.
191
192### I have added a new transform and the build is not using its magic!
193
194~Since persistify will only work on the files that have changed, and adding a transform
195does not cause a file change it is better to just use `--recreate` after adding a new trasform or plugin~
196
197Latest version of `persistify` will ignore the cache if the command used to execute it changes.
198
199## Changelog
200
201[Changelog](./changelog.md)
202
203## License
204
205[MIT](./LICENSE)