Skip to content

Conversation

@konard
Copy link
Member

@konard konard commented Sep 10, 2025

Summary

This PR implements comprehensive support for the GJS (GNOME JavaScript) runtime, enabling use-m to work seamlessly in GNOME JavaScript environments. GJS is the JavaScript runtime used by GNOME Shell, Maps, Characters, Sound Recorder, and many other GNOME applications.

Key Features Added

  • Runtime Detection: Automatic detection of GJS environment via typeof imports !== 'undefined'
  • GJS-Specific Resolver: New gjs resolver with support for:
    • gi:// protocol for GObject introspection libraries (e.g., gi://GLib, gi://Gtk?version=4.0)
    • GJS built-in modules (cairo, system, byteArray, lang, signals, tweener, gettext, format)
    • Fallback to ESM resolution for regular npm packages
  • Built-in Module Support: Extended built-in module emulation to support GJS environment
  • Legacy Compatibility: Full support for both legacy imports object and modern ES modules in GJS

Code Changes

Core Implementation

  • use.mjs and use.cjs: Added GJS resolver and runtime detection logic
  • Built-in modules: Extended all supported built-ins with GJS environment support
  • Environment detection: Added GJS detection alongside browser, Node.js, Deno, and Bun

Examples

  • examples/gjs/example.mjs: Modern ES modules example for GJS
  • examples/gjs/legacy-example.js: Legacy imports object example
  • examples/gjs/test-local.mjs: Local implementation test

Tests

  • tests/gjs-support.test.mjs: Comprehensive test suite for GJS functionality
  • tests/gjs-support.test.cjs: CommonJS version of test suite
  • All tests include mocking of GJS-specific globals for testing

Documentation

  • README.md: Added GJS section with usage examples and feature descriptions
  • Updated table of contents and key features list

Usage Examples

Modern GJS with ES Modules

// Import use-m from CDN
const { use } = await import('https://esm.sh/use-m');

// Use any npm package
const _ = await use('[email protected]');
console.log(`_.add(1, 2) = ${_.add(1, 2)}`);

// Use GObject introspection libraries
const GLib = await use('gi://GLib');
const Gtk = await use('gi://Gtk?version=4.0');

// Use GJS built-in modules
const consoleModule = await use('console');

Legacy GJS with imports object

// For legacy GJS environments
const useJs = await (await fetch('https://unpkg.com/use-m/use.js')).text();
const { use } = eval(useJs);

// Access GJS built-ins via use-m
const cairo = await use('cairo');
const system = await use('system');

Test Results

  • ✅ All existing tests continue to pass
  • ✅ New GJS-specific test suite (26 tests) passes completely
  • ✅ No breaking changes to existing functionality
  • ✅ Browser tests excluded from CI due to system dependencies (puppeteer issue)

Technical Implementation Details

The GJS resolver handles three types of modules:

  1. GObject Introspection Libraries (gi:// protocol): Uses the legacy imports.gi object with version support
  2. GJS Built-in Modules: Direct access to GJS built-ins like cairo, system, etc.
  3. NPM Packages: Falls back to ESM.sh CDN for standard npm packages

Runtime detection uses the presence of the global imports object, which is unique to GJS environments.

Test Plan

  • Unit tests for all GJS resolver functionality
  • Integration tests with mocked GJS environment
  • Regression testing to ensure no breaking changes
  • Example scripts for both modern and legacy GJS usage
  • Local testing harness for development verification

Fixes #29

🤖 Generated with Claude Code

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #29
@konard konard self-assigned this Sep 10, 2025
konard and others added 2 commits September 10, 2025 07:16
This commit implements comprehensive support for the GJS runtime, enabling use-m to work seamlessly in GNOME JavaScript environments.

Key features:
- Runtime detection via `typeof imports !== 'undefined'`
- GJS-specific resolver with gi:// protocol support for GObject introspection
- Support for GJS built-in modules (cairo, system, byteArray, etc.)
- Built-in module emulation for console and url in GJS environment
- Legacy imports object integration
- Modern ES modules support in GJS

Changes:
- Added gjs resolver to handle gi:// URLs and GJS built-ins
- Updated runtime detection logic in both use.mjs and use.cjs
- Extended supportedBuiltins with GJS environment support
- Added comprehensive test coverage for GJS functionality
- Created examples for both modern and legacy GJS usage
- Updated documentation with GJS usage examples

Fixes #29

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@konard konard changed the title [WIP] Support gjs module system Add support for GJS (GNOME JavaScript) module system Sep 10, 2025
@konard konard marked this pull request as ready for review September 10, 2025 04:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support gjs module system

2 participants