Skip to content

Commit

Permalink
fix(sc): css props
Browse files Browse the repository at this point in the history
  • Loading branch information
fz6m committed Nov 23, 2023
1 parent 3f6e6c5 commit 9200f21
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 11 deletions.
9 changes: 9 additions & 0 deletions examples/with-styled-components/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { CSSProp } from "styled-components";

interface MyTheme {}

declare module "react" {
interface Attributes {
css?: CSSProp<MyTheme>
}
}
1 change: 1 addition & 0 deletions examples/with-styled-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"dependencies": {
"@umijs/plugins": "workspace:*",
"styled-components": "6.1.1",
"umi": "workspace:*"
}
}
21 changes: 21 additions & 0 deletions examples/with-styled-components/pages/css.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { css, styled } from 'umi';

const Style = styled.div`
${css`
font-size: 18px;
`};
`;

export default function Page() {
return (
<Style
data-color="green"
css={`
background: papayawhip;
color: ${(props: any) => props['data-color']};
`}
>
css style
</Style>
);
}
3 changes: 3 additions & 0 deletions examples/with-styled-components/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./.umi/tsconfig.json"
}
2 changes: 1 addition & 1 deletion packages/plugins/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"react-intl": "3.12.1",
"react-redux": "^8.0.5",
"redux": "^4.2.1",
"styled-components": "6.1.0",
"styled-components": "6.1.1",
"tslib": "^2",
"warning": "^4.0.3"
},
Expand Down
23 changes: 20 additions & 3 deletions packages/plugins/src/styled-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { lodash, winPath } from '@umijs/utils';
import { dirname } from 'path';
import type { IStyleSheetManager } from 'styled-components';
import { IApi } from 'umi';
import { resolveProjectDep } from './utils/resolveProjectDep';
import { withTmpPath } from './utils/withTmpPath';

export default (api: IApi) => {
Expand All @@ -20,8 +21,9 @@ export default (api: IApi) => {
// dev: displayName
// prod: minify
api.modifyConfig((memo) => {
// only apply babel plugin in webpack and vite
if (!['webpack', 'vite'].includes(api.appData.bundler!)) return memo;
if (api.userConfig.mako || process.env.OKAM) {
return memo;
}

const isProd = api.env === 'production';
const pluginConfig = {
Expand Down Expand Up @@ -56,7 +58,22 @@ export default (api: IApi) => {
return ['styledComponents'];
});

const libPath = dirname(require.resolve('styled-components/package'));
const SC_NAME = `styled-components`;
let libPath: string;
try {
libPath =
resolveProjectDep({
pkg: api.pkg,
cwd: api.cwd,
dep: SC_NAME,
}) || dirname(require.resolve(`${SC_NAME}/package.json`));
} catch (e) {}

api.modifyConfig((memo) => {
memo.alias[SC_NAME] = libPath;
return memo;
});

api.onGenerateFiles(() => {
api.writeTmpFile({
path: 'index.tsx',
Expand Down
17 changes: 10 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9200f21

Please sign in to comment.