[gnome-builder] meson-templates: Port to standard JS imports



commit a770163676489a67fe922acfc987022ecee3142d
Author: Florian Müllner <fmuellner gnome org>
Date:   Sun Jan 30 22:41:59 2022 +0100

    meson-templates: Port to standard JS imports
    
    gjs added support for EcmaScript modules a while ago. We expect all
    apps to move away from gjs' own custom imports system eventually,
    so for new apps it makes most sense to use standard syntax from the
    get-go.

 .../meson-templates/resources/src/hello.js.in        |  4 +++-
 .../meson-templates/resources/src/main.js.tmpl       | 20 ++++++++------------
 .../meson-templates/resources/src/meson-js.build     |  1 +
 .../meson-templates/resources/src/window.js.tmpl     |  7 ++++---
 4 files changed, 16 insertions(+), 16 deletions(-)
---
diff --git a/src/plugins/meson-templates/resources/src/hello.js.in 
b/src/plugins/meson-templates/resources/src/hello.js.in
index 395118623..d9baf492e 100755
--- a/src/plugins/meson-templates/resources/src/hello.js.in
+++ b/src/plugins/meson-templates/resources/src/hello.js.in
@@ -6,4 +6,6 @@ imports.package.init({
   libdir: "@libdir@",
   datadir: "@datadir@",
 });
-imports.package.run(imports.main);
+import("resource://@resource_path@/js/main.js")
+  .then((main) => imports.package.run(main))
+  .catch(logError);
diff --git a/src/plugins/meson-templates/resources/src/main.js.tmpl 
b/src/plugins/meson-templates/resources/src/main.js.tmpl
index 2e9c6f5dc..63a5a9618 100644
--- a/src/plugins/meson-templates/resources/src/main.js.tmpl
+++ b/src/plugins/meson-templates/resources/src/main.js.tmpl
@@ -1,17 +1,14 @@
 {{include "license.js"}}
 
-pkg.initGettext();
-pkg.initFormat();
-pkg.require({
-  'Gio': '2.0',
-  'Gtk': '3.0'
-});
+import Gio from 'gi://Gio';
+import Gtk from 'gi://Gtk?version=3.0';
 
-const { Gio, Gtk } = imports.gi;
+import { {{PreFix}}Window } from './window.js';
 
-const { {{PreFix}}Window } = imports.window;
+pkg.initGettext();
+pkg.initFormat();
 
-function main(argv) {
+export function main(argv) {
     const application = new Gtk.Application({
         application_id: '{{appid}}',
         flags: Gio.ApplicationFlags.FLAGS_NONE,
@@ -19,10 +16,9 @@ function main(argv) {
 
     application.connect('activate', app => {
         let activeWindow = app.activeWindow;
-        
-        if (!activeWindow) {
+
+        if (!activeWindow)
             activeWindow = new {{PreFix}}Window(app);
-        }
 
         activeWindow.present();
     });
diff --git a/src/plugins/meson-templates/resources/src/meson-js.build 
b/src/plugins/meson-templates/resources/src/meson-js.build
index d27707007..5654bf897 100644
--- a/src/plugins/meson-templates/resources/src/meson-js.build
+++ b/src/plugins/meson-templates/resources/src/meson-js.build
@@ -22,6 +22,7 @@ bin_conf.set('PACKAGE_NAME', meson.project_name())
 bin_conf.set('prefix', get_option('prefix'))
 bin_conf.set('libdir', join_paths(get_option('prefix'), get_option('libdir')))
 bin_conf.set('datadir', join_paths(get_option('prefix'), get_option('datadir')))
+bin_conf.set('resource_path', '{{appid_path}}')
 
 configure_file(
   input: '{{appid}}.in',
diff --git a/src/plugins/meson-templates/resources/src/window.js.tmpl 
b/src/plugins/meson-templates/resources/src/window.js.tmpl
index da3affbed..4d1f4020c 100644
--- a/src/plugins/meson-templates/resources/src/window.js.tmpl
+++ b/src/plugins/meson-templates/resources/src/window.js.tmpl
@@ -1,11 +1,12 @@
 {{include "license.js"}}
 
-const { GObject, Gtk } = imports.gi;
+import GObject from 'gi://GObject';
+import Gtk from 'gi://Gtk';
 
-var {{PreFix}}Window = GObject.registerClass({
+export const {{PreFix}}Window = GObject.registerClass({
     GTypeName: '{{PreFix}}Window',
     Template: 'resource://{{appid_path}}/{{ui_file}}',
-    InternalChildren: ['label']
+    InternalChildren: ['label'],
 }, class {{PreFix}}Window extends Gtk.ApplicationWindow {
     _init(application) {
         super._init({ application });


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]