From ecba9ff980ef03121f42e5c52496d859f49ba6b8 Mon Sep 17 00:00:00 2001 From: bordun Date: Tue, 27 Oct 2020 10:30:09 +0100 Subject: [PATCH] custom-module: clean & docs --- resources/docs/docs/custom-module/custom-module.md | 11 +++++++++++ .../docs/docs/custom-module/managing-big-modules.md | 9 +-------- src/server/custom-module.js | 6 +++--- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/resources/docs/docs/custom-module/custom-module.md b/resources/docs/docs/custom-module/custom-module.md index aa28fcf4..40e2b465 100644 --- a/resources/docs/docs/custom-module/custom-module.md +++ b/resources/docs/docs/custom-module/custom-module.md @@ -99,6 +99,12 @@ Saves an object/array to a json file (path is relative to the custom module loca ---- +#### `require(path)` + +Load another javascript module. Returns the value of `module.exports` defined in this module. If `path` is a relative path, it will be resolved against its parent module's path. See [Managing Big Modules](../managing-big-modules) + +---- + #### Other javascript globals - `console` - `setTimeout` @@ -107,3 +113,8 @@ Saves an object/array to a json file (path is relative to the custom module loca - `clearInterval` ---- + + +## Autoreload + +Custom modules (including submodules loaded with `require()`) are reloaded automatically when they are modified. Upon reload, timers (`setTimeout` and `setInterval`) and event listeners (added to the  `app` object) are reset. diff --git a/resources/docs/docs/custom-module/managing-big-modules.md b/resources/docs/docs/custom-module/managing-big-modules.md index 6815784d..1a335c83 100644 --- a/resources/docs/docs/custom-module/managing-big-modules.md +++ b/resources/docs/docs/custom-module/managing-big-modules.md @@ -1,6 +1,4 @@ -Custom modules don't let you use node's `require` function at runtime, but you can manage your sources in multiple files and bundle them into a single file before loading it in OSC. - -The common pattern for this is using the [browserify](http://browserify.org/) library: +Starting with `v1.6.0`, custom modules can load submodules with the `require()` function. **1**. define some variable in `number.js` @@ -22,8 +20,3 @@ module.exports = { } } ``` - - -**3**. bundle your sources into `custom-module.js`: - -`browserify main.js -o custom-module.js --standalone module.exports` diff --git a/src/server/custom-module.js b/src/server/custom-module.js index 903dc87a..fd2118c9 100644 --- a/src/server/custom-module.js +++ b/src/server/custom-module.js @@ -16,7 +16,7 @@ class CustomModule { this.oscOutFilter = null this.submodule = !!parentDir - this.filename = this.submodule ? path.resolve(parentDir, file) : file + this.filename = this.submodule && !path.isAbsolute(file) ? path.resolve(parentDir, file) : file this.timeouts = [] this.intervals = [] @@ -27,7 +27,7 @@ class CustomModule { loadJSON: (url)=>{ if (url.split('.').pop() === 'json') { try { - url = path.resolve(path.dirname(customModule), url) + url = path.resolve(path.dirname(this.filename), url) return JSON.parse(fs.readFileSync(url, 'utf8')) } catch(e) { console.error('(ERROR) could not load json file from ' + url) @@ -39,7 +39,7 @@ class CustomModule { }, saveJSON: (url, data)=>{ if (url.split('.').pop() === 'json') { - url = path.resolve(path.dirname(customModule), url) + url = path.resolve(path.dirname(this.filename), url) try { return fs.writeFileSync(url, JSON.stringify(data, null, ' ')) } catch(e) {