Skip to content

Commit d2275be

Browse files
committed
Move to global config loading for token transformer
1 parent 1abb645 commit d2275be

File tree

4 files changed

+30
-35
lines changed

4 files changed

+30
-35
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/atlaspack_plugin_transformer_tokens/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ atlaspack_filesystem = { path = "../atlaspack_filesystem" }
1515
atlaspack_sourcemap = { path = "../atlaspack_sourcemap" }
1616
anyhow = { workspace = true }
1717
async-trait = { workspace = true }
18-
parcel_sourcemap = { workspace = true }
18+
parcel_sourcemap = { path = "../parcel_sourcemap" }
1919
serde = { workspace = true, features = ["derive"] }
2020
serde_json = { workspace = true }
2121

crates/atlaspack_plugin_transformer_tokens/src/tokens_transformer.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,20 @@ use crate::tokens_transformer_config::{PackageJson, TokensTransformerConfig};
1515
pub struct AtlaspackTokensTransformerPlugin {
1616
project_root: PathBuf,
1717
options: Arc<PluginOptions>,
18+
config: Option<TokensTransformerConfig>,
1819
}
1920

2021
impl AtlaspackTokensTransformerPlugin {
2122
pub fn new(ctx: &PluginContext) -> Result<Self, Error> {
23+
let config = Self::load_config(ctx.config.clone())?;
2224
Ok(AtlaspackTokensTransformerPlugin {
2325
project_root: ctx.options.project_root.clone(),
2426
options: ctx.options.clone(),
27+
config,
2528
})
2629
}
2730

2831
fn load_config(
29-
&self,
3032
config_loader: atlaspack_core::config_loader::ConfigLoaderRef,
3133
) -> Result<Option<TokensTransformerConfig>, Error> {
3234
let config = config_loader
@@ -52,7 +54,7 @@ impl AtlaspackTokensTransformerPlugin {
5254
impl TransformerPlugin for AtlaspackTokensTransformerPlugin {
5355
async fn transform(
5456
&self,
55-
context: TransformContext,
57+
_context: TransformContext,
5658
asset: Asset,
5759
) -> Result<TransformResult, Error> {
5860
// Check feature flag first
@@ -76,10 +78,7 @@ impl TransformerPlugin for AtlaspackTokensTransformerPlugin {
7678
});
7779
}
7880

79-
// Load config from package.json
80-
let config = self.load_config(context.config())?;
81-
82-
let Some(config) = config else {
81+
let Some(config) = &self.config else {
8382
// If no config provided, just return asset unchanged
8483
return Ok(TransformResult {
8584
asset,
@@ -108,8 +107,16 @@ impl TransformerPlugin for AtlaspackTokensTransformerPlugin {
108107
token_data_path,
109108
should_use_auto_fallback: config.should_use_auto_fallback.unwrap_or(true),
110109
should_force_auto_fallback: config.should_force_auto_fallback.unwrap_or(true),
111-
force_auto_fallback_exemptions: config.force_auto_fallback_exemptions.unwrap_or_default(),
112-
default_theme: config.default_theme.unwrap_or_else(|| "light".to_string()),
110+
force_auto_fallback_exemptions: config
111+
.force_auto_fallback_exemptions
112+
.as_ref()
113+
.cloned()
114+
.unwrap_or_default(),
115+
default_theme: config
116+
.default_theme
117+
.as_deref()
118+
.unwrap_or("light")
119+
.to_string(),
113120
},
114121
};
115122

crates/node-bindings/src/plugin_tokens.rs

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,6 @@ pub struct TokensPluginOptions {
1717
pub default_theme: String,
1818
}
1919

20-
impl From<TokensPluginOptions> for SharedTokensPluginOptions {
21-
fn from(opts: TokensPluginOptions) -> Self {
22-
SharedTokensPluginOptions {
23-
token_data_path: opts.token_data_path,
24-
should_use_auto_fallback: opts.should_use_auto_fallback,
25-
should_force_auto_fallback: opts.should_force_auto_fallback,
26-
force_auto_fallback_exemptions: opts.force_auto_fallback_exemptions,
27-
default_theme: opts.default_theme,
28-
}
29-
}
30-
}
31-
3220
#[napi(object)]
3321
#[derive(Clone)]
3422
pub struct TokensConfig {
@@ -39,18 +27,6 @@ pub struct TokensConfig {
3927
pub tokens_options: TokensPluginOptions,
4028
}
4129

42-
impl From<TokensConfig> for SharedTokensConfig {
43-
fn from(config: TokensConfig) -> Self {
44-
SharedTokensConfig {
45-
filename: config.filename,
46-
project_root: config.project_root,
47-
is_source: config.is_source,
48-
source_maps: config.source_maps,
49-
tokens_options: config.tokens_options.into(),
50-
}
51-
}
52-
}
53-
5430
#[napi(object)]
5531
#[derive(Clone, Debug, Serialize)]
5632
pub struct TokensPluginResult {
@@ -89,7 +65,19 @@ pub fn apply_tokens_plugin(
8965
let (deferred, promise) = env.create_deferred()?;
9066

9167
// Convert to shared config
92-
let shared_config: SharedTokensConfig = config.into();
68+
let shared_config = SharedTokensConfig {
69+
filename: config.filename,
70+
project_root: config.project_root,
71+
is_source: config.is_source,
72+
source_maps: config.source_maps,
73+
tokens_options: SharedTokensPluginOptions {
74+
token_data_path: config.tokens_options.token_data_path,
75+
should_use_auto_fallback: config.tokens_options.should_use_auto_fallback,
76+
should_force_auto_fallback: config.tokens_options.should_force_auto_fallback,
77+
force_auto_fallback_exemptions: config.tokens_options.force_auto_fallback_exemptions,
78+
default_theme: config.tokens_options.default_theme,
79+
},
80+
};
9381

9482
// Spawn the work on a Rayon thread
9583
rayon::spawn(move || {

0 commit comments

Comments
 (0)