简体   繁体   English

如何发布 react+typescript 组件

[英]How to publish react+typescript component

I'm trying to publish a React component written with Typescript and I have some doubts about what is the best way我正在尝试发布一个用 Typescript 编写的 React 组件,但我对什么是最好的方法有一些疑问

my workspace:我的工作区:

./src
   index.tsx
   styles.module.css
   logo.svg
   SubcomponentFolder
       index.tsx
       styles.module.css

-To publish it to a package manager (npm) - 将其发布到包管理器 (npm)
I create a tsconfig file with: "declarations":true, "module": "commonjs", "target":"es5", "jsx": "react"我创建了一个 tsconfig 文件: "declarations":true, "module": "commonjs", "target":"es5", "jsx": "react"
Then run tsc && npx copyfiles -F -u 2 \\"src/lib/**/*.svg\\" \\"src/lib/**/*.css\\" dist this generates a folder with the same structure but with the .js files and d.ts files然后运行tsc && npx copyfiles -F -u 2 \\"src/lib/**/*.svg\\" \\"src/lib/**/*.css\\" dist这会生成一个具有相同结构但带有.js 文件和 d.ts 文件
my output:我的输出:

./dist
   index.js
   index.d.ts
   styles.module.css
   logo.svg
   SubcomponentFolder
       index.js
       index.d.ts
       styles.module.css

Is this the way to it?这是通向它的方式吗? Or there's a best way to approach that?或者有最好的方法来解决这个问题? I readed and saw a lot of posts and answers saying that the way to go is using webpack, but I can't generate the definitions .d.ts files.我阅读并看到了很多帖子和答案,说要走的路是使用 webpack,但我无法生成定义 .d.ts 文件。 And also using webpack, it will bundle react and react-dom and will make the bundle heavier right?并且还使用 webpack,它将捆绑 react 和 react-dom 并使捆绑包更重,对吗? Or am I wrong?还是我错了?

-To use it in html file, like this: https://gist.github.com/gaearon/6668a1f6986742109c00a581ce704605 - 在 html 文件中使用它,像这样: https : //gist.github.com/gaearon/6668a1f6986742109c00a581ce704605
Also I'm wondering if I want to use my component directly in an html file importing react and react-dom using script tag, how can I bundle everything including images and css files?另外我想知道我是否想直接在使用 script 标签导入 react 和 react-dom 的 html 文件中使用我的组件,如何捆绑所有内容,包括图像和 css 文件?

It sound like you're a little far into this project for this to help too much, but the create-react-library npm package does all the setup for you (including typescript) to publish react components to npm.听起来您对这个项目的了解有点远,因此无法提供太多帮助,但是 create-react-library npm 包会为您完成所有设置(包括打字稿)以将 react 组件发布到 npm。 If you only have one component it shouldn't take too long to transfer over.如果您只有一个组件,则转移的时间不会太长。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM