Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use a regex in .babelrc?

Here's what my .babelrc looks like. Obviously it doesn't work because of the regex in a JSON file:

{
  "presets": ["es2015", "react"],
  "plugins": ["transform-object-rest-spread"],
  "only": [
    '/client/**/*',
    '/server/**/*',
    /diy-fe-shared/ // <--- regex here breaks
  ]
}

Is there a way to use a regex here?

like image 665
Evan Hobbs Avatar asked Jul 18 '16 17:07

Evan Hobbs


People also ask

What is babelrc in Node JS?

.babelrc.json or .babelrc files are loaded relative to the file being compiled. If this option is omitted, Babel will behave as if babelrc: false has been set. Used as the default value for Babel's sourceFileName option, and used as part of generation of filenames for the AMD / UMD / SystemJS module transforms.

What is the use of babelrc?

If this option is omitted, Babel will behave as if babelrc: false has been set. Used as the default value for Babel's sourceFileName option, and used as part of generation of filenames for the AMD / UMD / SystemJS module transforms.

How to load a babelrc file from a directory?

Babel loads .babelrc.json files, or an equivalent one using the supported extensions, by searching up the directory structure starting from the "filename" being compiled .babelrc would be useful if you want to run certain transformations / plugins on a subset of files /directories.

How do I stop Babel from looking for a specific file?

Babel will look for a .babelrcin the current directory of the file being transpiled. If one does not exist, it will travel up the directory tree until it finds either a .babelrc, or a package.jsonwith a "babel": {}hash within. Use "babelrc": falsein optionsto stop lookup behavior, or provide the --no-babelrcCLI flag.


1 Answers

Maybe you can use regex in .babelrc like this: https://github.com/probablyup/markdown-to-jsx

Create .babelrc.js:

const plugins = [ 'emotion', ];

if (process.env.NODE_ENV === 'production') {
  plugins.push('transform-react-remove-prop-types');
}

module.exports = {
  plugins,
  presets: [
    ['es2015', {
      loose: true,
      modules: process.env.BABEL_ENV === 'esm' ? false : 'commonjs',
    }],
    'react',
    'stage-2',
  ],
};

Then require .babelrc.js in .babelrc

{
  "presets": ["./.babelrc.js"]
}
like image 194
hegfirose Avatar answered Oct 02 '22 19:10

hegfirose