@@ -7,10 +7,11 @@ import { createPackageJson } from '../../fixtures.js';
77
88export  default  testSuite ( ( {  describe } ,  nodePath : string )  =>  { 
99	describe ( 'native modules' ,  ( {  test } )  =>  { 
10- 		test ( 'copies .node files to natives directory' ,  async  ( )  =>  { 
10+ 		test ( 'ESM:  copies .node files to natives directory' ,  async  ( )  =>  { 
1111			await  using fixture  =  await  createFixture ( { 
1212				'package.json' : createPackageJson ( { 
13- 					main : './dist/index.js' , 
13+ 					type : 'module' , 
14+ 					main : './dist/index.mjs' , 
1415				} ) , 
1516				'src/index.js' : ` 
1617					import native from './native.node'; 
@@ -42,9 +43,54 @@ export default testSuite(({ describe }, nodePath: string) => {
4243			const  files  =  await  fs . readdir ( path . join ( fixture . path ,  'dist/natives' ) ) ; 
4344			expect ( files . some ( file  =>  file . endsWith ( '.node' ) ) ) . toBe ( true ) ; 
4445
45- 			// Check that import was rewritten 
46- 			const  content  =  await  fixture . readFile ( 'dist/index.js ' ,  'utf8' ) ; 
46+ 			// Check that import was rewritten and uses createRequire for ESM  
47+ 			const  content  =  await  fixture . readFile ( 'dist/index.mjs ' ,  'utf8' ) ; 
4748			expect ( content ) . toMatch ( './natives' ) ; 
49+ 			expect ( content ) . toMatch ( 'createRequire' ) ; 
50+ 		} ) ; 
51+ 
52+ 		test ( 'CJS: copies .node files to natives directory' ,  async  ( )  =>  { 
53+ 			await  using fixture  =  await  createFixture ( { 
54+ 				'package.json' : createPackageJson ( { 
55+ 					type : 'commonjs' , 
56+ 					main : './dist/index.cjs' , 
57+ 				} ) , 
58+ 				'src/index.js' : ` 
59+ 					import native from './native.node'; 
60+ 					console.log(native); 
61+ 				` , 
62+ 			} ) ; 
63+ 
64+ 			// Create a dummy .node file after fixture is created 
65+ 			await  fs . writeFile ( 
66+ 				path . join ( fixture . path ,  'src/native.node' ) , 
67+ 				Buffer . from ( 'dummy native module' ) , 
68+ 			) ; 
69+ 
70+ 			const  pkgrollProcess  =  await  pkgroll ( [ ] ,  { 
71+ 				cwd : fixture . path , 
72+ 				nodePath, 
73+ 			} ) ; 
74+ 
75+ 			expect ( pkgrollProcess . exitCode ) . toBe ( 0 ) ; 
76+ 			expect ( pkgrollProcess . stderr ) . toBe ( '' ) ; 
77+ 
78+ 			// Check that natives directory was created 
79+ 			const  nativesExists  =  await  fs . access ( path . join ( fixture . path ,  'dist/natives' ) ) 
80+ 				. then ( ( )  =>  true ) 
81+ 				. catch ( ( )  =>  false ) ; 
82+ 			expect ( nativesExists ) . toBe ( true ) ; 
83+ 
84+ 			// Check that .node file was copied 
85+ 			const  files  =  await  fs . readdir ( path . join ( fixture . path ,  'dist/natives' ) ) ; 
86+ 			expect ( files . some ( file  =>  file . endsWith ( '.node' ) ) ) . toBe ( true ) ; 
87+ 
88+ 			// Check that import was transformed to require for CJS 
89+ 			const  content  =  await  fixture . readFile ( 'dist/index.cjs' ,  'utf8' ) ; 
90+ 			expect ( content ) . toMatch ( './natives' ) ; 
91+ 			expect ( content ) . toMatch ( 'require' ) ; 
92+ 			// Should NOT have createRequire in CJS output 
93+ 			expect ( content ) . not . toMatch ( 'createRequire' ) ; 
4894		} ) ; 
4995
5096		test ( 'handles multiple src:dist pairs' ,  async  ( )  =>  { 
0 commit comments