node-3d / glfw-raub

GLFW for Node.js
https://github.com/node-3d/node-3d
MIT License
57 stars 12 forks source link

ESM support #12

Closed trusktr closed 3 years ago

trusktr commented 3 years ago

When importing glfw-raub using import in a Node ESM file, it has an error:

/home/trusktr/src/lume+lume/packages/glas/node_modules/glfw-raub/js/window.js:76
                        const dirpath = path.dirname(process.mainModule.filename);
                                                                        ^

TypeError: Cannot read property 'filename' of undefined
    at new Window (/home/trusktr/src/lume+lume/packages/glas/node_modules/glfw-raub/js/window.js:76:52)
    at new Document (/home/trusktr/src/lume+lume/packages/glas/node_modules/glfw-raub/js/document.js:24:3)
    at file:///home/trusktr/src/lume+lume/packages/glas/src/as/test-cube-html.mjs:24:13
    at ModuleJob.run (internal/modules/esm/module_job.js:146:23)
    at async Loader.import (internal/modules/esm/loader.js:165:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)

This is because there is no process.mainModule in ESM mode.

trusktr commented 3 years ago

Solution is super simple. Replace:

        if ( ! opts.title ) {
            const dirpath = path.dirname(process.mainModule.filename);
            const dirname = path.basename(dirpath);
            this.title = dirname;
        } else {

with

        if ( ! opts.title ) {
            const dirname = process.cwd();
            this.title = dirname;
        } else {

It's just a default window title, so the difference won't matter much.

trusktr commented 3 years ago

PR: https://github.com/node-3d/glfw-raub/pull/13

raub commented 3 years ago

Hello. Thank you for the contribution! I've merged the proposed changes, and will release the updated package soon

raub commented 3 years ago

glfw-raub@4.5.1