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): Add outputFileExtension option #222

Merged
merged 27 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
12d6b66
fix: does not specify import path
JulienKode Oct 24, 2023
9bc221e
feat: add outputFileExtension to specify which output extension to use
JulienKode Oct 24, 2023
3d788f2
test: add outputFileExtension to specify which output extension to use
JulienKode Oct 24, 2023
f612da4
docs: add information on the readme
JulienKode Oct 24, 2023
ee0c195
Merge branch 'main' into fix/does-not-specify-import-path
JulienKode Oct 25, 2023
713ce99
Merge branch 'main' into fix/does-not-specify-import-path
JulienKode Oct 25, 2023
b075d50
Bump npm package: ./packages/constify
kdy1 Oct 25, 2023
4074663
Bump npm package: ./packages/emotion
kdy1 Oct 25, 2023
6d38182
Bump npm package: ./packages/jest
kdy1 Oct 25, 2023
646bb0e
Bump npm package: ./packages/loadable-components
kdy1 Oct 25, 2023
923eb69
Bump npm package: ./packages/noop
kdy1 Oct 25, 2023
06d1a3b
Bump npm package: ./packages/react-remove-properties
kdy1 Oct 25, 2023
053e9fc
Bump npm package: ./packages/relay
kdy1 Oct 25, 2023
dba5131
Bump npm package: ./packages/remove-console
kdy1 Oct 25, 2023
8947831
Bump npm package: ./packages/styled-components
kdy1 Oct 25, 2023
d686ca3
Bump npm package: ./packages/styled-jsx
kdy1 Oct 25, 2023
82e49c6
Bump npm package: ./packages/swc-magic
kdy1 Oct 25, 2023
e252b03
Bump npm package: ./packages/transform-imports
kdy1 Oct 25, 2023
b64c13f
Bump cargo crate: modularize_imports
kdy1 Oct 25, 2023
4aeff99
Bump cargo crate: react_remove_properties
kdy1 Oct 25, 2023
250c750
Bump cargo crate: remove_console
kdy1 Oct 25, 2023
07b189a
Bump cargo crate: styled_components
kdy1 Oct 25, 2023
d9a4e3b
Bump cargo crate: styled_jsx
kdy1 Oct 25, 2023
7ebe699
Bump cargo crate: swc_constify
kdy1 Oct 25, 2023
c298fe5
Bump cargo crate: swc_emotion
kdy1 Oct 25, 2023
1d60378
Bump cargo crate: swc_magic
kdy1 Oct 25, 2023
6930097
Bump cargo crate: swc_relay
kdy1 Oct 25, 2023
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
21 changes: 21 additions & 0 deletions packages/relay/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,24 @@ const swcConfig = require("./.swcrc.js")
```

> Note: We're using a `.swcrc.js` file extension up above and importing the config directly because Relay needs access to `__dirname`, which can't be derived from the default JSON parsed from `.swcrc`.

#### Output import paths

By default, `@swc/plugin-relay` will transpile import paths based on the `language` option.
You can use `outputFileExtension` to change the file extension of the generated import paths.

```js
plugins: [
[
"@swc/plugin-relay",
{
rootDir: __dirname,
artifactDirectory: "src/__generated__",
language: "typescript",
eagerEsModules: true,
outputFileExtension: "js",
},
],
],
```
In this example typescript graphql files will output transpiled import path of `javascript` ending with `.js`.
6 changes: 5 additions & 1 deletion packages/relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use swc_core::{
ecma::{ast::Program, visit::FoldWith},
plugin::{plugin_transform, proxies::TransformPluginProgramMetadata},
};
use swc_relay::{relay, Config, RelayLanguageConfig};
use swc_relay::{relay, Config, RelayLanguageConfig, OutputFileExtension};

#[plugin_transform]
fn relay_plugin_transform(program: Program, metadata: TransformPluginProgramMetadata) -> Program {
Expand Down Expand Up @@ -40,6 +40,9 @@ fn relay_plugin_transform(program: Program, metadata: TransformPluginProgramMeta
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();
Expand All @@ -48,6 +51,7 @@ fn relay_plugin_transform(program: Program, metadata: TransformPluginProgramMeta
artifact_directory,
language,
eager_es_modules,
output_file_extension
};

let mut relay = relay(
Expand Down
45 changes: 37 additions & 8 deletions packages/relay/transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ pub enum RelayLanguageConfig {
Flow,
}

#[derive(Copy, Clone, Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum OutputFileExtension {
TypeScript,
JavaScript,
Undefined
}

impl<'a> TryFrom<&'a str> for RelayLanguageConfig {
type Error = String;

Expand All @@ -33,12 +41,31 @@ impl<'a> TryFrom<&'a str> for RelayLanguageConfig {
}
}


impl<'a> TryFrom<&'a str> for OutputFileExtension {
type Error = String;

fn try_from(value: &'a str) -> Result<Self, Self::Error> {
match value {
"ts" => Ok(Self::TypeScript),
"js" => Ok(Self::JavaScript),
_ => Err(format!("Unexpected output file extension value '{}'", value)),
}
}
}

impl Default for RelayLanguageConfig {
fn default() -> Self {
Self::Flow
}
}

impl Default for OutputFileExtension {
fn default() -> Self {
Self::Undefined
}
}

#[derive(Debug, Clone)]
struct RelayImport {
path: JsWord,
Expand Down Expand Up @@ -81,6 +108,8 @@ pub struct Config {
pub language: RelayLanguageConfig,
#[serde(default)]
pub eager_es_modules: bool,
#[serde(default)]
pub output_file_extension: OutputFileExtension,
}

fn pull_first_operation_name_from_tpl(tpl: &TaggedTpl) -> Option<String> {
Expand Down Expand Up @@ -159,14 +188,14 @@ impl<'a> Relay<'a> {
real_file_name: &Path,
definition_name: &str,
) -> Result<PathBuf, BuildRequirePathError> {
let filename = match &self.config.language {
RelayLanguageConfig::Flow => format!("{}.graphql.js", definition_name),
RelayLanguageConfig::TypeScript => {
format!("{}.graphql.ts", definition_name)
}
RelayLanguageConfig::JavaScript => {
format!("{}.graphql.js", definition_name)
}
let filename = match &self.config.output_file_extension {
OutputFileExtension::JavaScript => format!("{}.graphql.js", definition_name),
OutputFileExtension::TypeScript => format!("{}.graphql.ts", definition_name),
OutputFileExtension::Undefined => match &self.config.language {
RelayLanguageConfig::Flow => format!("{}.graphql.js", definition_name),
RelayLanguageConfig::TypeScript => format!("{}.graphql.ts", definition_name),
RelayLanguageConfig::JavaScript => format!("{}.graphql.js", definition_name),
},
};

if let Some(artifact_directory) = &self.config.artifact_directory {
Expand Down
56 changes: 55 additions & 1 deletion packages/relay/transform/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::PathBuf;

use swc_common::FileName;
use swc_ecma_transforms_testing::test_fixture;
use swc_relay::{relay, Config, RelayLanguageConfig};
use swc_relay::{relay, Config, RelayLanguageConfig, OutputFileExtension};

#[testing::fixture("tests/fixture/simple/**/input.js")]
fn fixture(input: PathBuf) {
Expand All @@ -16,6 +16,7 @@ fn fixture(input: PathBuf) {
artifact_directory: None,
language: RelayLanguageConfig::TypeScript,
eager_es_modules: false,
output_file_extension: OutputFileExtension::Undefined,
},
FileName::Real("file.js".parse().unwrap()),
Default::default(),
Expand All @@ -41,6 +42,59 @@ fn fixture_es_modules(input: PathBuf) {
artifact_directory: None,
language: RelayLanguageConfig::TypeScript,
eager_es_modules: true,
output_file_extension: OutputFileExtension::Undefined,
},
FileName::Real("file.js".parse().unwrap()),
Default::default(),
None,
None,
)
},
&input,
&output,
Default::default(),
);
}

#[testing::fixture("tests/fixture/outputFileExtension/javascript/**/input.js")]
fn fixture_output_file_extension_javascript(input: PathBuf) {
let output = input.parent().unwrap().join("output.js");

test_fixture(
Default::default(),
&|_| {
relay(
&Config {
artifact_directory: None,
language: RelayLanguageConfig::TypeScript,
eager_es_modules: true,
output_file_extension: OutputFileExtension::JavaScript,
},
FileName::Real("file.js".parse().unwrap()),
Default::default(),
None,
None,
)
},
&input,
&output,
Default::default(),
);
}

#[testing::fixture("tests/fixture/outputFileExtension/typescript/**/input.js")]
fn fixture_output_file_extension_typescript(input: PathBuf) {
let output = input.parent().unwrap().join("output.js");

test_fixture(
Default::default(),
&|_| {
relay(
&Config {
artifact_directory: None,
language: RelayLanguageConfig::JavaScript,
eager_es_modules: true,
output_file_extension: OutputFileExtension::TypeScript,
},
FileName::Real("file.js".parse().unwrap()),
Default::default(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const myFragment = graphql`
fragment FooFragment on Bar {
id
}
`
useQuery(graphql`
query FooQuery {
id
}
`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import __FooFragment from "__generated__/FooFragment.graphql.js";
import __FooQuery from "__generated__/FooQuery.graphql.js";
const myFragment = __FooFragment;
useQuery(__FooQuery);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const myFragment = graphql`
fragment FooFragment on Bar {
id
}
`
useQuery(graphql`
query FooQuery {
id
}
`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import __FooFragment from "__generated__/FooFragment.graphql.ts";
import __FooQuery from "__generated__/FooQuery.graphql.ts";
const myFragment = __FooFragment;
useQuery(__FooQuery);
Loading