如何仅使用Babel 7转换node_modules文件夹? [英] How to transpile node_modules folder using just babel 7?

查看:1013
本文介绍了如何仅使用Babel 7转换node_modules文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用Mocha单元测试reactjs应用程序,但是从node_modules文件夹中使用的es6功能(导入/导出)中获取错误.有问题的应用程序使用babel进行了编译,但是由于react组件之一正在使用node_modules中的模块,因此其抛出错误Syntax Error:Unexpected token export.我知道babel默认情况下会忽略node_modules文件夹,但不确定如何处理.任何帮助,将不胜感激.谢谢.

Trying to unit test reactjs application using mocha but getting error from es6 features (import/export) used inside node_modules folder. The application in question is transpiled using babel but since one of react component is using a module from node_modules its throwing error Syntax Error:Unexpected token export. I am aware that babel ignores node_modules folder by default, but not sure how to approach this. Any help would be appreciated. Thanks.

测试命令:-

  "test": "SET NODE_ENV=test&& mocha --require @babel/register --require ignore-styles -r jsdom-global/register \"./src/**/*.test.js\"",

babel.config.js:-

babel.config.js :-

module.exports = function (api) {
  const presets = ["react-app"];
  const plugins = [
      "@babel/plugin-transform-modules-commonjs", 
      "inline-react-svg"
  ];
  const ignore = [/node_modules/]
  api.cache(false); 
  return {
      presets, 
      plugins,
      ignore
  }
};

推荐答案

让它正常工作!.由于我使用的是内部配置为使用Jest的 react-app-rewired ,因此不得不从 Mocha 切换到 Jest .需要对config-override.js进行小的更改.用"transformIgnorePatterns": ["node_modules/(?!MODULES_TO_BE_TRANSPILED)"]添加了笑话配置.希望对别人有帮助.

Got it working!. Had to switch from Mocha to Jest since I was using react-app-rewired which was internally configured to use Jest. A small change in config-override.js was needed. Added a jest config with "transformIgnorePatterns": ["node_modules/(?!MODULES_TO_BE_TRANSPILED)"]. Hope it helps others.

这篇关于如何仅使用Babel 7转换node_modules文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆