You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add the following required GraalJS dependencies to the `dependencies` block of your `build.gradle` file:
55
+
Add the following required GraalJS dependencies to the `dependencies` block of your _build.gradle_ file:
59
56
60
-
`build.gradle`
57
+
_build.gradle_
61
58
62
59
```gradle
63
60
dependencies {
@@ -77,9 +74,9 @@ The Node.js ecosystem has conventions about the filesystem layout of installed p
77
74
Use a bundler to repackage all dependencies into a single file and simplify integration.
78
75
You can use the [`com.github.node-gradle.node`](https://github.com/node-gradle/gradle-node-plugin) plugin to manage the download, installation, and bundling for you.
79
76
80
-
Configure the Gradle Node plugin in the `build.gradle` with the following:
77
+
Configure the Gradle Node plugin in the _build.gradle_ file with the following:
❷ Configures Node.js and npm versions, download settings, and working directories.
113
110
114
-
❸ Registers a 'webpackBuild' task to run 'npm run build' in the frontend directory. It ensures dependencies are installed first and sets the output directory.
111
+
❸ Registers a `webpackBuild` task to run `npm run build` in the frontend directory. It ensures dependencies are installed first and sets the output directory.
115
112
116
113
❹ Ensures that the `webpackBuild` task is executed before the processResources task, so the bundled JavaScript is included in your JAR file.
117
114
118
-
### Create a new Maven Java Project
115
+
### Create a New Maven Java Project
119
116
120
117
You can start with any Maven application that runs on JDK 21 or newer.
121
118
To follow this guide, generating the application from the [Maven Quickstart Archetype](https://maven.apache.org/archetypes/maven-archetype-quickstart/) is sufficient:
Add the following required GraalJS dependencies to the `<dependencies>` section of your POM file:
131
128
132
-
`pom.xml`
129
+
_pom.xml_
133
130
134
131
```xml
135
132
<!-- <dependencies> -->
@@ -161,9 +158,9 @@ The Node.js ecosystem has conventions about the filesystem layout of installed p
161
158
Use a bundler to repackage all dependencies into a single file and simplify integration.
162
159
You can use the [`frontend-maven-plugin`](https://github.com/eirslett/frontend-maven-plugin) plugin to manage the download, installation, and bundling for you.
163
160
164
-
Configure the Maven Frontend plugin in your `pom.xml` file to automate Node.js and JavaScript bundling steps:
161
+
Configure the Maven Frontend plugin in your _pom.xml_ file to automate Node.js and JavaScript bundling steps:
165
162
166
-
`pom.xml`
163
+
_pom.xml_
167
164
168
165
```xml
169
166
<!-- <build> -->
@@ -215,9 +212,7 @@ Configure the Maven Frontend plugin in your `pom.xml` file to automate Node.js a
215
212
216
213
❸ Runs `webpack` to build a bundle of the JS sources in _target/classes/bundle_, which will be later included in the application's JAR file and can be loaded as a resource.
217
214
218
-
---
219
-
220
-
## 2. Setting Up the JavaScript Build
215
+
## 2. Set Up the JavaScript Build
221
216
222
217
Create the directory for your JavaScript sources:
223
218
@@ -234,9 +229,9 @@ You can manually set up the build environment with these steps:
234
229
4. Run `npm install --save qrcode` to install and add the `qrcode` dependency.
235
230
5. Run `npm install --save assert util stream-browserify browserify-zlib fast-text-encoding` to install the polyfill packages to build with the webpack configuration below.
236
231
237
-
Alternatively, you can use the following `package.json` file to define your dependencies and build scripts:
232
+
Alternatively, you can use the following _package.json_ file to define your dependencies and build scripts:
238
233
239
-
`package.json`
234
+
_package.json_
240
235
241
236
```json
242
237
{
@@ -270,9 +265,9 @@ Alternatively, you can use the following `package.json` file to define your depe
270
265
}
271
266
```
272
267
273
-
Create a `webpack.config.js` file, or open the one created by `webpack-cli init`, and fill it with the following contents:
268
+
Create a _webpack.config.js_ file, or open the one created by `webpack-cli init`, and fill it with the following contents:
274
269
275
-
`webpack.config.js`
270
+
_webpack.config.js_
276
271
277
272
```js
278
273
constpath=require('path');
@@ -326,9 +321,7 @@ import 'fast-text-encoding';
326
321
export*asQRCodefrom'qrcode';
327
322
```
328
323
329
-
---
330
-
331
-
## 3. Using the JavaScript Library from Java
324
+
## 3. Use the JavaScript Library from Java
332
325
333
326
After reading the [qrcode](https://www.npmjs.com/package/qrcode) docs, you can write Java interfaces that match the [JavaScript types](https://www.npmjs.com/package/@types/qrcode) you want to use and methods you want to call on them.
334
327
GraalJS makes it easy to access JavaScript objects via these interfaces.
@@ -421,9 +414,7 @@ You can cast the exported `QRCode` object to the declared `QRCode` interface so
421
414
422
415
❹ Invoke the `then` method of the `Promise` to eventually obtain the QRCode string and print it to `stdout`.
423
416
424
-
---
425
-
426
-
## 4. Building and Running the Application
417
+
## 4. Build and Run the Application
427
418
428
419
If using Gradle, run the following commands to build and run your application:
429
420
@@ -459,14 +450,12 @@ Successfully generated QR code for "https://www.graalvm.org/".
459
450
▀▀▀▀▀▀▀ ▀ ▀▀▀▀ ▀▀▀▀▀▀
460
451
```
461
452
462
-
---
463
-
464
453
## Conclusion
465
454
466
455
By following this guide, you've learned how to:
467
456
468
457
- Use GraalJS and the GraalVM Polyglot API to embed a JavaScript library in your Java application.
469
-
- Use Webpack to bundle an NPM package into a self-contained _.mjs_ file, including its dependencies and polyfills for Node.js core modules that may be required to run on GraalJS.
458
+
- Use Webpack to bundle an NPM package into a self-contained _.mjs_ file, including its dependencies and `polyfills` for Node.js core modules that may be required to run on GraalJS.
470
459
- Integrate the JavaScript build into your Java project using either Gradle or Maven.
471
460
472
-
Feel free to use this demo as inspiration and a starting point for your own applications!
461
+
Feel free to use this demo as inspiration or as a starting point for your own applications!
0 commit comments