diff --git a/lib/cache.js b/lib/cache.js new file mode 100644 index 00000000..2c608673 --- /dev/null +++ b/lib/cache.js @@ -0,0 +1,5 @@ +const NodeCache = require('node-cache') + +const cache = new NodeCache() + +module.exports = cache diff --git a/lib/compilers/babel-compiler.js b/lib/compilers/babel-compiler.js index 775b83fe..e4eb18a6 100644 --- a/lib/compilers/babel-compiler.js +++ b/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) diff --git a/package.json b/package.json index 7f45d977..bc2d62cd 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/Babel.spec.js b/test/Babel.spec.js index 044e29bf..76ba1588 100644 --- a/test/Babel.spec.js +++ b/test/Babel.spec.js @@ -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() }) diff --git a/yarn.lock b/yarn.lock index 269fa38e..07b85832 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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"