Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm lint` - [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
- Loading branch information
Showing
51 changed files
with
576 additions
and
393 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use swc_atoms::JsWord; | ||
use swc_common::collections::AHashMap; | ||
|
||
use crate::{EmotionModuleConfig, ExportItem}; | ||
|
||
/// key: `importSource` | ||
pub type ImportMap = AHashMap<JsWord, ImportMapValue>; | ||
|
||
/// key: `localExportName` | ||
pub type ImportMapValue = AHashMap<JsWord, Config>; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Config { | ||
canonical_import: ImportItem, | ||
} | ||
|
||
/// `(packageName, exportName)` | ||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub(crate) struct ImportItem(JsWord, JsWord); | ||
|
||
pub(crate) fn expand_import_map( | ||
map: Option<&ImportMap>, | ||
mut imports: Vec<EmotionModuleConfig>, | ||
) -> Vec<EmotionModuleConfig> { | ||
if let Some(map) = map { | ||
map.iter().for_each(|(import_source, value)| { | ||
value | ||
.iter() | ||
.for_each(|(local_export_name, Config { canonical_import })| { | ||
let ImportItem(package_name, export_name) = canonical_import; | ||
|
||
if &*package_name == "@emotion/react" && &*export_name == "jsx" { | ||
return; | ||
} | ||
|
||
let package_transformers = imports | ||
.iter() | ||
.find(|v| v.module_name == *package_name) | ||
.unwrap_or_else(|| { | ||
panic!( | ||
"There is no transformer for the export '{}' in '{}'", | ||
export_name, package_name | ||
) | ||
}) | ||
.clone(); | ||
|
||
let kind = package_transformers | ||
.exported_names | ||
.iter() | ||
.find(|v| v.name == **export_name) | ||
.map(|v| v.kind) | ||
.or_else(|| { | ||
if export_name == "default" { | ||
package_transformers.default_export | ||
} else { | ||
None | ||
} | ||
}) | ||
.unwrap_or_else(|| { | ||
panic!( | ||
"failed to find export '{}' from package '{}'", | ||
export_name, package_name | ||
) | ||
}); | ||
|
||
imports.push(EmotionModuleConfig { | ||
module_name: import_source.clone(), | ||
exported_names: vec![ExportItem { | ||
name: local_export_name.to_string(), | ||
kind, | ||
}], | ||
default_export: package_transformers.default_export, | ||
}); | ||
}) | ||
}); | ||
} | ||
|
||
imports | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
packages/next-swc/crates/emotion/tests/fixture/import-map/global-needs-css/input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import * as React from 'react' | ||
import { SomeGlobalFromCore } from 'package-two' | ||
|
||
const getBgColor = () => ({ backgroundColor: '#fff' }) | ||
|
||
export default () => ( | ||
<SomeGlobalFromCore styles={{ color: 'hotpink', ...getBgColor() }} /> | ||
) |
15 changes: 15 additions & 0 deletions
15
packages/next-swc/crates/emotion/tests/fixture/import-map/global-needs-css/output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { jsx as _jsx } from "react/jsx-runtime"; | ||
import * as React from 'react'; | ||
import { SomeGlobalFromCore } from 'package-two'; | ||
const getBgColor = ()=>({ | ||
backgroundColor: '#fff' | ||
}); | ||
export default (()=>/*#__PURE__*/ _jsx(SomeGlobalFromCore, { | ||
styles: [ | ||
{ | ||
color: 'hotpink', | ||
...getBgColor() | ||
}, | ||
"/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5wdXQudHMiLCJzb3VyY2VzIjpbImlucHV0LnRzIl0sInNvdXJjZXNDb250ZW50IjpbImltcG9ydCAqIGFzIFJlYWN0IGZyb20gJ3JlYWN0J1xuaW1wb3J0IHsgU29tZUdsb2JhbEZyb21Db3JlIH0gZnJvbSAncGFja2FnZS10d28nXG5cbmNvbnN0IGdldEJnQ29sb3IgPSAoKSA9PiAoeyBiYWNrZ3JvdW5kQ29sb3I6ICcjZmZmJyB9KVxuXG5leHBvcnQgZGVmYXVsdCAoKSA9PiAoXG4gIDxTb21lR2xvYmFsRnJvbUNvcmUgc3R5bGVzPXt7IGNvbG9yOiAnaG90cGluaycsIC4uLmdldEJnQ29sb3IoKSB9fSAvPlxuKVxuIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQU1HIn0= */" | ||
] | ||
})); |
4 changes: 4 additions & 0 deletions
4
packages/next-swc/crates/emotion/tests/fixture/import-map/global/input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import * as React from 'react' | ||
import { SomeGlobalFromCore } from 'package-two' | ||
|
||
export default () => <SomeGlobalFromCore styles={{ color: 'hotpink' }} /> |
11 changes: 11 additions & 0 deletions
11
packages/next-swc/crates/emotion/tests/fixture/import-map/global/output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { jsx as _jsx } from "react/jsx-runtime"; | ||
import * as React from 'react'; | ||
import { SomeGlobalFromCore } from 'package-two'; | ||
export default (()=>/*#__PURE__*/ _jsx(SomeGlobalFromCore, { | ||
styles: [ | ||
{ | ||
color: 'hotpink' | ||
}, | ||
"/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5wdXQudHMiLCJzb3VyY2VzIjpbImlucHV0LnRzIl0sInNvdXJjZXNDb250ZW50IjpbImltcG9ydCAqIGFzIFJlYWN0IGZyb20gJ3JlYWN0J1xuaW1wb3J0IHsgU29tZUdsb2JhbEZyb21Db3JlIH0gZnJvbSAncGFja2FnZS10d28nXG5cbmV4cG9ydCBkZWZhdWx0ICgpID0+IDxTb21lR2xvYmFsRnJvbUNvcmUgc3R5bGVzPXt7IGNvbG9yOiAnaG90cGluaycgfX0gLz5cbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFHc0IifQ== */" | ||
] | ||
})); |
3 changes: 3 additions & 0 deletions
3
...ges/next-swc/crates/emotion/tests/fixture/import-map/non-default-styled-aliased/input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { nonDefaultStyled as someAlias } from 'package-one' | ||
|
||
let SomeComp = someAlias.div({ color: 'hotpink' }) |
7 changes: 7 additions & 0 deletions
7
...ges/next-swc/crates/emotion/tests/fixture/import-map/non-default-styled-aliased/output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { nonDefaultStyled as someAlias } from 'package-one'; | ||
let SomeComp = /*#__PURE__*/ someAlias("div", { | ||
target: "ekie5mj0", | ||
label: "SomeComp" | ||
})({ | ||
color: 'hotpink' | ||
}, "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5wdXQudHMiLCJzb3VyY2VzIjpbImlucHV0LnRzIl0sInNvdXJjZXNDb250ZW50IjpbImltcG9ydCB7IG5vbkRlZmF1bHRTdHlsZWQgYXMgc29tZUFsaWFzIH0gZnJvbSAncGFja2FnZS1vbmUnXG5cbmxldCBTb21lQ29tcCA9IHNvbWVBbGlhcy5kaXYoeyBjb2xvcjogJ2hvdHBpbmsnIH0pXG4iXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBRWUifQ== */"); |
3 changes: 3 additions & 0 deletions
3
packages/next-swc/crates/emotion/tests/fixture/import-map/non-default-styled/input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { nonDefaultStyled } from 'package-one' | ||
|
||
let SomeComp = nonDefaultStyled.div({ color: 'hotpink' }) |
7 changes: 7 additions & 0 deletions
7
packages/next-swc/crates/emotion/tests/fixture/import-map/non-default-styled/output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { nonDefaultStyled } from 'package-one'; | ||
let SomeComp = /*#__PURE__*/ nonDefaultStyled("div", { | ||
target: "ekie5mj0", | ||
label: "SomeComp" | ||
})({ | ||
color: 'hotpink' | ||
}, "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5wdXQudHMiLCJzb3VyY2VzIjpbImlucHV0LnRzIl0sInNvdXJjZXNDb250ZW50IjpbImltcG9ydCB7IG5vbkRlZmF1bHRTdHlsZWQgfSBmcm9tICdwYWNrYWdlLW9uZSdcblxubGV0IFNvbWVDb21wID0gbm9uRGVmYXVsdFN0eWxlZC5kaXYoeyBjb2xvcjogJ2hvdHBpbmsnIH0pXG4iXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBRWUifQ== */"); |
3 changes: 3 additions & 0 deletions
3
...ges/next-swc/crates/emotion/tests/fixture/import-map/styled-with-base-specified/input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { nonDefaultStyled } from 'package-four' | ||
|
||
let SomeComp = nonDefaultStyled.div({ color: 'hotpink' }) |
7 changes: 7 additions & 0 deletions
7
...ges/next-swc/crates/emotion/tests/fixture/import-map/styled-with-base-specified/output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { nonDefaultStyled } from 'package-four'; | ||
let SomeComp = /*#__PURE__*/ nonDefaultStyled("div", { | ||
target: "ekie5mj0", | ||
label: "SomeComp" | ||
})({ | ||
color: 'hotpink' | ||
}, "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5wdXQudHMiLCJzb3VyY2VzIjpbImlucHV0LnRzIl0sInNvdXJjZXNDb250ZW50IjpbImltcG9ydCB7IG5vbkRlZmF1bHRTdHlsZWQgfSBmcm9tICdwYWNrYWdlLWZvdXInXG5cbmxldCBTb21lQ29tcCA9IG5vbkRlZmF1bHRTdHlsZWQuZGl2KHsgY29sb3I6ICdob3RwaW5rJyB9KVxuIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUVlIn0= */"); |
3 changes: 3 additions & 0 deletions
3
packages/next-swc/crates/emotion/tests/fixture/import-map/vanilla/input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { something } from 'package-three' | ||
|
||
something({ color: 'green' }) |
4 changes: 4 additions & 0 deletions
4
packages/next-swc/crates/emotion/tests/fixture/import-map/vanilla/output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { something } from 'package-three'; | ||
/*#__PURE__*/ something({ | ||
color: 'green' | ||
}, "label:[local]", "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5wdXQudHMiLCJzb3VyY2VzIjpbImlucHV0LnRzIl0sInNvdXJjZXNDb250ZW50IjpbImltcG9ydCB7IHNvbWV0aGluZyB9IGZyb20gJ3BhY2thZ2UtdGhyZWUnXG5cbnNvbWV0aGluZyh7IGNvbG9yOiAnZ3JlZW4nIH0pXG4iXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBRUEifQ== */"); |
Oops, something went wrong.