@@ -372,6 +372,16 @@ describe('Loader hooks', { concurrency: true }, () => {
372
372
} ) ;
373
373
374
374
describe ( 'globalPreload' , ( ) => {
375
+ it ( 'should emit deprecation warning' , async ( ) => {
376
+ const { stderr } = await spawnPromisified ( execPath , [
377
+ '--experimental-loader' ,
378
+ 'data:text/javascript,export function globalPreload(){}' ,
379
+ fixtures . path ( 'empty.js' ) ,
380
+ ] ) ;
381
+
382
+ assert . match ( stderr , / ` g l o b a l P r e l o a d ` w i l l b e r e m o v e d / ) ;
383
+ } ) ;
384
+
375
385
it ( 'should handle globalPreload returning undefined' , async ( ) => {
376
386
const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
377
387
'--no-warnings' ,
@@ -501,4 +511,164 @@ describe('Loader hooks', { concurrency: true }, () => {
501
511
assert . strictEqual ( code , 0 ) ;
502
512
assert . strictEqual ( signal , null ) ;
503
513
} ) ;
514
+
515
+ it ( 'should invoke `initialize` correctly' , async ( ) => {
516
+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
517
+ '--no-warnings' ,
518
+ '--experimental-loader' ,
519
+ fixtures . fileURL ( '/es-module-loaders/hooks-initialize.mjs' ) ,
520
+ '--input-type=module' ,
521
+ '--eval' ,
522
+ 'import os from "node:os";' ,
523
+ ] ) ;
524
+
525
+ const lines = stdout . trim ( ) . split ( '\n' ) ;
526
+
527
+ assert . strictEqual ( lines . length , 1 ) ;
528
+ assert . strictEqual ( lines [ 0 ] , 'hooks initialize 1' ) ;
529
+
530
+ assert . strictEqual ( stderr , '' ) ;
531
+
532
+ assert . strictEqual ( code , 0 ) ;
533
+ assert . strictEqual ( signal , null ) ;
534
+ } ) ;
535
+
536
+ it ( 'should allow communicating with loader via `register` ports' , async ( ) => {
537
+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
538
+ '--no-warnings' ,
539
+ '--input-type=module' ,
540
+ '--eval' ,
541
+ `
542
+ import {MessageChannel} from 'node:worker_threads';
543
+ import {register} from 'node:module';
544
+ const {port1, port2} = new MessageChannel();
545
+ port1.on('message', (msg) => {
546
+ console.log('message', msg);
547
+ });
548
+ const result = register(
549
+ ${ JSON . stringify ( fixtures . fileURL ( '/es-module-loaders/hooks-initialize-port.mjs' ) ) } ,
550
+ {data: port2, transferList: [port2]},
551
+ );
552
+ console.log('register', result);
553
+
554
+ await import('node:os');
555
+ port1.close();
556
+ ` ,
557
+ ] ) ;
558
+
559
+ const lines = stdout . split ( '\n' ) ;
560
+
561
+ assert . strictEqual ( lines [ 0 ] , 'register ok' ) ;
562
+ assert . strictEqual ( lines [ 1 ] , 'message initialize' ) ;
563
+ assert . strictEqual ( lines [ 2 ] , 'message resolve node:os' ) ;
564
+
565
+ assert . strictEqual ( stderr , '' ) ;
566
+
567
+ assert . strictEqual ( code , 0 ) ;
568
+ assert . strictEqual ( signal , null ) ;
569
+ } ) ;
570
+
571
+ it ( 'should have `register` work with cjs' , async ( ) => {
572
+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
573
+ '--no-warnings' ,
574
+ '--input-type=commonjs' ,
575
+ '--eval' ,
576
+ `
577
+ const {register} = require('node:module');
578
+ register(
579
+ ${ JSON . stringify ( fixtures . fileURL ( '/es-module-loaders/hooks-initialize.mjs' ) ) } ,
580
+ );
581
+ register(
582
+ ${ JSON . stringify ( fixtures . fileURL ( '/es-module-loaders/loader-load-foo-or-42.mjs' ) ) } ,
583
+ );
584
+
585
+ import('node:os').then((result) => {
586
+ console.log(result.default);
587
+ });
588
+ ` ,
589
+ ] ) ;
590
+
591
+ const lines = stdout . split ( '\n' ) ;
592
+
593
+ assert . strictEqual ( lines [ 0 ] , 'hooks initialize 1' ) ;
594
+ assert . strictEqual ( lines [ 1 ] , 'foo' ) ;
595
+
596
+ assert . strictEqual ( stderr , '' ) ;
597
+
598
+ assert . strictEqual ( code , 0 ) ;
599
+ assert . strictEqual ( signal , null ) ;
600
+ } ) ;
601
+
602
+ it ( '`register` should work with `require`' , async ( ) => {
603
+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
604
+ '--no-warnings' ,
605
+ '--require' ,
606
+ fixtures . path ( '/es-module-loaders/register-loader.cjs' ) ,
607
+ '--input-type=module' ,
608
+ '--eval' ,
609
+ 'import "node:os";' ,
610
+ ] ) ;
611
+
612
+ const lines = stdout . split ( '\n' ) ;
613
+
614
+ assert . strictEqual ( lines [ 0 ] , 'resolve passthru' ) ;
615
+
616
+ assert . strictEqual ( stderr , '' ) ;
617
+
618
+ assert . strictEqual ( code , 0 ) ;
619
+ assert . strictEqual ( signal , null ) ;
620
+ } ) ;
621
+
622
+ it ( '`register` should work with `import`' , async ( ) => {
623
+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
624
+ '--no-warnings' ,
625
+ '--import' ,
626
+ fixtures . fileURL ( '/es-module-loaders/register-loader.mjs' ) ,
627
+ '--input-type=module' ,
628
+ '--eval' ,
629
+ `
630
+ import 'node:os';
631
+ ` ,
632
+ ] ) ;
633
+
634
+ const lines = stdout . split ( '\n' ) ;
635
+
636
+ assert . strictEqual ( lines [ 0 ] , 'resolve passthru' ) ;
637
+
638
+ assert . strictEqual ( stderr , '' ) ;
639
+
640
+ assert . strictEqual ( code , 0 ) ;
641
+ assert . strictEqual ( signal , null ) ;
642
+ } ) ;
643
+
644
+ it ( 'should execute `initialize` in sequence' , async ( ) => {
645
+ const { code, signal, stdout, stderr } = await spawnPromisified ( execPath , [
646
+ '--no-warnings' ,
647
+ '--input-type=module' ,
648
+ '--eval' ,
649
+ `
650
+ import {register} from 'node:module';
651
+ console.log('result', register(
652
+ ${ JSON . stringify ( fixtures . fileURL ( '/es-module-loaders/hooks-initialize.mjs' ) ) }
653
+ ));
654
+ console.log('result', register(
655
+ ${ JSON . stringify ( fixtures . fileURL ( '/es-module-loaders/hooks-initialize.mjs' ) ) }
656
+ ));
657
+
658
+ await import('node:os');
659
+ ` ,
660
+ ] ) ;
661
+
662
+ const lines = stdout . split ( '\n' ) ;
663
+
664
+ assert . strictEqual ( lines [ 0 ] , 'result 1' ) ;
665
+ assert . strictEqual ( lines [ 1 ] , 'result 2' ) ;
666
+ assert . strictEqual ( lines [ 2 ] , 'hooks initialize 1' ) ;
667
+ assert . strictEqual ( lines [ 3 ] , 'hooks initialize 2' ) ;
668
+
669
+ assert . strictEqual ( stderr , '' ) ;
670
+
671
+ assert . strictEqual ( code , 0 ) ;
672
+ assert . strictEqual ( signal , null ) ;
673
+ } ) ;
504
674
} ) ;
0 commit comments