-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Description
Do you want to request a feature or report a bug?
Feature.
What is the current behavior?
.babelrc.js export is flattened to JSON
This effectively ignores any features you would use .babelrc.js for, and shows up as things like unexpected token 'import' when the function export is treated as an empty config, or overrides are ignored because RegExp tests are stripped, etc..., so no transforms are run.
What is the expected behavior?
- existing non-JSON values like RegExps (e.g.
only, newoverrides[].testetc...), functions (e.g.getModuleId) or even plugin instances are preserved. .babelrc.jsconfig files can now export a function, which takes an API in order to support caching: Cache configs based on mtime and allow .babelrc.js functions babel/babel#5608
In short, babel-jest looks like it should be using the new loadOptions() API in @babel/core with at least { babelrc: true, filename, envName: 'test' }, though it is not documented yet 😟
Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.
// package.json
{
"private": true,
"devDependencies": {
"@babel/core": "7.0.0-beta.37",
"@babel/preset-env": "7.0.0-beta.37",
"@babel/preset-flow": "7.0.0-beta.37",
"@babel/preset-react": "7.0.0-beta.37",
"@babel/preset-stage-3": "7.0.0-beta.37",
"@babel/preset-typescript": "7.0.0-beta.37",
"babel-jest": "22.0.6",
"jest": "22.0.6"
}
}
// .babelrc.js example
module.exports = api => {
// Can also use arbitrary tests with `const val = api.cache(() => expr)`
const env = api.env();
// Jest needs CommonJS, but otherwise we want webpack to see ES modules.
const modules = env === 'test' ? 'commonjs' : false;
const targets = env === 'test'
? { node: 'current' }
: { browsers: ['> 5%', 'IE 11'] };
const presets = [
['@babel/env', { modules, useBuiltIns: 'usage', targets }],
'@babel/stage-3',
];
return {
overrides: [
{ test: /\.jsx?$/, presets: [...presets, '@babel/flow', '@babel/react'] },
{ test: /\.ts$/, presets: [...presets, '@babel/typescript'] },
{ test: /\.tsx$/, presets: [...presets, '@babel/typescript', '@babel/react'] },
],
};
};
Example loadOptions() usage:
> node -p "require('@babel/core').loadOptions({ babelrc: true, filename: 'src/index.js', envName: 'test' })"
{ plugins:
[ Plugin {
key: 'C:\\code\\skilitics\\prototypes\\layout\\node_modules\\@babel\\preset-react\\lib\\index.js$0',
manipulateOptions: [Function: manipulateOptions],
post: undefined,
pre: undefined,
visitor: [Object],
parserOverride: undefined,
generatorOverride: undefined,
options: [Object] },
<snip>
Plugin {
key: 'use-built-ins',
manipulateOptions: undefined,
post: [Function: post],
pre: [Function: pre],
visitor: [Object],
parserOverride: undefined,
generatorOverride: undefined,
options: [Object] } ],
overrides:
[ { test: /\.jsx?$/, presets: [Object] },
{ test: /\.ts$/, presets: [Object] },
{ test: /\.tsx$/, presets: [Object] } ],
test: /\.jsx?$/,
babelrc: false,
filename: 'src/index.js',
envName: 'test',
presets: [],
passPerPreset: false,
cwd: 'C:\\code\\skilitics\\prototypes\\layout' }