React.js 当前未启用对实验性语法 'classProperties' 的支持错误
Support for the experimental syntax 'classProperties' isn't currently enabled error on a React.js
当我尝试将 react-native-vector-icon
与使用 react-native-web
构建的 React 项目一起使用时,我得到
Support for the experimental syntax 'classProperties' isn't currently
enabled
我尝试了以下解决方案,但 none 对我有用。
- Support for the experimental syntax 'classProperties' isn't currently enabled
- "loose": true is not fixing Support for the experimental syntax 'classProperties' isn't currently enabled
我的package.json
{
"name": "react-webiste",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.19.2",
"fsevents": "^2.1.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-native": "^0.62.1",
"react-native-svg": "^12.1.0",
"react-native-vector-icons": "^6.6.0",
"react-native-web": "^0.12.2",
"react-router-dom": "^5.1.2",
"react-scripts": "3.0.0",
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@babel/plugin-proposal-class-properties": "^7.8.3"
}
}
babel.config.js
module.exports = {
presets: ["@babel/preset-env", "@babel/preset-react"],
plugins: ["@babel/plugin-proposal-class-properties",{"loose": true}]
};
任何人都可以建议我应该如何解决这个问题。
编辑:您的问题似乎是图书馆所有者当前遇到的问题。该库是为 react-native
构建的,用户习惯于手动转译。你可以找到 the GitHub project issue here
但是,正如某些用户所建议的那样,您可以尝试使用 config-overrides.js
,将 resolveApp('../node_modules/react-native-vector-icons')
添加到您的捆绑步骤中,例如 described in this GitHub comment。
为了使用 config-overrides
,您应该迁移到 react-app-rewired
(https://github.com/timarney/react-app-rewired).
我将粘贴配置覆盖更改以供将来参考,感谢 jookovjok
,他已经满了 config-overrides.js:
const appIncludes = [
...
resolveApp('../node_modules/react-native-vector-icons') // <- HERE
]
...
config.module.rules[2].oneOf[1].options.plugins = [
...
require.resolve('@babel/plugin-proposal-class-properties') // <- HERE
]...
编辑前的旧答案:
您是否正确粘贴了 babel.config.js
?在我看来,您的 plugins
声明拼错了:
将插件行更改为:(删除多余的 t
):
plugins: ["@babel/plugin-proposal-class-properties", {"loose": true}]
在一般情况下,所有 babel 配置都可以从 babel.config.js
或任何其他受支持的 formate 文件传递下来。但就我而言,webpack.config.js
中的 babel-loader
有自己的选项,因此所有配置都被覆盖为 described by Babel team on Github Comment.
其次,我使用 metro-react-native-babel-preset
而不是 @babel/plugin-proposal-class-properties
来解决 babel 丢失的问题。
这是我的 Webpack Config 弹出应用程序后:
{
test: /\.(js|mjs)$/,
exclude: /@babel(?:\/|\{1,2})runtime/,
loader: require.resolve('babel-loader'),
options: {
babelrc: false,
configFile: false,
compact: false,
// Added the module here
presets: [
[
require.resolve('babel-preset-react-app/dependencies'),
{ helpers: true },
],["module:metro-react-native-babel-preset"]
],
// More configuration codes goes here
{
test: /\.(js|mjs|jsx|ts|tsx)$/,
enforce: 'pre',
use: [
{
options: {
cache: true,
formatter: require.resolve('react-dev-utils/eslintFormatter'),
eslintPath: require.resolve('eslint'),
resolvePluginsRelativeTo: __dirname,
},
loader: require.resolve('eslint-loader'),
},
],
include:[ //Change here as well
paths.appSrc,
path.resolve('node_modules/react-native-vector-icons'),
]
},
完成所有这些后,react-native-vector-icon
将呈现但不会加载任何内容。为此,我们需要将以下样式添加到 App.css
或任何根样式 sheet.
/*Import the Icon CSS*/
@font-face {
font-family: "Entypo";
src: url("~react-native-vector-icons/Fonts/Entypo.ttf") format("truetype");
}
@font-face {
font-family: "EvilIcons";
src: url("~react-native-vector-icons/Fonts/EvilIcons.ttf") format("truetype");
}
@font-face {
font-family: "FontAwesome";
src: url("~react-native-vector-icons/Fonts/FontAwesome.ttf")
format("truetype");
}
@font-face {
font-family: "fontcustom";
src: url("~react-native-vector-icons/Fonts/Foundation.ttf") format("truetype");
}
@font-face {
font-family: "Ionicons";
src: url("~react-native-vector-icons/Fonts/Ionicons.ttf") format("truetype");
}
@font-face {
/*font-family: 'MaterialCommunityIcons';*/
font-family: "Material Design Icons";
src: url("~react-native-vector-icons/Fonts/MaterialCommunityIcons.ttf")
format("truetype");
}
@font-face {
font-family: "MaterialIcons";
src: url("~react-native-vector-icons/Fonts/MaterialIcons.ttf")
format("truetype");
}
@font-face {
font-family: "Octicons";
src: url("~react-native-vector-icons/Fonts/Octicons.ttf") format("truetype");
}
@font-face {
font-family: "simple-line-icons";
src: url("~react-native-vector-icons/Fonts/SimpleLineIcons.ttf")
format("truetype");
}
@font-face {
font-family: "Zocial";
src: url("~react-native-vector-icons/Fonts/Zocial.ttf") format("truetype");
}
我在几天内尝试了很多东西。让 classProperties
在 react-native-web 上工作的最终解决方案是遵循这个非常受欢迎的 repo 的简单指南:https://reactnativeelements.com/docs/web_usage/
您必须在文件 config-overrides.js
:
中添加导致问题的包 (react-native-vector-icons)
const path = require('path');
const { override, addBabelPlugins, babelInclude } = require('customize-cra');
module.exports = override(
...addBabelPlugins('@babel/plugin-proposal-class-properties'),
babelInclude([
path.resolve(__dirname, 'node_modules/react-native-elements'),
path.resolve(__dirname, 'node_modules/react-native-vector-icons'),
path.resolve(__dirname, 'node_modules/react-native-ratings'),
path.resolve(__dirname, 'src'),
])
);
当我尝试将 react-native-vector-icon
与使用 react-native-web
构建的 React 项目一起使用时,我得到
Support for the experimental syntax 'classProperties' isn't currently enabled
我尝试了以下解决方案,但 none 对我有用。
- Support for the experimental syntax 'classProperties' isn't currently enabled
- "loose": true is not fixing Support for the experimental syntax 'classProperties' isn't currently enabled
我的package.json
{
"name": "react-webiste",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.19.2",
"fsevents": "^2.1.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-native": "^0.62.1",
"react-native-svg": "^12.1.0",
"react-native-vector-icons": "^6.6.0",
"react-native-web": "^0.12.2",
"react-router-dom": "^5.1.2",
"react-scripts": "3.0.0",
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@babel/plugin-proposal-class-properties": "^7.8.3"
}
}
babel.config.js
module.exports = {
presets: ["@babel/preset-env", "@babel/preset-react"],
plugins: ["@babel/plugin-proposal-class-properties",{"loose": true}]
};
任何人都可以建议我应该如何解决这个问题。
编辑:您的问题似乎是图书馆所有者当前遇到的问题。该库是为 react-native
构建的,用户习惯于手动转译。你可以找到 the GitHub project issue here
但是,正如某些用户所建议的那样,您可以尝试使用 config-overrides.js
,将 resolveApp('../node_modules/react-native-vector-icons')
添加到您的捆绑步骤中,例如 described in this GitHub comment。
为了使用 config-overrides
,您应该迁移到 react-app-rewired
(https://github.com/timarney/react-app-rewired).
我将粘贴配置覆盖更改以供将来参考,感谢 jookovjok
,他已经满了 config-overrides.js:
const appIncludes = [
...
resolveApp('../node_modules/react-native-vector-icons') // <- HERE
]
...
config.module.rules[2].oneOf[1].options.plugins = [
...
require.resolve('@babel/plugin-proposal-class-properties') // <- HERE
]...
编辑前的旧答案:
您是否正确粘贴了 babel.config.js
?在我看来,您的 plugins
声明拼错了:
将插件行更改为:(删除多余的 t
):
plugins: ["@babel/plugin-proposal-class-properties", {"loose": true}]
在一般情况下,所有 babel 配置都可以从 babel.config.js
或任何其他受支持的 formate 文件传递下来。但就我而言,webpack.config.js
中的 babel-loader
有自己的选项,因此所有配置都被覆盖为 described by Babel team on Github Comment.
其次,我使用 metro-react-native-babel-preset
而不是 @babel/plugin-proposal-class-properties
来解决 babel 丢失的问题。
这是我的 Webpack Config 弹出应用程序后:
{
test: /\.(js|mjs)$/,
exclude: /@babel(?:\/|\{1,2})runtime/,
loader: require.resolve('babel-loader'),
options: {
babelrc: false,
configFile: false,
compact: false,
// Added the module here
presets: [
[
require.resolve('babel-preset-react-app/dependencies'),
{ helpers: true },
],["module:metro-react-native-babel-preset"]
],
// More configuration codes goes here
{
test: /\.(js|mjs|jsx|ts|tsx)$/,
enforce: 'pre',
use: [
{
options: {
cache: true,
formatter: require.resolve('react-dev-utils/eslintFormatter'),
eslintPath: require.resolve('eslint'),
resolvePluginsRelativeTo: __dirname,
},
loader: require.resolve('eslint-loader'),
},
],
include:[ //Change here as well
paths.appSrc,
path.resolve('node_modules/react-native-vector-icons'),
]
},
完成所有这些后,react-native-vector-icon
将呈现但不会加载任何内容。为此,我们需要将以下样式添加到 App.css
或任何根样式 sheet.
/*Import the Icon CSS*/
@font-face {
font-family: "Entypo";
src: url("~react-native-vector-icons/Fonts/Entypo.ttf") format("truetype");
}
@font-face {
font-family: "EvilIcons";
src: url("~react-native-vector-icons/Fonts/EvilIcons.ttf") format("truetype");
}
@font-face {
font-family: "FontAwesome";
src: url("~react-native-vector-icons/Fonts/FontAwesome.ttf")
format("truetype");
}
@font-face {
font-family: "fontcustom";
src: url("~react-native-vector-icons/Fonts/Foundation.ttf") format("truetype");
}
@font-face {
font-family: "Ionicons";
src: url("~react-native-vector-icons/Fonts/Ionicons.ttf") format("truetype");
}
@font-face {
/*font-family: 'MaterialCommunityIcons';*/
font-family: "Material Design Icons";
src: url("~react-native-vector-icons/Fonts/MaterialCommunityIcons.ttf")
format("truetype");
}
@font-face {
font-family: "MaterialIcons";
src: url("~react-native-vector-icons/Fonts/MaterialIcons.ttf")
format("truetype");
}
@font-face {
font-family: "Octicons";
src: url("~react-native-vector-icons/Fonts/Octicons.ttf") format("truetype");
}
@font-face {
font-family: "simple-line-icons";
src: url("~react-native-vector-icons/Fonts/SimpleLineIcons.ttf")
format("truetype");
}
@font-face {
font-family: "Zocial";
src: url("~react-native-vector-icons/Fonts/Zocial.ttf") format("truetype");
}
我在几天内尝试了很多东西。让 classProperties
在 react-native-web 上工作的最终解决方案是遵循这个非常受欢迎的 repo 的简单指南:https://reactnativeelements.com/docs/web_usage/
您必须在文件 config-overrides.js
:
const path = require('path');
const { override, addBabelPlugins, babelInclude } = require('customize-cra');
module.exports = override(
...addBabelPlugins('@babel/plugin-proposal-class-properties'),
babelInclude([
path.resolve(__dirname, 'node_modules/react-native-elements'),
path.resolve(__dirname, 'node_modules/react-native-vector-icons'),
path.resolve(__dirname, 'node_modules/react-native-ratings'),
path.resolve(__dirname, 'src'),
])
);