Multiple Files
Multi-files configuration structure is supported via the ES modules, i.e. through the export
and import
declarations.
Static Import
The export
declaration is used to export values from a module. Then exported values can be imported into other modules with import
declaration (or dynamic import). For example we have below configuration:
$HOME/.rsvim
|- rsvim.js
|- utils.js
|- utils/
|- add.js
|- echo.js
utils/add.js
export function add(a, b) {
return a + b;
}
utils/echo.js
export function echo(value) {
Rsvim.cmd.echo(value);
}
utils.js
import { echo } from "./utils/echo.js";
import { add } from "./utils/add.js";
export default { echo, add };
rsvim.js
import util from "./utils.js";
util.echo(util.add(1, 2));
When Rsvim starts, it will print the message 3
.
The string value passed to the import
is called specifier
. In this example, it is a file path relative to current javascript file. You can use an absolute file path as well. The import
declaration here is called static import, it runs synchronously.
But, there are some limitations, since Rsvim is not 100% compatible with node:
- Rsvim only supports ES modules, it doesn't support CommonJS modules as node do,
require
is not implemented. - File path based
specifier
must contain the file extension, it cannot be omitted like node.
Dynamic Import
Dynamic import allows user loading a module lazily and asynchronously. Recall the above example, let's rewrite the rsvim.js
:
import("./utils.js")
.then((utils) => {
util.echo(util.add(1, 2));
})
.catch((e) => {
Rsvim.cmd.echo(e);
});
// Or
try {
const utils = await import("./utils.js");
utils.echo(utils.add(1, 2));
} catch (e) {
Rsvim.cmd.echo(e);
}
When Rsvim starts, it will schedules a "./utils.js"
module loading task to background, then completes the configuration phase and initializes the TUI and editor. Once the "./utils.js"
module is loaded, Rsvim will continue to execute code logic in the remaining script.
Working with ~/.rsvim.{js,ts}
When working with the ~/.rsvim.{js,ts}
entry, you can still import modules through either relative file path or absolute file path. A recommended way is to put all your configuration files under the ~/.rsvim/
directory.
For example:
$HOME
|- .rsvim.js
|- .rsvim/
|- utils.js
.rsvim.js
import utils from "./.rsvim/utils.js";
The specifier "./.rsvim/utils.js"
is exactly the file path relative to the config entry ~/.rsvim.js
.