【问题标题】:You may need an appropriate loader to handle this file type.eith react.js,webpack,babel你可能需要一个合适的加载器来处理这个文件类型。无论是 react.js,webpack,babel
【发布时间】:2017-01-05 19:35:40
【问题描述】:

我正在尝试使用带有 babel 的 webpack 编译 js 文件,但我收到以下错误消息:

./js/IntroAndWorkSpace.js 中的错误 模块解析失败:C:\FULLTIME\STUDY METERIAL\ReactJS\NewReactPoject\js\IntroAndWorkSpace.js 意外令牌 (1:8) 您可能需要适当的加载程序来处理此文件类型。 SyntaxError: Unexpected token (1:8)

这是我的 js 文件

import* React from 'react'
import*  ReactDOM from 'react-dom',


ReactDOM.render(<h1>headingthe  of page</h1>,
	document.getElementById('head')
	);
这是 pakeje.json

{
  "name": "newreactproject",
  "version": "1.0.0",
  "description": "myreactproject for learning react",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "priya",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.14.0",
    "babel-loader": "^6.2.5",
    "babel-plugin-add-module-exports": "^0.2.1",
    "babel-plugin-react-html-attrs": "^2.0.0",
    "babel-plugin-transform-class-properties": "^6.11.5",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-preset-es2015": "^6.14.0",
    "babel-preset-react": "^6.11.1",
    "babel-preset-stage-0": "^6.5.0",
    "react": "^15.3.1",
    "react-dom": "^15.3.1",
    "webpack": "^1.13.2"
  }
}
这是 config.js

var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');

module.exports = {
  context: __dirname,
  devtool: debug ? "inline-sourcemap" : null,
  entry: "./js/scripts.js",
     loaders: [
      {
        test: /\.jsx?$/,
        exclude: /(node_modules|bower_components)/,
        loader: 'babel-loader',
        query: {
          presets: ['react', 'es2015', 'stage-0'],
          plugins: ['react-html-attrs', 'transform-class-properties', 'transform-decorators-legacy'],
        }
      }
    ],
  output: {
    path: __dirname + "/js",
    filename: "scripts.min.js"
  },
  plugins: debug ? [] : [
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
  ],
};   

【问题讨论】:

  • 我以前没见过import*,你可能只是想要import React from 'react'?此行末尾的逗号也无效import* ReactDOM from 'react-dom',

标签: reactjs webpack babeljs


【解决方案1】:

看起来您已正确设置所有内容。我相信问题在于您的 webpack 配置文件配置为使用 babel 加载器使用 reactES2015 预设加载扩展名为 .jsx 的文件,但是您尝试加载的文件(IntroAndWorkSpace. js) 不以.jsx 扩展名结尾。

由于与文件扩展名保持一致可能是个好主意,因此您有两种解决方法:

1) 将您尝试加载的文件的扩展名更改为 .js 而不是 .jsx

2) 更改加载器测试以加载扩展名为 .js 而不是 .jsx 的文件:

loaders: [
    {
        test: /\.js?$/,
        ...
    }
]

【讨论】:

    【解决方案2】:

    loaders: [
              {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                loader: 'babel-loader',
                query: {
                  presets: ['react', 'es2015', 'stage-0'],
                  plugins: ['react-html-attrs', 'transform-class-properties', 'transform-decorators-legacy'],
                }
              }
            ],

    【讨论】:

      猜你喜欢
      • 2016-09-28
      • 2016-07-13
      • 2016-02-01
      • 2017-06-20
      • 2018-11-22
      • 1970-01-01
      • 2017-11-29
      • 2017-10-21
      相关资源
      最近更新 更多