Import Functions
Learn how to import functions from other plugins written in different languages and use them in your own.
In Plugify, the JavaScript language module allows you to import and use functions from plugins written in other languages. This is achieved through runtime-generated synthetic modules, which enable seamless inter-plugin communication. Additionally, type files (.d.ts
) can be used to provide TypeScript type information for better development experience in IDEs like VSCode.
This guide explains how to import functions in JavaScript and how to use type files for development.
Overview
When importing functions from another plugin in JavaScript:
- Runtime-Generated Modules: Plugify dynamically generates synthetic modules at runtime, allowing you to import and call functions from other plugins directly.
- Type Files for Development: Type files (
.d.ts
) provide TypeScript type information for JavaScript APIs. These files are not required for runtime execution but are useful for development. - Direct Function Calls: Functions are called directly using the imported synthetic modules.
Importing Functions in JavaScript
Import Synthetic Modules
To import functions from another plugin, use Plugify's import system to load the synthetic module for the target plugin.
plugify
: The core Plugify module, providing utility classes likeVector2
,Vector3
, etc.plugin_from_another_language
: The synthetic module for the target plugin. Replace this with the name of the plugin you want to import functions from.
Call the Imported Functions
Once the synthetic module is imported, you can call its functions directly.
Example 1: Calling a Simple Function
ParamCallback
: This is an example function exported by the target plugin. Replace it with the actual function name.- Parameters: Pass the required parameters as defined by the function signature.
Example 2: Calling a Function with a Callback Parameter
If the imported function requires a callback as a parameter, you can define the callback in JavaScript and pass it to the function. Here’s an example:
CallFuncCallback
: This is an example function that accepts a callback as a parameter.mockFunc
: This is the callback function defined in JavaScript. It modifies the parameters passed by reference and returns them in an array.- Return Values: The callback function returns
null
(for the void return type) and the modified parameters.
Using Type Files for Development
Type files (.d.ts
) are not required for runtime execution but are highly recommended for development. They provide TypeScript type information, making it easier to understand the available functions and their parameters.
Generating Type Files
Plugify provides a generator.js
script to automatically generate type files for imported plugins.
Locate the Generator Script:
- The
generator.py
script is located in thegenerator
folder of the JavaScript language module.
Run the Generator Script:
- Open a terminal or command prompt and navigate to the folder containing
generator.js
. - Run the script with the following command:
path_to_plugin.pplugin
: The path to the plugin manifest file (.pplugin
) of the plugin you want to import functions from.output_folder
: The directory where the generated type file will be saved.
Example:
Generated Type File:
- The script will generate a type file (e.g.,
MyPlugin.d.ts
) in the specified output folder. - Example type file content:
Using Type Files in Your IDE:
- Place the generated type file (
.d.ts
) in your project directory (but not in the plugins folder). - IDEs like VSCode will use the type file to provide type hints and autocompletion.
VSCode Support:
- VSCode automatically detects type files in your project. Ensure the
types
folder (or the folder containing your.d.ts
files) is included in yourtsconfig.json
orjsconfig.json
file. - Example
tsconfig.json
configuration:
Notes
- Type Files Are Optional: Type files are only for development and should not be included in your plugin's runtime folder.
- Runtime Efficiency: Since JavaScript is dynamically typed, function calls are resolved at runtime without the need for pre-compiled headers or type definitions.
By following this guide, you can easily import and use functions from other plugins in your JavaScript plugin, while leveraging type files for a better development experience.