[英]How can I include files with Gjs Gnome Javascript
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.
我希望能够浏览一个文件夹并加载每个Javascript文件。
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. 使用Seed JS,我可以使用Seed.include()评估文件,就像在调用include时将其包含在文件中一样。 ( reference )
( 参考 )
Does Gnome Javascript (Gjs) have an equivalent function? Gnome Javascript(Gjs)是否具有等效功能?
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. 因此,我将只分享我在gnome扩展程序中所做的工作。
TL;DR: Use Node + Webpack to have nice module system and access to huge library of modules. TL; DR:使用Node + Webpack拥有不错的模块系统并访问庞大的模块库。
package.json
file, here it is: package.json
文件,它是: {
"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: webpack.config.js
文件: 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. 所以现在我开始使用Node的模块系统,我只需要将我的东西放入文件即可。 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. 然后,我运行
npm run build
并且webpack给了我一个不错的紧凑文件,我可以运行其中所需的所有内容。
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
}
You can add a new entry to imports.searchPath
: 您可以将新条目添加到
imports.searchPath
:
$ mkdir -p ~/gjs/dynmodules/
$ touch ~/gjs/dynmodules/hello.js
imports.searchPath.push("/home/ole/gjs")
hello = imports.dynmodules.hello;
You can load the file contents into a string and eval()
it to get the same effect. 您可以将文件内容加载到字符串中,然后使用
eval()
获得相同的效果。
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.
您还可以浏览目录并导入每个JS文件。 That will prevent plugins from dumping arbitrary values into your global namespace.
这样可以防止插件将任意值转储到全局名称空间中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.