【问题标题】:publish:npm - How can I set react npm components location on each built发布:npm - 如何在每个构建上设置反应 npm 组件位置
【发布时间】:2021-09-26 13:17:53
【问题描述】:

我是创建 npm 包的新手。

我使用以下博客创建了我的第一个 react npm 包 - https://www.codementor.io/@peterodekwo/create-a-simple-react-npm-package-in-simple-steps-using-cra-w966okagi

它已成功发布,并且在反应中正常工作。

那么有什么问题。

当我安装我的 npm 包并实现它时。我必须使用下面的代码来导入。

import Card from 'owaiz-test/dist/Card';
import Button from 'owaiz-test/dist/Button';

https://prnt.sc/1cpkvsy

dist文件夹的问题。我不想用dist文件夹来写。

我想要类似的东西

import Card from 'owaiz-test/Card';
import Button from 'owaiz-test/Button';

我知道它正在从下面提到的 package.json 文件中编译代码。

"publish:npm": "rm -rf dist && mkdir dist &&  babel ./src/components -d dist --copy-files"

但是,如何更改上述代码或任何其他解决方案?

更新:

添加Package.json

{
  "name": "owaiz-test",
  "version": "0.1.1",
  "private": false,
  "babel": {
    "presets": [
      "@babel/preset-react"
    ]
  },
  "dependencies": {
    "@testing-library/jest-dom": "^5.14.1",
    "@testing-library/react": "^11.2.7",
    "@testing-library/user-event": "^12.8.3",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "4.0.3",
    "web-vitals": "^1.1.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "publish:npm": "rm -rf dist && mkdir dist &&  babel ./src/components -d dist --copy-files"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@babel/cli": "^7.5.5",
    "@babel/preset-react": "^7.0.0"
  }
}

更新 2

{
  "name": "owaiz-test",
  "version": "0.1.1",
  "private": false,
  "main": "dist/index.cjs.js",
  "browser": "dist/index.cjs.js",
  "module": "dist/index.esm.js",
  "types": "dist/index.d.ts",
  "babel": {
    "presets": [
      "@babel/preset-react"
    ]
  },
  "peerDependencies": {
    "@testing-library/jest-dom": "^5.14.1",
    "@testing-library/react": "^11.2.7",
    "@testing-library/user-event": "^12.8.3",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "4.0.3",
    "web-vitals": "^1.1.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "publish:npm": "rm -rf dist && mkdir dist &&  babel ./src/index.js -d dist --copy-files"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@babel/cli": "^7.5.5",
    "@babel/preset-react": "^7.0.0"
  }
}

【问题讨论】:

    标签: node.js reactjs npm


    【解决方案1】:

    您需要编辑 package.json

    创建 index.js 文件作为包的主要条目

    import Card from './components/Card'
    import Button from './components/Button'
    
    export { Card, Button }
    

    在打包时使用 index.js:

    "publish:npm": "...babel ./src/index.js ..."
    

    设置主字段和浏览器字段(esm 包的模块可以跳过,TypeScript 定义的类型):

      "main": "dist/index.cjs.js",
      "browser": "dist/index.cjs.js",
      "module": "dist/index.esm.js",
      "types": "dist/index.d.ts",
    

    Docs

    还需要将reactreact-dom添加到peerDependencies而不是dependencies,这样就不会与您的代码捆绑在一起What is the correct way of adding a dependency to react in your package.json for a react component

    UPD 来自问题的文章完全是废话,那些更好: https://blog.logrocket.com/the-complete-guide-to-publishing-a-react-package-to-npm/

    https://dev.to/jimjunior/how-to-create-an-npm-library-from-react-components-2m2

    但仍有一些注意事项。

    【讨论】:

    • 感谢您的回复。你能解释一下吗
    • 只是指向包主入口点的字段,所以指向 index.js 文件并从那里导出所有内容。
    • 我需要创建cjs文件吗?
    • 使用 babel 或一些捆绑器,可以是 index.module.js 之类的任何扩展名,也可以只是 index.js
    • 在里面添加我当前的 package.json 文件
    猜你喜欢
    • 2018-05-25
    • 2018-01-09
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    相关资源
    最近更新 更多