【问题标题】:Webpack can't handle JSX even with appropiate loaders即使使用适当的加载器,Webpack 也无法处理 JSX
【发布时间】:2016-12-02 22:33:03
【问题描述】:

首先,我知道在 SO 中有各种这样的问题。我跟着他们,但我仍然得到一个错误。我正在用 gulp 学习 React.js,现在我想转移到 webpack 以在浏览器上重新加载热代码。我正在学习基于此代码配置 webpack:

https://github.com/learncodeacademy/react-js-tutorials/tree/master/1-basic-react

当我运行webpack --watch 命令时,我得到这个错误:

Hash: 826e21c818de1882d366
Version: webpack 1.13.1
Time: 42ms
   [0] ./js/scripts.js 0 bytes [built] [failed]

ERROR in ./js/scripts.js
Module parse failed: C:\Users\Oscar\Documents\Proyectos\react_webpack/src\js\scripts.js Unexpected token (4:16)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (4:16)
    at Parser.pp$4.raise (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\acorn\dist\acorn.js:2221:15)
    at Parser.pp.unexpected (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\acorn\dist\acorn.js:603:10)
    at Parser.pp$3.parseExprAtom (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\acorn\dist\acorn.js:1822:12)
    at Parser.pp$3.parseExprSubscripts (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\acorn\dist\acorn.js:1715:21)
    at Parser.pp$3.parseMaybeUnary (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\acorn\dist\acorn.js:1692:19)
    at Parser.pp$3.parseExprOps (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\acorn\dist\acorn.js:1637:21)
    at Parser.pp$3.parseMaybeConditional (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\acorn\dist\acorn.js:1620:21)
    at Parser.pp$3.parseMaybeAssign (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\acorn\dist\acorn.js:1597:21)
    at Parser.pp$3.parseExprList (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\acorn\dist\acorn.js:2165:22)
    at Parser.pp$3.parseSubscripts (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\acorn\dist\acorn.js:1741:35)
    at Parser.pp$3.parseExprSubscripts (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\acorn\dist\acorn.js:1718:17)
    at Parser.pp$3.parseMaybeUnary (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\acorn\dist\acorn.js:1692:19)
    at Parser.pp$3.parseExprOps (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\acorn\dist\acorn.js:1637:21)
    at Parser.pp$3.parseMaybeConditional (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\acorn\dist\acorn.js:1620:21)
    at Parser.pp$3.parseMaybeAssign (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\acorn\dist\acorn.js:1597:21)
    at Parser.pp$3.parseExpression (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\acorn\dist\acorn.js:1573:21)
    at Parser.pp$1.parseStatement (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\acorn\dist\acorn.js:727:47)
    at Parser.pp$1.parseTopLevel (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\acorn\dist\acorn.js:638:25)
    at Parser.parse (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\acorn\dist\acorn.js:516:17)
    at Object.parse (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\acorn\dist\acorn.js:3098:39)
    at Parser.parse (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\webpack\lib\Parser.js:902:15)
    at DependenciesBlock.<anonymous> (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\webpack\lib\NormalModule.js:104:16)
    at DependenciesBlock.onModuleBuild (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\webpack-core\lib\NormalModuleMixin.js:310:10)
    at nextLoader (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\webpack-core\lib\NormalModuleMixin.js:275:25)
    at C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\webpack-core\lib\NormalModuleMixin.js:259:5
    at Storage.finished (C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\enhanced-resolve\lib\CachedInputFileSystem.js:38:16)
    at C:\Users\Oscar\Documents\Proyectos\react_webpack\node_modules\graceful-fs\graceful-fs.js:78:16
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:404:3)

这是我项目的结构:

|-- node_modules
|   |-- //I have all the libraries listed in package.json below
|-- src
|   |-- js
|   |   |-- comments.jsx
|   |   |-- scripts.js
|   |-- index.html
|-- .babelrc
|-- package.json
|-- webpack.config.js

webpack.config.js

var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');
var SRC_FOLDER = __dirname + "/src";

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

package.json

{
  "name": "react_webpack",
  "version": "1.0.0",
  "description": "Learn how to use webpack",
  "main": "webpack.config.js",
  "author": "",
  "license": "ISC",
  "dependencies": {
    "webpack": "^1.13.1",
    "babel-core": "^6.11.4",
    "babel-loader": "^6.2.4",
    "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.9.0",
    "babel-preset-react": "^6.11.1",
    "babel-preset-stage-0": "^6.5.0",
    "react": "^15.2.1",
    "react-dom": "^15.2.1",
    "webpack-dev-server": "^1.14.1"
  },
  "devDependencies": {

  },
  "scripts": {
    "dev": "./node_modules/.bin/webpack-dev-server --content-base src --inline --hot",
    "test": "echo \"Error: no test specified\" && exit 1"
}
}

.babelrc

{
    "presets": [
        "es2015",
        "stage-0",
        "react"
    ]
}

以及引发错误的入口点,scripts.js

import React from 'react';
import ReactDOM from 'react-dom';
import {Comment, CommentsList} from 'comments';
ReactDOM.render(<CommentsList />, document.getElementById("app"));

我认为没有必要发布 cmets.jsx,因为我在使用 gulp 时已经在我的应用程序中对其进行了测试,并且可以正常工作。

我试图修复它,但没有奏效:

  • 创建 .babelrc 文件,即使我在 webpack.config.js 中设置了预设
  • 使用 --config 标志执行 webpack 以检查它是否可以找到配置文件
  • 使用 github repo 项目中使用的相同库(当我下载该源代码并执行 webpack --watch 时它可以工作)
  • 检查它是否找到了每个文件(cmets.jsx、scripts.js)
  • 将扩展名更改为 js 或 jsx(并更新 webpack.config.js)以检查是否是因为文件扩展名

所有问题都是在这行的scripts.js中引起的:

ReactDOM.render(<CommentsList />, document.getElementById("app"));

恰好在

【问题讨论】:

  • 我认为你的RegExp 应该是这样的/\.jsx?$/
  • 是的,加载程序的测试不正确。
  • 是的,你是对的。我的错。我想,因为我的 js/jsx 文件在 /js 下,我应该把它放在正则表达式中。 @AlexanderT.,我无法将您的评论标记为答案,您可以将其发布为答案吗?

标签: javascript reactjs webpack jsx


【解决方案1】:

在这种情况下,您需要从 RegExp 中删除 js\/

 test: /\.jsx?$/

因为/js\/\.jsx?$/ 匹配这样的文件

 console.log(/js\/\.jsx?$/.test('js/.jsx'));
 console.log(/js\/\.jsx?$/.test('js/.js'));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    相关资源
    最近更新 更多