使用 ReactJS、Webpack 和 Babel 进行 Hello World
我想用 React 打印简单的 Hello World。这是我的应用程序目录结构:
webpack.config.js
const path = require('path');
module.exports = {
entry: {
app: './js/app.js'
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'build')
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react']
}
}
]
},
stats: {
colors: true
},
devtool: 'source-map'
};
js/app.js
var React = require('react');
var ReactDOM = require('react-dom');
ReactDOM.render(
<h1>Hello, world!</h1>,
document.getElementById('root')
);
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link href='http://fonts.googleapis.com/css?family=Roboto:400,300,700' rel='stylesheet' type='text/css'>
<link href="css/styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="root"></div>
<script src="build/app.bundle.js"></script>
</body>
</html>
package.json
{
"name": "es6-tutorial-react",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"webpack": "webpack --mode=development"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ccoenraets/es6-tutorial-react.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/ccoenraets/es6-tutorial-react/issues"
},
"homepage": "https://github.com/ccoenraets/es6-tutorial-react#readme",
"devDependencies": {
"@babel/core": "^7.1.2",
"@babel/preset-env": "^7.1.0",
"@babel/preset-react": "^7.0.0",
"babel-core": "^6.26.3",
"babel-loader": "^8.0.4",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"react": "^16.5.2",
"react-dom": "^16.5.2",
"webpack": "^4.20.2",
"webpack-cli": "^3.1.2"
}
}
当我运行
npm run webpack
时,它返回错误:
Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema. - configuration.module has an unknown property 'loaders'. These properties are valid: object { defaultRules?, exprContextCritical?, exprContextRecursive?, exprContextRegExp?, exprContextRequest?, noParse?, rules?, strictExportPresence?, strictThisContextOnImports?, unknownContextCritical?, unknownContextRecursive?, unknownContextRegExp?, unknownContextRequest?, unsafeCache?, wrappedContextCritical?, wrappedContextRecursive?, wrappedContextRegExp? } -> Options affecting the normal modules (
NormalModuleFactory
). npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! [email protected] webpack:webpack --mode=development
npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the [email protected] webpack script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.npm ERR! A complete log of this run can be found in: npm ERR!
C:\Users\User\AppData\Roaming\npm-cache_logs\2018-10-15T10_57_59_640Z-debug.log
请。帮助谢谢。
正如 @Dominic 所说,使用
rules
属性,而不是立即使用
loaders
。如下所示:
...
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react']
}
}
}
]
},
...
您需要修复的另一件事是您的
index.html
:
Change this: <script src="build/app.bundle.js"></script>
To this: <script src="build/bundle.js"></script>
因为在您的 webpack.config.js 中,您已将输出文件名指定为
bunlde.js
,而不是
app.bundle.js
。
希望对您有所帮助!
您正在使用来自 webpack v3 的代码,但已安装 webpack v4。在 v4 中,Loaders 称为
rules
。请遵循最新指南:
https://webpack.js.org/guides/getting-started/
或
https://www.valentinog.com/blog/react-webpack-babel/
等
您还安装了来自
@babel
的新预设,因此请确保您的 .babelrc 正确无误,并且不要遵循某些旧的 <= v6 指南。
降低版本,你的问题将得到解决。
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-eslint": "^8.2.3",
"babel-loader": "^7.1.4",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",