Skip to content

Commit

Permalink
feat(babel-transpiler): support .js babel config files (#1422)
Browse files Browse the repository at this point in the history
The babel transpiler will now also look for babel.config.js files. New algoritm:

* Check if the provided optionsFile is a `.babelrc.js`-file. If so, require and return it.
* Check if the provided optionsFile is a `babel.config.js`. If so, require it, call it with an optionsApi object provided by the user and return the result
  • Loading branch information
ollelauribostrom authored and nicojs committed Mar 29, 2019
1 parent 03e6d9a commit 9e321f0
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 5 deletions.
15 changes: 11 additions & 4 deletions packages/babel-transpiler/src/BabelConfigReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface StrykerBabelConfig {
extensions: ReadonlyArray<string>;
options: babel.TransformOptions;
optionsFile: string | null;
optionsApi?: Partial<babel.ConfigAPI>;
}

export const CONFIG_KEY = 'babel';
Expand All @@ -33,21 +34,27 @@ export class BabelConfigReader {
...strykerOptions[CONFIG_KEY]
};
babelConfig.options = {
...this.readBabelOptionsFromFile(babelConfig.optionsFile),
...this.readBabelOptionsFromFile(babelConfig.optionsFile, babelConfig.optionsApi),
...babelConfig.options
};
this.log.debug(`Babel config is: ${JSON.stringify(babelConfig, null, 2)}`);
return babelConfig;
}

private readBabelOptionsFromFile(relativeFileName: string | null): babel.TransformOptions {
private readBabelOptionsFromFile(relativeFileName: string | null, optionsApi?: Partial<babel.ConfigAPI>): babel.TransformOptions {
if (relativeFileName) {
const babelrcPath = path.resolve(relativeFileName);
this.log.debug(`Reading .babelrc file from path "${babelrcPath}"`);
if (fs.existsSync(babelrcPath)) {
try {
const config: babel.TransformOptions = JSON.parse(fs.readFileSync(babelrcPath, 'utf8'));
return config;
if (path.basename(babelrcPath) === '.babelrc.js') {
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);
}
return JSON.parse(fs.readFileSync(babelrcPath, 'utf8')) as babel.TransformOptions;
} catch (error) {
this.log.error(`Error while reading "${babelrcPath}" file: ${error}`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { expect } from 'chai';
import { testInjector } from '@stryker-mutator/test-helpers';
import { CONFIG_KEY, StrykerBabelConfig } from '../../src/BabelConfigReader';
import { commonTokens } from '@stryker-mutator/api/plugin';
import { ConfigAPI } from '@babel/core';

function describeIntegrationTest(projectName: string, babelConfig: Partial<StrykerBabelConfig> = {}) {

const projectDir = path.resolve(__dirname, '..', '..', 'testResources', projectName);
babelConfig.optionsFile = path.join(projectDir, '.babelrc');
babelConfig.optionsFile = path.join(projectDir, babelConfig.optionsFile || '.babelrc');
let projectFiles: File[] = [];
let resultFiles: File[] = [];
let babelTranspiler: BabelTranspiler;
Expand Down Expand Up @@ -65,3 +66,17 @@ describe('Project with binary files', () => {
describe('Different extensions', () => {
describeIntegrationTest('differentExtensions');
});
describe('A Babel project with babel.config.js config file', () => {
const noop = () => {};
describeIntegrationTest('babelProjectWithBabelConfigJs', {
extensions: ['.ts'],
optionsApi: { cache: { forever: noop }} as ConfigAPI,
optionsFile: 'babel.config.js',
});
});
describe('A Babel project with .babelrc.js config file', () => {
describeIntegrationTest('babelProjectWithBabelRcJs', {
extensions: ['.ts'],
optionsFile: '.babelrc.js'
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = api => {
api.cache.forever();

return {
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;
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 9e321f0

Please sign in to comment.