[英]Allow auto import my React library on vscode
I'm trying to do a library of components for React and publish on npm using webpack and babel to compile to Es5.我正在尝试为 React 创建一个组件库,并使用 webpack 和 babel 在 npm 上发布以编译为 Es5。
Almost everything worked, but for some reason, the project that consumes this lib cant auto import their components几乎一切正常,但由于某种原因,使用此库的项目无法自动导入其组件
I have a project on github with the setup I used:我在 github 上有一个项目,使用了我使用的设置:
https://github.com/dattebayorob/react-loading https://github.com/dattebayorob/react-loading
//webpack.config.js
https://github.com/dattebayorob/react-loading/blob/master/webpack.config.js
//.babelrc
https://github.com/dattebayorob/react-loading/blob/master/.babelrc
//package.json
https://github.com/dattebayorob/react-loading/blob/master/package.json
I'm expecting to import components from my lib with 'CTRL+space' when typing then我希望在输入时使用“CTRL+空格”从我的库中导入组件
Now, I can import from my lib manualy with import { Component } from 'my-react-lib'
现在,我可以使用
import { Component } from 'my-react-lib'
从我的库手动import { Component } from 'my-react-lib'
在dattebayorob/react-loading/index.d.ts
尝试:
export * from './src/components'
In package.json
, you have "main": "./index.d.ts"
, but that's not a valid JS file, as it does not contain actual code, only type definitions.在
package.json
,您有"main": "./index.d.ts"
,但这不是有效的 JS 文件,因为它不包含实际代码,仅包含类型定义。
In a library, usually you need to have an src/index.js
file that imports / exports all components and in package.json
you add the build artifact as main: "main": "dist/index.js"
.在库中,通常您需要有一个
src/index.js
文件来导入/导出所有组件,并在package.json
中将构建工件添加为 main: "main": "dist/index.js"
。
Also, don't forget to explicitly specify the files: ["dist"]
attribute in package.json
so the src folder is not downloaded when your package is installed.另外,不要忘记在
package.json
明确指定files: ["dist"]
属性,以便在安装包时不会下载 src 文件夹。
Sometimes, when using Typescript in VSCode, you have to run the Typescript: Restart TS Server
command in your command palette for auto import to work after creating new files.有时,在 VSCode 中使用 Typescript 时,您必须在命令面板中运行
Typescript: Restart TS Server
命令,以便在创建新文件后自动导入工作。 It's a bug.这是一个错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.