简体   繁体   English

在 npm package 中添加对实验语法“classProperties”的支持

[英]Add support for experimental syntax 'classProperties' in an npm package

I would like to publish an npm package with some functions to use it with my create-react-app project.我想发布一个 npm package ,它具有一些功能,可以与我的 create-react-app 项目一起使用。 When I import the functions from the js file inside create-react-app project, it works fine.当我从 create-react-app 项目中的 js 文件导入函数时,它工作正常。 But when I install it as an npm package I get an error: Support for the experimental syntax 'classProperties' isn't currently enabled .但是,当我将它安装为 npm package 时,我收到一个错误: Support for the experimental syntax 'classProperties' isn't currently enabled

I tried to add the following code to a package.json file of the npm package:我尝试将以下代码添加到 npm ZEFE90A8E604A7C840D88B0 的package.json文件中:

"devDependencies": {
  "@babel/plugin-proposal-class-properties": "^7.8.3"
},
"babel": {
  "plugins": [
    "@babel/plugin-proposal-class-properties"
  ]
}

But it didn't solve the issue and I get the same error.但它并没有解决问题,我得到了同样的错误。

What else do I need to add as dependencies to my npm package to be able to use it in my create-react-app project?我还需要添加什么作为依赖项到我的 npm package 才能在我的 create-react-app 项目中使用它? Or is it better to rewrite the functions without such experimental syntax?还是在没有这种实验性语法的情况下重写函数更好?

Based on this SO answer I did the following:基于this SO answer我做了以下事情:

.npmignore .npmignore

/src

.gitignore .gitignore

/lib
/node_modules

Install Babel安装通天塔

$ npm install @babel/core @babel/cli @babel/preset-env --save-dev

Install @babel/plugin-proposal-class-properties安装@babel/plugin-proposal-class-properties

$ npm install --save-dev @babel/plugin-proposal-class-properties

package.json package.json

"main": "lib/index.js",
"scripts": {
  "prepublish": "babel src -d lib"
},
"babel": {
  "presets": [
    "@babel/preset-env"
  ],
  "plugins": [
    "@babel/plugin-proposal-class-properties"
  ]
}

You need to add plugin-proposal-class-properties in your build dependency您需要在构建依赖项中添加 plugin-proposal-class-properties

Working example with setup steps: https://github.com/hitsio/Node-JS-ES6设置步骤的工作示例: https://github.com/hitsio/Node-JS-ES6

Here are the steps to Set up Babel for precompiling ES6 into JavaScript以下是设置 Babel 以将 ES6 预编译为 JavaScript 的步骤

  1. Install Babel安装通天塔

npm install --save-dev @babel/core @babel/cli

  1. Install Babel Preset & Class Properties Plugin安装Babel Preset & Class 属性插件

npm install @babel/preset-env --save-dev
npm install --save-dev @babel/plugin-proposal-class-properties

@babel/preset-env Allows you to use the latest JavaScript without needing to micromanage which syntax transforms (and optionally, browser polyfills) are needed by your target environment(s). @babel/preset-env允许您使用最新的 JavaScript,而无需微观管理目标环境需要哪些语法转换(以及可选的浏览器 polyfill)。 This both makes your life easier and JavaScript bundles smaller!这既让您的生活更轻松,而且 JavaScript 捆绑更小!

@babel/plugin-proposal-class-properties transform ES6 class synthecic sugar into JavaScript __Prototype__ @babel/plugin-proposal-class-propertiesES6 class合成糖转换为 JavaScript __Prototype__

  1. Create .babelrc file in your project's root directory & add the following code in .babelrc在项目的根目录中创建.babelrc文件并在.babelrc中添加以下代码

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

If someone is working with react-native-calendars and encounters this error, follow this link.如果有人在使用 react-native-calendars 并遇到此错误,请点击链接。

暂无
暂无

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

相关问题 当前未启用对实验语法“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 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 “松散”:true 未修复当前未启用对实验性语法“classProperties”的支持 - "loose": true is not fixing Support for the experimental syntax 'classProperties' isn't currently enabled React.js 当前未启用对实验性语法“classProperties”的支持错误 - Support for the experimental syntax 'classProperties' isn't currently enabled error on a React.js 使用 Jest、Babel、Webpack 和 React 进行测试。 当前未启用对实验性语法“classProperties”的支持 - Testing with Jest, Babel, Webpack, and React. Support for the experimental syntax 'classProperties' isn't currently enabled 如何让npm包bin运行实验模块选项? - how to let npm package bin run with experimental-modules option? 如何解决当前未启用对实验语法“jsx”的支持 - How to solve Support for the experimental syntax 'jsx' isn't currently enabled 错误:当前未启用对实验性语法“可选链”的支持,但已启用 - Error: Support for the experimental syntax 'optionalChaining' isn't currently enabled, but it is Expo 目前未启用对实验语法“jsx”的支持 - Expo Support for the experimental syntax 'jsx' isn't currently enabled
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM