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 all 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
18 changes: 9 additions & 9 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/constify/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-constify",
"version": "0.1.23",
"version": "0.1.24",
"description": "SWC plugin for optimization",
"main": "swc_plugin_constify.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/constify/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_constify"
repository = "https://github.com/swc-project/plugins.git"
version = "0.18.0"
version = "0.19.0"

# 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/emotion/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-emotion",
"version": "2.5.93",
"version": "2.5.94",
"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.54.0"
version = "0.55.0"

# 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.93",
"version": "1.5.94",
"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.93",
"version": "0.3.94",
"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.91",
"version": "1.5.92",
"description": "Noop SWC plugin, for debugging",
"main": "swc_plugin_noop.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-remove-properties/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-react-remove-properties",
"version": "1.5.93",
"version": "1.5.94",
"description": "SWC plugin for https://www.npmjs.com/package/babel-plugin-react-remove-properties",
"main": "swc_plugin_react_remove_properties.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-remove-properties/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 = "react_remove_properties"
repository = "https://github.com/swc-project/plugins.git"
version = "0.6.0"
version = "0.7.0"

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

Expand Down
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`.
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.93",
"version": "1.5.94",
"description": "SWC plugin for relay",
"main": "swc_plugin_relay.wasm",
"types": "./types.d.ts",
Expand Down
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
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.26.0"
version = "0.27.0"

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

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);
Loading
Loading