11import { platform } from 'node:process' ;
2- import { init , addThreeHelpers , glfw } from '../ts/index.ts' ;
32import * as three from 'three' ;
3+ import type { TGlfw , TInitOpts } from '../ts/types.ts' ;
4+
5+ const bootstrapMacosGlfw = async ( ) : Promise < TGlfw | null > => {
6+ if ( platform !== 'darwin' ) {
7+ return null ;
8+ }
9+
10+ const nodeGlobal = globalThis as unknown as Record < string , unknown > ;
11+
12+ // @node -3d/glfw normally initializes on import. The macOS CI path needs
13+ // glfwInitHint before glfwInit so it can use the Null platform.
14+ nodeGlobal [ '__isGlfwInited' ] = true ;
15+ const { glfw : bootstrappedGlfw } = await import ( '@node-3d/glfw' ) ;
16+ bootstrappedGlfw . initHint ( bootstrappedGlfw . PLATFORM , bootstrappedGlfw . PLATFORM_NULL ) ;
17+
18+ if ( ! bootstrappedGlfw . init ( ) ) {
19+ throw new Error ( 'Failed to initialize GLFW for macOS tests' ) ;
20+ }
21+
22+ bootstrappedGlfw . defaultWindowHints ( ) ;
23+ nodeGlobal [ '__isGlfwInited' ] = true ;
24+
25+ return bootstrappedGlfw ;
26+ } ;
27+
28+ const macosGlfw = await bootstrapMacosGlfw ( ) ;
29+ const core = await import ( '../ts/index.ts' ) ;
30+ const { init, addThreeHelpers, glfw } = core ;
431
532const initOptsLinux = {
633 width : 400 ,
@@ -15,21 +42,44 @@ const initOpts = {
1542 major : 2 ,
1643 minor : 1 ,
1744} ;
45+ const initOptsMacos : TInitOpts = {
46+ ...initOpts ,
47+ isVisible : false ,
48+ onBeforeWindow ( _window , currentGlfw ) {
49+ const windowGlfw = currentGlfw as TGlfw ;
50+ windowGlfw . windowHint ( windowGlfw . CONTEXT_CREATION_API , windowGlfw . OSMESA_CONTEXT_API ) ;
51+ windowGlfw . windowHint ( windowGlfw . STENCIL_BITS , 8 ) ;
52+ windowGlfw . windowHint ( windowGlfw . DEPTH_BITS , 0 ) ;
53+ windowGlfw . windowHint ( windowGlfw . SAMPLES , 0 ) ;
54+ } ,
55+ } ;
1856
1957if ( platform === 'darwin' ) {
20- glfw . windowHint ( glfw . STENCIL_BITS , 8 ) ;
58+ ( macosGlfw ?? glfw ) . windowHint ( glfw . STENCIL_BITS , 8 ) ;
2159 // this would be nice... - https://github.com/glfw/glfw/pull/2571
2260 // glfw.windowHint(glfw.CONTEXT_RENDERER, glfw.SOFTWARE_RENDERER);
2361}
2462
25- const inited = init ( platform === 'linux' ? initOptsLinux : initOpts ) ;
63+ const getInitOpts = ( ) : TInitOpts => {
64+ if ( platform === 'linux' ) {
65+ return initOptsLinux ;
66+ }
67+
68+ if ( platform === 'darwin' ) {
69+ return initOptsMacos ;
70+ }
71+
72+ return initOpts ;
73+ } ;
74+
75+ const inited = init ( getInitOpts ( ) ) ;
2676addThreeHelpers ( three ) ;
2777
2878const { doc } = inited ;
2979const window = doc ;
3080const document = doc ;
3181
32- export { Image } from '../ts/index.ts' ;
82+ export const { Image } = core ;
3383
3484export { doc , window , document } ;
3585
0 commit comments