Skip to content

Commit

Permalink
feat(babel-transpiler): support object-style babel.config.js (#1762)
Browse files Browse the repository at this point in the history
The babel transpiler will now also work with babel.config.js files that export object instead of function.
  • Loading branch information
Djaler authored and nicojs committed Nov 7, 2019
1 parent 1b00ab5 commit 31410c8
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 3 deletions.
9 changes: 7 additions & 2 deletions packages/babel-transpiler/src/BabelConfigReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ export class BabelConfigReader {
return require(babelrcPath) as babel.TransformOptions;
}
if (path.basename(babelrcPath) === 'babel.config.js') {
const config: babel.ConfigFunction = require(babelrcPath);
return config(optionsApi as babel.ConfigAPI);
const config = require(babelrcPath);
if (typeof config === 'function') {
const configFunction = config as babel.ConfigFunction;
return configFunction(optionsApi as babel.ConfigAPI);
} else {
return config as babel.TransformOptions;
}
}
return JSON.parse(fs.readFileSync(babelrcPath, 'utf8')) as babel.TransformOptions;
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,20 @@ describe('Project with binary files', () => {
describe('Different extensions', () => {
describeIntegrationTest('differentExtensions');
});
describe('A Babel project with babel.config.js config file', () => {
describe('A Babel project with babel.config.js config file that exports function', () => {
const noop = () => {};
describeIntegrationTest('babelProjectWithBabelConfigJs', {
extensions: ['.ts'],
optionsApi: { cache: { forever: noop } } as ConfigAPI,
optionsFile: 'babel.config.js'
});
});
describe('A Babel project with babel.config.js config file that exports object', () => {
describeIntegrationTest('babelProjectWithBabelConfigJsObject', {
extensions: ['.ts'],
optionsFile: 'babel.config.js'
});
});
describe('A Babel project with .babelrc.js config file', () => {
describeIntegrationTest('babelProjectWithBabelRcJs', {
extensions: ['.ts'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: ["@babel/preset-typescript"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 4;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const x = 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo: number = 4;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const x: number = 0;

0 comments on commit 31410c8

Please sign in to comment.