Babel 7-装饰器转换不适用于babel-loader [英] Babel 7 - Decorators transform doesn't work with babel-loader

查看:97
本文介绍了Babel 7-装饰器转换不适用于babel-loader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用mobX应用程序的React中的装饰器存在问题.它可以在.babelrc下正常工作,但是我在webpack下也可以使用babel-loader.我将babel配置复制到webpack配置,但是它不起作用.我检查了所有可以在google中找到的解决方案,但它们均引用bebelrc,而不是webpack配置.我的webpack配置:

I have a problem with decorators in React with mobX app. And it works ok with .babelrc, but I also have babel-loader with webpack. I copy babel config to webpack config, but it doesn't work. I checked all solutions that I can found in google, but they all refers to bebelrc, not webpack config. My webpack config:

const path = require('path');
const webpack = require('webpack');

module.exports = {
  devtool: 'cheap-module-eval-source-map',
  entry: [
    'webpack-hot-middleware/client',
    'babel-polyfill',
    path.join(__dirname, '../../src/web/index')
  ],
  output: {
    path: path.join(__dirname, '../public'),
    filename: 'bundle.js',
    publicPath: '/'
  },
  module: {
    rules: [
      {
        test: /\.json$/,
        loader: 'json-loader'
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        options: {
          presets: [
            ["@babel/preset-env", { "modules": false }],
            "@babel/preset-react",
          ],
          plugins: [
            [
              "@babel/plugin-proposal-decorators",
              {
                "legacy": true
              }
            ],
            "@babel/plugin-proposal-optional-chaining",
            "@babel/plugin-syntax-dynamic-import",
            "@babel/plugin-syntax-import-meta",
            "@babel/plugin-proposal-class-properties",
            "@babel/plugin-proposal-json-strings",
            ["@babel/plugin-transform-runtime", {
              "helpers": true,
              "regenerator": false
            }],
            "@babel/plugin-proposal-function-sent",
            "@babel/plugin-proposal-export-namespace-from",
            "@babel/plugin-proposal-numeric-separator",
            "@babel/plugin-proposal-throw-expressions",
            "react-hot-loader/babel"
          ]
        }
      }
    ]
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify('development'),
        PLATFORM_ENV: JSON.stringify('web')
      }
    }),
    new webpack.optimize.OccurrenceOrderPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin()
  ]
};

错误:

ERROR in ./src/web/stores/UIStore.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /Users/alder/Projects/_apps/service_exchange/service-exchange-app/src/web/stores/UIStore.js: Decorators transform is necessary.

似乎babel-loaderplugin-proposal-decorators不兼容

有解决方案吗?

package.json:

"scripts": {
  "web-dev": "webpack-dev-server --content-base web/public/ --config web/webpack/web.dev.config.js --port 3002 --inline --hot --colors --mode development"
 },
"dependencies": {
  "@babel/core": "^7.0.0-rc.1",
  "@babel/plugin-proposal-class-properties": "7.0.0-rc.1",
  "@babel/plugin-proposal-decorators": "7.0.0-rc.1",
  "@babel/plugin-proposal-export-namespace-from": "7.0.0-rc.1",
  "@babel/plugin-proposal-function-sent": "7.0.0-rc.1",
  "@babel/plugin-proposal-json-strings": "7.0.0-rc.1",
  "@babel/plugin-proposal-numeric-separator": "7.0.0-rc.1",
  "@babel/plugin-proposal-optional-chaining": "^7.0.0-rc.1",
  "@babel/plugin-proposal-throw-expressions": "7.0.0-rc.1",
  "@babel/plugin-syntax-dynamic-import": "7.0.0-rc.1",
  "@babel/plugin-syntax-import-meta": "7.0.0-rc.1",
  "@babel/plugin-transform-runtime": "^7.0.0-rc.1",
  "@babel/polyfill": "7.0.0-rc.1",
  "@babel/preset-env": "^7.0.0-rc.1",
  "@babel/preset-react": "^7.0.0-rc.1",
  "@babel/register": "7.0.0-rc.1",
  "@babel/runtime": "^7.0.0-rc.1",
  "@material-ui/core": "^1.5.0",
  "@material-ui/icons": "^2.0.2",
  "axios": "^0.18.0",
  "babel-loader": "^8.0.0-beta.0",
  "final-form": "^4.9.1",
  "history": "^4.7.2",
  "material-ui-chip-input": "^1.0.0-beta.5",
  "material-ui-superselectfield": "^1.9.8",
  "material-ui-time-picker": "^1.0.0",
  "mobx": "^5.0.3",
  "mobx-react": "^5.2.5",
  "mobx-state-router": "^3.1.2",
  "prop-types": "^15.6.2",
  "react": "^16.4.2",
  "react-dom": "^16.4.2",
  "react-event-listener": "^0.5.10",
  "react-final-form": "^3.6.5",
  "react-hot-loader": "4.3.4",
  "react-native": "0.56.0",
  "react-router-dom": "^4.3.1",
  "react-select": "^2.0.0",
  "styled-components": "^3.4.2"
},
"devDependencies": {
  "@babel/cli": "7.0.0-rc.1",
  "babel-eslint": "^8.2.6",
  "eslint": "^5.3.0",
  "eslint-plugin-react": "^7.11.1",
  "jest": "23.5.0",
  "json-loader": "^0.5.7",
  "mobx-logger": "^0.7.1",
  "mobx-react-devtools": "^6.0.2",
  "react-native-cli": "^2.0.1",
  "react-native-scripts": "^1.14.0",
  "react-test-renderer": "16.4.2",
  "react-transform-catch-errors": "^1.0.2",
  "react-transform-hmr": "^1.0.4",
  "redbox-react": "^1.6.0",
  "uglifyjs-webpack-plugin": "^1.3.0",
  "url-loader": "^1.1.0",
  "webpack": "^4.16.5",
  "webpack-cli": "^3.1.0",
  "webpack-dev-middleware": "^3.1.3",
  "webpack-dev-server": "^3.1.5",
  "webpack-hot-middleware": "^2.22.3"
}

在我必须从beta 54版本中删除stage-2后,它停止工作

It stopped working after I have to remove stage-2 from version beta 54

更新:

我发现我可以在webpack中使用babel config,例如:

I found that I can use babel config in webpack like:

    loader: 'babel-loader',
    options: {
      babelrc: true,
      cacheDirectory: true
    }

但这没有帮助,我有同样的错误.

But it didn't help, I have the same error.

更新2:

在babel-loader存储库中发现了类似的问题,但没有也有帮助.

Found similar issue in babel-loader repository, but it didn't help too.

推荐答案

我在一个块的开头移动了decorators插件,它起作用了!

I moved decorators plugin at the start of a block and it works!

{
  "presets": [
    ["@babel/preset-env", { "modules": false }],
    "@babel/preset-react"
  ],
  "plugins": [
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    "@babel/plugin-proposal-optional-chaining",
    "@babel/plugin-syntax-dynamic-import",
    "@babel/plugin-syntax-import-meta",
    "@babel/plugin-proposal-class-properties",
    "@babel/plugin-proposal-json-strings",
    "@babel/plugin-proposal-function-sent",
    "@babel/plugin-proposal-export-namespace-from",
    "@babel/plugin-proposal-numeric-separator",
    "@babel/plugin-proposal-throw-expressions"
  ],
  "env": {
    "development": {
      "plugins": ["react-hot-loader/babel"]
    }
  }
}

这篇关于Babel 7-装饰器转换不适用于babel-loader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆