Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

An Error will be thrown if there are any problems loading the module, such as a file not found or syntax error.

ExamplesExample 1.

Code Block
languagejavascript
titleModule 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;

...

Code Block
languagejavascript
titleImporting 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;


Example 2.

2.1. Create a module containing the external function to called by the main app.

Code Block
languagejs
titleModule - myexternfile.js
linenumberstrue
function mainExternal() {
  let myOtherString = "I am imported.";
  return myOtherString;
}
exports.handleImport = mainExternal;

2.2. Create a module that serves as the main app, configured as the App Start File or Route. [ Reference: "Configuring Routes to Run Profound.js Modules", Using Workspaces. https://docs.profoundlogic.com/display/PUI/Using+Workspaces ]

Code Block
languagejs
titleApp Start File/Route - mylocalapp.js
linenumberstrue
function mainLocal() {
  pjs.import("./myexternalfile.js");
  let myString = "I am local.";
  pjs.messageBox(myString);
  myString = handleImport();
  pjs.messageBox(myString);
}
exports.run = mainLocal;

2.3. On the Profound.js IDE, go to [ Home Ribbon -> Launch -> Launch App in Browser Tab ]. The app will display "local" first and "imported" next on message boxes.