Skip to content

Commit b703c10

Browse files
committed
fix(next-core): allow sass loader for foreign codes
1 parent c8b2d1b commit b703c10

File tree

2 files changed

+71
-11
lines changed

2 files changed

+71
-11
lines changed

packages/next-swc/crates/next-core/src/next_client/context.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,29 @@ pub async fn get_client_module_options_context(
212212
false,
213213
next_config,
214214
);
215-
let webpack_rules =
216-
*maybe_add_babel_loader(project_path, *next_config.webpack_rules().await?).await?;
217-
let webpack_rules = maybe_add_sass_loader(next_config.sass_config(), webpack_rules).await?;
215+
216+
// A separate webpack rules will be applied to codes matching
217+
// foreign_code_context_condition. This allows to import codes from
218+
// node_modules that requires webpack loaders, which next-dev implicitly
219+
// does by default.
220+
let foreign_webpack_rules = maybe_add_sass_loader(
221+
next_config.sass_config(),
222+
*next_config.webpack_rules().await?,
223+
)
224+
.await?;
225+
let foreign_webpack_loaders = foreign_webpack_rules.map(|rules| {
226+
WebpackLoadersOptions {
227+
rules,
228+
loader_runner_package: Some(get_external_next_compiled_package_mapping(Vc::cell(
229+
"loader-runner".to_owned(),
230+
))),
231+
}
232+
.cell()
233+
});
234+
235+
// Now creates a webpack rules that applies to all codes.
236+
let webpack_rules = *foreign_webpack_rules.clone();
237+
let webpack_rules = *maybe_add_babel_loader(project_path, webpack_rules).await?;
218238
let enable_webpack_loaders = webpack_rules.map(|rules| {
219239
WebpackLoadersOptions {
220240
rules,
@@ -252,9 +272,14 @@ pub async fn get_client_module_options_context(
252272
preset_env_versions: Some(env),
253273
execution_context: Some(execution_context),
254274
custom_ecma_transform_plugins,
275+
..Default::default()
276+
};
277+
278+
let foreign_codes_options_context = ModuleOptionsContext {
279+
enable_webpack_loaders: foreign_webpack_loaders,
255280
// NOTE(WEB-1016) PostCSS transforms should also apply to foreign code.
256281
enable_postcss_transform: postcss_transform_options.clone(),
257-
..Default::default()
282+
..module_options_context.clone()
258283
};
259284

260285
let module_options_context = ModuleOptionsContext {
@@ -270,7 +295,7 @@ pub async fn get_client_module_options_context(
270295
rules: vec![
271296
(
272297
foreign_code_context_condition(next_config, project_path).await?,
273-
module_options_context.clone().cell(),
298+
foreign_codes_options_context.cell(),
274299
),
275300
// If the module is an internal asset (i.e overlay, fallback) coming from the embedded
276301
// FS, don't apply user defined transforms.

packages/next-swc/crates/next-core/src/next_server/context.rs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,28 @@ pub async fn get_server_module_options_context(
272272
..Default::default()
273273
});
274274

275-
let webpack_rules =
276-
*maybe_add_babel_loader(project_path, *next_config.webpack_rules().await?).await?;
277-
let webpack_rules = maybe_add_sass_loader(next_config.sass_config(), webpack_rules).await?;
275+
// A separate webpack rules will be applied to codes matching
276+
// foreign_code_context_condition. This allows to import codes from
277+
// node_modules that requires webpack loaders, which next-dev implicitly
278+
// does by default.
279+
let foreign_webpack_rules = maybe_add_sass_loader(
280+
next_config.sass_config(),
281+
*next_config.webpack_rules().await?,
282+
)
283+
.await?;
284+
let foreign_webpack_loaders = foreign_webpack_rules.map(|rules| {
285+
WebpackLoadersOptions {
286+
rules,
287+
loader_runner_package: Some(get_external_next_compiled_package_mapping(Vc::cell(
288+
"loader-runner".to_owned(),
289+
))),
290+
}
291+
.cell()
292+
});
293+
294+
// Now creates a webpack rules that applies to all codes.
295+
let webpack_rules = *foreign_webpack_rules.clone();
296+
let webpack_rules = *maybe_add_babel_loader(project_path, webpack_rules).await?;
278297
let enable_webpack_loaders = webpack_rules.map(|rules| {
279298
WebpackLoadersOptions {
280299
rules,
@@ -348,6 +367,12 @@ pub async fn get_server_module_options_context(
348367
..Default::default()
349368
};
350369

370+
let foreign_code_module_options_context = ModuleOptionsContext {
371+
custom_rules: internal_custom_rules.clone(),
372+
enable_webpack_loaders: foreign_webpack_loaders,
373+
..module_options_context.clone()
374+
};
375+
351376
let internal_module_options_context = ModuleOptionsContext {
352377
enable_typescript_transform: Some(TypescriptTransformOptions::default().cell()),
353378
enable_jsx: Some(JsxTransformOptions::default().cell()),
@@ -365,7 +390,7 @@ pub async fn get_server_module_options_context(
365390
rules: vec![
366391
(
367392
foreign_code_context_condition,
368-
module_options_context.clone().cell(),
393+
foreign_code_module_options_context.cell(),
369394
),
370395
(
371396
ContextCondition::InPath(next_js_fs().root()),
@@ -407,6 +432,11 @@ pub async fn get_server_module_options_context(
407432
execution_context: Some(execution_context),
408433
..Default::default()
409434
};
435+
let foreign_code_module_options_context = ModuleOptionsContext {
436+
custom_rules: internal_custom_rules.clone(),
437+
enable_webpack_loaders: foreign_webpack_loaders,
438+
..module_options_context.clone()
439+
};
410440
let internal_module_options_context = ModuleOptionsContext {
411441
enable_typescript_transform: Some(TypescriptTransformOptions::default().cell()),
412442
custom_rules: internal_custom_rules,
@@ -423,7 +453,7 @@ pub async fn get_server_module_options_context(
423453
rules: vec![
424454
(
425455
foreign_code_context_condition,
426-
module_options_context.clone().cell(),
456+
foreign_code_module_options_context.cell(),
427457
),
428458
(
429459
ContextCondition::InPath(next_js_fs().root()),
@@ -474,6 +504,11 @@ pub async fn get_server_module_options_context(
474504
execution_context: Some(execution_context),
475505
..Default::default()
476506
};
507+
let foreign_code_module_options_context = ModuleOptionsContext {
508+
custom_rules: internal_custom_rules.clone(),
509+
enable_webpack_loaders: foreign_webpack_loaders,
510+
..module_options_context.clone()
511+
};
477512
let internal_module_options_context = ModuleOptionsContext {
478513
enable_typescript_transform: Some(TypescriptTransformOptions::default().cell()),
479514
custom_rules: internal_custom_rules,
@@ -489,7 +524,7 @@ pub async fn get_server_module_options_context(
489524
rules: vec![
490525
(
491526
foreign_code_context_condition,
492-
module_options_context.clone().cell(),
527+
foreign_code_module_options_context.cell(),
493528
),
494529
(
495530
ContextCondition::InPath(next_js_fs().root()),

0 commit comments

Comments
 (0)