Skip to content

Commit

Permalink
fix(turbopack): postcss should be applied to @imported CSS files (v…
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony authored Nov 21, 2023
1 parent 024fa99 commit 91adb44
Showing 1 changed file with 29 additions and 40 deletions.
69 changes: 29 additions & 40 deletions crates/turbopack/src/module_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,32 @@ impl ModuleOptions {
),
]);
} else {
if let Some(options) = enable_postcss_transform {
let execution_context = execution_context
.context("execution_context is required for the postcss_transform")?;

let import_map = if let Some(postcss_package) = options.postcss_package {
package_import_map_from_import_mapping("postcss".to_string(), postcss_package)
} else {
package_import_map_from_context("postcss".to_string(), path)
};

rules.push(ModuleRule::new(
ModuleRuleCondition::ResourcePathEndsWith(".css".to_string()),
vec![ModuleRuleEffect::SourceTransforms(Vc::cell(vec![
Vc::upcast(PostCssTransform::new(
node_evaluate_asset_context(
execution_context,
Some(import_map),
None,
"postcss".to_string(),
),
execution_context,
)),
]))],
));
}

rules.extend([
ModuleRule::new(
ModuleRuleCondition::all(vec![
Expand All @@ -404,40 +430,7 @@ impl ModuleOptions {
ReferenceType::Css(CssReferenceSubType::AtImport),
)),
]),
[
if let Some(options) = enable_postcss_transform {
let execution_context = execution_context.context(
"execution_context is required for the postcss_transform",
)?;

let import_map = if let Some(postcss_package) = options.postcss_package
{
package_import_map_from_import_mapping(
"postcss".to_string(),
postcss_package,
)
} else {
package_import_map_from_context("postcss".to_string(), path)
};
Some(ModuleRuleEffect::SourceTransforms(Vc::cell(vec![
Vc::upcast(PostCssTransform::new(
node_evaluate_asset_context(
execution_context,
Some(import_map),
None,
"postcss".to_string(),
),
execution_context,
)),
])))
} else {
None
},
Some(ModuleRuleEffect::ModuleType(ModuleType::CssGlobal)),
]
.into_iter()
.flatten()
.collect(),
vec![ModuleRuleEffect::ModuleType(ModuleType::CssGlobal)],
),
ModuleRule::new(
ModuleRuleCondition::all(vec![
Expand Down Expand Up @@ -478,18 +471,14 @@ impl ModuleOptions {
})],
),
ModuleRule::new_internal(
ModuleRuleCondition::all(vec![ModuleRuleCondition::ResourcePathEndsWith(
".css".to_string(),
)]),
ModuleRuleCondition::ResourcePathEndsWith(".css".to_string()),
vec![ModuleRuleEffect::ModuleType(ModuleType::Css {
ty: CssModuleAssetType::Default,
use_lightningcss,
})],
),
ModuleRule::new_internal(
ModuleRuleCondition::all(vec![ModuleRuleCondition::ResourcePathEndsWith(
".module.css".to_string(),
)]),
ModuleRuleCondition::ResourcePathEndsWith(".module.css".to_string()),
vec![ModuleRuleEffect::ModuleType(ModuleType::Css {
ty: CssModuleAssetType::Module,
use_lightningcss,
Expand Down

0 comments on commit 91adb44

Please sign in to comment.