Skip to content

Commit 2e1f555

Browse files
committed
perf: store babel config in cache
1 parent 7ce2ee6 commit 2e1f555

File tree

5 files changed

+40
-10
lines changed

5 files changed

+40
-10
lines changed

lib/cache.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const NodeCache = require('node-cache')
2+
3+
const cache = new NodeCache()
4+
5+
module.exports = cache

lib/compilers/babel-compiler.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
11
const babel = require('babel-core')
22
const findBabelConfig = require('find-babel-config')
33
const logger = require('../logger')
4+
const cache = require('../cache')
45

56
var defaultBabelOptions = {
67
presets: ['es2015'],
78
plugins: ['transform-runtime']
89
}
910

10-
module.exports = function compileBabel (scriptContent, inputSourceMap) {
11-
const { file, config } = findBabelConfig.sync(process.cwd(), 0)
12-
13-
if (!file) {
14-
logger.info('no .babelrc found, defaulting to default babel options')
11+
function getBabelConfig () {
12+
const cachedConfig = cache.get('babel-config')
13+
if (cachedConfig) {
14+
return cachedConfig
15+
} else {
16+
const { file, config } = findBabelConfig.sync(process.cwd(), 0)
17+
if (!file) {
18+
logger.info('no .babelrc found, defaulting to default babel options')
19+
}
20+
const babelConfig = file ? config : defaultBabelOptions
21+
cache.set('babel-config', babelConfig)
22+
return babelConfig
1523
}
24+
}
1625

26+
module.exports = function compileBabel (scriptContent, inputSourceMap) {
1727
const sourceMapOptions = {
1828
sourceMaps: true,
19-
inputSourceMap: inputSourceMap || null
29+
inputSourceMap: inputSourceMap
2030
}
2131

22-
const baseBabelOptions = file ? config : defaultBabelOptions
23-
const babelOptions = Object.assign(sourceMapOptions, baseBabelOptions)
32+
const babelConfig = getBabelConfig()
33+
34+
const babelOptions = Object.assign(sourceMapOptions, babelConfig)
2435

2536
const res = babel.transform(scriptContent, babelOptions)
2637

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"convert-source-map": "^1.5.0",
5656
"find-babel-config": "^1.1.0",
5757
"js-beautify": "^1.6.14",
58+
"node-cache": "^4.1.1",
5859
"object-assign": "^4.1.1",
5960
"source-map": "^0.5.6",
6061
"tsconfig": "^7.0.0",

test/Babel.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import {
77
writeFileSync,
88
renameSync
99
} from 'fs'
10-
const clearModule = require('clear-module')
10+
import clearModule from 'clear-module'
11+
import cache from '../lib/cache'
1112

1213
beforeEach(() => {
14+
cache.flushAll()
1315
clearModule.all()
1416
})
1517

yarn.lock

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,10 @@ cliui@^3.2.0:
948948
strip-ansi "^3.0.1"
949949
wrap-ansi "^2.0.0"
950950

951+
952+
version "2.1.1"
953+
resolved "https://registry.yarnpkg.com/clone/-/clone-2.1.1.tgz#d217d1e961118e3ac9a4b8bba3285553bf647cdb"
954+
951955
co@^4.6.0:
952956
version "4.6.0"
953957
resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184"
@@ -2646,7 +2650,7 @@ lodash.templatesettings@^4.0.0:
26462650
dependencies:
26472651
lodash._reinterpolate "~3.0.0"
26482652

2649-
lodash@^4.0.0, lodash@^4.14.0, lodash@^4.17.4, lodash@^4.2.1, lodash@^4.3.0:
2653+
lodash@4.x, lodash@^4.0.0, lodash@^4.14.0, lodash@^4.17.4, lodash@^4.2.1, lodash@^4.3.0:
26502654
version "4.17.4"
26512655
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
26522656

@@ -2781,6 +2785,13 @@ natural-compare@^1.4.0:
27812785
version "1.4.0"
27822786
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
27832787

2788+
node-cache@^4.1.1:
2789+
version "4.1.1"
2790+
resolved "https://registry.yarnpkg.com/node-cache/-/node-cache-4.1.1.tgz#08524645ee4039dedc3dcc1dd7c6b979e0619e44"
2791+
dependencies:
2792+
clone "2.x"
2793+
lodash "4.x"
2794+
27842795
node-fetch@^1.0.1:
27852796
version "1.7.2"
27862797
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-1.7.2.tgz#c54e9aac57e432875233525f3c891c4159ffefd7"

0 commit comments

Comments
 (0)