Unverified Commit d4d78b57 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

[Turbopack] pass sourceMap flag to webpack loaders and postcss (#75144)

### What?

* pass sourceMap flag to webpack loaders
* run postcss transform without SourceMaps when specified
parent f556189b
...@@ -325,6 +325,14 @@ pub async fn get_client_module_options_context( ...@@ -325,6 +325,14 @@ pub async fn get_client_module_options_context(
}, },
..Default::default() ..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), preset_env_versions: Some(env),
execution_context: Some(execution_context), execution_context: Some(execution_context),
tree_shaking_mode: tree_shaking_mode_for_user_code, tree_shaking_mode: tree_shaking_mode_for_user_code,
......
...@@ -542,6 +542,11 @@ pub async fn get_server_module_options_context( ...@@ -542,6 +542,11 @@ pub async fn get_server_module_options_context(
}, },
execution_context: Some(execution_context), execution_context: Some(execution_context),
css: CssOptionsContext { css: CssOptionsContext {
source_maps: if *next_config.turbo_source_maps().await? {
SourceMapsType::Full
} else {
SourceMapsType::None
},
..Default::default() ..Default::default()
}, },
tree_shaking_mode: tree_shaking_mode_for_user_code, tree_shaking_mode: tree_shaking_mode_for_user_code,
......
...@@ -74,15 +74,16 @@ export const init = async (ipc: Ipc<IpcInfoMessage, IpcRequestMessage>) => { ...@@ -74,15 +74,16 @@ export const init = async (ipc: Ipc<IpcInfoMessage, IpcRequestMessage>) => {
export default async function transform( export default async function transform(
ipc: Ipc<IpcInfoMessage, IpcRequestMessage>, ipc: Ipc<IpcInfoMessage, IpcRequestMessage>,
cssContent: string, cssContent: string,
name: string name: string,
sourceMap: boolean
) { ) {
const { css, map, messages } = await processor!.process(cssContent, { const { css, map, messages } = await processor!.process(cssContent, {
from: name, from: name,
to: name, to: name,
map: { map: sourceMap ? {
inline: false, inline: false,
annotation: false, annotation: false,
}, } : undefined,
}); });
const assets = []; const assets = [];
...@@ -93,7 +94,9 @@ export default async function transform( ...@@ -93,7 +94,9 @@ export default async function transform(
file: msg.file, file: msg.file,
content: msg.content, content: msg.content,
sourceMap: sourceMap:
typeof msg.sourceMap === "string" !sourceMap
? undefined
: typeof msg.sourceMap === "string"
? msg.sourceMap ? msg.sourceMap
: JSON.stringify(msg.sourceMap), : JSON.stringify(msg.sourceMap),
// There is also an info field, which we currently ignore // There is also an info field, which we currently ignore
...@@ -133,7 +136,7 @@ export default async function transform( ...@@ -133,7 +136,7 @@ export default async function transform(
} }
return { return {
css, css,
map: JSON.stringify(map), map: sourceMap ? JSON.stringify(map) : undefined,
assets, assets,
}; };
} }
...@@ -184,7 +184,8 @@ const transform = ( ...@@ -184,7 +184,8 @@ const transform = (
content: string, content: string,
name: string, name: string,
query: string, query: string,
loaders: LoaderConfig[] loaders: LoaderConfig[],
sourceMap: boolean
) => { ) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const resource = pathResolve(contextDir, name); const resource = pathResolve(contextDir, name);
...@@ -205,6 +206,7 @@ const transform = ( ...@@ -205,6 +206,7 @@ const transform = (
}, },
currentTraceSpan: new DummySpan(), currentTraceSpan: new DummySpan(),
rootContext: contextDir, rootContext: contextDir,
sourceMap,
getOptions() { getOptions() {
const entry = this.loaders[this.loaderIndex]; const entry = this.loaders[this.loaderIndex];
return entry.options && typeof entry.options === "object" return entry.options && typeof entry.options === "object"
......
...@@ -108,6 +108,7 @@ pub struct PostCssTransform { ...@@ -108,6 +108,7 @@ pub struct PostCssTransform {
evaluate_context: ResolvedVc<Box<dyn AssetContext>>, evaluate_context: ResolvedVc<Box<dyn AssetContext>>,
execution_context: ResolvedVc<ExecutionContext>, execution_context: ResolvedVc<ExecutionContext>,
config_location: PostCssConfigLocation, config_location: PostCssConfigLocation,
source_maps: bool,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -117,11 +118,13 @@ impl PostCssTransform { ...@@ -117,11 +118,13 @@ impl PostCssTransform {
evaluate_context: ResolvedVc<Box<dyn AssetContext>>, evaluate_context: ResolvedVc<Box<dyn AssetContext>>,
execution_context: ResolvedVc<ExecutionContext>, execution_context: ResolvedVc<ExecutionContext>,
config_location: PostCssConfigLocation, config_location: PostCssConfigLocation,
source_maps: bool,
) -> Vc<Self> { ) -> Vc<Self> {
PostCssTransform { PostCssTransform {
evaluate_context, evaluate_context,
execution_context, execution_context,
config_location, config_location,
source_maps,
} }
.cell() .cell()
} }
...@@ -137,6 +140,7 @@ impl SourceTransform for PostCssTransform { ...@@ -137,6 +140,7 @@ impl SourceTransform for PostCssTransform {
execution_context: self.execution_context, execution_context: self.execution_context,
config_location: self.config_location, config_location: self.config_location,
source, source,
source_map: self.source_maps,
} }
.cell(), .cell(),
) )
...@@ -149,6 +153,7 @@ struct PostCssTransformedAsset { ...@@ -149,6 +153,7 @@ struct PostCssTransformedAsset {
execution_context: ResolvedVc<ExecutionContext>, execution_context: ResolvedVc<ExecutionContext>,
config_location: PostCssConfigLocation, config_location: PostCssConfigLocation,
source: ResolvedVc<Box<dyn Source>>, source: ResolvedVc<Box<dyn Source>>,
source_map: bool,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -506,6 +511,7 @@ impl PostCssTransformedAsset { ...@@ -506,6 +511,7 @@ impl PostCssTransformedAsset {
}; };
let content = content.content().to_str()?; let content = content.content().to_str()?;
let evaluate_context = self.evaluate_context; let evaluate_context = self.evaluate_context;
let source_map = self.source_map;
// This invalidates the transform when the config changes. // This invalidates the transform when the config changes.
let config_changed = config_changed(*evaluate_context, config_path) let config_changed = config_changed(*evaluate_context, config_path)
...@@ -541,6 +547,7 @@ impl PostCssTransformedAsset { ...@@ -541,6 +547,7 @@ impl PostCssTransformedAsset {
args: vec![ args: vec![
ResolvedVc::cell(content.into()), ResolvedVc::cell(content.into()),
ResolvedVc::cell(css_path.into()), ResolvedVc::cell(css_path.into()),
ResolvedVc::cell(source_map.into()),
], ],
additional_invalidation: config_changed, additional_invalidation: config_changed,
}) })
......
...@@ -94,6 +94,7 @@ pub struct WebpackLoaders { ...@@ -94,6 +94,7 @@ pub struct WebpackLoaders {
loaders: ResolvedVc<WebpackLoaderItems>, loaders: ResolvedVc<WebpackLoaderItems>,
rename_as: Option<RcStr>, rename_as: Option<RcStr>,
resolve_options_context: ResolvedVc<ResolveOptionsContext>, resolve_options_context: ResolvedVc<ResolveOptionsContext>,
source_maps: bool,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -105,6 +106,7 @@ impl WebpackLoaders { ...@@ -105,6 +106,7 @@ impl WebpackLoaders {
loaders: ResolvedVc<WebpackLoaderItems>, loaders: ResolvedVc<WebpackLoaderItems>,
rename_as: Option<RcStr>, rename_as: Option<RcStr>,
resolve_options_context: ResolvedVc<ResolveOptionsContext>, resolve_options_context: ResolvedVc<ResolveOptionsContext>,
source_maps: bool,
) -> Vc<Self> { ) -> Vc<Self> {
WebpackLoaders { WebpackLoaders {
evaluate_context, evaluate_context,
...@@ -112,6 +114,7 @@ impl WebpackLoaders { ...@@ -112,6 +114,7 @@ impl WebpackLoaders {
loaders, loaders,
rename_as, rename_as,
resolve_options_context, resolve_options_context,
source_maps,
} }
.cell() .cell()
} }
...@@ -250,6 +253,7 @@ impl WebpackLoadersProcessedAsset { ...@@ -250,6 +253,7 @@ impl WebpackLoadersProcessedAsset {
ResolvedVc::cell(resource_path.to_string().into()), ResolvedVc::cell(resource_path.to_string().into()),
ResolvedVc::cell(this.source.ident().query().await?.to_string().into()), ResolvedVc::cell(this.source.ident().query().await?.to_string().into()),
ResolvedVc::cell(json!(*loaders)), ResolvedVc::cell(json!(*loaders)),
ResolvedVc::cell(transform.source_maps.into()),
], ],
additional_invalidation: Completion::immutable().to_resolved().await?, additional_invalidation: Completion::immutable().to_resolved().await?,
}) })
...@@ -270,7 +274,9 @@ impl WebpackLoadersProcessedAsset { ...@@ -270,7 +274,9 @@ impl WebpackLoadersProcessedAsset {
.context("Unable to deserializate response from webpack loaders transform operation")?; .context("Unable to deserializate response from webpack loaders transform operation")?;
// handle SourceMap // handle SourceMap
let source_map = if let Some(source_map) = processed.map { let source_map = if !transform.source_maps {
None
} else if let Some(source_map) = processed.map {
SourceMap::new_from_file_content(FileContent::Content(File::from(source_map)).cell()) SourceMap::new_from_file_content(FileContent::Content(File::from(source_map)).cell())
.await? .await?
.map(|source_map| source_map.resolved_cell()) .map(|source_map| source_map.resolved_cell())
......
...@@ -688,8 +688,7 @@ async fn externals_tracing_module_context(ty: ExternalType) -> Result<Vc<ModuleA ...@@ -688,8 +688,7 @@ async fn externals_tracing_module_context(ty: ExternalType) -> Result<Vc<ModuleA
..Default::default() ..Default::default()
}, },
css: CssOptionsContext { css: CssOptionsContext {
// TODO add source maps handling for css source_maps: SourceMapsType::None,
// source_maps: SourceMapsType::None,
..Default::default() ..Default::default()
}, },
..Default::default() ..Default::default()
......
...@@ -85,7 +85,12 @@ impl ModuleOptions { ...@@ -85,7 +85,12 @@ impl ModuleOptions {
}, },
enable_mdx, enable_mdx,
enable_mdx_rs, enable_mdx_rs,
css: CssOptionsContext { enable_raw_css, .. }, css:
CssOptionsContext {
enable_raw_css,
source_maps: css_source_maps,
..
},
ref enable_postcss_transform, ref enable_postcss_transform,
ref enable_webpack_loaders, ref enable_webpack_loaders,
preset_env_versions, preset_env_versions,
...@@ -426,6 +431,7 @@ impl ModuleOptions { ...@@ -426,6 +431,7 @@ impl ModuleOptions {
), ),
*execution_context, *execution_context,
options.config_location, options.config_location,
matches!(css_source_maps, SourceMapsType::Full),
) )
.to_resolved() .to_resolved()
.await?, .await?,
...@@ -578,6 +584,7 @@ impl ModuleOptions { ...@@ -578,6 +584,7 @@ impl ModuleOptions {
*rule.loaders, *rule.loaders,
rule.rename_as.clone(), rule.rename_as.clone(),
resolve_options_context, resolve_options_context,
matches!(ecmascript_source_maps, SourceMapsType::Full),
) )
.to_resolved() .to_resolved()
.await?, .await?,
......
...@@ -181,9 +181,9 @@ pub struct CssOptionsContext { ...@@ -181,9 +181,9 @@ pub struct CssOptionsContext {
pub minify_type: MinifyType, pub minify_type: MinifyType,
// TODO add source maps handling for css /// Specifies how Source Maps are handled.
// Specifies how Source Maps are handled. pub source_maps: SourceMapsType,
// pub source_maps: SourceMapsType,
pub placeholder_for_future_extensions: (), pub placeholder_for_future_extensions: (),
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment