简体   繁体   English

尝试在monorepo项目中配置Babel-当前未启用'classProperties'。 如何在monorepo项目中设置Babel?

[英]Trying to configure Babel in a monorepo project - 'classProperties' isn't currently enabled. How to set up Babel in a monorepo project?

I have a monorepo that is set according to this tutorial: tutorial . 我有一个monorepo,它是根据本教程设置的: tutorial

This project is composed by a main folder (series4) that holds three packages, which are focused on web, mobile applications and common code - they are named web, app and common folders respectively. 该项目由一个主文件夹(系列4)组成,该文件夹包含三个软件包,分别针对Web,移动应用程序和通用代码-它们分别命名为Web,app和common文件夹。

Summarizing ... these folders are: 总结...这些文件夹是:

/series4/packages/web
/series4/packages/common
/series4/packages/app

I am trying to use the Modal module, however I get this error: 我正在尝试使用Modal模块,但是出现此错误:

SyntaxError: series4/node_modules/react-native-modal/src/index.js: Support for the experimental syntax 'classProperties' isn't currently enabled (25:20) 语法错误:series4 / node_modules / react-native-modal / src / index.js:当前未启用对实验语法'classProperties'的支持(25:20)

I have read the documentation that is here: Babel . 我已经阅读了以下文档: Babel

However I was not able to solve this error. 但是我无法解决此错误。

The package.json that is in "series4" directory is: “ series4”目录中的package.json是:

{
  "name": "@wow/common",
  "version": "1.0.0",
  "description": "",
  "main": "dist/index.js",
  "scripts": {
    "build": "rimraf dist && tsc",
    "watch": "tsc --watch"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "16.8.2",
    "react-native": "0.59.0-rc.1",
    "react-native-elements": "^1.1.0",
    "react-native-image-slider-box": "^1.0.5",
    "react-native-snap-carousel": "^3.8.0"
  },
  "devDependencies": {
    "@babel/plugin-proposal-class-properties": "^7.5.5",
    "@types/react-native": "0.57.36",
    "rimraf": "^2.6.3",
    "typescript": "3.3.3"
  }
}

The babel.config.js file (it is in series4 directory) is: babel.config.js文件(位于series4目录中)为:

module.exports =  {
    presets: [
        "@babel/preset-env",
        "@babel/preset-react"
    ],
    plugins: [
      '@babel/proposal-class-properties',
      '@babel/plugin-proposal-class-properties'
    ],
    babelrcRoots: [
        ".",
        "packages/*",
      ],
  };

I have configured the following babelrc in web and common folders. 我已经在Web和公用文件夹中配置了以下babelrc。

{
    "presets": [
        "@babel/preset-env",
        "@babel/preset-react"
    ],
    "plugins": [
        [
          "@babel/plugin-proposal-class-properties"
        ]
    ]
}

The package.json in common folder is: 公用文件夹中的package.json是:

{
  "name": "@wow/common",
  "version": "1.0.0",
  "description": "",
  "main": "dist/index.js",
  "scripts": {
    "build": "rimraf dist && tsc",
    "watch": "tsc --watch"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "16.8.2",
    "react-native": "0.59.0-rc.1",
    "react-native-elements": "^1.1.0",
    "react-native-image-slider-box": "^1.0.5",
    "react-native-snap-carousel": "^3.8.0"
  },
  "devDependencies": {
    "@babel/plugin-proposal-class-properties": "^7.5.5",
    "@types/react-native": "0.57.36",
    "rimraf": "^2.6.3",
    "typescript": "3.3.3"
  }
}

The package.json in web folder is: Web文件夹中的package.json是:

{
  "name": "react-native-web-workout-series",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@types/jest": "24.0.5",
    "@types/node": "11.9.4",
    "@types/react": "16.8.3",
    "@types/react-dom": "16.8.1",
    "@wow/common": "1.0.0",
    "react": "^16.8.2",
    "react-art": "16.8.2",
    "react-dom": "^16.8.2",
    "react-native": "0.55.4",
    "react-native-modal": "^11.3.1",
    "react-native-web": "0.10.0",
    "react-scripts": "2.1.5",
    "typescript": "3.3.3"
  },
  "scripts": {
    "start": "cross-env SKIP_PREFLIGHT_CHECK=true react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "devDependencies": {
    "@babel/plugin-proposal-class-properties": "^7.5.5",
    "@types/react-native": "0.55.4",
    "babel-plugin-module-resolver": "^3.2.0",
    "cross-env": "^5.2.0"
  }
}

I get the error when I run the 当我运行

yarn start 

command in web folder (the command works perfectly when I execute it without the Modal module). Web文件夹中的命令(当我在不使用Modal模块的情况下执行该命令时,该命令可以完美运行)。

What am I missing? 我想念什么?

This is probably caused by react-native-modal not transpiling class property syntax. 这可能是由于 react-native-modal不转换类属性语法引起的。 And, by default, node_modules will not be transpiled. 而且,默认情况下,node_modules不会被编译。 When js engine read the class property syntax, it bails. 当js引擎读取类属性语法时,它会失败。

You need babel.config.js to affect the node_modules 您需要 babel.config.js来影响node_modules

 
 
 
  
  module.exports = function (api) { api.cache(true); return { babelrcRoots: [ '.', './modules/*' ], ignore: [/node_modules/(?!react-native-modal)/], presets: ["@babel/preset-env"] }; }
 
  

'classProperties' isn't currently enabled also implies that the JS engine support class property syntax with some configuration, maybe there is a way to make the engine accept this syntax 'classProperties' isn't currently enabled还意味着JS引擎支持具有某些配置的类属性语法,也许有一种方法可以使引擎接受该语法

See this 看到 这个

TL;DR TL; DR

To have a custom config for babel in create-react-app , you need to first eject and work on the config directly after that. 要在create-react-app为babel create-react-app自定义配置,您需要先eject并在此之后直接处理该配置。

Run npm run eject , go to config/webpack.config.js , find and replace the babel-loader with test: /\\.(js|mjs|jsx|ts|tsx)$/ . 运行npm run eject config/webpack.config.js ,转到config/webpack.config.js ,找到并用test: /\\.(js|mjs|jsx|ts|tsx)$/替换babel-loader test: /\\.(js|mjs|jsx|ts|tsx)$/ .( test: /\\.(js|mjs|jsx|ts|tsx)$/

 ... { test: /\\.(js|mjs|jsx|ts|tsx)$/, include: [paths.appSrc, paths.appNodeModules], exclude: /node_modules\\/(?!react-native-modal|react-native-animatable)/, loader: require.resolve('babel-loader'), options: { customize: require.resolve( 'babel-preset-react-app/webpack-overrides' ), presets: [ "@babel/preset-env", "@babel/preset-react", "@babel/preset-typescript", ], plugins: [ [ require.resolve('babel-plugin-named-asset-import'), { loaderMap: { svg: { ReactComponent: '@svgr/webpack?-svgo,+titleProp,+ref![path]', }, }, }, ], ['@babel/plugin-proposal-class-properties', { loose: true }], ], // This is a feature of `babel-loader` for webpack (not Babel itself). // It enables caching results in ./node_modules/.cache/babel-loader/ // directory for faster rebuilds. cacheDirectory: true, cacheCompression: isEnvProduction, compact: isEnvProduction, }, }, ... 

What I said is partially true, babel is not transpiling react-native-modal . 我所说的部分是正确的,巴别塔不是在转嫁react-native-modal And, why creating a babel.cofig.js doesn't work is, create-react-app uses a config that lives in another universe ( see this SO ). 而且,为什么创建babel.cofig.js不起作用是, create-react-app使用位于另一个Universe中的配置( 请参见此SO )。

At first I noticed there is a syntax error in your babel.config.js but building the project is not complaining anything about that. 起初,我注意到您的babel.config.js存在语法错误,但构建项目并未对此有任何抱怨。 So I suspect it is not being used and come across the above SO post. 因此,我怀疑它没有被使用,并且遇到了上述SO帖子。

It now compiles successfully but throws some runtime error because some packages are built for mobile app and expo . 现在,它可以成功编译,但由于某些软件包是为移动应用程序和expo构建的,因此会引发一些运行时错误。


out of scope 超出范围

One notable one is in react-native-modal . 一个值得注意的例子是react-native-modal

module.exports = {
  get DeviceEventEmitter() { ... }
};
// which is approximately
export default {
  DeviceEventEmitter,
}

However, in react-native , 但是,在react-native

presets: [
  ["@babel/preset-env", { module: 'commonjs' }],
  "@babel/preset-react",
  "@babel/preset-typescript",
],

Using named import as object destructuring is considered wrong by babel. babel认为使用命名导入作为对象销毁是错误的。 You could workaround this by 您可以通过以下方法解决此问题

 presets: [ ["@babel/preset-env", { module: 'commonjs' }], "@babel/preset-react", "@babel/preset-typescript", ], 

However, DeviceEventEmitter will be undefined then. 但是, DeviceEventEmitter将是undefined Other export works fine tho. 其他出口工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Jest、Babel、Webpack 和 React 进行测试。 当前未启用对实验性语法“classProperties”的支持 - Testing with Jest, Babel, Webpack, and React. Support for the experimental syntax 'classProperties' isn't currently enabled 当前未启用对实验语法“classProperties”的支持 (8:16)。 添加@babel/plugin-proposal-class-properties - Support for the experimental syntax 'classProperties' isn't currently enabled (8:16). Add @babel/plugin-proposal-class-properties @babel/plugin-proposal-class-properties 仍然失败,“classProperties 当前未启用” - @babel/plugin-proposal-class-properties Still Fails with "classProperties isn't currently enabled" React - 当前未启用“classProperties” - React - 'classProperties' isn't currently enabled 如何配置 Nextjs 项目以转译 monorepo 项目? - How do I configure a Nextjs project to transpile a monorepo project? 当前未启用反应组件模块“classProperties” - react component module 'classProperties' isn't currently enabled Laravel:当前未启用对实验语法“classProperties”的支持 - Laravel: Support for the experimental syntax 'classProperties' isn't currently enabled 如何解决问题当前未启用对实验性语法“classProperties”的支持 - How to solve the problem Support for the experimental syntax 'classProperties' isn't currently enabled Babel-Standalone 错误:内联 Babel 脚本:当前未启用对实验性语法“decorators-legacy”的支持 (14:1): - Babel-Standalone Error : Inline Babel script: Support for the experimental syntax 'decorators-legacy' isn't currently enabled (14:1): Babel为什么不从事我的React Native项目? - Why isn't Babel working on my React Native project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM