Javascript 网页包可以';即使使用合适的装载机也不能处理JSX
首先,我知道在SO中有很多类似的问题。我跟踪了他们,但还是有错误。我正在用gulp学习React.js,现在我想转到webpack,在浏览器上重新加载热代码。我正在学习根据以下代码配置webpack: 运行Javascript 网页包可以';即使使用合适的装载机也不能处理JSX,javascript,reactjs,webpack,jsx,Javascript,Reactjs,Webpack,Jsx,首先,我知道在SO中有很多类似的问题。我跟踪了他们,但还是有错误。我正在用gulp学习React.js,现在我想转到webpack,在浏览器上重新加载热代码。我正在学习根据以下代码配置webpack: 运行webpack--watch命令时,出现以下错误: Hash: 826e21c818de1882d366 Version: webpack 1.13.1 Time: 42ms [0] ./js/scripts.js 0 bytes [built] [failed] ERROR in
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)
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"
}
}
B.法律改革委员会
{
"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"));
从“React”导入React;
从“react dom”导入react dom;
从“评论”导入{Comment,CommentsList};
ReactDOM.render(,document.getElementById(“app”);
我认为没有必要发布comments.jsx,因为当我使用gulp时,我已经在我的应用程序中测试了它,并且工作没有问题
我试图修复但不起作用的内容:
- 创建.babelrc文件,即使我在webpack.config.js中设置了预设
- 使用--config标志执行webpack以检查它是否可以找到配置文件
- 使用github repo项目中使用的相同库(当我下载源代码并执行webpack时——观察它的工作情况)
- 检查它是否找到每个文件(comments.jsx、scripts.js)
- 将扩展名更改为js或jsx(并更新webpack.config.js),以检查是否是由于文件扩展名引起的
ReactDOM.render(<CommentsList />, document.getElementById("app"));
ReactDOM.render(,document.getElementById(“app”);
确切地说是什么时候开始的。即使我添加了必要的预设并具有所有依赖项,它也无法处理JSX。会发生什么情况?在这种情况下,您需要从
RegExp
中删除js\/
test: /\.jsx?$/
因为/js\/\.jsx?$/
匹配这样的文件
console.log(/js\/\.jsx?$/.test('js/.jsx'));
log(/js\/\.jsx?$/.test('js/.js'))
我认为您的RegExp
应该是这样的/\.jsx?$/
是的,加载程序的测试不正确。是的,您是对的。我的错误。我想,因为我的js/jsx文件在/js下,所以我应该把它放在regexp中@AlexanderT.,我不能将你的评论标记为答案,你能将其作为答案发布吗?
test: /\.jsx?$/