Skip to content

Commit

Permalink
feat: Add support for babel.config.js (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
HeroProtagonist authored and tleunen committed Mar 4, 2019
1 parent 7b6cd67 commit 593aa1c
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/index.js
Expand Up @@ -6,13 +6,19 @@ 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 };

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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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');
Expand Down
9 changes: 9 additions & 0 deletions 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,
};
};
9 changes: 9 additions & 0 deletions 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,
};
};
43 changes: 43 additions & 0 deletions test/index.test.js
Expand Up @@ -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 }) => {
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 593aa1c

Please sign in to comment.