Sign in Join
Wiki copytemporary

pas2js modules: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11# Module import
22
33pas2js can convert the **\$linklib** directive to a JS import statement:
44
55The following:
66
7-`{$linklib ./modules/canvas.js canvas}`
7+```text
8+{$linklib ./modules/canvas.js canvas}
9+```
810
911is converted to
1012
11-`import * as canvas from "./modules/canvas.js";`
13+```text
14+import * as canvas from "./modules/canvas.js";
15+```
1216
1317If the alias is omitted, one is constructed for you:
1418
15-`{$linklib ./modules/some-api.js}`
19+```text
20+{$linklib ./modules/some-api.js}
21+```
1622
17-is converted to `import * as some_api from "./modules/some-api.js";`
23+is converted to
1824
25+```text
26+import * as some_api from "./modules/some-api.js";
27+```
28+
1929The import statements are always inserted in the main program. This is because modules are like windows libraries: self-contained programs, which only import and export well-defined symbols.
2030
2131For this reason, a new target "operating system" has been invented: the module. This means that you must compile with target module:
2232
23-`pas2js -Tmodule -Jirtl.js main.pp`
33+```text
34+pas2js -Tmodule -Jirtl.js main.pp
35+```
2436
2537The nodejs target will also work, but in the future the 2 targets may diverge.
2638
2739# Demos
2840
2941The pas2js demos/modules directory contains a series of subdirectories. Each subdirectory has 1 demo. Compile with the command-line as above:
3042
31-`pas2js -Tmodule -Jirtl.js main.pp`
43+```text
44+pas2js -Tmodule -Jirtl.js main.pp
45+```
3246
3347## Basic
3448
3549This shows a simple program, no units, that uses the linklib directive to import a javascript module. It uses an external class definition to access the exported symbols from the modules.
3650
3751## Basic-Units
3852
3953This is the same as the Basic example, but the import definitions are split out in units.
4054
4155## Flat
4256
4357This shows a simple program, no units, that uses the linklib directive to import a javascript module. It uses only external 'name' definitions to access the exported symbols from the modules; no external class is used.
4458
4559## Flat-Units
4660
4761This is the same as the flat example, but the import definitions are split out in units.
4862
4963# Module export
5064
5165Module export is implemented using the existing pascal **Library** concept.
5266
5367A pas2js library is transpiled into a Javascript module with its own *rtl.js* and *rtl.run*. It can export global functions, static methods, and global variables.
5468
5569For example a library exporting a function twice, as name *Swim* and as *Move*:
5670
5771``` pascal
5872library Fish;
5973procedure Swim(w: word);
6074begin
6175 writeln('Swimming ',w,' meters');
6276end;
6377exports
6478 Swim;
6579 Swim name 'Move';
6680begin
6781end.
6882```
6983
7084Compiled with *"pas2js -Tmodule -Jirtl.js -Jc fish.pas"* generates the following *fish.js*:
7185
7286``` javascript
7387...rtl.js and system unit...
7488rtl.module("library", [], function () {
7589 var $mod = this;
7690 this.Swim = function (w) {
7791 pas.System.Writeln("Swimming ",w," meters");
7892 };
7993 $mod.$main = function () {
8094 };
8195});
8296rtl.run("library");
8397export const Swim = pas.library.Swim;
8498export const Move = pas.library.Swim;
8599```
86100
87101# Using a Pascal Library
88102
89103Javascript modules can be used using the Javascript [import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) statement.
90104
91105Consider the following HTML:
92106
93107``` html
94108<!DOCTYPE html>
95109<html lang="en-US">
96110 <head>
97111 <meta charset="utf-8">
98112 <title>Basic Pas2JS library example</title>
99113 <script type="module" src="fish.js"></script>
100114 </head>
101115 <body>
102116
103117 </body>
104118</html>
105119```
106120
107121Note that the script has type **module**, and that no script tag with the **rtl.run();** statement is present in the HTML body. It is not possible to use or import a module from a regular script tag.
108122
109123## Using Pascal Library from JavaScript
110124
111125The imports from a pascal library are no different from a regular Javascript import.
112126
113127Taking the above HTML as the example, if the file **swim.js** contains the following javascript:
114128
115129``` javascript
116130import Swim from "fish.js";
117131
118132Swim(10);
119133```
120134
121135Then the browser console will show the output of the program:
122136
123-`   Swimming 10 meters`
137+```text
138+ Swimming 10 meters
139+```
124140
125141## Using Pascal Library from Pascal
126142
127143As mentioned above, using a Pascal produced library from pascal is done no different from using a Javascript module, and is no different from using a library in a native program. Our 'fish' library can be used using the following program:
128144
129145``` pascal
130146// Import the functions in an object called aqua
131147{$linklib ./fish.js aqua}
132148
133149// We use the aqua object name in the external:
134150procedure Swim(aMeters: word); external name 'aqua.Swim';
135151procedure SwimHarder(aMeters: word); external name 'aqua.Move';
136152
137153begin
138154 Swim(3);
139155 SwimHarder(10);
140156end.
141157```
142158
143159compile using the module target:
144160
145-`   ``pas2js -Tmodule -Jc -Jirtl.js swim.lpr`
161+```text
162+ pas2js -Tmodule -Jc -Jirtl.js swim.lpr
163+```
146164
147165Again, the output will be shown on the console.
148166
149167You can find another example in the pas2js [Pas2JS demos](https://gitlab.com/freepascal.org/fpc/pas2js/-/tree/main/demo/library)
150168
151169# Navigation
152170
153171- Back to [[pas2js]]
154172- Back to [[lazarus pas2js integration]]
155173
156174Note that we made a pascal program for the swim.js, but we could just as well have made a library.
157175
158176---
159177*From the Free Pascal wiki, page "pas2js modules", as it stood on 2022-02-22 after a change by Michael, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
160178Category: Pas2js

Back to history