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(relay): support unresolved mark #179

Merged
merged 14 commits into from
Apr 27, 2023
10 changes: 5 additions & 5 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion packages/emotion/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-emotion",
"version": "2.5.60",
"version": "2.5.61",
"description": "SWC plugin for emotion css-in-js library",
"main": "swc_plugin_emotion.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/emotion/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
license = "Apache-2.0"
name = "swc_emotion"
repository = "https://github.com/swc-project/plugins.git"
version = "0.30.6"
version = "0.30.7"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
2 changes: 1 addition & 1 deletion packages/jest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-jest",
"version": "1.5.60",
"version": "1.5.61",
"description": "SWC plugin for jest",
"main": "swc_plugin_jest.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/loadable-components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-loadable-components",
"version": "0.3.60",
"version": "0.3.61",
"description": "SWC plugin for `@loadable/components`",
"main": "swc_plugin_loadable_components.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/noop/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-noop",
"version": "1.5.58",
"version": "1.5.59",
"description": "Noop SWC plugin, for debugging",
"main": "swc_plugin_noop.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-relay",
"version": "1.5.60",
"version": "1.5.61",
"description": "SWC plugin for relay",
"main": "swc_plugin_relay.wasm",
"types": "./types.d.ts",
Expand Down
8 changes: 7 additions & 1 deletion packages/relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ fn relay_plugin_transform(program: Program, metadata: TransformPluginProgramMeta
eager_es_modules,
};

let mut relay = relay(&config, filename, root_dir, None);
let mut relay = relay(
&config,
filename,
root_dir,
None,
Some(metadata.unresolved_mark),
);

program.fold_with(&mut relay)
}
2 changes: 1 addition & 1 deletion packages/relay/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
license = "Apache-2.0"
name = "swc_relay"
repository = "https://github.com/swc-project/plugins.git"
version = "0.2.6"
version = "0.2.7"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
23 changes: 18 additions & 5 deletions packages/relay/transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use once_cell::sync::Lazy;
use regex::Regex;
use serde::Deserialize;
use swc_core::{
common::FileName,
common::{FileName, Mark, DUMMY_SP},
ecma::{
ast::*,
atoms::JsWord,
Expand Down Expand Up @@ -74,6 +74,7 @@ struct Relay<'a> {
file_name: FileName,
config: &'a Config,
imports: Vec<RelayImport>,
unresolved_mark: Option<Mark>,
}

#[derive(Deserialize, Debug, Default, Clone)]
Expand All @@ -97,10 +98,14 @@ fn pull_first_operation_name_from_tpl(tpl: &TaggedTpl) -> Option<String> {
})
}

fn build_require_expr_from_path(path: &str) -> Expr {
fn build_require_expr_from_path(path: &str, mark: Option<Mark>) -> Expr {
Expr::Call(CallExpr {
span: Default::default(),
callee: quote_ident!("require").as_callee(),
callee: quote_ident!(
mark.map(|m| DUMMY_SP.apply_mark(m)).unwrap_or(DUMMY_SP),
"require"
)
.as_callee(),
args: vec![Lit::Str(Str {
span: Default::default(),
value: JsWord::from(path),
Expand Down Expand Up @@ -226,13 +231,19 @@ impl<'a> Relay<'a> {
item: ident_name.clone(),
});
let operation_ident = Ident {
span: Default::default(),
span: self
.unresolved_mark
.map(|m| DUMMY_SP.apply_mark(m))
.unwrap_or(Default::default()),
sym: ident_name,
optional: false,
};
Some(Expr::Ident(operation_ident))
} else {
Some(build_require_expr_from_path(&final_path))
Some(build_require_expr_from_path(
&final_path,
self.unresolved_mark,
))
}
}
Err(_err) => {
Expand Down Expand Up @@ -262,12 +273,14 @@ pub fn relay(
file_name: FileName,
root_dir: PathBuf,
pages_dir: Option<PathBuf>,
unresolved_mark: Option<Mark>,
) -> impl Fold + '_ {
Relay {
root_dir,
file_name,
config,
pages_dir,
imports: vec![],
unresolved_mark,
}
}
2 changes: 2 additions & 0 deletions packages/relay/transform/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fn fixture(input: PathBuf) {
FileName::Real("file.js".parse().unwrap()),
Default::default(),
None,
None,
)
},
&input,
Expand All @@ -44,6 +45,7 @@ fn fixture_es_modules(input: PathBuf) {
FileName::Real("file.js".parse().unwrap()),
Default::default(),
None,
None,
)
},
&input,
Expand Down
2 changes: 1 addition & 1 deletion packages/styled-components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-styled-components",
"version": "1.5.60",
"version": "1.5.61",
"description": "SWC plugin for styled-components",
"main": "swc_plugin_styled_components.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/styled-components/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ include = ["Cargo.toml", "src/**/*.rs"]
license = "Apache-2.0"
name = "styled_components"
repository = "https://github.com/swc-project/plugins.git"
version = "0.54.6"
version = "0.54.7"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
2 changes: 1 addition & 1 deletion packages/styled-jsx/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-styled-jsx",
"version": "1.5.60",
"version": "1.5.61",
"description": "SWC plugin for styled-jsx",
"main": "swc_plugin_styled_jsx.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/styled-jsx/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = "AST transforms visitor for styled-jsx"
edition = "2021"
license = "Apache-2.0"
name = "styled_jsx"
version = "0.31.6"
version = "0.31.7"

[features]
custom_transform = ["swc_core/common_concurrent"]
Expand Down
2 changes: 1 addition & 1 deletion packages/transform-imports/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-transform-imports",
"version": "1.5.60",
"version": "1.5.61",
"description": "SWC plugin for https://www.npmjs.com/package/babel-plugin-transform-imports",
"main": "swc_plugin_transform_imports.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/transform-imports/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
license = "Apache-2.0"
name = "modularize_imports"
repository = "https://github.com/swc-project/plugins.git"
version = "0.27.6"
version = "0.27.7"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down