Skip to content

Commit

Permalink
feat(turbopack): initial sass-loader support (vercel#49882)
Browse files Browse the repository at this point in the history
<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation or adding/fixing Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md



## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change



### Why?

### How?

Closes NEXT-
Fixes #

-->

### What?
WEB-654.

This PR attempts to implement initial sass support in turbopack, via
sass-loadder. Internally it points to sass-loader compiled in next.js
(`next/dist/sass-loader..`) with next.config.js's option. Still it is
not a complete implementation, for example sourceMap is not supported by
upstream turbopack.

PR requires turbopack side changes as well.
  • Loading branch information
kwonoj authored and hydRAnger committed Jun 12, 2023
1 parent 43b2cd3 commit 7a9c122
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 10 deletions.
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
6 changes: 4 additions & 2 deletions packages/next-swc/crates/next-core/src/next_client/context.rs
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
6 changes: 4 additions & 2 deletions packages/next-swc/crates/next-core/src/next_server/context.rs
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();

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.

Empty file.
Empty file.

0 comments on commit 7a9c122

Please sign in to comment.