Reputation: 31
I understand I can import files easily enough, but I'm trying to make some kind of plugin structure. I'd like to be able scan through a folder and load each Javascript file.
With Seed JS I can use Seed.include() to evalute a file as if it were included in the file at the point include is called. (reference)
Does Gnome Javascript (Gjs) have an equivalent function?
Upvotes: 3
Views: 2778
Reputation: 1328
You can add a new entry to imports.searchPath
:
$ mkdir -p ~/gjs/dynmodules/
$ touch ~/gjs/dynmodules/hello.js
imports.searchPath.push("/home/ole/gjs")
hello = imports.dynmodules.hello;
Upvotes: 2
Reputation: 312
There is a thread about this problem, the guy apparently complains that it doesn't actually work. I don't know if it worth trying because anyway the approach sucks big way - it too cumbersome in my opinion.
So I'll just share what I did in my gnome extension.
TL;DR: Use Node + Webpack to have nice module system and access to huge library of modules.
package.json
file, here it is:{
"name": "blah",
"version": "0.0.1",
"description": "blah",
"scripts": {
"watch": "nodemon --exec 'npm run build'",
"build": "webpack"
},
"author": "me",
"license": "ISC",
"devDependencies": {
"nodemon": "^1.11.0",
"webpack": "^2.2.1"
},
"dependencies": {
"string-format": "^0.5.0"
}
}
webpack.config.js
file:var path = require('path');
module.exports = {
entry: {
main: './src/main.js',
ui: './src/ui.js'
},
output: {
filename: '[name].js',
path: path.resolve(__dirname),
libraryTarget: 'var',
library: '[name]'
},
resolve: {
modules: [
path.resolve('./src'),
'node_modules'
]
},
externals: {
'gnome': 'global',
'lang': 'imports.lang',
'gi/meta': 'imports.gi.Meta',
'gi/shell': 'imports.gi.Shell',
'ui/main': 'imports.ui.main',
'ui/popupMenu': 'imports.ui.popupMenu',
'ui/panelMenu': 'imports.ui.panelMenu',
'gi/atk': 'imports.gi.Atk',
'gi/st': 'imports.gi.St',
'gi/gtk': 'imports.gi.Gtk',
'gi/gdk': 'imports.gi.Gdk',
'gi/gobject': 'imports.gi.GObject',
'gi/gio': 'imports.gi.Gio',
'gi/soup': 'imports.gi.Soup',
'gi/glib': 'imports.gi.GLib',
'gi/clutter': 'imports.gi.Clutter',
'misc/config': 'imports.misc.config',
'me': 'imports.misc.extensionUtils.getCurrentExtension()'
}
};
So now I get to use Node's module system and I can just require my stuff into files. I then run npm run build
and webpack gives me one nice tight file which I can run with all of the stuff required inside.
Have an example of a file:
const Lang = require('lang')
const Gdk = require('gi/gdk')
const parser = require('./parser')
const modifier = require('./modifier')
const rules = [
parser((state, result) => {
const name = result.string
const rule = new RegExp('^[a-zA-Z0-9]+$')
return name && rule.test(name)
}, '', true),
modifier(Gdk.ModifierType.SUPER_MASK, 'super', false),
modifier(Gdk.ModifierType.MOD1_MASK, 'alt', false),
modifier(Gdk.ModifierType.CONTROL_MASK, 'control', false),
// this makes sure that we have at least one modifier enabled
modifier(Gdk.ModifierType.MODIFIER_MASK, false, true)
]
module.exports = function(state, keyval) {
const result = {
valid: false,
string: '',
}
if(state[0] && keyval[0]) {
result.valid = true,
result.string = Gdk.keyval_name(keyval[1])
rules.forEach((rule) => {
result = rule(state[1], result)
})
}
return result
}
Upvotes: 9
Reputation: 57920
You can load the file contents into a string and eval()
it to get the same effect.
Do you have a particular reason why you can't use imports to get what you want in a plugin structure? You can also scan through a directory and import each JS file. That will prevent plugins from dumping arbitrary values into your global namespace.
Upvotes: 0