简体   繁体   English

Babel插件 - 提议装饰器未按预期工作

[英]Babel plugin-proposal-decorators not working as expected

I have added these two devDependencies in my package.json : 我在package.json添加了这两个devDependencies:

"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/plugin-proposal-decorators": "^7.1.6",

In .babelrc a file I have added them as plugins: .babelrc我添加了一个文件作为插件:

{
    "presets": ["module:metro-react-native-babel-preset"],
    "plugins": [
        ["@babel/plugin-proposal-decorators", { "legacy": true}],
        ["@babel/plugin-proposal-class-properties", { "loose": true}]
    ]
}

I am using mobx so observable is the clean syntax, my file looks like this: 我使用mobx所以observable是干净的语法,我的文件看起来像这样:

class AppStore {
  @observable username = ''
}

export default (new AppStore())

But it is always showing this error: 但它始终显示此错误:

I think I have done it correctly but there is no way to detect whether babel plugins are loaded or not. 我想我已经正确完成了,但无法检测babel插件是否已加载。

First, make sure that you are on the latest version of metro-react-native-babel-preset , they released a new minor 0.50.0 only 9 days ago. 首先,确保您使用的是最新版本的metro-react-native-babel-preset ,他们仅在9天前发布了一个新的小调0.50.0

If that didn't help, it's likely the problem comes from the fact that the metro-react-native-babel-preset already includes the class property plugin, and as you know the order of the plugins matter , you need to have the decorator run before the class property plugin . 如果这没有帮助,可能问题来自于metro-react-native-babel-preset 已经包含了class属性插件这一事实,并且你知道插件的顺序很重要你需要有装饰器在类属性插件之前运行

There has been a lot of discussion on this subject of ordering in babel, and although plugins are supposed to run before presets, it still would be a problem . 关于babel的这个主题有很多讨论,尽管插件应该在预设之前运行,但它仍然是个问题 Unfortunately PR #5735 to add plugin ordering capabilities is still a work in progress. 不幸的是,添加插件订购功能的PR #5735仍在进行中。

What you could do in the meantime could be to either fork your own metro-react-native-babel-preset and add the decorator plugin before the class property plugin at the place I pointed. 在此期间你可以做的就是分叉你自己的metro-react-native-babel-preset并在我指向的地方的类属性插件之前添加装饰器插件。 You might also try to make your own babel preset including the two plugins in the right order and add it after the metro one, as presets are loaded in reverse order, last to first as seen here . 您也可以尝试让自己的通天预设,包括以正确的顺序的两个插件和地铁相继添加它,为预设以相反的顺序加载,最后到第一所看到这里

Also worth a try would be to start the packager using yarn start --reset-cache which might solve issues caused by a bad/outdated cache. 另外值得一试的是使用yarn start --reset-cache启动打包程序,这可能会解决由错误/过时缓存引起的问题。

暂无
暂无

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

相关问题 如何使用 vite 启用“@babel/plugin-proposal-decorators” - How do i enable "@babel/plugin-proposal-decorators" with vite 如何使用Babel 7的插件提议装饰器和异步函数来装饰异步方法? - How do I decorate an async method using Babel 7's plugin-proposal-decorators with an async function? 错误:无法从“/home/dolphin/sync/source/cruise-web”找到模块“@babel/plugin-proposal-decorators” - Error: Cannot find module '@babel/plugin-proposal-decorators' from '/home/dolphin/sync/source/cruise-web' 使用 Firebase 在 CodeSandbox 上找不到/安装 babel 插件“proposal-decorators” - Could not find/install babel plugin 'proposal-decorators' on CodeSandbox with Firebase @babel/plugin-proposal-optional-chaining 在 Vue.js 中不起作用<script> tag - @babel/plugin-proposal-optional-chaining not working inside Vue.js <script> tag @ babel / plugin-proposal-class-properties不起作用 - @babel/plugin-proposal-class-properties doesn't work 带有 Babel plugin-proposal-export-default-from 的 ESLint - ESLint with Babel plugin-proposal-export-default-from 错误:找不到模块“@babel/plugin-proposal-class-properties” - Error: Cannot find module '@babel/plugin-proposal-class-properties' 带有 Babel 的 ESLint:在 .eslintrc 中未找到声明的 plugin-proposal-class-properties - ESLint with Babel: plugin-proposal-class-properties not found declared in .eslintrc 如何在Codesandbox中使用Babel插件提案抛出表达式? - How to use Babel Plugin Proposal Throw Expressions in Codesandbox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM