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

Allow skipping postcss relative config resolving #7012

Merged
merged 3 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 4 additions & 2 deletions crates/turbopack-cli/src/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ use turbopack_ecmascript_plugins::transform::{
styled_components::{StyledComponentsTransformConfig, StyledComponentsTransformer},
styled_jsx::StyledJsxTransformer,
};
use turbopack_node::execution_context::ExecutionContext;
use turbopack_node::{
execution_context::ExecutionContext, transforms::postcss::PostCssTransformOptions,
};

#[turbo_tasks::value(shared)]
pub enum NodeEnv {
Expand Down Expand Up @@ -139,7 +141,7 @@ async fn get_client_module_options_context(

let module_options_context = ModuleOptionsContext {
enable_jsx,
enable_postcss_transform: Some(Default::default()),
enable_postcss_transform: Some(PostCssTransformOptions::default().cell()),
enable_typescript_transform: Some(Default::default()),
rules: vec![(
foreign_code_context_condition().await?,
Expand Down
88 changes: 63 additions & 25 deletions crates/turbopack-node/src/transforms/postcss.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use anyhow::{bail, Context, Result};
use indexmap::indexmap;
use serde::{Deserialize, Serialize};
use turbo_tasks::{Completion, Completions, TryFlatJoinIterExt, Value, Vc};
use turbo_tasks::{
trace::TraceRawVcs, Completion, Completions, TaskInput, TryFlatJoinIterExt, Value, Vc,
};
use turbo_tasks_bytes::stream::SingleValue;
use turbo_tasks_fs::{
json::parse_json_with_source_context, File, FileContent, FileSystemEntryType, FileSystemPath,
Expand All @@ -16,7 +18,7 @@ use turbopack_core::{
Issue, IssueDescriptionExt, IssueExt, IssueSeverity, OptionStyledString, StyledString,
},
reference_type::{EntryReferenceSubType, InnerAssets, ReferenceType},
resolve::{find_context_file, FindContextFileResult},
resolve::{find_context_file, options::ImportMapping, FindContextFileResult},
source::Source,
source_transform::SourceTransform,
virtual_source::VirtualSource,
Expand All @@ -38,6 +40,23 @@ struct PostCssProcessingResult {
assets: Option<Vec<EmittedAsset>>,
}

#[derive(
Default, Copy, Clone, PartialEq, Eq, Debug, TraceRawVcs, Serialize, Deserialize, TaskInput,
)]
pub enum PostCssConfigLocation {
#[default]
ProjectPath,
ProjectPathOrLocalPath,
}

#[turbo_tasks::value(shared)]
#[derive(Clone, Default)]
pub struct PostCssTransformOptions {
pub postcss_package: Option<Vc<ImportMapping>>,
pub config_location: PostCssConfigLocation,
pub placeholder_for_future_extensions: u8,
}

#[turbo_tasks::function]
fn postcss_configs() -> Vc<Vec<String>> {
Vc::cell(
Expand Down Expand Up @@ -70,6 +89,7 @@ fn postcss_configs() -> Vc<Vec<String>> {
pub struct PostCssTransform {
evaluate_context: Vc<Box<dyn AssetContext>>,
execution_context: Vc<ExecutionContext>,
config_location: PostCssConfigLocation,
}

#[turbo_tasks::value_impl]
Expand All @@ -78,10 +98,12 @@ impl PostCssTransform {
pub fn new(
evaluate_context: Vc<Box<dyn AssetContext>>,
execution_context: Vc<ExecutionContext>,
config_location: PostCssConfigLocation,
) -> Vc<Self> {
PostCssTransform {
evaluate_context,
execution_context,
config_location,
}
.cell()
}
Expand All @@ -95,6 +117,7 @@ impl SourceTransform for PostCssTransform {
PostCssTransformedAsset {
evaluate_context: self.evaluate_context,
execution_context: self.execution_context,
config_location: self.config_location,
source,
}
.cell(),
Expand All @@ -106,6 +129,7 @@ impl SourceTransform for PostCssTransform {
struct PostCssTransformedAsset {
evaluate_context: Vc<Box<dyn AssetContext>>,
execution_context: Vc<ExecutionContext>,
config_location: PostCssConfigLocation,
source: Vc<Box<dyn Source>>,
}

Expand Down Expand Up @@ -196,6 +220,28 @@ async fn postcss_executor(
))
}

async fn find_config_in_location(
project_path: Vc<FileSystemPath>,
location: PostCssConfigLocation,
source: Vc<Box<dyn Source>>,
) -> Result<Option<Vc<FileSystemPath>>> {
if let FindContextFileResult::Found(config_path, _) =
*find_context_file(project_path, postcss_configs()).await?
{
return Ok(Some(config_path));
}

if matches!(location, PostCssConfigLocation::ProjectPathOrLocalPath) {
if let FindContextFileResult::Found(config_path, _) =
*find_context_file(source.ident().path().parent(), postcss_configs()).await?
{
return Ok(Some(config_path));
}
}

Ok(None)
}

#[turbo_tasks::value_impl]
impl PostCssTransformedAsset {
#[turbo_tasks::function]
Expand All @@ -218,31 +264,23 @@ impl PostCssTransformedAsset {
// - pkg1/(postcss.config.js) // The actual config we're looking for
//
// We look for the config in the project path first, then the source path
let config_path = match *find_context_file(project_path, postcss_configs()).await? {
FindContextFileResult::Found(config_path, _) => config_path,
_ => {
let FindContextFileResult::Found(config_path, _) =
*find_context_file(this.source.ident().path().parent(), postcss_configs())
.await?
else {
PostCssTransformIssue {
source: this.source.ident().path(),
title: "PostCSS transform skipped".to_string(),
description: "Unable to find PostCSS config".to_string(),
severity: IssueSeverity::Warning.cell(),
}
.cell()
.emit();

return Ok(ProcessPostCssResult {
content: this.source.content(),
assets: Vec::new(),
}
.cell());
};
let Some(config_path) =
find_config_in_location(project_path, this.config_location, this.source).await?
else {
PostCssTransformIssue {
source: this.source.ident().path(),
title: "PostCSS transform skipped".to_string(),
description: "Unable to find PostCSS config".to_string(),
severity: IssueSeverity::Warning.cell(),
}
.cell()
.emit();

config_path
return Ok(ProcessPostCssResult {
content: this.source.content(),
assets: Vec::new(),
}
.cell());
};

let source_content = this.source.content();
Expand Down
2 changes: 2 additions & 0 deletions crates/turbopack/src/module_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ impl ModuleOptions {
]);
} else {
if let Some(options) = enable_postcss_transform {
let options = options.await?;
let execution_context = execution_context
.context("execution_context is required for the postcss_transform")?;

Expand All @@ -417,6 +418,7 @@ impl ModuleOptions {
"postcss".to_string(),
),
execution_context,
options.config_location,
)),
]))],
));
Expand Down
11 changes: 3 additions & 8 deletions crates/turbopack/src/module_options/module_options_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@ use turbo_tasks::{trace::TraceRawVcs, ValueDefault, Vc};
use turbopack_core::{environment::Environment, resolve::options::ImportMapping};
use turbopack_ecmascript::{references::esm::UrlRewriteBehavior, TransformPlugin, TreeShakingMode};
use turbopack_node::{
execution_context::ExecutionContext, transforms::webpack::WebpackLoaderItems,
execution_context::ExecutionContext,
transforms::{postcss::PostCssTransformOptions, webpack::WebpackLoaderItems},
};

use super::ModuleRule;
use crate::condition::ContextCondition;

#[derive(Default, Clone, PartialEq, Eq, Debug, TraceRawVcs, Serialize, Deserialize)]
pub struct PostCssTransformOptions {
pub postcss_package: Option<Vc<ImportMapping>>,
pub placeholder_for_future_extensions: (),
}

#[derive(Clone, PartialEq, Eq, Debug, TraceRawVcs, Serialize, Deserialize)]
pub struct LoaderRuleItem {
pub loaders: Vc<WebpackLoaderItems>,
Expand Down Expand Up @@ -139,7 +134,7 @@ impl MdxTransformModuleOptions {
#[serde(default)]
pub struct ModuleOptionsContext {
pub enable_jsx: Option<Vc<JsxTransformOptions>>,
pub enable_postcss_transform: Option<PostCssTransformOptions>,
pub enable_postcss_transform: Option<Vc<PostCssTransformOptions>>,
pub enable_webpack_loaders: Option<Vc<WebpackLoadersOptions>>,
pub enable_types: bool,
pub enable_typescript_transform: Option<Vc<TypescriptTransformOptions>>,
Expand Down
Loading