最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can you use a regex in .babelrc? - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Jul 18, 2016 at 17:59 Evan Hobbs asked Jul 18, 2016 at 17:48 Evan HobbsEvan Hobbs 3,6726 gold badges33 silver badges45 bronze badges 6
  • use the string '/*/diy-fe-shared/**' instead? – Patrick Roberts Commented Jul 18, 2016 at 17:51
  • Please spellcheck your title. – user663031 Commented Jul 18, 2016 at 17:53
  • Why would you need a regex here? Isn't a simple glob enough? – Bergi Commented Jul 18, 2016 at 18:15
  • @Bergi to be honest I'm not sure. the directory is in node_modules, ie. node_modules/diy-fe-shared/ and it doesn't seem to work with glob matching but does with regex when I do it in javascript. Maybe something like babel ignores node_modules? – Evan Hobbs Commented Jul 18, 2016 at 18:24
  • I'm surprised this question does not have a definitive answer yet. I'm also trying to find out! – Bill Commented Aug 30, 2017 at 9:09
 |  Show 1 more ment

2 Answers 2

Reset to default 5

Maybe you can use regex in .babelrc like this: https://github./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 : 'monjs',
    }],
    'react',
    'stage-2',
  ],
};

Then require .babelrc.js in .babelrc

{
  "presets": ["./.babelrc.js"]
}

No, you cannot use regular expressions in .babelrc files. But you can in .babelrc.js files:

Rename your .babelrc to .babelrc.js and export your configuration object:

module.exports = {
  "env": {
    "production": {
      "plugins": [/* Only run if webpack configures babel-loader's options.envName="production" */]
    }
  },
  "presets": ["es2015", "react"],
  "plugins": ["transform-object-rest-spread"],
  "only": [
    '/client/**/*',
    '/server/**/*',
    /diy-fe-shared/ // <--- regex here works
  ]
};

https://babeljs.io/docs/en/configuration#babelrcjs

发布评论

评论列表(0)

  1. 暂无评论