Search code examples
gjs

Difference between "import" and "imports" in GJS


gjs.guide recommends writing GJS import statements like this:

const Gtk = imports.gi.Gtk;

while most actual code that I see, and also the built-in Gnome Builder templates have this:

import Gtk from 'gi://Gtk';

What's the difference between the two? When should I prefer one over the other?


Solution

  • After bumming around in the gjs repository for a while I've stumbled upon this file: doc/ESModules.md

    In it they refer to "imports" as the legacy way of importing modules. From what I understood from that document:

    • imports predates import (it was a way of describing modules in GJS before ES actually had modules)
    • imports gives you all vars from the module, while import only gives you things that were explicitly exported with the export keyword;
    • so far you can use both mechanisms, but import should be preferred for new code.