使用 create-react-app build 转译第三方模块

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

我正在开发最近弹出的 create-react-app application and need to use an outside module

我做了 npm install --save-dev starparam 并且正在像这样导入模块:import starparam from 'starparam';.

当我 运行 我在 NodeJS 中的代码和我的单元测试 (npm test) 时,一切都按预期工作。

但是,当我在浏览器中 运行(使用 npm start)时,我得到语法错误。

这似乎是因为我使用的模块 uses ES6 features 类似于箭头函数。

我需要对 webpack 进行哪些更改才能将此第三方模块包含在转译文件中?

我通过以下方式实现了我的目标:

  • 正在config/paths.js中添加新路径到第三方模块文件夹
  • config/webpack.config.dev.js
  • 中的 module 对象中的 loaders 数组中添加新加载器
  • config/webpack.config.prod.js
  • 中的类似变化

--

config/paths.js 中添加新路径;

starparamSrc: resolveApp('node_modules/starparam'),

config/webpack.config.dev.js 中添加新加载器:

{
  test: /\.(js|jsx)$/,
  include: paths.starparamSrc,
  loader: 'babel',
  query: {
   cacheDirectory: true
  }
},