Skip to content

Commit

Permalink
fix(next-core): allow sass loader for foreign codes (#56679)
Browse files Browse the repository at this point in the history
### What?

- closes #55785

Similar to #56539, next.config's sass applies webpack loaders to node_modules implicitly and this PR mimics those for the turbopack.


Closes WEB-1753
  • Loading branch information
kwonoj committed Oct 10, 2023
1 parent c8b2d1b commit d79f8a2
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 11 deletions.
35 changes: 30 additions & 5 deletions packages/next-swc/crates/next-core/src/next_client/context.rs
Expand Up @@ -212,9 +212,29 @@ pub async fn get_client_module_options_context(
false,
next_config,
);
let webpack_rules =
*maybe_add_babel_loader(project_path, *next_config.webpack_rules().await?).await?;
let webpack_rules = maybe_add_sass_loader(next_config.sass_config(), webpack_rules).await?;

// A separate webpack rules will be applied to codes matching
// foreign_code_context_condition. This allows to import codes from
// node_modules that requires webpack loaders, which next-dev implicitly
// does by default.
let foreign_webpack_rules = maybe_add_sass_loader(
next_config.sass_config(),
*next_config.webpack_rules().await?,
)
.await?;
let foreign_webpack_loaders = foreign_webpack_rules.map(|rules| {
WebpackLoadersOptions {
rules,
loader_runner_package: Some(get_external_next_compiled_package_mapping(Vc::cell(
"loader-runner".to_owned(),
))),
}
.cell()
});

// Now creates a webpack rules that applies to all codes.
let webpack_rules = *foreign_webpack_rules.clone();
let webpack_rules = *maybe_add_babel_loader(project_path, webpack_rules).await?;
let enable_webpack_loaders = webpack_rules.map(|rules| {
WebpackLoadersOptions {
rules,
Expand Down Expand Up @@ -252,9 +272,14 @@ pub async fn get_client_module_options_context(
preset_env_versions: Some(env),
execution_context: Some(execution_context),
custom_ecma_transform_plugins,
..Default::default()
};

let foreign_codes_options_context = ModuleOptionsContext {
enable_webpack_loaders: foreign_webpack_loaders,
// NOTE(WEB-1016) PostCSS transforms should also apply to foreign code.
enable_postcss_transform: postcss_transform_options.clone(),
..Default::default()
..module_options_context.clone()
};

let module_options_context = ModuleOptionsContext {
Expand All @@ -270,7 +295,7 @@ pub async fn get_client_module_options_context(
rules: vec![
(
foreign_code_context_condition(next_config, project_path).await?,
module_options_context.clone().cell(),
foreign_codes_options_context.cell(),
),
// If the module is an internal asset (i.e overlay, fallback) coming from the embedded
// FS, don't apply user defined transforms.
Expand Down
47 changes: 41 additions & 6 deletions packages/next-swc/crates/next-core/src/next_server/context.rs
Expand Up @@ -272,9 +272,28 @@ pub async fn get_server_module_options_context(
..Default::default()
});

let webpack_rules =
*maybe_add_babel_loader(project_path, *next_config.webpack_rules().await?).await?;
let webpack_rules = maybe_add_sass_loader(next_config.sass_config(), webpack_rules).await?;
// A separate webpack rules will be applied to codes matching
// foreign_code_context_condition. This allows to import codes from
// node_modules that requires webpack loaders, which next-dev implicitly
// does by default.
let foreign_webpack_rules = maybe_add_sass_loader(
next_config.sass_config(),
*next_config.webpack_rules().await?,
)
.await?;
let foreign_webpack_loaders = foreign_webpack_rules.map(|rules| {
WebpackLoadersOptions {
rules,
loader_runner_package: Some(get_external_next_compiled_package_mapping(Vc::cell(
"loader-runner".to_owned(),
))),
}
.cell()
});

// Now creates a webpack rules that applies to all codes.
let webpack_rules = *foreign_webpack_rules.clone();
let webpack_rules = *maybe_add_babel_loader(project_path, webpack_rules).await?;
let enable_webpack_loaders = webpack_rules.map(|rules| {
WebpackLoadersOptions {
rules,
Expand Down Expand Up @@ -348,6 +367,12 @@ pub async fn get_server_module_options_context(
..Default::default()
};

let foreign_code_module_options_context = ModuleOptionsContext {
custom_rules: internal_custom_rules.clone(),
enable_webpack_loaders: foreign_webpack_loaders,
..module_options_context.clone()
};

let internal_module_options_context = ModuleOptionsContext {
enable_typescript_transform: Some(TypescriptTransformOptions::default().cell()),
enable_jsx: Some(JsxTransformOptions::default().cell()),
Expand All @@ -365,7 +390,7 @@ pub async fn get_server_module_options_context(
rules: vec![
(
foreign_code_context_condition,
module_options_context.clone().cell(),
foreign_code_module_options_context.cell(),
),
(
ContextCondition::InPath(next_js_fs().root()),
Expand Down Expand Up @@ -407,6 +432,11 @@ pub async fn get_server_module_options_context(
execution_context: Some(execution_context),
..Default::default()
};
let foreign_code_module_options_context = ModuleOptionsContext {
custom_rules: internal_custom_rules.clone(),
enable_webpack_loaders: foreign_webpack_loaders,
..module_options_context.clone()
};
let internal_module_options_context = ModuleOptionsContext {
enable_typescript_transform: Some(TypescriptTransformOptions::default().cell()),
custom_rules: internal_custom_rules,
Expand All @@ -423,7 +453,7 @@ pub async fn get_server_module_options_context(
rules: vec![
(
foreign_code_context_condition,
module_options_context.clone().cell(),
foreign_code_module_options_context.cell(),
),
(
ContextCondition::InPath(next_js_fs().root()),
Expand Down Expand Up @@ -474,6 +504,11 @@ pub async fn get_server_module_options_context(
execution_context: Some(execution_context),
..Default::default()
};
let foreign_code_module_options_context = ModuleOptionsContext {
custom_rules: internal_custom_rules.clone(),
enable_webpack_loaders: foreign_webpack_loaders,
..module_options_context.clone()
};
let internal_module_options_context = ModuleOptionsContext {
enable_typescript_transform: Some(TypescriptTransformOptions::default().cell()),
custom_rules: internal_custom_rules,
Expand All @@ -489,7 +524,7 @@ pub async fn get_server_module_options_context(
rules: vec![
(
foreign_code_context_condition,
module_options_context.clone().cell(),
foreign_code_module_options_context.cell(),
),
(
ContextCondition::InPath(next_js_fs().root()),
Expand Down

0 comments on commit d79f8a2

Please sign in to comment.