Skip to content

Commit 543918e

Browse files
lynnshaoyufacebook-github-bot
authored andcommitted
- add ability to configure operationModuleProvider path separately from componentModuleProvider
Reviewed By: tyao1 Differential Revision: D73121116 fbshipit-source-id: 332ae7951c310d60c9c58f99fbcc0117a7feb406
1 parent 2784dfc commit 543918e

File tree

10 files changed

+146
-96
lines changed

10 files changed

+146
-96
lines changed

compiler/crates/relay-codegen/src/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use graphql_syntax::FloatValue;
1111
use graphql_syntax::OperationKind;
1212
use indexmap::IndexSet;
1313
use intern::string_key::StringKey;
14-
use relay_config::DynamicModuleProvider;
14+
use relay_config::ModuleProvider;
1515

1616
#[derive(Eq, PartialEq, Hash, Debug)]
1717
pub struct ObjectEntry {
@@ -122,7 +122,7 @@ pub enum Primitive {
122122
// skip_printing_nulls is enabled
123123
SkippableNull,
124124
DynamicImport {
125-
provider: DynamicModuleProvider,
125+
provider: ModuleProvider,
126126
module: StringKey,
127127
},
128128
RelayResolverModel {

compiler/crates/relay-codegen/src/build_ast.rs

Lines changed: 28 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ use graphql_syntax::OperationKind;
3939
use lazy_static::lazy_static;
4040
use md5::Digest;
4141
use md5::Md5;
42-
use relay_config::DynamicModuleProvider;
4342
use relay_config::JsModuleFormat;
4443
use relay_config::ProjectConfig;
4544
use relay_config::Surface;
@@ -2504,84 +2503,34 @@ impl<'schema, 'builder, 'config> CodegenBuilder<'schema, 'builder, 'config> {
25042503
.module_import_config
25052504
.dynamic_module_provider
25062505
{
2507-
match self.project_config.module_import_config.surface {
2508-
None | Some(Surface::All) => {
2509-
module_import.push(ObjectEntry {
2510-
key: CODEGEN_CONSTANTS.component_module_provider,
2511-
value: Primitive::DynamicImport {
2512-
provider: dynamic_module_provider,
2513-
module: module_metadata.module_name,
2514-
},
2515-
});
2516-
module_import.push(ObjectEntry {
2517-
key: CODEGEN_CONSTANTS.operation_module_provider,
2518-
value: Primitive::DynamicImport {
2519-
provider: dynamic_module_provider,
2520-
module: get_normalization_fragment_filename(fragment_name),
2521-
},
2522-
});
2523-
}
2524-
Some(Surface::Resolvers) => {
2525-
if module_metadata.read_time_resolvers {
2526-
module_import.push(ObjectEntry {
2527-
key: CODEGEN_CONSTANTS.component_module_provider,
2528-
value: Primitive::DynamicImport {
2529-
provider: dynamic_module_provider,
2530-
module: module_metadata.module_name,
2531-
},
2532-
});
2533-
match dynamic_module_provider {
2534-
DynamicModuleProvider::JSResource => {
2535-
module_import.push(ObjectEntry {
2536-
key: CODEGEN_CONSTANTS.operation_module_provider,
2537-
value: Primitive::DynamicImport {
2538-
provider: dynamic_module_provider,
2539-
module: get_normalization_fragment_filename(
2540-
fragment_name,
2541-
),
2542-
},
2543-
});
2544-
}
2545-
DynamicModuleProvider::Custom { .. } => {
2546-
let custom_dynamic_module_provider =
2547-
match self.project_config.js_module_format {
2548-
JsModuleFormat::Haste => dynamic_module_provider,
2549-
_ => {
2550-
let path = self
2551-
.project_config
2552-
.create_path_for_artifact(
2553-
module_metadata
2554-
.fragment_source_location
2555-
.source_location(),
2556-
fragment_name.0.to_string(),
2557-
);
2558-
let path_for_module =
2559-
path.display().to_string().replace(
2560-
&fragment_name.0.to_string(),
2561-
"<$module>",
2562-
);
2563-
DynamicModuleProvider::Custom {
2564-
statement: format!(
2565-
"() => require('{}')",
2566-
path_for_module
2567-
)
2568-
.intern(),
2569-
}
2570-
}
2571-
};
2572-
module_import.push(ObjectEntry {
2573-
key: CODEGEN_CONSTANTS.operation_module_provider,
2574-
value: Primitive::DynamicImport {
2575-
provider: custom_dynamic_module_provider,
2576-
module: get_normalization_fragment_filename(
2577-
fragment_name,
2578-
),
2579-
},
2580-
});
2581-
}
2582-
};
2583-
}
2584-
}
2506+
if self.project_config.module_import_config.surface.is_none()
2507+
|| self.project_config.module_import_config.surface == Some(Surface::All)
2508+
|| (self.project_config.module_import_config.surface
2509+
== Some(Surface::Resolvers)
2510+
&& module_metadata.read_time_resolvers)
2511+
{
2512+
let operation_module_provider = match self
2513+
.project_config
2514+
.module_import_config
2515+
.operation_module_provider
2516+
{
2517+
Some(operation_module_provider) => operation_module_provider,
2518+
None => dynamic_module_provider,
2519+
};
2520+
module_import.push(ObjectEntry {
2521+
key: CODEGEN_CONSTANTS.component_module_provider,
2522+
value: Primitive::DynamicImport {
2523+
provider: dynamic_module_provider,
2524+
module: module_metadata.module_name,
2525+
},
2526+
});
2527+
module_import.push(ObjectEntry {
2528+
key: CODEGEN_CONSTANTS.operation_module_provider,
2529+
value: Primitive::DynamicImport {
2530+
provider: operation_module_provider,
2531+
module: get_normalization_fragment_filename(fragment_name),
2532+
},
2533+
});
25852534
}
25862535
}
25872536
}

compiler/crates/relay-codegen/src/printer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use graphql_ir::reexport::Intern;
2020
use indexmap::IndexMap;
2121
use intern::Lookup;
2222
use intern::string_key::StringKey;
23-
use relay_config::DynamicModuleProvider;
23+
use relay_config::ModuleProvider;
2424
use relay_config::ProjectConfig;
2525
use schema::SDLSchema;
2626

@@ -568,7 +568,7 @@ impl<'b> JSONPrinter<'b> {
568568
self.write_resolver_module_reference(f, resolver_function_name.clone(), field_type)
569569
}
570570
Primitive::DynamicImport { provider, module } => match provider {
571-
DynamicModuleProvider::JSResource => {
571+
ModuleProvider::JSResource => {
572572
self.top_level_statements.insert(
573573
"JSResource".to_string(),
574574
TopLevelStatement::ImportStatement(JSModuleDependency {
@@ -578,7 +578,7 @@ impl<'b> JSONPrinter<'b> {
578578
);
579579
write!(f, "() => JSResource('m#{}')", module)
580580
}
581-
DynamicModuleProvider::Custom { statement } => {
581+
ModuleProvider::Custom { statement } => {
582582
f.push_str(&statement.lookup().replace(
583583
"<$module>",
584584
&get_module_path(self.js_module_format, *module),

compiler/crates/relay-compiler/relay-compiler-config-schema.json

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,6 +2148,7 @@
21482148
"description": "Configuration for @module",
21492149
"default": {
21502150
"dynamicModuleProvider": null,
2151+
"operationModuleProvider": null,
21512152
"surface": null
21522153
},
21532154
"type": "object",
@@ -2198,6 +2199,52 @@
21982199
}
21992200
]
22002201
},
2202+
"operationModuleProvider": {
2203+
"description": "Defines the custom import statement to be generated for the `operationModuleProvider` function on the `NormalizationModuleImport` node in ASTs. Used in exec time client 3D.",
2204+
"anyOf": [
2205+
{
2206+
"oneOf": [
2207+
{
2208+
"description": "Generates a module provider using JSResource",
2209+
"type": "object",
2210+
"required": [
2211+
"mode"
2212+
],
2213+
"properties": {
2214+
"mode": {
2215+
"type": "string",
2216+
"enum": [
2217+
"JSResource"
2218+
]
2219+
}
2220+
}
2221+
},
2222+
{
2223+
"description": "Generates a custom JS import, Use `<$module>` as the placeholder for the actual module. e.g. `\"() => import('<$module>')\"`",
2224+
"type": "object",
2225+
"required": [
2226+
"mode",
2227+
"statement"
2228+
],
2229+
"properties": {
2230+
"mode": {
2231+
"type": "string",
2232+
"enum": [
2233+
"Custom"
2234+
]
2235+
},
2236+
"statement": {
2237+
"type": "string"
2238+
}
2239+
}
2240+
}
2241+
]
2242+
},
2243+
{
2244+
"type": "null"
2245+
}
2246+
]
2247+
},
22012248
"surface": {
22022249
"description": "Defines the surface upon which @module is enabled.",
22032250
"type": [
@@ -6937,6 +6984,7 @@
69376984
"description": "Configuration for @module.",
69386985
"default": {
69396986
"dynamicModuleProvider": null,
6987+
"operationModuleProvider": null,
69406988
"surface": null
69416989
},
69426990
"type": "object",
@@ -6987,6 +7035,52 @@
69877035
}
69887036
]
69897037
},
7038+
"operationModuleProvider": {
7039+
"description": "Defines the custom import statement to be generated for the `operationModuleProvider` function on the `NormalizationModuleImport` node in ASTs. Used in exec time client 3D.",
7040+
"anyOf": [
7041+
{
7042+
"oneOf": [
7043+
{
7044+
"description": "Generates a module provider using JSResource",
7045+
"type": "object",
7046+
"required": [
7047+
"mode"
7048+
],
7049+
"properties": {
7050+
"mode": {
7051+
"type": "string",
7052+
"enum": [
7053+
"JSResource"
7054+
]
7055+
}
7056+
}
7057+
},
7058+
{
7059+
"description": "Generates a custom JS import, Use `<$module>` as the placeholder for the actual module. e.g. `\"() => import('<$module>')\"`",
7060+
"type": "object",
7061+
"required": [
7062+
"mode",
7063+
"statement"
7064+
],
7065+
"properties": {
7066+
"mode": {
7067+
"type": "string",
7068+
"enum": [
7069+
"Custom"
7070+
]
7071+
},
7072+
"statement": {
7073+
"type": "string"
7074+
}
7075+
}
7076+
}
7077+
]
7078+
},
7079+
{
7080+
"type": "null"
7081+
}
7082+
]
7083+
},
69907084
"surface": {
69917085
"description": "Defines the surface upon which @module is enabled.",
69927086
"type": [

compiler/crates/relay-config/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ pub use defer_stream_interface::DeferStreamInterface;
2525
pub use diagnostic_report_config::DiagnosticLevel;
2626
pub use diagnostic_report_config::DiagnosticReportConfig;
2727
pub use js_module_format::JsModuleFormat;
28-
pub use module_import_config::DynamicModuleProvider;
2928
pub use module_import_config::ModuleImportConfig;
29+
pub use module_import_config::ModuleProvider;
3030
pub use module_import_config::Surface;
3131
pub use non_node_id_fields_config::NonNodeIdFieldsConfig;
3232
pub use project_config::ExtraArtifactsConfig;

compiler/crates/relay-config/src/module_import_config.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ pub struct ModuleImportConfig {
1717
/// Defines the custom import statement to be generated on the
1818
/// `ModuleImport` node in ASTs, used for dynamically loading
1919
/// components at runtime.
20-
pub dynamic_module_provider: Option<DynamicModuleProvider>,
20+
pub dynamic_module_provider: Option<ModuleProvider>,
21+
/// Defines the custom import statement to be generated for the
22+
/// `operationModuleProvider` function on the `NormalizationModuleImport`
23+
/// node in ASTs. Used in exec time client 3D.
24+
pub operation_module_provider: Option<ModuleProvider>,
2125
/// Defines the surface upon which @module is enabled.
2226
pub surface: Option<Surface>,
2327
}
@@ -34,7 +38,7 @@ pub struct ModuleImportConfig {
3438
JsonSchema
3539
)]
3640
#[serde(tag = "mode")]
37-
pub enum DynamicModuleProvider {
41+
pub enum ModuleProvider {
3842
/// Generates a module provider using JSResource
3943
JSResource,
4044
/// Generates a custom JS import, Use `<$module>` as the placeholder

compiler/crates/relay-transforms/tests/generate_data_driven_dependency_metadata.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
use common::FeatureFlags;
99
use fixture_tests::Fixture;
1010
use graphql_test_helpers::apply_transform_for_test;
11-
use relay_config::DynamicModuleProvider;
1211
use relay_config::ModuleImportConfig;
12+
use relay_config::ModuleProvider;
1313
use relay_config::Surface;
1414
use relay_transforms::generate_data_driven_dependency_metadata;
1515
use relay_transforms::transform_match;
1616

1717
pub async fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
1818
let module_import_config = ModuleImportConfig {
19-
dynamic_module_provider: Some(DynamicModuleProvider::JSResource),
19+
dynamic_module_provider: Some(ModuleProvider::JSResource),
20+
operation_module_provider: None,
2021
surface: Some(Surface::Resolvers),
2122
};
2223
apply_transform_for_test(fixture, |program| {

compiler/crates/relay-transforms/tests/match_transform_client_only.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ use common::FeatureFlags;
99
use fixture_tests::Fixture;
1010
use graphql_test_helpers::apply_transform_for_test;
1111
use relay_config::DeferStreamInterface;
12-
use relay_config::DynamicModuleProvider;
1312
use relay_config::ModuleImportConfig;
13+
use relay_config::ModuleProvider;
1414
use relay_config::Surface;
1515
use relay_transforms::transform_match;
1616

1717
pub async fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
1818
let flags = FeatureFlags::default();
1919
let module_import_config = ModuleImportConfig {
20-
dynamic_module_provider: Some(DynamicModuleProvider::JSResource),
20+
dynamic_module_provider: Some(ModuleProvider::JSResource),
21+
operation_module_provider: None,
2122
surface: Some(Surface::All),
2223
};
2324
let defer_stream_interface = DeferStreamInterface::default();

compiler/crates/relay-transforms/tests/match_transform_client_resolver.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ use common::FeatureFlags;
99
use fixture_tests::Fixture;
1010
use graphql_test_helpers::apply_transform_for_test;
1111
use relay_config::DeferStreamInterface;
12-
use relay_config::DynamicModuleProvider;
1312
use relay_config::ModuleImportConfig;
13+
use relay_config::ModuleProvider;
1414
use relay_config::Surface;
1515
use relay_transforms::transform_match;
1616

1717
pub async fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
1818
let flags = FeatureFlags::default();
1919
let module_import_config = ModuleImportConfig {
20-
dynamic_module_provider: Some(DynamicModuleProvider::JSResource),
20+
dynamic_module_provider: Some(ModuleProvider::JSResource),
21+
operation_module_provider: None,
2122
surface: Some(Surface::Resolvers),
2223
};
2324
let defer_stream_interface = DeferStreamInterface::default();

0 commit comments

Comments
 (0)