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

feat(turbopack): initial sass-loader support #49882

Merged
merged 4 commits into from
May 20, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/next-swc/crates/next-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub mod pages_structure;
pub mod router;
pub mod router_source;
mod runtime;
mod sass;
mod transform_options;
pub mod url_node;
mod util;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ use crate::{
styled_jsx::get_styled_jsx_transform_plugin,
},
},
sass::maybe_add_sass_loader,
transform_options::{
get_decorators_transform_options, get_jsx_transform_options,
get_typescript_transform_options,
Expand Down Expand Up @@ -201,11 +202,12 @@ pub async fn get_client_module_options_context(
loader_runner_package: Some(get_external_next_compiled_package_mapping(
StringVc::cell("loader-runner".to_owned()),
)),
placeholder_for_future_extensions: (),
..Default::default()
}
.cell();

maybe_add_babel_loader(project_path, loaders_options)
let loaders_options = maybe_add_babel_loader(project_path, loaders_options);
maybe_add_sass_loader(next_config.sass_config(), loaders_options)
.await?
.clone_if()
};
Expand Down
11 changes: 9 additions & 2 deletions packages/next-swc/crates/next-core/src/next_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use turbo_tasks::{
primitives::{BoolVc, StringsVc},
primitives::{BoolVc, JsonValueVc, StringsVc},
trace::TraceRawVcs,
CompletionVc, Value,
};
Expand Down Expand Up @@ -59,6 +59,7 @@ pub struct NextConfig {
pub rewrites: Rewrites,
pub transpile_packages: Option<Vec<String>>,
pub modularize_imports: Option<IndexMap<String, ModularizeImportPackageConfig>>,
sass_options: Option<serde_json::Value>,

// Partially supported
pub compiler: Option<CompilerConfig>,
Expand Down Expand Up @@ -92,7 +93,6 @@ pub struct NextConfig {
production_browser_source_maps: bool,
public_runtime_config: IndexMap<String, serde_json::Value>,
redirects: Vec<Redirect>,
sass_options: IndexMap<String, serde_json::Value>,
server_runtime_config: IndexMap<String, serde_json::Value>,
static_page_generation_timeout: f64,
swc_minify: bool,
Expand Down Expand Up @@ -570,6 +570,13 @@ impl NextConfigVc {
self.await?.experimental.mdx_rs.unwrap_or(false),
))
}

#[turbo_tasks::function]
pub async fn sass_config(self) -> Result<JsonValueVc> {
Ok(JsonValueVc::cell(
self.await?.sass_options.clone().unwrap_or_default(),
))
}
}

fn next_configs() -> StringsVc {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ use crate::{
styled_jsx::get_styled_jsx_transform_plugin,
},
},
sass::maybe_add_sass_loader,
transform_options::{
get_decorators_transform_options, get_jsx_transform_options,
get_typescript_transform_options,
Expand Down Expand Up @@ -281,11 +282,12 @@ pub async fn get_server_module_options_context(
loader_runner_package: Some(get_external_next_compiled_package_mapping(
StringVc::cell("loader-runner".to_owned()),
)),
placeholder_for_future_extensions: (),
..Default::default()
}
.cell();

maybe_add_babel_loader(project_path, loaders_options)
let loaders_options = maybe_add_babel_loader(project_path, loaders_options);
maybe_add_sass_loader(next_config.sass_config(), loaders_options)
.await?
.clone_if()
};
Expand Down
42 changes: 42 additions & 0 deletions packages/next-swc/crates/next-core/src/sass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use anyhow::Result;
use turbo_tasks::primitives::JsonValueVc;
use turbopack_binding::turbopack::{
node::transforms::webpack::{WebpackLoaderConfigItem, WebpackLoaderConfigItemsVc},
turbopack::module_options::WebpackLoadersOptionsVc,
};

#[turbo_tasks::function]
pub async fn maybe_add_sass_loader(
sass_options: JsonValueVc,
webpack_options: WebpackLoadersOptionsVc,
) -> Result<WebpackLoadersOptionsVc> {
let mut options = (*webpack_options.await?).clone();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let mut options = (*webpack_options.await?).clone();
let mut options = webpack_options.await?.clone_value();


let sass_options = sass_options.await?.as_object().unwrap().clone();
for ext in [".scss", ".sass"] {
let loader = WebpackLoaderConfigItem::LoaderNameWithOptions {
loader: "next/dist/compiled/sass-loader".to_string(),
options: serde_json::json!({
//https://github.com/vercel/turbo/blob/d527eb54be384a4658243304cecd547d09c05c6b/crates/turbopack-node/src/transforms/webpack.rs#L191
"sourceMap": false,
"sassOptions": sass_options,
})
.as_object()
.unwrap()
.clone(),
};

options.extension_to_loaders.insert(
ext.to_owned(),
if options.extension_to_loaders.contains_key(ext) {
let mut new_configs = (*(options.extension_to_loaders[ext].await?)).clone();
new_configs.push(loader);
WebpackLoaderConfigItemsVc::cell(new_configs)
} else {
WebpackLoaderConfigItemsVc::cell(vec![loader])
},
);
}

Ok(options.cell())
}

This file was deleted.