Skip to content

Commit

Permalink
run postcss transform without SourceMaps when specified
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jan 21, 2025
1 parent 1623493 commit 03873fd
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 11 deletions.
8 changes: 8 additions & 0 deletions crates/next-core/src/next_client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,14 @@ pub async fn get_client_module_options_context(
},
..Default::default()
},
css: CssOptionsContext {
source_maps: if *next_config.turbo_source_maps().await? {
SourceMapsType::Full
} else {
SourceMapsType::None
},
..Default::default()
},
preset_env_versions: Some(env),
execution_context: Some(execution_context),
tree_shaking_mode: tree_shaking_mode_for_user_code,
Expand Down
5 changes: 5 additions & 0 deletions crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,11 @@ pub async fn get_server_module_options_context(
},
execution_context: Some(execution_context),
css: CssOptionsContext {
source_maps: if *next_config.turbo_source_maps().await? {
SourceMapsType::Full
} else {
SourceMapsType::None
},
..Default::default()
},
tree_shaking_mode: tree_shaking_mode_for_user_code,
Expand Down
13 changes: 8 additions & 5 deletions turbopack/crates/turbopack-node/js/src/transforms/postcss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,16 @@ export const init = async (ipc: Ipc<IpcInfoMessage, IpcRequestMessage>) => {
export default async function transform(
ipc: Ipc<IpcInfoMessage, IpcRequestMessage>,
cssContent: string,
name: string
name: string,
sourceMap: boolean
) {
const { css, map, messages } = await processor!.process(cssContent, {
from: name,
to: name,
map: {
map: sourceMap ? {
inline: false,
annotation: false,
},
} : undefined,
});

const assets = [];
Expand All @@ -93,7 +94,9 @@ export default async function transform(
file: msg.file,
content: msg.content,
sourceMap:
typeof msg.sourceMap === "string"
!sourceMap
? undefined
: typeof msg.sourceMap === "string"
? msg.sourceMap
: JSON.stringify(msg.sourceMap),
// There is also an info field, which we currently ignore
Expand Down Expand Up @@ -133,7 +136,7 @@ export default async function transform(
}
return {
css,
map: JSON.stringify(map),
map: sourceMap ? JSON.stringify(map) : undefined,
assets,
};
}
7 changes: 7 additions & 0 deletions turbopack/crates/turbopack-node/src/transforms/postcss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ pub struct PostCssTransform {
evaluate_context: ResolvedVc<Box<dyn AssetContext>>,
execution_context: ResolvedVc<ExecutionContext>,
config_location: PostCssConfigLocation,
source_maps: bool,
}

#[turbo_tasks::value_impl]
Expand All @@ -117,11 +118,13 @@ impl PostCssTransform {
evaluate_context: ResolvedVc<Box<dyn AssetContext>>,
execution_context: ResolvedVc<ExecutionContext>,
config_location: PostCssConfigLocation,
source_maps: bool,
) -> Vc<Self> {
PostCssTransform {
evaluate_context,
execution_context,
config_location,
source_maps,
}
.cell()
}
Expand All @@ -137,6 +140,7 @@ impl SourceTransform for PostCssTransform {
execution_context: self.execution_context,
config_location: self.config_location,
source,
source_map: self.source_maps,
}
.cell(),
)
Expand All @@ -149,6 +153,7 @@ struct PostCssTransformedAsset {
execution_context: ResolvedVc<ExecutionContext>,
config_location: PostCssConfigLocation,
source: ResolvedVc<Box<dyn Source>>,
source_map: bool,
}

#[turbo_tasks::value_impl]
Expand Down Expand Up @@ -506,6 +511,7 @@ impl PostCssTransformedAsset {
};
let content = content.content().to_str()?;
let evaluate_context = self.evaluate_context;
let source_map = self.source_map;

// This invalidates the transform when the config changes.
let config_changed = config_changed(*evaluate_context, config_path)
Expand Down Expand Up @@ -541,6 +547,7 @@ impl PostCssTransformedAsset {
args: vec![
ResolvedVc::cell(content.into()),
ResolvedVc::cell(css_path.into()),
ResolvedVc::cell(source_map.into()),
],
additional_invalidation: config_changed,
})
Expand Down
3 changes: 1 addition & 2 deletions turbopack/crates/turbopack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,8 +688,7 @@ async fn externals_tracing_module_context(ty: ExternalType) -> Result<Vc<ModuleA
..Default::default()
},
css: CssOptionsContext {
// TODO add source maps handling for css
// source_maps: SourceMapsType::None,
source_maps: SourceMapsType::None,
..Default::default()
},
..Default::default()
Expand Down
8 changes: 7 additions & 1 deletion turbopack/crates/turbopack/src/module_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ impl ModuleOptions {
},
enable_mdx,
enable_mdx_rs,
css: CssOptionsContext { enable_raw_css, .. },
css:
CssOptionsContext {
enable_raw_css,
source_maps: css_source_maps,
..
},
ref enable_postcss_transform,
ref enable_webpack_loaders,
preset_env_versions,
Expand Down Expand Up @@ -426,6 +431,7 @@ impl ModuleOptions {
),
*execution_context,
options.config_location,
matches!(css_source_maps, SourceMapsType::Full),
)
.to_resolved()
.await?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ pub struct CssOptionsContext {

pub minify_type: MinifyType,

// TODO add source maps handling for css
// Specifies how Source Maps are handled.
// pub source_maps: SourceMapsType,
/// Specifies how Source Maps are handled.
pub source_maps: SourceMapsType,

pub placeholder_for_future_extensions: (),
}

Expand Down

0 comments on commit 03873fd

Please sign in to comment.