Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transpiling third-party modules with create-react-app build

I'm developing a recently-ejected create-react-app application and need to use an outside module.

I did npm install --save-dev starparam and am importing the module like this: import starparam from 'starparam';.

When I run my code in NodeJS with my unit-tests (npm test) everything works as expected.

But, when I run in the browser (using npm start) I get syntax errors.

This seems to be because the module I'm using uses ES6 features like arrow functions.

What webpack changes do I need to make so this third-party module is included in the transpile?

like image 774
Jonathan.Brink Avatar asked Jun 28 '17 14:06

Jonathan.Brink


1 Answers

I was able to accomplish my goal by:

  • adding a new path in config/paths.js to the third-party module folder
  • adding a new loader in the loaders array in the module object in config/webpack.config.dev.js
  • a similar change in config/webpack.config.prod.js

--

Adding a new path in config/paths.js;

starparamSrc: resolveApp('node_modules/starparam'),

Adding a new loader in config/webpack.config.dev.js:

{
  test: /\.(js|jsx)$/,
  include: paths.starparamSrc,
  loader: 'babel',
  query: {
   cacheDirectory: true
  }
},
like image 86
Jonathan.Brink Avatar answered Oct 14 '22 23:10

Jonathan.Brink