imports.searchPath
Douglas が言うように、ライブラリの場所を含めるように変更する必要があります。使い方.
は簡単ですが、常に同じディレクトリの場所から実行されるファイルに依存します。残念ながら、現在実行中のスクリプトのディレクトリを見つけるのは非常に困難です。Gnome Shell が拡張機能 API に対して行う方法は次のとおりです。
これを一般的な使用のために次の関数に適合させました。
const Gio = imports.gi.Gio;
function getCurrentFile() {
let stack = (new Error()).stack;
// Assuming we're importing this directly from an extension (and we shouldn't
// ever not be), its UUID should be directly in the path here.
let stackLine = stack.split('\n')[1];
if (!stackLine)
throw new Error('Could not find current file');
// The stack line is like:
// init([object Object])@/home/user/data/gnome-shell/extensions/u@u.id/prefs.js:8
//
// In the case that we're importing from
// module scope, the first field is blank:
// @/home/user/data/gnome-shell/extensions/u@u.id/prefs.js:8
let match = new RegExp('@(.+):\\d+').exec(stackLine);
if (!match)
throw new Error('Could not find current file');
let path = match[1];
let file = Gio.File.new_for_path(path);
return [file.get_path(), file.get_parent().get_path(), file.get_basename()];
}
関数app.js
を定義した後、エントリ ポイント ファイルからそれを使用する方法は次のとおりです。getCurrentFile
let file_info = getCurrentFile();
// define library location relative to entry point file
const LIB_PATH = file_info[1] + '/lib';
// then add it to the imports search path
imports.searchPath.unshift(LIB_PATH);
ウィー!ライブラリのインポートが非常に簡単になりました。
// import your app libraries (if they were in lib/app_name)
const Core = imports.app_name.core;