Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transpile node_modules folder using just babel 7?

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 Command :-

  "test": "SET NODE_ENV=test&& mocha --require @babel/register --require ignore-styles -r jsdom-global/register \"./src/**/*.test.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
  }
};
like image 215
Suraj A J Avatar asked Nov 06 '22 15:11

Suraj A J


1 Answers

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.

like image 146
Suraj A J Avatar answered Nov 15 '22 05:11

Suraj A J