gpt4 book ai didi

javascript - Webpack 和 React -- 意外的标记

转载 作者:行者123 更新时间:2023-12-03 13:56:18 25 4
gpt4 key购买 nike

我是 React 和 Webpack 的新手。我正在设置我的第一个项目,当我尝试运行 webpack-dev-server 时,我的代码无法编译!

更新下面的答案是正确的。我需要将 'react' 添加到 babel loader 预设中。您可以在此处查看项目的完整源代码:https://github.com/cleechtech/redux-todo

错误:

$ webpack-dev-server             
http://localhost:8080/webpack-dev-server/
webpack result is served from /
content is served from ./dist
Hash: d437a155a1da4cdfeeeb
Version: webpack 1.12.14
Time: 5938ms
Asset Size Chunks Chunk Names
bundle.js 1.51 kB 0 [emitted] main
chunk {0} bundle.js (main) 28 bytes [rendered]
[0] multi main 28 bytes {0} [built] [1 error]

ERROR in ./src/index.js
Module build failed: SyntaxError: /Users/connorleech/Projects/redux-todo/src/index.js: Unexpected token (7:16)
console.log(ReactDOM);

ReactDOM.render(<App />,document.getElementById('root'));

src/index.js:

var react = require('react');
var ReactDOM = require('react-dom');
var App = require('./components/App');

console.log(ReactDOM);

ReactDOM.render(<App />,document.getElementById('root'));

src/components/App.js

var React = require('react');

var App = React.createClass({
render: function() {
return (
<div>
<h1>I am app!</h1>
</div>
);
}
});

console.log(App);

module.exports = App;

dist/index.html

<!doctype html>
<head>
<title>Redux todo</title>
</head>
<body>
<h1>Hello world</h1>

<div id='root'></div>
<script src='bundle.js'></script>
</body>

最后这是我的 webpack 配置和 package.json:

module.exports = {
// starting point
entry: [
'./src/index.js'
],
module: {
loaders: [
{
test: /\.js?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel', // 'babel-loader' is also a legal name to reference
query: {
presets: ['es2015']
}
}
]
},
resolve: {
extensions: ['', '.js', '.jsx']
},
// create bundle.js file
output: {
path: __dirname + '/dist',
publicPath: '/',
filename: 'bundle.js'
},
devServer: {
contentBase: './dist'
}
};

package.json

{
"name": "redux-todo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/cleechtech/redux-todo.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/cleechtech/redux-todo/issues"
},
"homepage": "https://github.com/cleechtech/redux-todo#readme",
"dependencies": {
"react": "^0.14.8",
"react-dom": "^0.14.8",
"react-redux": "^4.4.1",
"redux": "^3.3.1"
},
"devDependencies": {
"babel-core": "^6.7.4",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.6.0",
"webpack": "^1.12.14"
}
}

最佳答案

您还需要将 react 预设添加到您的 babel-loader 配置中

并且它必须位于es2015之后

  {
test: /\.js?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel', // 'babel-loader' is also a legal name to reference
query: {
presets: ['es2015', 'react']
}
}

您遇到的问题的发生是因为 babel 知道如何转译 JSX - 它应该知道它的语法,而它并不是开箱即用的。

正如评论中提到的 - 你还必须安装 babel-preset-react npm 包(这很明显,因为 babel 会在第一次运行时告诉你它) )。

引用文献:

关于javascript - Webpack 和 React -- 意外的标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36392826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com