From 593aa1ceffd2897aab76e9c94930c609cff5d658 Mon Sep 17 00:00:00 2001 From: Jordan Tepper Date: Mon, 4 Mar 2019 10:51:54 -0500 Subject: [PATCH] feat: Add support for babel.config.js (#32) --- src/index.js | 31 +++++++++++++ test/data/babelconfigjs/babel.config.js | 9 ++++ .../dir1/dir2/dir3/babel.config.js | 9 ++++ test/index.test.js | 43 +++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 test/data/babelconfigjs/babel.config.js create mode 100644 test/data/babelconfigjs/dir1/dir2/dir3/babel.config.js diff --git a/src/index.js b/src/index.js index e0a0e5c..d4cabea 100644 --- a/src/index.js +++ b/src/index.js @@ -6,6 +6,7 @@ const pathExists = require('path-exists'); const INFINITY = 1 / 0; const BABELRC_FILENAME = '.babelrc'; const BABELRC_JS_FILENAME = '.babelrc.js'; +const BABEL_CONFIG_JS_FILENAME = 'babel.config.js'; const PACKAGE_FILENAME = 'package.json'; const nullConf = { file: null, config: null }; @@ -13,6 +14,11 @@ const nullConf = { file: null, config: null }; function getBabelJsConfig(file) { // eslint-disable-next-line global-require, import/no-dynamic-require const configModule = require(file); + + if (typeof configModule === 'function') { + return configModule(); + } + // eslint-disable-next-line no-underscore-dangle return configModule && configModule.__esModule ? configModule.default : configModule; } @@ -72,6 +78,22 @@ function asyncFind(resolve, dir, depth) { } return exists; }) + .then((exists) => { + if (!exists) { + const babelConfigJSrc = path.join(dir, BABEL_CONFIG_JS_FILENAME); + return pathExists(babelConfigJSrc).then((ex) => { + if (ex) { + const config = getBabelJsConfig(babelConfigJSrc); + resolve({ + file: babelConfigJSrc, + config, + }); + } + }); + } + return exists; + }) + .then((exists) => { if (!exists) { const nextDir = path.dirname(dir); @@ -129,6 +151,15 @@ module.exports.sync = function findBabelConfigSync(start, depth = INFINITY) { }; } + const babelConfigJSrc = path.join(dir, BABEL_CONFIG_JS_FILENAME); + if (pathExists.sync(babelConfigJSrc)) { + const config = getBabelJsConfig(babelConfigJSrc); + return { + file: babelConfigJSrc, + config, + }; + } + const packageFile = path.join(dir, PACKAGE_FILENAME); if (pathExists.sync(packageFile)) { const packageContent = fs.readFileSync(packageFile, 'utf8'); diff --git a/test/data/babelconfigjs/babel.config.js b/test/data/babelconfigjs/babel.config.js new file mode 100644 index 0000000..825c8c0 --- /dev/null +++ b/test/data/babelconfigjs/babel.config.js @@ -0,0 +1,9 @@ +module.exports = function config() { + const presets = ['fake-preset-babel-config-js']; + const plugins = ['fake-plugin-babel-config-js']; + + return { + presets, + plugins, + }; +}; diff --git a/test/data/babelconfigjs/dir1/dir2/dir3/babel.config.js b/test/data/babelconfigjs/dir1/dir2/dir3/babel.config.js new file mode 100644 index 0000000..818da79 --- /dev/null +++ b/test/data/babelconfigjs/dir1/dir2/dir3/babel.config.js @@ -0,0 +1,9 @@ +module.exports = function config() { + const presets = ['fake-preset-dir3-babel-config-js']; + const plugins = ['fake-plugin-dir3-babel-config-js']; + + return { + presets, + plugins, + }; +}; diff --git a/test/index.test.js b/test/index.test.js index f5b3538..d093515 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -96,6 +96,29 @@ describe('find-babel-config', () => { ); }); + describe('babel.config.js', () => { + it('should return the config in the direct directory', () => + findBabelConfig('test/data/babelconfigjs').then(({ file, config }) => { + expect(file).toBe(path.join(process.cwd(), 'test/data/babelconfigjs/babel.config.js')); + expect(config).toEqual({ presets: ['fake-preset-babel-config-js'], plugins: ['fake-plugin-babel-config-js'] }); + }), + ); + + it('should return the config in the parent directory', () => + findBabelConfig(path.join(process.cwd(), 'test/data/babelconfigjs/dir1')).then(({ file, config }) => { + expect(file).toBe(path.join(process.cwd(), 'test/data/babelconfigjs/babel.config.js')); + expect(config).toEqual({ presets: ['fake-preset-babel-config-js'], plugins: ['fake-plugin-babel-config-js'] }); + }), + ); + + it('should return the first config found in the parents', () => + findBabelConfig('test/data/babelconfigjs/dir1/dir2/dir3/dir4/dir5').then(({ file, config }) => { + expect(file).toBe(path.join(process.cwd(), 'test/data/babelconfigjs/dir1/dir2/dir3/babel.config.js')); + expect(config).toEqual({ presets: ['fake-preset-dir3-babel-config-js'], plugins: ['fake-plugin-dir3-babel-config-js'] }); + }), + ); + }); + describe('with depth', () => { it('should check only the direct directory with a depth 0', () => findBabelConfig('test/data/babelrc/dir1/dir2/dir3/dir4', 0).then(({ file, config }) => { @@ -204,6 +227,26 @@ describe('find-babel-config', () => { }); }); + describe('babel.config.js', () => { + it('should return the config in the specified directory', () => { + const { file, config } = findBabelConfig.sync('test/data/babelconfigjs'); + expect(file).toBe(path.join(process.cwd(), 'test/data/babelconfigjs/babel.config.js')); + expect(config).toEqual({ presets: ['fake-preset-babel-config-js'], plugins: ['fake-plugin-babel-config-js'] }); + }); + + it('should return the config in the parent directory', () => { + const { file, config } = findBabelConfig.sync(path.join(process.cwd(), 'test/data/babelconfigjs/dir1')); + expect(file).toBe(path.join(process.cwd(), 'test/data/babelconfigjs/babel.config.js')); + expect(config).toEqual({ presets: ['fake-preset-babel-config-js'], plugins: ['fake-plugin-babel-config-js'] }); + }); + + it('should return the first config found in the parents', () => { + const { file, config } = findBabelConfig.sync('test/data/babelconfigjs/dir1/dir2/dir3/dir4/dir5'); + expect(file).toBe(path.join(process.cwd(), 'test/data/babelconfigjs/dir1/dir2/dir3/babel.config.js')); + expect(config).toEqual({ presets: ['fake-preset-dir3-babel-config-js'], plugins: ['fake-plugin-dir3-babel-config-js'] }); + }); + }); + describe('both files', () => { it('should return the first babel config in package.json', () => { const { file, config } = findBabelConfig.sync('test/data/both/dir1/dir2/dir3');