Skip to content

Commit

Permalink
refactor(relay): Improve config parsing in Wasm mode (#299)
Browse files Browse the repository at this point in the history
We now use `#[derive(Deserialize)]` instead of `serde_json::Value`.
  • Loading branch information
kdy1 authored May 10, 2024
1 parent 5b0fd44 commit 68c13ad
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
6 changes: 6 additions & 0 deletions packages/relay/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @swc/plugin-relay

## 2.0.4

### Patch Changes

- 1379d24: Make config parsing typed.

## 2.0.3

### Patch Changes
Expand Down
6 changes: 6 additions & 0 deletions packages/relay/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ In this example typescript graphql files will output transpiled import path of `

# @swc/plugin-relay

## 2.0.4

### Patch Changes

- 1379d24: Make config parsing typed.

## 2.0.3

### Patch Changes
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": "2.0.3",
"version": "2.0.4",
"description": "SWC plugin for relay",
"main": "swc_plugin_relay.wasm",
"types": "./types.d.ts",
Expand Down
48 changes: 25 additions & 23 deletions packages/relay/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
use std::path::PathBuf;

use serde_json::Value;
use serde::Deserialize;
use swc_common::{plugin::metadata::TransformPluginMetadataContextKind, FileName};
use swc_core::{
ecma::{ast::Program, visit::FoldWith},
plugin::{plugin_transform, proxies::TransformPluginProgramMetadata},
};
use swc_relay::{relay, Config, OutputFileExtension, RelayLanguageConfig};

#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct WasmConfig {
root_dir: PathBuf,

#[serde(default)]
artifact_directory: Option<PathBuf>,

#[serde(default)]
language: RelayLanguageConfig,

#[serde(default)]
output_file_extension: OutputFileExtension,

#[serde(default)]
eager_es_modules: bool,
}

#[plugin_transform]
fn relay_plugin_transform(program: Program, metadata: TransformPluginProgramMetadata) -> Program {
let filename = if let Some(filename) =
Expand All @@ -18,7 +36,7 @@ fn relay_plugin_transform(program: Program, metadata: TransformPluginProgramMeta
FileName::Anon
};

let plugin_config: Value = serde_json::from_str(
let plugin_config: WasmConfig = serde_json::from_str(
&metadata
.get_transform_plugin_config()
.expect("failed to get plugin config for relay"),
Expand All @@ -29,29 +47,13 @@ fn relay_plugin_transform(program: Program, metadata: TransformPluginProgramMeta
// as well as `/cwd` alias. current_dir cannot resolve to actual path,
// `/cwd` alias won't expand to `real` path but only gives access to the cwd as
// mounted path, which we can't use in this case.
let root_dir = PathBuf::from(
plugin_config["rootDir"]
.as_str()
.expect("rootDir is expected"),
);
let artifact_directory = plugin_config["artifactDirectory"]
.as_str()
.map(PathBuf::from);
let language = plugin_config["language"]
.as_str()
.map_or(RelayLanguageConfig::TypeScript, |v| v.try_into().unwrap());
let output_file_extension = plugin_config["outputFileExtension"]
.as_str()
.map_or(OutputFileExtension::Undefined, |v| v.try_into().unwrap());
let eager_es_modules = plugin_config["eagerEsModules"]
.as_bool()
.unwrap_or_default();
let root_dir = plugin_config.root_dir;

let config = Config {
artifact_directory,
language,
eager_es_modules,
output_file_extension,
artifact_directory: plugin_config.artifact_directory,
language: plugin_config.language,
eager_es_modules: plugin_config.eager_es_modules,
output_file_extension: plugin_config.output_file_extension,
};

let mut relay = relay(
Expand Down

0 comments on commit 68c13ad

Please sign in to comment.