@@ -272,9 +272,28 @@ pub async fn get_server_module_options_context(
272
272
..Default :: default ( )
273
273
} ) ;
274
274
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 ?;
278
297
let enable_webpack_loaders = webpack_rules. map ( |rules| {
279
298
WebpackLoadersOptions {
280
299
rules,
@@ -348,6 +367,12 @@ pub async fn get_server_module_options_context(
348
367
..Default :: default ( )
349
368
} ;
350
369
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
+
351
376
let internal_module_options_context = ModuleOptionsContext {
352
377
enable_typescript_transform : Some ( TypescriptTransformOptions :: default ( ) . cell ( ) ) ,
353
378
enable_jsx : Some ( JsxTransformOptions :: default ( ) . cell ( ) ) ,
@@ -365,7 +390,7 @@ pub async fn get_server_module_options_context(
365
390
rules : vec ! [
366
391
(
367
392
foreign_code_context_condition,
368
- module_options_context . clone ( ) . cell( ) ,
393
+ foreign_code_module_options_context . cell( ) ,
369
394
) ,
370
395
(
371
396
ContextCondition :: InPath ( next_js_fs( ) . root( ) ) ,
@@ -407,6 +432,11 @@ pub async fn get_server_module_options_context(
407
432
execution_context : Some ( execution_context) ,
408
433
..Default :: default ( )
409
434
} ;
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
+ } ;
410
440
let internal_module_options_context = ModuleOptionsContext {
411
441
enable_typescript_transform : Some ( TypescriptTransformOptions :: default ( ) . cell ( ) ) ,
412
442
custom_rules : internal_custom_rules,
@@ -423,7 +453,7 @@ pub async fn get_server_module_options_context(
423
453
rules : vec ! [
424
454
(
425
455
foreign_code_context_condition,
426
- module_options_context . clone ( ) . cell( ) ,
456
+ foreign_code_module_options_context . cell( ) ,
427
457
) ,
428
458
(
429
459
ContextCondition :: InPath ( next_js_fs( ) . root( ) ) ,
@@ -474,6 +504,11 @@ pub async fn get_server_module_options_context(
474
504
execution_context : Some ( execution_context) ,
475
505
..Default :: default ( )
476
506
} ;
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
+ } ;
477
512
let internal_module_options_context = ModuleOptionsContext {
478
513
enable_typescript_transform : Some ( TypescriptTransformOptions :: default ( ) . cell ( ) ) ,
479
514
custom_rules : internal_custom_rules,
@@ -489,7 +524,7 @@ pub async fn get_server_module_options_context(
489
524
rules : vec ! [
490
525
(
491
526
foreign_code_context_condition,
492
- module_options_context . clone ( ) . cell( ) ,
527
+ foreign_code_module_options_context . cell( ) ,
493
528
) ,
494
529
(
495
530
ContextCondition :: InPath ( next_js_fs( ) . root( ) ) ,
0 commit comments