试图通过扩大“未来倡议”的某些组成部分而形成一个组成部分。
I wanted to start simple by doing the classic button. But unfortuantly I got stuck... when I build the npm package I get this:
stories/components/Button/Button.tsx(55,7): error TS2742: The inferred type of MyButton cannot be named without a reference to ../../../node_modules/.pnpm/registry.npmjs.org+@[email protected]_@[email protected][email protected][email protected]/node_modules/@nextui-org/system-rsc/dist/types . This is likely not portable. A type annotation is necessary.
stories/components/Button/Button.tsx(55,7): error TS2742: The inferred type of MyButton cannot be named without a reference to ../../../node_modules/.pnpm/registry.npmjs.org+@[email protected][email protected]/node_modules/@nextui-org/react-utils/dist/refs . This is likely not portable. A type annotation is necessary.
stories/components/Button/Button.tsx(55,7): error TS2742: The inferred type of MyButton cannot be named without a reference to .pnpm/registry.npmjs.org+@[email protected][email protected]/node_modules/@react-types/shared . This is likely not portable. A type annotation is necessary.
stories/components/Button/Button.tsx(55,7): error TS2742: The inferred type of MyButton cannot be named without a reference to .pnpm/registry.npmjs.org+@[email protected][email protected]/node_modules/@react-types/shared . This is likely not portable. A type annotation is necessary.
stories/components/Button/Button.tsx(55,7): error TS2742: The inferred type of MyButton cannot be named without a reference to .pnpm/registry.npmjs.org+@[email protected][email protected]/node_modules/@react-types/shared . This is likely not portable. A type annotation is necessary.
stories/components/Button/Button.tsx(55,7): error TS2742: The inferred type of MyButton cannot be named without a reference to .pnpm/registry.npmjs.org+@[email protected][email protected]/node_modules/@react-types/shared . This is likely not portable. A type annotation is necessary.
stories/components/Button/Button.tsx(55,7): error TS2742: The inferred type of MyButton cannot be named without a reference to .pnpm/registry.npmjs.org+@[email protected][email protected]/node_modules/@react-types/shared . This is likely not portable. A type annotation is necessary.
stories/components/Button/Button.tsx(55,7): error TS2742: The inferred type of MyButton cannot be named without a reference to .pnpm/registry.npmjs.org+@[email protected][email protected]/node_modules/@react-types/shared . This is likely not portable. A type annotation is necessary.
由于这一错误,在使用时,单板就失去其类型安全。
也许我会做一些错误的事情。 首先,我收到了,即,(也给出错误)。
Then I added my own variants to the button to test, the end result is this:
import { extendVariants, Button as NextUiButton } from @nextui-org/react ;
import { ReactNode, Ref } from react ;
import { type VariantProps } from tailwind-variants ;
const MyButton = extendVariants(NextUiButton, {
variants: {
// <- modify/add variants
color: {
primary: bg-blue-500 text-black ,
secondary: bg-purple-500 text-black ,
},
isDisabled: {
true: bg-[#eaeaea] text-[#000] opacity-50 cursor-not-allowed ,
},
size: {
xs: px-unit-2 min-w-unit-12 h-unit-6 text-tiny gap-unit-1 rounded-small ,
md: px-unit-4 min-w-unit-20 h-unit-10 text-small gap-unit-2 rounded-small ,
xl: px-unit-8 min-w-unit-28 h-unit-14 text-large gap-unit-4 rounded-medium ,
},
},
defaultVariants: {
// <- modify/add default variants
color: primary ,
size: xl ,
},
compoundVariants: [
// <- modify/add compound variants
{
isDisabled: true,
color: secondary ,
class: bg-[#84cc16]/80 opacity-100 ,
},
],
});
interface MyButtonProps extends VariantProps<typeof MyButton> {
ref?: Ref<HTMLButtonElement>;
className?: string;
children?: ReactNode;
label: string;
}
export const Button = ({ size, className, color, label, children, ref }: MyButtonProps) => {
return (
<MyButton
{...{
ref,
className,
size,
color,
type: button ,
}}>
{children}
{label}
</MyButton>
);
};
但是,对建筑步骤中的类型进行抽取,造成上述错误。
V:
import {defineConfig} from vite
import react from @vitejs/plugin-react
import dts from vite-plugin-dts
import {libInjectCss} from vite-plugin-lib-inject-css
import {extname,relative,resolve} from path
import {fileURLToPath} from node:url
import {glob} from glob
export default defineConfig({
plugins: [react(),libInjectCss(),dts({include: [ stories/components ]})],
build: {
copyPublicDir: false,
lib: {
entry: resolve(__dirname, stories/main.ts ),
formats: [ es ],
name: "my-components",
},
rollupOptions: {
external: [ react , react/jsx-runtime ,"tailwind-variants","framer-motion",RegExp("^(@nextui-org/).+")],
input: Object.fromEntries(
glob.sync( stories/**/*.{ts,tsx,js,jsx} ,{ignore: stories/**/*.stories.{ts,tsx,js,jsx} }).map(file => [
relative(
stories ,
file.slice(0,file.length-extname(file).length)
),
fileURLToPath(new URL(file,import.meta.url))
])
),
output: {
assetFileNames: assets/[name][extname] ,
entryFileNames: [name].js ,
}
},
},
})
作为副注,如果我使用尾气var制造自己的纽子,所有东西都会奏效,我就在部件上打耳,没有错误。
import { ReactNode, Ref } from react ;
import { tv, type VariantProps } from tailwind-variants ;
const button = tv({
base: font-medium bg-blue-500 text-white rounded-full active:opacity-80 ,
variants: {
color: {
primary: bg-blue-500 text-white ,
secondary: bg-purple-500 text-white ,
},
size: {
sm: text-sm ,
md: text-base ,
lg: px-4 py-3 text-lg ,
},
},
compoundVariants: [
{
size: [ sm , md ],
class: px-3 py-1 ,
},
],
defaultVariants: {
size: md ,
color: primary ,
},
});
interface ButtonProps extends VariantProps<typeof button> {
ref?: Ref<HTMLButtonElement>;
className?: string;
children?: ReactNode;
label: string;
}
export const Button = ({ size, className, color, label, children, ref }: ButtonProps) => {
return (
<button
{...{
ref,
type: button ,
title: label,
className: button({ className, color, size }),
}}>
{children}
{label}
</button>
);
};
EditTwo:
I have created a sandbox to show the issue.
Run pnpm build-sb
to create the component lib and get the error. And pnpm sb
just to start it, which work.