Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bundler-okam): enable emotion when configured @emotion babel plugin #908

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions e2e/fixtures.umi/emotion/.umirc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
mfsu: false,
extraBabelPlugins: [
'@emotion',
],
};
7 changes: 7 additions & 0 deletions e2e/fixtures.umi/emotion/expect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const assert = require("assert");
const { parseBuildResult, moduleReg } = require("../../../scripts/test-utils");
const { files } = parseBuildResult(__dirname);

let content = files["umi.js"];
content = content.replace(/\s/g, "");

6 changes: 6 additions & 0 deletions e2e/fixtures.umi/emotion/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"devDependencies": {
"@emotion/react": "^11.11.3",
"@emotion/styled": "^11.11.0"
}
}
4 changes: 4 additions & 0 deletions e2e/fixtures.umi/emotion/pages/index.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

.title {
background: rgb(222, 208, 9);
}
25 changes: 25 additions & 0 deletions e2e/fixtures.umi/emotion/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';
import styles from './index.less';
import { css } from "@emotion/react";
import styled from "@emotion/styled";

const style = css`
color: rgb(55, 255, 0);
`;

const Foo = styled.div({
background: "rgb(150, 255, 3)",
color: '#fff',
});

1;

export default function Page() {
return (
<div css={style}>
<Foo>Hello</Foo>
<h1 className={styles.title}>hello</h1>
<span>abcd</span>
</div>
);
}
13 changes: 10 additions & 3 deletions packages/bundler-okam/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,9 @@ function checkConfig(opts) {
['beforeBabelPlugins', 'extraBabelPlugins', 'config.extraBabelPlugins']
.reduce((acc, key) => acc.concat(lodash.get(opts, key) || []), [])
.some((p) => {
if (!/^import$|babel-plugin-import/.test(p[0])) {
const isImportPlugin = /^import$|babel-plugin-import/.test(p[0]);
const isEmotionPlugin = p === '@emotion' || p === '@emotion/babel-plugin';
if (!isImportPlugin && !isEmotionPlugin) {
warningKeys.push('extraBabelPlugins');
return true;
}
Expand Down Expand Up @@ -497,10 +499,11 @@ async function getOkamConfig(opts) {
minify = false;
}
// transform babel-plugin-import plugins to transformImport
const transformImport = [
const extraBabelPlugins = [
...(opts.extraBabelPlugins || []),
...(opts.config.extraBabelPlugins || []),
]
];
const transformImport = extraBabelPlugins
.filter((p) => /^import$|babel-plugin-import/.test(p[0]))
.map(([_, v]) => {
const { libraryName, libraryDirectory, style, ...others } = v;
Expand All @@ -521,6 +524,9 @@ async function getOkamConfig(opts) {

return { libraryName, libraryDirectory, style };
});
const emotion = extraBabelPlugins.some((p) => {
return p === '@emotion' || p === '@emotion/babel-plugin';
});
// transform externals
const externalsConfig = Object.entries(externals).reduce((ret, [k, v]) => {
// handle [string] with script type
Expand Down Expand Up @@ -583,6 +589,7 @@ async function getOkamConfig(opts) {
clean,
flexBugs: true,
react: opts.react || {},
emotion,
...(opts.disableCopy ? { copy: [] } : { copy: ['public'].concat(copy) }),
};

Expand Down
Loading