[英]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"
]
}
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 的步骤
npm install --save-dev @babel/core @babel/cli
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-properties将
ES6 class
合成糖转换为 JavaScript __Prototype__
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"] }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.