Skip to content

Commit e66a771

Browse files
committed
Persistent cache (#426)
* Persistent cache Cache transform results in `~/.cache/bankai` using [browserify-persist-fs](https://github.com/martinheidegger/browserify-persist-fs). This should not be merged until browserify/module-deps#127 is, since currently this does not work great with watchify (if I understand the linked PR correctly). Anyway, on the `./example` folder in this repo, results: ```bash $ time npm run build # cold run npm run build 13.79s user 0.35s system 159% cpu 8.843 total $ time npm run build # warm run npm run build 9.64s user 0.30s system 170% cpu 5.818 total ``` This is already a pretty big win, but on a larger app it should be much much more noticeable still. * Add babelify and preset to persistent cache key * Move persistent cache creation to separate file so we can run gc on build
1 parent 3768270 commit e66a771

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

lib/graph-script.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var tinyify = require('tinyify')
1616
var glslify = require('glslify')
1717
var brfs = require('brfs')
1818

19+
var createPersistentCache = require('./persistent-cache')
1920
var ttyError = require('./tty-error')
2021
var exorcise = require('./exorcise')
2122

@@ -44,7 +45,7 @@ function node (state, createEdge) {
4445
var self = this
4546
var entry = state.metadata.entry
4647
var fullPaths = Boolean(state.metadata.fullPaths)
47-
var b = browserify(browserifyOpts([entry], fullPaths))
48+
var b = browserify(browserifyOpts([entry], fullPaths, createPersistentCache(state)))
4849
var shouldMinify = !state.metadata.watch
4950

5051
if (state.metadata.watch) {
@@ -56,6 +57,8 @@ function node (state, createEdge) {
5657
})
5758
}
5859

60+
// When adding a transform or plugin, also add it to the `getCacheId` function below!
61+
5962
b.ignore('sheetify/insert')
6063
b.transform(sheetify)
6164
b.transform(glslify)
@@ -155,13 +158,14 @@ function node (state, createEdge) {
155158
}
156159
}
157160

158-
function browserifyOpts (entries, fullPaths) {
161+
function browserifyOpts (entries, fullPaths, persistentCache) {
159162
assert.ok(Array.isArray(entries), 'browserifyOpts: entries should be an array')
160163
return {
161164
debug: true,
162165
fullPaths: fullPaths,
163166
entries: entries,
164167
packageCache: {},
165-
cache: {}
168+
cache: {},
169+
persistentCache: persistentCache
166170
}
167171
}

lib/persistent-cache.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var path = require('path')
2+
var os = require('os')
3+
var browserifyPersistFs = require('browserify-persist-fs')
4+
5+
module.exports = function (state) {
6+
var cacheDir = path.join(os.homedir(), '.cache', 'bankai')
7+
return browserifyPersistFs(cacheDir, getCacheId(state))
8+
}
9+
10+
function getCacheId (state) {
11+
return {
12+
watch: state.metadata.watch,
13+
entry: state.metadata.entry,
14+
transforms: [
15+
'sheetify',
16+
'glslify',
17+
'tfilter',
18+
'brfs',
19+
'yo-yoify',
20+
'css-extract',
21+
'tinyify',
22+
'split-require',
23+
// TODO should probably add custom .babelrc here too if we can
24+
'babelify',
25+
'babel-preset-env'
26+
].map(version)
27+
}
28+
29+
function version (mod) {
30+
return require(mod + '/package.json').version
31+
}
32+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"brfs": "^1.4.4",
2626
"brotli": "^1.3.2",
2727
"browserify": "^16.0.0",
28+
"browserify-persist-fs": "^1.2.1",
2829
"buffer-graph": "^4.0.0",
2930
"clean-css": "^4.1.9",
3031
"concat-stream": "^1.6.0",

0 commit comments

Comments
 (0)