Possible problem on the rollupOptions external and global configs #16044
-
I have a library published in a private repository (the name of the libraby is My component: import { ReactNode } from "react";
import styled from "styled-components";
export type Variants = "h1";
export interface TypographyProps {
variant: Variants;
children: ReactNode;
className?: string;
}
const H1 = styled.h1`
color: ${({ theme }) => theme.color.neutral.dark};
font-size: 32px;
line-height: 40px;
font-weight: 700;
`;
const variantMapping = {
h1: H1,
};
export const Typography = ({
variant = "h2",
children,
className,
}: TypographyProps) => {
const Component = variantMapping[variant];
return <Component className={className}>{children}</Component>;
}; After I published it, I created a demo project to use it and add a single test of a component of my library: import { Typography } from "@bee-ui/bee-ui";
test("should render children", () => {
render(<Typography variant="h1">H1 content</Typography>);
expect(screen.getByText(/h1 content/i)).toBeInTheDocument();
}); When I run this, I receive this error:
This is the way I export my "custom theme provider" on my bee-ui, to be used by client apps: import { ThemeProvider as DefaultThemeProvider } from "styled-components";
import merge from "lodash.merge";
import { defaultTheme } from "./default.ts";
import { ReactNode } from "react";
import { GlobalStyle } from "./global.ts";
import { ThemeStructure } from "@themes/default/types.ts";
interface Props {
theme?: ThemeStructure;
children: ReactNode;
}
export const ThemeProvider = ({ theme = defaultTheme, children }: Props) => {
const mergedTheme = merge(defaultTheme, theme);
return (
<DefaultThemeProvider theme={mergedTheme}>
<GlobalStyle />
{children}
</DefaultThemeProvider>
);
}; There it is my import { join, resolve } from "path";
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import dts from "vite-plugin-dts";
import { UserConfigExport } from "vitest/config";
import { compilerOptions } from "./tsconfig.json";
const sanitizeAlias = (val: string): string => val.replace("/*", "");
const aliasWithAbsolutePath = Object.entries(compilerOptions.paths).reduce(
(acc: { [key: string]: string }, [key, value]) => {
acc[sanitizeAlias(key)] = sanitizeAlias(join(__dirname, value[0]));
return acc;
},
{} as { [key: string]: string },
);
const config: UserConfigExport = {
test: {
globals: true,
environment: "jsdom",
setupFiles: ["./src/tests/setup.ts"],
},
build: {
lib: {
entry: resolve(__dirname, "src/main.tsx"),
formats: ["es", "cjs"],
name: "BeeUi",
fileName: (format) => `bee-ui.${format}.js`,
},
rollupOptions: {
external: ["react", "react-dom", "styled-components"],
output: {
globals: {
react: "React",
"react-dom": "ReactDOM",
"styled-components": "styled",
},
exports: 'named'
},
},
},
plugins: [react(), dts({ rollupTypes: true, insertTypesEntry: true })],
resolve: {
alias: {
...aliasWithAbsolutePath,
"styled-components": resolve(__dirname, "node_modules/styled-components"),
}
},
};
export default defineConfig(config); And my "name": "@bee-ui/bee-ui",
"version": "1.2.5-beta-2",
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/bee-ui.es.js",
"require": "./dist/bee-ui.cjs.js",
"default": "./dist/bee-ui.es.js"
}
},
"module": "dist/bee-ui.es.js",
"main": "dist/bee-ui.cjs.js",
"types": "dist/index.d.ts",
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"styled-components": "^6.1.8"
},
"peerDependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"styled-components": "^6.1.8"
},
... After investigating for a long time, I discovered that if I remove the But another problem happens, the symptom is that I can no longer correctly propagate the An important piece of information is that, when I remove styled-components from the rollupOptions mentioned before, the only way that my component works with his theme is to use it this way: import { Typography, BeeUiThemeProvider } from "@bee-ui/bee-ui";
import { ThemeProvider } from "styled-components";
// This works:
<ThemeProvider theme={theme}>
<BeeUiThemeProvider theme={theme}>
<Typography variant="h1">Examples with default theme</Typography>
</BeeUiThemeProvider>
</ThemeProvider>
// It doesn't work (which seems really bad to me)
import { Typography, BeeUiThemeProvider } from "@bee-ui/bee-ui";
<BeeUiThemeProvider theme={theme}>
<Typography variant="h1">Examples with default theme</Typography>
</BeeUiThemeProvider> |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
If you cross post an issue, please put links between two. Otherwise multiple teams would end up debugging the same issue without knowing what's investigated so far. Original discussion is here vitest-dev/vitest#5286 (reply in thread) |
Beta Was this translation helpful? Give feedback.
If you cross post an issue, please put links between two. Otherwise multiple teams would end up debugging the same issue without knowing what's investigated so far.
Original discussion is here vitest-dev/vitest#5286 (reply in thread)