Webpack4とBabel7でリアクションアプリをビルドしようとすると、次のエラーが発生します。
./src/index.jsモジュールのビルドエラー(./node_modules/babel-loader/lib/index.jsから):エラー: '/ Users/me/Desktop /からモジュール' babel-preset-react 'が見つかりませんreflask '-「react」を解決する場合は、「module:react」を使用します-「@ babel/react」を意味しますか? Function.module.exports [as sync](/Users/me/Desktop/reflask/node_modules/resolve/lib/sync.js:43:15)at resolveStandardizedName(/ Users/me/Desktop/reflask/node_modules/@ babel /core/lib/config/files/plugins.js:101:31)resolvePreset(/Users/me/Desktop/reflask/node_modules/@babel/core/lib/config/files/plugins.js:58:10) loadPreset(/Users/me/Desktop/reflask/node_modules/@babel/core/lib/config/files/plugins.js:77:20)at createDescriptor(/ Users/me/Desktop/reflask/node_modules/@ babel/core/lib/config/config-descriptors.js:154:9)at items.map(/Users/me/Desktop/reflask/node_modules/@babel/core/lib/config/config-descriptors.js:109:50 )Array.map()でcreateDescriptors(/Users/me/Desktop/reflask/node_modules/@babel/core/lib/config/config-descriptors.js:109:29)at createPresetDescriptors(/ Users/me/Desktop/reflask/node_modules/@babel/core/lib/config/config-descriptors.js:101:10)at passPerPreset(/ Users/me/Desktop/reflask/node_modules/@ babel/core/lib/config/config-descriptors。 js:58:96) @ multi(webpack)-dev-server/client? http:// localhost:808 ./src main [1]
Node_modulesフォルダーを削除して、次の依存関係を再インストールしようとしました。
ターミナル
rm -rf node_modules/
npm install
構成
package.json
{
"name": "reflask",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --open --mode development",
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.1.0",
"@babel/preset-env": "^7.1.0",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.2",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"prop-types": "^15.6.2",
"webpack": "^4.19.1",
"webpack-cli": "^3.1.1",
"webpack-dev-server": "^3.1.9"
},
"dependencies": {
"bootstrap": "^4.1.3",
"react": "^16.5.2",
"react-bootstrap": "^0.32.4",
"react-dom": "^16.5.2",
"react-router-dom": "^4.3.1"
}
}
webpack.config.js
const HtmlWebPackPlugin = require("html-webpack-plugin");
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ['react']
}
}
},
{
test: /\.html$/,
use: [
{
loader: "html-loader"
}
]
}
]
},
plugins: [
new HtmlWebPackPlugin({
template: "./src/index.html",
filename: "./index.html"
})
]
};
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './js/components/App.jsx';
import registerServiceWorker from './registerServiceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();
。babelrc
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}
あなたのウェブパック設定で、あなたはすでにreact
の代わりに@babel/preset-react
を試しましたか?
ところで/\.js$/
のテスト/\.jsx?$/
のテストの改善(x?はxがオプションであることを意味します)
ない
options: {
presets: ['react']
}
しかし
options: {
presets: ['@babel/preset-react']
}
.babelrcファイルをルートディレクトリに配置し、これを内部に入れます
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}
babel-loader webpack cfgからプリセットを削除します
options: {
presets: ['react']
}
最後にこの設定でそれを行いました(es2015が変更されたため https://www.npmjs.com/package/babel-preset-es2015 )
そして、package.jsonまたは。babelrcまたはbabel.config::
use: { loader: 'babel-loader', options: { presets: ['@babel/react', '@babel/preset-env'], plugins: ['@babel/plugin-proposal-class-properties'] } }
そして..
"devDependencies": { "@babel/cli": "^7.1.5", "@babel/core": "^7.1.6", "@babel/plugin-proposal-class-properties": "^7.1.0", "@babel/plugin-transform-react-constant-elements": "^7.0.0", "@babel/plugin-transform-react-inline-elements": "^7.0.0", "@babel/preset-env": "^7.1.6", "@babel/preset-react": "^7.0.0", "@babel/register": "^7.0.0", "@babel/runtime": "^7.1.5", "babel-eslint": "^8.2.6", "babel-loader": "^8.0.4", "babel-node": "^6.5.3", "babel-plugin-react-transform": "^3.0.0", "babel-plugin-transform-class-properties": "^6.24.1", "babel-plugin-transform-react-pure-class-to-function": "^1.0.1", "babel-plugin-transform-react-remove-prop-types": "^0.4.20", "babel-polyfill": "^6.26.0", "babel-preset-env": "^1.7.0", "babel-preset-es2015": "^6.24.1", "babel-preset-react": "^6.24.1", "webpack": "^4.26.1" }
プリセット例に呼び出すコード:
"theme:build": "babel theme/src -d theme/dist --presets @babel/react --plugins transform-class-properties --quiet && npm run webpack:store",
プリセット@ babel/react
gitHubでこのテーマに関する非常に多くのフォーラム!