Skip to content

Commit

Permalink
Print a warning when importing Sass files (vercel/turborepo#4789)
Browse files Browse the repository at this point in the history
### Description


![image](https://user-images.githubusercontent.com/1621758/235940115-a8958bbb-76df-4353-b3e7-231231960623.png)

### Testing Instructions

See [Next.js PR](#49151)

link WEB-981
  • Loading branch information
alexkirsz authored May 3, 2023
1 parent 42836c1 commit 8981a07
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
1 change: 1 addition & 0 deletions crates/turbopack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub mod rebase;
pub mod resolve;
pub mod resolve_options_context;
pub mod transition;
pub(crate) mod unsupported_sass;

pub use turbopack_css as css;
pub use turbopack_ecmascript as ecmascript;
Expand Down
10 changes: 8 additions & 2 deletions crates/turbopack/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ use turbopack_ecmascript::typescript::resolve::{
apply_tsconfig_resolve_options, tsconfig, tsconfig_resolve_options,
};

use crate::resolve_options_context::ResolveOptionsContextVc;
use crate::{
resolve_options_context::ResolveOptionsContextVc,
unsupported_sass::UnsupportedSassResolvePluginVc,
};

const NODE_EXTERNALS: [&str; 51] = [
"assert",
Expand Down Expand Up @@ -109,6 +112,9 @@ async fn base_resolve_options(
}
let import_map = import_map.cell();

let mut plugins = opt.plugins.clone();
plugins.push(UnsupportedSassResolvePluginVc::new(root).as_resolve_plugin());

Ok(ResolveOptions {
extensions: if let Some(environment) = emulating {
environment.resolve_extensions().await?.clone_value()
Expand Down Expand Up @@ -220,7 +226,7 @@ async fn base_resolve_options(
},
import_map: Some(import_map),
resolved_map: opt.resolved_map,
plugins: opt.plugins.clone(),
plugins,
..Default::default()
}
.into())
Expand Down
90 changes: 90 additions & 0 deletions crates/turbopack/src/unsupported_sass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//! TODO(WEB-741) Remove this file once Sass is supported.
use anyhow::Result;
use turbo_tasks::primitives::StringVc;
use turbo_tasks_fs::{glob::GlobVc, FileSystemPathVc};
use turbopack_core::{
issue::{Issue, IssueSeverity, IssueSeverityVc, IssueVc},
resolve::{
parse::RequestVc,
plugin::{ResolvePlugin, ResolvePluginConditionVc, ResolvePluginVc},
ResolveResultOptionVc,
},
};

/// Resolve plugins that warns when importing a sass file.
#[turbo_tasks::value]
pub(crate) struct UnsupportedSassResolvePlugin {
root: FileSystemPathVc,
}

#[turbo_tasks::value_impl]
impl UnsupportedSassResolvePluginVc {
#[turbo_tasks::function]
pub fn new(root: FileSystemPathVc) -> Self {
UnsupportedSassResolvePlugin { root }.cell()
}
}

#[turbo_tasks::value_impl]
impl ResolvePlugin for UnsupportedSassResolvePlugin {
#[turbo_tasks::function]
fn after_resolve_condition(&self) -> ResolvePluginConditionVc {
ResolvePluginConditionVc::new(self.root.root(), GlobVc::new("**/*.{sass,scss}"))
}

#[turbo_tasks::function]
async fn after_resolve(
&self,
fs_path: FileSystemPathVc,
context: FileSystemPathVc,
request: RequestVc,
) -> Result<ResolveResultOptionVc> {
let extension = fs_path.extension().await?;
if ["sass", "scss"].iter().any(|ext| ext == &*extension) {
UnsupportedSassModuleIssue { context, request }
.cell()
.as_issue()
.emit();
}

Ok(ResolveResultOptionVc::none())
}
}

#[turbo_tasks::value(shared)]
struct UnsupportedSassModuleIssue {
context: FileSystemPathVc,
request: RequestVc,
}

#[turbo_tasks::value_impl]
impl Issue for UnsupportedSassModuleIssue {
#[turbo_tasks::function]
fn severity(&self) -> IssueSeverityVc {
IssueSeverity::Warning.into()
}

#[turbo_tasks::function]
fn category(&self) -> StringVc {
StringVc::cell("resolve".to_string())
}

#[turbo_tasks::function]
async fn title(&self) -> Result<StringVc> {
Ok(StringVc::cell(format!(
"Unsupported Sass request: {}",
self.request.await?.request().as_deref().unwrap_or("N/A")
)))
}

#[turbo_tasks::function]
fn context(&self) -> FileSystemPathVc {
self.context
}

#[turbo_tasks::function]
fn description(&self) -> StringVc {
StringVc::cell("Turbopack does not yet support importing Sass modules.".to_string())
}
}

0 comments on commit 8981a07

Please sign in to comment.