【问题标题】:Importing npm React Component imports the ES6 fileImporting npm React Component 导入 ES6 文件
【发布时间】:2018-06-13 09:31:52
【问题描述】:

我正在尝试导入以下组件:https://github.com/airbnb/rheostat

不幸的是,它试图导入 JSX 文件而不是编译的 ES5 组件,这会导致以下错误:

Module parse failed: /Users/sean/development/platform/node_modules/rheostat/src/Slider.jsx Unexpected token (34:11) You may need an appropriate loader to handle this file type.

我能够在存储库here 中找到相关问题,但答案有点神秘。我用干净的create-react-app 设置尝试了同样的方法,效果很好。

我在这里遗漏了什么吗?正如您在我的 webpack 配置中看到的那样,我只转换了一个文件夹和两个扩展,这意味着我希望从 node_modules 导入 ES5 组件,而不是任何需要 babel 处理的东西。

包.json

"babel": {    
      "presets": [ "react", "es2015", "stage-0" ],
      "env": {
        "development": {
          "presets": ["react-hmre"]
        }
      }    
  }

网页包

module: {
        loaders: [{
            test: /\.jsx?$/,
            loader: 'babel-loader',
            include: [
                path.resolve(__dirname + "/../../react")
            ],
            exclude: /node_modules/
        },

【问题讨论】:

  • 试试import Rheostat from 'rheostat/lib';

标签: reactjs webpack babeljs


【解决方案1】:

这可能对我有用

  {
    test: /\.(js|jsx)$/,
    exclude: /(node_modules|bower_components)/,
    use: ['babel-loader']  
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 2015-03-18
    • 1970-01-01
    相关资源
    最近更新 更多