This API is used to load exported functions from a Profound.js module into the current scope, while injecting all strongly defined fields and tables from the current scope into those functions.
Parameters
- Module path / name - A String containing the module file name to load. An absolute path, a relative path starting with ./ or ../, or a path relative to the 'modules' directory can be used. If only the file name is given, the pathlist will be searched to locate the module. If the file extension is omitted, ".js" is assumed. When specifying the path, a file pattern (e.g. "./dirname/*.js") can be used to load multiple modules.
- Import List (optional) - An Array of function names to import. If omitted, all exported functions are imported.
Exception Handling
An Error will be thrown if there are any problems loading the module, such as a file not found or syntax error.
Example
Module 1 - increment.js
function increment() { // x and y must be declared in the parent scope where pjs.import() is used x += 1; y += 2; } exports.increment = increment;
Module 2 - decrement.js
function decrement() { // x and y must be declared in the parent scope where pjs.import() is used x -= 1; y -= 2; } exports.decrement = decrement;
Importing 1 module
function main() { pjs.define("x", { type: "integer" }); pjs.define("y", { type: "integer" }); x = 1; y = 2; pjs.import("./main/increment.js"); increment(); console.log(x, y); // outputs 2 4 } exports.run = main;
Importing one module and providing an import list
function main() { pjs.define("x", { type: "integer" }); pjs.define("y", { type: "integer" }); x = 1; y = 2; pjs.import("./main/increment.js", ["increment"]); increment(); console.log(x, y); // outputs 2 4 } exports.run = main;
Importing all modules in a directory
function main() { pjs.define("x", { type: "integer" }); pjs.define("y", { type: "integer" }); x = 1; y = 2; pjs.import("./main/*.js"); increment(); decrement(); decrement(); console.log(x, y); // outputs 0 0 } exports.run = main;