You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by jo...@apache.org on 2019/10/28 17:49:26 UTC
[royale-docs] 01/02: Node.js modules expanded with example
This is an automated email from the ASF dual-hosted git repository.
joshtynjala pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/royale-docs.git
commit c6dc4a1684c4e2e203d73318a3c6f5e374fa3324
Author: Josh Tynjala <jo...@bowlerhat.dev>
AuthorDate: Mon Oct 28 10:48:52 2019 -0700
Node.js modules expanded with example
---
features/nodejs/modules.md | 73 +++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 69 insertions(+), 4 deletions(-)
diff --git a/features/nodejs/modules.md b/features/nodejs/modules.md
index c3345d3..470dc91 100644
--- a/features/nodejs/modules.md
+++ b/features/nodejs/modules.md
@@ -24,15 +24,80 @@ permalink: /features/nodejs/modules
Write modules with ActionScript that can be loaded with `require()` in Node.js
-One of the reasons why the [Node.js](features/nodejs) ecosystem is so vibrant is the massive number of open source modules available to install from [npm](https://www.npmjs.com/). Those modules are all deployed as JavaScript, but they can be written in a variety of languages that can compile to JavaScript, including [ActionScript](features/as3).
+One of the reasons why the [Node.js](features/nodejs) ecosystem is so vibrant is the massive number of open source modules available to install from [the npm registry](https://www.npmjs.com/). Those modules are all deployed as JavaScript, but they can be written in a variety of languages that can compile to JavaScript, including [ActionScript](features/as3).
+
+## A simple module
+
+Create a file named *leftPad.as* and add the following code:
+
+```actionscript
+package
+{
+ public function leftPad(input:String, minLength:int, char:String = " "):String
+ {
+ var result:String = input;
+ while(result.length < minLength)
+ {
+ result = char + result;
+ }
+ return result;
+ }
+}
+```
+
+This function takes an input string, and if its length is less than the specified minimum, additional characters are appended to to beginning until the result meets the length requirement.
## Compile with asnodec
-Using Apache Royale's **asnodec** compiler, compile our project into JavaScript that can be run with Node.js.
+Using Apache Royale's **asnodec** compiler, compile the project into JavaScript code that can be run with Node.js:
```sh
-asnodec -targets=JSNodeModule src/MyModule.as
+asnodec -targets=JSNodeModule src/leftPad.as
+```
+
+Use the `-targets` compiler option to specify that the output should be a module instead of a [standalone script](features/nodejs/scripting).
+
+## Run the project
+
+To test the module, create a file named *script.js* in the root folder of your project, and add the following content:
+
+```js
+var leftPad = require("./bin/js-release/index.js");
+var inputString = "64";
+var result = leftPad(inputString, 4, "0");
+console.log(result);
```
-Use the `-targets` compiler option to specify that the output should be a module instead of a [standalone script](features/nodejs/scripting).
\ No newline at end of file
+> To run the generated JavaScript, you must have [Node.js](https://nodejs.org/) installed.
+
+
+Use the following command to run the script:
+
+```sh
+node script.js
+```
+
+The following output will appear in the console:
+
+```
+0064
+```
+
+## Deploy the module
+
+To deploy a module to [the npm registry](https://www.npmjs.com/), you must create a *package.json* file.
+
+For a project compiled with Apache Royale, the `main` field should point to the generated JavaScript entry point:
+
+```
+{
+ "name": "my-left-pad",
+ "version": "1.0.0",
+ "main": "bin/js-release/index.js"
+}
+```
+
+> When your package is installed in a user's project, they should call `require()` using the value of `name` field in your module's *package.json*.
+
+For complete details about publishing a package to the npm registry, please see [npm: Packages and modules](https://docs.npmjs.com/packages-and-modules/).
\ No newline at end of file