|
| 1 | +import { testSuite, expect } from 'manten'; |
| 2 | +import { createFixture } from 'fs-fixture'; |
| 3 | +import { pkgroll } from '../../utils.js'; |
| 4 | +import { createPackageJson } from '../../fixtures.js'; |
| 5 | + |
| 6 | +export default testSuite(({ describe }, nodePath: string) => { |
| 7 | + describe('native modules', ({ test }) => { |
| 8 | + test('ESM: copies .node files to natives directory', async () => { |
| 9 | + await using fixture = await createFixture({ |
| 10 | + 'package.json': createPackageJson({ |
| 11 | + type: 'module', |
| 12 | + main: './dist/index.mjs', |
| 13 | + }), |
| 14 | + 'src/index.js': ` |
| 15 | + import native from './native.node'; |
| 16 | + console.log(native); |
| 17 | + `, |
| 18 | + 'src/native.node': Buffer.from('dummy native module'), |
| 19 | + }); |
| 20 | + |
| 21 | + const pkgrollProcess = await pkgroll([], { |
| 22 | + cwd: fixture.path, |
| 23 | + nodePath, |
| 24 | + }); |
| 25 | + |
| 26 | + expect(pkgrollProcess.exitCode).toBe(0); |
| 27 | + expect(pkgrollProcess.stderr).toBe(''); |
| 28 | + |
| 29 | + // Check that natives directory was created |
| 30 | + expect(await fixture.exists('dist/natives')).toBe(true); |
| 31 | + |
| 32 | + // Check that .node file was copied |
| 33 | + const files = await fixture.readdir('dist/natives'); |
| 34 | + expect(files.some(file => file.endsWith('.node'))).toBe(true); |
| 35 | + |
| 36 | + // Check that import was rewritten and uses createRequire for ESM |
| 37 | + const content = await fixture.readFile('dist/index.mjs', 'utf8'); |
| 38 | + expect(content).toMatch('./natives'); |
| 39 | + expect(content).toMatch('createRequire'); |
| 40 | + }); |
| 41 | + |
| 42 | + test('CJS: copies .node files to natives directory', async () => { |
| 43 | + await using fixture = await createFixture({ |
| 44 | + 'package.json': createPackageJson({ |
| 45 | + type: 'commonjs', |
| 46 | + main: './dist/index.cjs', |
| 47 | + }), |
| 48 | + 'src/index.js': ` |
| 49 | + import native from './native.node'; |
| 50 | + console.log(native); |
| 51 | + `, |
| 52 | + 'src/native.node': Buffer.from('dummy native module'), |
| 53 | + }); |
| 54 | + |
| 55 | + const pkgrollProcess = await pkgroll([], { |
| 56 | + cwd: fixture.path, |
| 57 | + nodePath, |
| 58 | + }); |
| 59 | + |
| 60 | + expect(pkgrollProcess.exitCode).toBe(0); |
| 61 | + expect(pkgrollProcess.stderr).toBe(''); |
| 62 | + |
| 63 | + // Check that natives directory was created |
| 64 | + expect(await fixture.exists('dist/natives')).toBe(true); |
| 65 | + |
| 66 | + // Check that .node file was copied |
| 67 | + const files = await fixture.readdir('dist/natives'); |
| 68 | + expect(files.some(file => file.endsWith('.node'))).toBe(true); |
| 69 | + |
| 70 | + // Check that import was transformed to require for CJS |
| 71 | + const content = await fixture.readFile('dist/index.cjs', 'utf8'); |
| 72 | + expect(content).toMatch('./natives'); |
| 73 | + expect(content).toMatch('require'); |
| 74 | + // Should NOT have createRequire in CJS output |
| 75 | + expect(content).not.toMatch('createRequire'); |
| 76 | + }); |
| 77 | + |
| 78 | + test('handles multiple src:dist pairs', async () => { |
| 79 | + await using fixture = await createFixture({ |
| 80 | + 'package.json': createPackageJson({ |
| 81 | + exports: { |
| 82 | + './a': './dist-a/index.js', |
| 83 | + './b': './dist-b/index.js', |
| 84 | + }, |
| 85 | + }), |
| 86 | + 'src-a/index.js': ` |
| 87 | + import native from './native-a.node'; |
| 88 | + console.log(native); |
| 89 | + `, |
| 90 | + 'src-a/native-a.node': Buffer.from('native module a'), |
| 91 | + 'src-b/index.js': ` |
| 92 | + import native from './native-b.node'; |
| 93 | + console.log(native); |
| 94 | + `, |
| 95 | + 'src-b/native-b.node': Buffer.from('native module b'), |
| 96 | + }); |
| 97 | + |
| 98 | + const pkgrollProcess = await pkgroll([ |
| 99 | + '--srcdist', |
| 100 | + 'src-a:dist-a', |
| 101 | + '--srcdist', |
| 102 | + 'src-b:dist-b', |
| 103 | + ], { |
| 104 | + cwd: fixture.path, |
| 105 | + nodePath, |
| 106 | + }); |
| 107 | + |
| 108 | + expect(pkgrollProcess.exitCode).toBe(0); |
| 109 | + expect(pkgrollProcess.stderr).toBe(''); |
| 110 | + |
| 111 | + // Should create natives in the first dist directory (like shared chunks) |
| 112 | + expect(await fixture.exists('dist-a/natives')).toBe(true); |
| 113 | + expect(await fixture.exists('dist-b/natives')).toBe(false); |
| 114 | + |
| 115 | + // Check both .node files were copied to first dist |
| 116 | + const files = await fixture.readdir('dist-a/natives'); |
| 117 | + expect(files.length).toBeGreaterThanOrEqual(2); |
| 118 | + expect(files.some(file => file.includes('native-a'))).toBe(true); |
| 119 | + expect(files.some(file => file.includes('native-b'))).toBe(true); |
| 120 | + }); |
| 121 | + }); |
| 122 | +}); |
0 commit comments