javascript - @babel/plugin-proposal-class-properties 不起作用

标签 javascript node.js reactjs babeljs

我有这样一个项目结构:

/main-folder

  /apps
    /app — // I want to import "project-b" here
      /node_modules
      /public
      /src
        |app.js
        |app.css
      |.babelrc
      |package.json
      |package-lock.json
      |webpack.config.js

  /static
    /project-b — // Project that I want to export
      /node_modules
      /public
      /src
        /lib
          ...components
      |.babelrc
      |package.json
      |package-lock.json

当我尝试导入没有类属性的组件时一切正常。 但是,如果我导入具有类属性的组件:

enter image description here

我想如果我添加 .babelrc 它会起作用(我将 .babelrc 添加到 appproject-b 文件夹,它仍然不起作用)

.babelrc:

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

app package.json:

{
  "name": "auth",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "webpack": "webpack",
    "start:dev": "webpack-dev-server",
    "webpack:production": "webpack -p"
  },
  "author": "namename",
  "license": "MIT",
  "dependencies": {
    "css-loader": "^1.0.1",
    "entrl-ui-kit": "^1.0.3",
    "file-loader": "^2.0.0",
    "react": "^16.6.3",
    "react-dom": "^16.6.3",
    "react-router-dom": "^4.3.1",
    "react-transition-group": "^2.5.0",
    "style-loader": "^0.23.1"
  },
  "devDependencies": {
    "@babel/core": "^7.1.6",
    "@babel/plugin-proposal-class-properties": "^7.1.0",
    "@babel/preset-env": "^7.1.6",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.4",
    "webpack": "^4.26.0",
    "webpack-cli": "^3.1.2",
    "webpack-dev-server": "^3.1.10"
  }
}

project-b package.json:

 {
  "name": "project-b",
  "version": "1.0.0",
  "author": "namename",
  "license": "MIT",
  "main": "dist/index.js",
  "module": "dist/index.js",
  "files": [
    "dist"
  ],
  "scripts": {
    "start": "react-scripts start",
    "build-examples": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "build": "rimraf ./build && mkdirp ./build && babel -d ./build ./src"
  },
  "devDependencies": {
    "@babel/cli": "^7.1.5",
    "@babel/plugin-proposal-class-properties": "^7.1.0",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.0-beta.6",
    "babel-preset-latest": "^6.24.1",
    "mkdirp": "^0.5.1",
    "react": "^16.6.3",
    "react-dom": "^16.6.3",
    "react-scripts": "^2.1.1",
    "react-transition-group": "^2.5.0",
    "rimraf": "^2.6.2"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "dependencies": {
    "babel-core": "^7.0.0-bridge.0"
  }
}  

最佳答案

我需要做的就是将 babel.config.js 添加到根文件夹:

感谢loganfsmyth !

/main-folder
  |babel.config.js — //HERE!
  /apps
    /app — // I want to import "project-b" here
      /node_modules
      /public
      /src
        |app.js
        |app.css
      |.babelrc
      |package.json
      |package-lock.json
      |webpack.config.js

  /static
    /project-b — // Project that I want to export
      /node_modules
      /public
      /src
        /lib
          ...components
      |.babelrc
      |package.json
      |package-lock.json

babel.config.js:

module.exports = {
  babelrcRoots: [
    ".",
    "static/project-b/*"
  ]
};

关于javascript - @babel/plugin-proposal-class-properties 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53542173/

相关文章:

javascript - 如何从 Node js app.post() 调用 html 中的 javascript 函数?

node.js - mongoose#populate 在数组内的嵌套对象中返回 null

node.js - Heroku 推送失败,远程被拒绝

javascript - 如何切换 Polymer 入门套件中的应用程序抽屉

node.js - 从nodejs中的请求模块调用时,github搜索api没有给出结果

javascript - 在 React 中更改时淡出/淡入文本

reactjs - React SPA动态环境配置

javascript - jQuery 相当于 body onLoad

javascript - If/else 语句比较数字

javascript - Angular 指令 ng-model 适用于数组,但不适用于字符串