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
Utopia integrates with Yarn and provides a [bake task](https://github.com/ioquatix/bake)to simplify deployment packages distributed using `yarn` that implement the `dist` sub-directory convention.
3
+
This guide explains how to integrate JavaScript into your Utopia application.
4
4
5
-
## Installing Yarn
5
+
## Using Import Maps
6
6
7
-
If you don't already have yarn installed, make sure you have npm installed and then run the following command:
7
+
Import maps provide a modern way to manage JavaScript module dependencies. Utopia includes built-in support for import maps through the {ruby Utopia::ImportMap} class.
8
+
9
+
### Installing JavaScript Libraries
10
+
11
+
First, install the library using npm:
8
12
9
13
```bash
10
-
$ sudo npm install -g yarn
14
+
$ npm install jquery
11
15
```
12
16
13
-
## Installing jQuery
14
-
15
-
Firstly, ensure your project has a `package.json` file:
17
+
Copy the distribution files to `public/_components`:
16
18
17
19
```bash
18
-
$ yarn init
20
+
$ bundle exec bake utopia:node:update
19
21
```
20
22
21
-
Then install jquery using `yarn`:
23
+
This will copy the library's distribution files (typically from `node_modules/*/dist/`) to your `public/_components/` directory, making them available for local serving.
22
24
23
-
```bash
24
-
$ yarn add jquery
25
+
### Creating the Import Map
26
+
27
+
Create a global import map in `lib/my_website/import_map.rb`:
28
+
29
+
```ruby
30
+
require"utopia/import_map"
31
+
32
+
moduleMyWebsite
33
+
IMPORT_MAP=Utopia::ImportMap.build(base:"/_components/") do |map|
34
+
map.import("jquery", "./jquery/jquery.js")
35
+
end
36
+
end
25
37
```
26
38
27
-
Copy the distribution scripts to `public/_components`:
39
+
Then load this in `lib/my_website.rb`:
28
40
29
-
```bash
30
-
$ bundle exec bake utopia:node:update
41
+
```ruby
42
+
require_relative"my_website/import_map"
43
+
```
44
+
45
+
### Adding to Your Pages
46
+
47
+
Add it to your page template (`pages/_page.xnode`), using `relative_to` to adjust paths for the current page:
You can use JavaScript by embedding it directly into your HTML, or by creating a JavaScript source file and referencing that.
132
+
When you're on a page at `/admin/dashboard` and you `import "utils"`, it will resolve to `/admin/utils.js`. On other pages, it resolves to `/utils.js`.
133
+
134
+
## Traditional JavaScript
135
+
136
+
You can also use JavaScript by embedding it directly into your HTML, or by creating a JavaScript source file and referencing that.
42
137
43
138
### Embedding Code
44
139
45
-
In your HTML view:
140
+
When embedding JavaScript directly in XRB templates, wrap the code in CDATA comments to prevent XRB's parser from interpreting special characters like `<`, `>`, and `&`:
0 commit comments