Skip to content

Commit

Permalink
fix: ignore babel.config.js files by default
Browse files Browse the repository at this point in the history
I assume this file was added as part of babel v7 and is not a direct replacement for `.babelrc`, so we now have to specify both `babelrc` and `configFile` options.
  • Loading branch information
eventualbuddha committed Nov 27, 2018
1 parent eb12f7c commit f939368
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/CLIEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default class CLIEngine {

runner = new TransformRunner(
sourcesIterator,
new InlineTransformer(plugins)
new InlineTransformer(plugins, this.config.findBabelConfig)
);

for await (let result of runner.run()) {
Expand Down
20 changes: 14 additions & 6 deletions src/InlineTransformer.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { transformAsync } from '@babel/core';
import { transformAsync, TransformOptions } from '@babel/core';
import { BabelPlugin } from './BabelPluginTypes';
import Config from './Config';
import Transformer from './Transformer';

export default class InlineTransformer implements Transformer {
constructor(private readonly plugins: Array<BabelPlugin>) {}
constructor(
private readonly plugins: Array<BabelPlugin>,
private readonly findBabelConfig: boolean = false
) {}

async transform(filepath: string, content: string): Promise<string> {
let result = await transformAsync(content, {
let options: TransformOptions = {
filename: filepath,
babelrc: false,
babelrc: this.findBabelConfig,
plugins: this.plugins
});
};

if (!this.findBabelConfig) {
options.configFile = this.findBabelConfig;
}

let result = await transformAsync(content, options);

if (!result) {
throw new Error(`[${filepath}] babel transform returned null`);
Expand Down
14 changes: 9 additions & 5 deletions src/transpile-requires.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { addHook } from 'pirates';
import buildAllSyntaxPlugin from './AllSyntaxPlugin';
import { PluginExtensions, TypeScriptExtensions } from './extensions';

let useBabelrc = false;
let useBabelConfig = false;
let revert: (() => void) | null = null;

export function hook(code: string, filename: string): string {
Expand All @@ -17,13 +17,17 @@ export function hook(code: string, filename: string): string {
let presets: Array<string> = [];
let options: TransformOptions = {
filename,
babelrc: useBabelrc,
babelrc: useBabelConfig,
presets: presets,
plugins: [buildAllSyntaxPlugin('module')],
sourceMaps: 'inline'
};

if (!useBabelrc) {
if (!useBabelConfig) {
options.configFile = useBabelConfig;
}

if (!useBabelConfig) {
if (TypeScriptExtensions.has(ext)) {
presets.push(require.resolve('@babel/preset-typescript'));
}
Expand All @@ -40,9 +44,9 @@ export function hook(code: string, filename: string): string {
return result.code as string;
}

export function enable(babelrc: boolean = false) {
export function enable(shouldUseBabelConfig: boolean = false) {
disable();
useBabelrc = babelrc;
useBabelConfig = shouldUseBabelConfig;
revert = addHook(hook, {
exts: Array.from(PluginExtensions),
ignoreNodeModules: true
Expand Down
54 changes: 54 additions & 0 deletions test/cli/CLITest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,4 +557,58 @@ describe('CLI', function() {

deepEqual({ status, stdout }, { status: 1, stdout: '' });
});

it('ignores babel.config.js files by default', async function() {
let workspace = await copyFixturesInto(
'babel-config',
await createTemporaryDirectory('babel-config')
);
let { status, stdout, stderr } = await runCodemodCLI(
['index.js'],
undefined,
workspace
);

strictEqual(
await readFile(join(workspace, 'index.js'), 'utf8'),
'const a = 1;\n',
'file contents should not change'
);

deepEqual(
{ status, stdout, stderr },
{
status: 0,
stdout: 'index.js\n1 file(s), 0 modified, 0 errors\n',
stderr: ''
}
);
});

it('reads babel.config.js files if requested', async function() {
let workspace = await copyFixturesInto(
'babel-config',
await createTemporaryDirectory('babel-config')
);
let { status, stdout, stderr } = await runCodemodCLI(
['index.js', '--find-babel-config'],
undefined,
workspace
);

strictEqual(
await readFile(join(workspace, 'index.js'), 'utf8'),
'"use strict";\nvar a = 1;\n',
'file should have been transpiled'
);

deepEqual(
{ status, stdout, stderr },
{
status: 0,
stdout: 'index.js\n1 file(s), 1 modified, 0 errors\n',
stderr: ''
}
);
});
});
13 changes: 13 additions & 0 deletions test/fixtures/babel-config/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = function(api) {
api.cache(true);
return {
presets: [
[
'@babel/preset-env',
{
forceAllTransforms: true
}
]
]
}
};
1 change: 1 addition & 0 deletions test/fixtures/babel-config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const a = 1;
16 changes: 14 additions & 2 deletions test/helpers/runCodemodCLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export type CLIResult = { status: number; stdout: string; stderr: string };

export default async function runCodemodCLI(
args: Array<string>,
stdin: string = ''
stdin: string = '',
cwd?: string
): Promise<CLIResult> {
let stdinStream = new PassThrough();
let stdoutStream = new PassThrough();
Expand All @@ -15,7 +16,18 @@ export default async function runCodemodCLI(
stdinStream.end(new Buffer(stdin));

let argv = [process.argv[0], require.resolve('../../bin/codemod'), ...args];
let status = await run(argv, stdinStream, stdoutStream, stderrStream);
let status: number;
let oldCwd = process.cwd();

try {
if (cwd) {
process.chdir(cwd);
}

status = await run(argv, stdinStream, stdoutStream, stderrStream);
} finally {
process.chdir(oldCwd);
}

stdoutStream.end();
stderrStream.end();
Expand Down

0 comments on commit f939368

Please sign in to comment.