在npm包中增加对实验性语法 "classProperties "的支持。

问题描述 投票:0回答:1

我想发布一个包含一些函数的npm包,以便在我的creation-react-app项目中使用。当我从js文件中导入函数到creation-react-app项目中时,它工作正常。但是当我把它安装成一个npm包时,我得到一个错误。Support for the experimental syntax 'classProperties' isn't currently enabled.

我试着将以下代码添加到一个 package.json 文件的npm包。

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

但这并没有解决这个问题,我得到了同样的错误。

我还需要在我的npm包中添加什么作为依赖关系,才能在我的create-react-app项目中使用它?还是重写函数而不使用这种实验性语法更好?

javascript node.js npm ecmascript-6 babel
1个回答
0
投票

基于这个 所以回答 我做了以下工作。

.npmignore

/src

.gitignore

/lib
/node_modules

安装Babel

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

安装@babelplugin-proposal-class-properties。

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

软件包.json

"main": "lib/index.js",
"scripts": {
  "prepublish": "babel src -d lib"
},
"babel": {
  "presets": [
    "@babel/preset-env"
  ],
  "plugins": [
    "@babel/plugin-proposal-class-properties"
  ]
}
© www.soinside.com 2019 - 2024. All rights reserved.