Skip to content

Commit f9b9104

Browse files
committed
Implements LRU cache.
1 parent 1b4b0e1 commit f9b9104

File tree

3 files changed

+91
-51
lines changed

3 files changed

+91
-51
lines changed

index.js

Lines changed: 59 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,74 @@ var applySourceMap = require('vinyl-sourcemaps-apply');
66
var objectAssign = require('object-assign');
77
var replaceExt = require('replace-ext');
88
var babel = require('babel-core');
9+
var lru = require('lru-cache');
10+
var crypto = require('crypto');
911

1012
function 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
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"dependencies": {
3737
"babel-core": "^6.0.2",
3838
"gulp-util": "^3.0.0",
39+
"lru-cache": "^4.0.0",
3940
"object-assign": "^4.0.1",
4041
"replace-ext": "0.0.1",
4142
"through2": "^2.0.0",

readme.md

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,28 @@ gulp.task('default', () =>
3030

3131
## API
3232

33-
### babel([options])
34-
35-
#### options
36-
37-
See the Babel [options](https://babeljs.io/docs/usage/options/), except for `sourceMap` and `filename` which is handled for you.
38-
33+
```js
34+
const createBabel = require('gulp-babel');
35+
36+
/**
37+
* @see https://github.com/isaacs/node-lru-cache#options
38+
*/
39+
type lruOptions = {
40+
max: ?number,
41+
length: ?Function,
42+
dispose: ?Function,
43+
maxAge: ?number
44+
};
45+
46+
/**
47+
* @see See the Babel options (https://babeljs.io/docs/usage/options/), except for `sourceMap` and `filename` which is handled for you.
48+
*/
49+
type babelOptions = {};
50+
51+
const babel = createBabel({max: 500}: lruOptions);
52+
53+
babel({}: babelOptions);
54+
```
3955
4056
## Source Maps
4157
@@ -44,9 +60,11 @@ Use [gulp-sourcemaps](https://github.com/floridoo/gulp-sourcemaps) like this:
4460
```js
4561
const gulp = require('gulp');
4662
const sourcemaps = require('gulp-sourcemaps');
47-
const babel = require('gulp-babel');
63+
const createBabel = require('gulp-babel');
4864
const concat = require('gulp-concat');
4965

66+
const babel = createBabel();
67+
5068
gulp.task('default', () =>
5169
gulp.src('src/**/*.js')
5270
.pipe(sourcemaps.init())
@@ -59,7 +77,6 @@ gulp.task('default', () =>
5977
);
6078
```
6179
62-
6380
## Babel Metadata
6481
6582
Files in the stream are annotated with a `babel` property, which contains the metadata from [`babel.transform()`](https://babeljs.io/docs/usage/api/).
@@ -68,9 +85,11 @@ Files in the stream are annotated with a `babel` property, which contains the me
6885
6986
```js
7087
const gulp = require('gulp');
71-
const babel = require('gulp-babel');
88+
const createBabel = require('gulp-babel');
7289
const through = require('through2');
7390

91+
const babel = createBabel();
92+
7493
function logFileHelpers() {
7594
return through.obj((file, enc, cb) => {
7695
console.log(file.babel.usedHelpers);
@@ -102,7 +121,9 @@ Use it as plugin:
102121
103122
```js
104123
const gulp = require('gulp');
105-
const babel = require('gulp-babel');
124+
const createBabel = require('gulp-babel');
125+
126+
const babel = createBabel();
106127

107128
gulp.task('default', () =>
108129
gulp.src('src/app.js')

0 commit comments

Comments
 (0)