@@ -4,29 +4,33 @@ import { fileURLToPath } from 'node:url'
4
4
import path from 'node:path'
5
5
import type { Plugin } from 'rollup'
6
6
7
+ /**
8
+ * A plugin that simplifies bundling Koffi with user apps.
9
+ */
7
10
export function koffi ( ) : Plugin {
8
11
9
- // The path to Koffi's .node file .
12
+ // The path to the orginal `koffi .node` binary .
10
13
const koffiSource = fileURLToPath ( new URL (
11
14
`./build/koffi/${ platform } _${ arch } /koffi.node` ,
12
- import . meta. resolve ( 'koffi' ) // Unflagged in Node v20.6.0, v18.19.0
15
+ import . meta. resolve ( 'koffi' )
13
16
) )
14
17
15
- // The path we'll copy it to.
16
- const koffiDest = `koffi-${ platform } -${ arch } .node`
18
+ // The name under which we'll copy it to.
19
+ const koffi_win32_x64 = `koffi-${ platform } -${ arch } .node`
17
20
21
+ // Won't do if this is still false at writeBundle() time.
18
22
let copy = false
19
23
20
24
return {
21
- name : 'koffi-cream' ,
25
+ name : 'libwin32- koffi-cream' ,
22
26
23
27
async buildStart ( ) {
24
28
copy = false
25
29
26
30
// Make sure Koffi's installed and accessible.
27
31
const err = await fs . access ( koffiSource , fs . constants . R_OK ) . catch ( ( err : NodeJS . ErrnoException ) => err )
28
32
if ( err instanceof Error )
29
- this . error ( { message : `Cannot access "${ koffiSource } ": ${ err . code } .` , stack : undefined } )
33
+ this . error ( { message : `Cannot access "${ koffiSource } ": ${ err . code } . Did you install koffi as a dependency? ` , stack : undefined } )
30
34
} ,
31
35
32
36
buildEnd ( error ) {
@@ -36,35 +40,37 @@ export function koffi(): Plugin {
36
40
async writeBundle ( options ) {
37
41
if ( copy ) {
38
42
39
- // Create all necessary directories.
43
+ // Create all the necessary directories.
40
44
const outDir = path . join (
41
45
options . dir ?? path . dirname ( options . file ! ) ,
42
46
'node_modules' , 'koffi'
43
47
)
44
48
await fs . mkdir ( outDir , { recursive : true } ) . catch ( ( ) => { } )
45
49
46
- // Copy Koffi's native code .
47
- const destFile = path . join ( outDir , koffiDest )
48
- let err = await fs . copyFile ( koffiSource , destFile ) . catch ( ( err : NodeJS . ErrnoException ) => err )
49
- if ( err instanceof Error )
50
- this . error ( { message : `Cannot write "${ destFile } ": ${ err . code } .` , stack : undefined } )
50
+ // Copy Koffi's binary .
51
+ const outFile = path . join ( outDir , koffi_win32_x64 )
52
+ let err = await fs . copyFile ( koffiSource , outFile ) . catch ( ( err : NodeJS . ErrnoException ) => err )
53
+ if ( err )
54
+ this . error ( { message : `Cannot write "${ outFile } ": ${ err . code } .` , stack : undefined } )
51
55
52
56
// Write index.cjs
53
- err = await fs . writeFile ( path . join ( outDir , 'index.cjs' ) , [
54
- `module.exports = require('./${ koffiDest } ');`
55
- ] . join ( '\n' ) ) . catch ( ( err : NodeJS . ErrnoException ) => err )
56
- if ( err instanceof Error )
57
+ err = await fs . writeFile (
58
+ path . join ( outDir , 'index.cjs' ) ,
59
+ `module.exports = require('./${ koffi_win32_x64 } ');`
60
+ ) . catch ( ( err : NodeJS . ErrnoException ) => err )
61
+ if ( err )
57
62
this . error ( { message : `Cannot write index.cjs: ${ err . code } .` , stack : undefined } )
58
63
59
64
// Write package.json
60
65
err = await fs . writeFile ( path . join ( outDir , 'package.json' ) , JSON . stringify ( {
61
66
name : 'koffi' ,
67
+ description : `koffi binary for ${ platform } ${ arch } systems` ,
62
68
type : 'commonjs' ,
63
69
main : './index.cjs'
64
70
} , undefined , 2 ) ) . catch ( ( err : NodeJS . ErrnoException ) => err )
65
- if ( err instanceof Error )
71
+ if ( err )
66
72
this . error ( { message : `Cannot write package.json: ${ err . code } .` , stack : undefined } )
67
73
}
68
- } ,
74
+ }
69
75
}
70
76
}
0 commit comments