Skip to content

Commit

Permalink
perf: store babel config in cache
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyerburgh committed Oct 1, 2017
1 parent 7ce2ee6 commit 2e1f555
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
5 changes: 5 additions & 0 deletions lib/cache.js
@@ -0,0 +1,5 @@
const NodeCache = require('node-cache')

const cache = new NodeCache()

module.exports = cache
27 changes: 19 additions & 8 deletions lib/compilers/babel-compiler.js
@@ -1,26 +1,37 @@
const babel = require('babel-core')
const findBabelConfig = require('find-babel-config')
const logger = require('../logger')
const cache = require('../cache')

var defaultBabelOptions = {
presets: ['es2015'],
plugins: ['transform-runtime']
}

module.exports = function compileBabel (scriptContent, inputSourceMap) {
const { file, config } = findBabelConfig.sync(process.cwd(), 0)

if (!file) {
logger.info('no .babelrc found, defaulting to default babel options')
function getBabelConfig () {
const cachedConfig = cache.get('babel-config')
if (cachedConfig) {
return cachedConfig
} else {
const { file, config } = findBabelConfig.sync(process.cwd(), 0)
if (!file) {
logger.info('no .babelrc found, defaulting to default babel options')
}
const babelConfig = file ? config : defaultBabelOptions
cache.set('babel-config', babelConfig)
return babelConfig
}
}

module.exports = function compileBabel (scriptContent, inputSourceMap) {
const sourceMapOptions = {
sourceMaps: true,
inputSourceMap: inputSourceMap || null
inputSourceMap: inputSourceMap
}

const baseBabelOptions = file ? config : defaultBabelOptions
const babelOptions = Object.assign(sourceMapOptions, baseBabelOptions)
const babelConfig = getBabelConfig()

const babelOptions = Object.assign(sourceMapOptions, babelConfig)

const res = babel.transform(scriptContent, babelOptions)

Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -55,6 +55,7 @@
"convert-source-map": "^1.5.0",
"find-babel-config": "^1.1.0",
"js-beautify": "^1.6.14",
"node-cache": "^4.1.1",
"object-assign": "^4.1.1",
"source-map": "^0.5.6",
"tsconfig": "^7.0.0",
Expand Down
4 changes: 3 additions & 1 deletion test/Babel.spec.js
Expand Up @@ -7,9 +7,11 @@ import {
writeFileSync,
renameSync
} from 'fs'
const clearModule = require('clear-module')
import clearModule from 'clear-module'
import cache from '../lib/cache'

beforeEach(() => {
cache.flushAll()
clearModule.all()
})

Expand Down
13 changes: 12 additions & 1 deletion yarn.lock
Expand Up @@ -948,6 +948,10 @@ cliui@^3.2.0:
strip-ansi "^3.0.1"
wrap-ansi "^2.0.0"

clone@2.x:
version "2.1.1"
resolved "https://registry.yarnpkg.com/clone/-/clone-2.1.1.tgz#d217d1e961118e3ac9a4b8bba3285553bf647cdb"

co@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184"
Expand Down Expand Up @@ -2646,7 +2650,7 @@ lodash.templatesettings@^4.0.0:
dependencies:
lodash._reinterpolate "~3.0.0"

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

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

node-cache@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/node-cache/-/node-cache-4.1.1.tgz#08524645ee4039dedc3dcc1dd7c6b979e0619e44"
dependencies:
clone "2.x"
lodash "4.x"

node-fetch@^1.0.1:
version "1.7.2"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-1.7.2.tgz#c54e9aac57e432875233525f3c891c4159ffefd7"
Expand Down

0 comments on commit 2e1f555

Please sign in to comment.