@marko/run 0.5.12 → 0.5.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +85 -76
- package/dist/.tsbuildinfo +1 -1
- package/dist/adapter/default-entry.mjs +12 -13
- package/dist/adapter/index.cjs +470 -454
- package/dist/adapter/index.d.ts +2 -2
- package/dist/adapter/index.js +473 -457
- package/dist/adapter/load-dev-worker.mjs +5 -5
- package/dist/adapter/middleware.cjs +5 -3
- package/dist/adapter/middleware.d.ts +1 -1
- package/dist/adapter/middleware.js +5 -3
- package/dist/adapter/polyfill.d.ts +1 -1
- package/dist/cli/default.config.mjs +2 -2
- package/dist/cli/index.mjs +1491 -1466
- package/dist/runtime/index.d.ts +3 -3
- package/dist/runtime/internal.d.ts +1 -1
- package/dist/runtime/namespace.d.ts +1 -1
- package/dist/vite/codegen/index.d.ts +1 -1
- package/dist/vite/constants.d.ts +1 -1
- package/dist/vite/index.cjs +1445 -1420
- package/dist/vite/index.d.ts +2 -2
- package/dist/vite/index.js +1440 -1415
- package/dist/vite/plugin.d.ts +1 -1
- package/dist/vite/routes/vdir.d.ts +1 -1
- package/dist/vite/types.d.ts +3 -3
- package/dist/vite/utils/config.d.ts +1 -1
- package/dist/vite/utils/log.d.ts +1 -1
- package/dist/vite/utils/route.d.ts +1 -1
- package/dist/vite/utils/server.d.ts +1 -1
- package/package.json +64 -63
package/README.md
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
1
|
<div align="center">
|
|
4
2
|
<picture>
|
|
5
3
|
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/marko-js/run/raw/main/assets/marko-run-darkmode.png">
|
|
@@ -10,25 +8,26 @@
|
|
|
10
8
|
</div>
|
|
11
9
|
|
|
12
10
|
## Features
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
11
|
+
|
|
12
|
+
- 🚀 Fastest way to build a Marko app
|
|
13
|
+
- 💖 Scales from zero configuration
|
|
14
|
+
- ⚡️ Pages live-reload as you make changes
|
|
15
|
+
- 📁 Directory-based routes, layouts, and middleware
|
|
16
|
+
- 🖌️ TypeScript powered editor support
|
|
17
|
+
- 🧬 [Designed with web standards](https://developer.mozilla.org/en-US/docs/Web/API/URLPattern/URLPattern) to run anywhere
|
|
19
18
|
|
|
20
19
|
And when you build your production-ready app:
|
|
21
20
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
- 🔥 Blazing fast server-side rendering
|
|
22
|
+
- 🌊 Streams content to users ASAP
|
|
23
|
+
- 📦 Partial hydration & automatic code splitting
|
|
24
|
+
- 🚢 Deploy to multiple platforms
|
|
26
25
|
|
|
27
26
|
## Installation
|
|
27
|
+
|
|
28
28
|
> **Warning**
|
|
29
29
|
> This project is in BETA - use at your own peril, but please do provide helpful feedback.
|
|
30
30
|
|
|
31
|
-
|
|
32
31
|
```sh
|
|
33
32
|
npm install @marko/run
|
|
34
33
|
```
|
|
@@ -38,11 +37,13 @@ npm install @marko/run
|
|
|
38
37
|
`marko-run` makes it easy to get started without little to no config. The package ships with a default Vite config and node-based adapter.
|
|
39
38
|
|
|
40
39
|
To get started from a template:
|
|
40
|
+
|
|
41
41
|
1. `npm init marko -- -t basic`
|
|
42
42
|
2. `cd ./<PROJECT_NAME>`
|
|
43
|
-
|
|
43
|
+
3. `npm run dev`
|
|
44
44
|
|
|
45
45
|
Or manually create a project:
|
|
46
|
+
|
|
46
47
|
1. Install `@marko/run`
|
|
47
48
|
2. Create file `src/routes/+page.marko`
|
|
48
49
|
3. Run `npm exec marko-run`
|
|
@@ -52,21 +53,25 @@ Finally open `http://localhost:3000` 🚀
|
|
|
52
53
|
### CLI
|
|
53
54
|
|
|
54
55
|
**`dev`** - Start a development server in watch mode
|
|
56
|
+
|
|
55
57
|
```sh
|
|
56
58
|
> npm exec marko-run
|
|
57
59
|
```
|
|
60
|
+
|
|
58
61
|
or (with explicit sub command)
|
|
62
|
+
|
|
59
63
|
```sh
|
|
60
64
|
> npm exec marko-run dev
|
|
61
65
|
```
|
|
62
66
|
|
|
63
|
-
|
|
64
67
|
**`build`** - Create a production build
|
|
68
|
+
|
|
65
69
|
```sh
|
|
66
70
|
> npm exec marko-run build
|
|
67
71
|
```
|
|
68
72
|
|
|
69
73
|
**`preview`** - Create a production build and start the preview server
|
|
74
|
+
|
|
70
75
|
```sh
|
|
71
76
|
> npm exec marko-run preview
|
|
72
77
|
```
|
|
@@ -89,10 +94,12 @@ import { defineConfig } from "vite";
|
|
|
89
94
|
import marko from "@marko/run/vite";
|
|
90
95
|
|
|
91
96
|
export default defineConfig({
|
|
92
|
-
plugins: [
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
})
|
|
97
|
+
plugins: [
|
|
98
|
+
marko({
|
|
99
|
+
routesDir: "src/pages", // Use `./src/pages` (relative to this file) as the routes directory
|
|
100
|
+
}),
|
|
101
|
+
],
|
|
102
|
+
});
|
|
96
103
|
```
|
|
97
104
|
|
|
98
105
|
### Routeable Files
|
|
@@ -158,8 +165,8 @@ Typically, these will be `.js` or `.ts` files depending on your project. Like pa
|
|
|
158
165
|
return new Response('Successfully removed', { status: 204 });
|
|
159
166
|
}
|
|
160
167
|
```
|
|
161
|
-
</details>
|
|
162
168
|
|
|
169
|
+
</details>
|
|
163
170
|
|
|
164
171
|
#### `+middleware.*`
|
|
165
172
|
|
|
@@ -195,6 +202,7 @@ These files are like layouts, but for handlers. Middleware files are called befo
|
|
|
195
202
|
}
|
|
196
203
|
}
|
|
197
204
|
```
|
|
205
|
+
|
|
198
206
|
</details>
|
|
199
207
|
|
|
200
208
|
#### `+meta.*`
|
|
@@ -212,7 +220,7 @@ These special pages are subject to a root layout file (`pages/+layout.marko` in
|
|
|
212
220
|
This special page responds to any request where:
|
|
213
221
|
|
|
214
222
|
- The `Accept` request header includes `text/html`
|
|
215
|
-
-
|
|
223
|
+
- _And_ no other handler or page rendered the request
|
|
216
224
|
|
|
217
225
|
Responses with this page will have a `404` status code.
|
|
218
226
|
|
|
@@ -221,7 +229,7 @@ Responses with this page will have a `404` status code.
|
|
|
221
229
|
This special page responds to any request where:
|
|
222
230
|
|
|
223
231
|
- The `Accept` request header includes `text/html`
|
|
224
|
-
-
|
|
232
|
+
- _And_ an uncaught error occurs while serving the request
|
|
225
233
|
|
|
226
234
|
Responses with this page will have a `500` status code.
|
|
227
235
|
|
|
@@ -273,40 +281,44 @@ Within the [routes directory](#routes-directory), the directory structure determ
|
|
|
273
281
|
|
|
274
282
|
1. **Static directories** - The most common type, and the default behavior. Each static directory contributes its name as a segment in the route's served path, like a traditional fileserver. Unless a directory name matches the requirements for one of the below types, it is seen as a static directory.
|
|
275
283
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
284
|
+
Examples:
|
|
285
|
+
|
|
286
|
+
```
|
|
287
|
+
/foo
|
|
288
|
+
/users
|
|
289
|
+
/projects
|
|
290
|
+
```
|
|
282
291
|
|
|
283
292
|
2. **Pathless directories** - These directories do **not** contribute their name to the route's served path. Directory names that start with an underscore (`_`) will be ignored when parsing the route.
|
|
284
293
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
294
|
+
Examples:
|
|
295
|
+
|
|
296
|
+
```
|
|
297
|
+
/_users
|
|
298
|
+
/_public
|
|
299
|
+
```
|
|
290
300
|
|
|
291
301
|
3. **Dynamic directories** - These directories introduce a dynamic parameter to the route's served path and will match any value at that segment. Any directory name that starts with a single dollar sign (`$`) will be a dynamic directory, and the remaining directory name will be the parameter at runtime. If the directory name is exactly `$`, the parameter will not be captured but it will be matched.
|
|
292
302
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
303
|
+
Examples:
|
|
304
|
+
|
|
305
|
+
```
|
|
306
|
+
/$id
|
|
307
|
+
/$name
|
|
308
|
+
/$
|
|
309
|
+
```
|
|
299
310
|
|
|
300
311
|
4. **Catch-all directories** - These directories are similar to dynamic directories and introduce a dynamic parameter, but instead of matching a single path segment, they match to the end of the path. Any directory that starts with two dollar signs (`$$`) will be a catch-all directory, and the remaining directory name will be the parameter at runtime. In the case of a directory named `$$`, the parameter name will not be captured but it will match. Catch-all directories can be used to make `404` Not Found routes at any level, including the root.
|
|
301
312
|
|
|
302
|
-
|
|
313
|
+
Because catch-all directories match any path segment and consume the rest of the path, you cannot nest route files in them and no further directories will be traversed.
|
|
314
|
+
|
|
315
|
+
Examples:
|
|
303
316
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
```
|
|
317
|
+
```
|
|
318
|
+
/$$all
|
|
319
|
+
/$$rest
|
|
320
|
+
/$$
|
|
321
|
+
```
|
|
310
322
|
|
|
311
323
|
### Flat Routes
|
|
312
324
|
|
|
@@ -357,8 +369,6 @@ routes/
|
|
|
357
369
|
projects.$projectId+page.marko
|
|
358
370
|
```
|
|
359
371
|
|
|
360
|
-
|
|
361
|
-
|
|
362
372
|
### Multiple Paths, Groups and Optional Segments
|
|
363
373
|
|
|
364
374
|
Along with descibing multiple segements, flat route syntax supports defining routes that match more than one path and segments that are optional. To describe a route that matches multiple paths, use a comma (`,`) and define each route.
|
|
@@ -393,7 +403,9 @@ The last concept is **optionallity**. By introducing an empty segment or pathles
|
|
|
393
403
|
routes/
|
|
394
404
|
projects.(home,)+page.marko
|
|
395
405
|
```
|
|
406
|
+
|
|
396
407
|
or
|
|
408
|
+
|
|
397
409
|
```
|
|
398
410
|
routes/
|
|
399
411
|
projects.(home,_pathless)+page.marko
|
|
@@ -401,18 +413,10 @@ routes/
|
|
|
401
413
|
|
|
402
414
|
While both of these create a route which matches the paths, they have slightly different semantics. Using a pathless segment is the same as creating a pathless folder which allows you to isolate middleware and loayouts. Using an empty segement is the same as defining a file at the current location.
|
|
403
415
|
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
416
|
<!-- ### Match Ranking
|
|
410
417
|
|
|
411
418
|
*TODO: Write some things* -->
|
|
412
419
|
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
420
|
## Vite Plugin
|
|
417
421
|
|
|
418
422
|
This package’s Vite plugin discovers your route files, generates the routing code, and registers the `@marko/vite` plugin to compile your `.marko` files.
|
|
@@ -423,8 +427,8 @@ import { defineConfig } from "vite";
|
|
|
423
427
|
import marko from "@marko/run/vite"; // Import the Vite plugin
|
|
424
428
|
|
|
425
429
|
export default defineConfig({
|
|
426
|
-
plugins: [marko()] // Register the Vite plugin
|
|
427
|
-
})
|
|
430
|
+
plugins: [marko()], // Register the Vite plugin
|
|
431
|
+
});
|
|
428
432
|
```
|
|
429
433
|
|
|
430
434
|
### Adapters
|
|
@@ -437,13 +441,15 @@ Specify your adapter in the Vite config when registering the `@marko/run` plugin
|
|
|
437
441
|
// vite.config.ts
|
|
438
442
|
import { defineConfig } from "vite";
|
|
439
443
|
import marko from "@marko/run/vite";
|
|
440
|
-
import netlify from "@marko/run-adapter-netlify" // Import the adapter
|
|
444
|
+
import netlify from "@marko/run-adapter-netlify"; // Import the adapter
|
|
441
445
|
|
|
442
446
|
export default defineConfig({
|
|
443
|
-
plugins: [
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
})
|
|
447
|
+
plugins: [
|
|
448
|
+
marko({
|
|
449
|
+
adapter: netlify({ edge: true }), // Configure and apply the adapter
|
|
450
|
+
}),
|
|
451
|
+
],
|
|
452
|
+
});
|
|
447
453
|
```
|
|
448
454
|
|
|
449
455
|
#### Available Adapters
|
|
@@ -451,6 +457,7 @@ export default defineConfig({
|
|
|
451
457
|
- [@marko/run-adapter-node](https://github.com/marko-js/run/blob/main/packages/adapters/node/README.md)
|
|
452
458
|
- [@marko/run-adapter-netlify](https://github.com/marko-js/run/blob/main/packages/adapters/netlify/README.md)
|
|
453
459
|
- [@marko/run-adapter-static](https://github.com/marko-js/run/blob/main/packages/adapters/static/README.md)
|
|
460
|
+
|
|
454
461
|
## Runtime
|
|
455
462
|
|
|
456
463
|
Generally, when using an adapter, this runtime will be abstracted away.
|
|
@@ -464,16 +471,12 @@ import * as Run from '@marko/run/router`;
|
|
|
464
471
|
|
|
465
472
|
### Emdedding in Existing Server
|
|
466
473
|
|
|
467
|
-
|
|
468
|
-
|
|
469
474
|
### `Run.fetch`
|
|
470
475
|
|
|
471
476
|
```ts
|
|
472
477
|
async function fetch<T>(request: Request, platform: T) => Promise<Response | void>;
|
|
473
478
|
```
|
|
474
479
|
|
|
475
|
-
|
|
476
|
-
|
|
477
480
|
This asynchronous function takes a [WHATWG `Request` object](https://fetch.spec.whatwg.org/#request-class) and an object containing any platform-specific data you may want access to, and returns any of
|
|
478
481
|
|
|
479
482
|
- a [WHATWG `Response` object](https://fetch.spec.whatwg.org/#response-class) (generated from executing any matched route files)
|
|
@@ -482,6 +485,7 @@ This asynchronous function takes a [WHATWG `Request` object](https://fetch.spec.
|
|
|
482
485
|
- a `500` status code response (if an error occurs)
|
|
483
486
|
|
|
484
487
|
Express example:
|
|
488
|
+
|
|
485
489
|
```ts
|
|
486
490
|
import express from "express";
|
|
487
491
|
import * as Run from "@marko/run/router";
|
|
@@ -504,7 +508,6 @@ express()
|
|
|
504
508
|
.listen(3000);
|
|
505
509
|
```
|
|
506
510
|
|
|
507
|
-
|
|
508
511
|
### Other APIs
|
|
509
512
|
|
|
510
513
|
In some cases, you might want more control over when route matching and invocation (creating a response) occur. For instance, you may have middleware in your server which needs to know if there is a matched route. The runtime provides these additional methods:
|
|
@@ -530,9 +533,11 @@ This synchronous function takes an HTTP method and path name and returns an obje
|
|
|
530
533
|
```ts
|
|
531
534
|
async function invoke(route: Route, request: Request, platform: any) => Promise<Response | void>;
|
|
532
535
|
```
|
|
536
|
+
|
|
533
537
|
This asynchronous function takes a route object returned by [Run.match](#Run.match) the request and platform data and returns a response in the same way the [Run.fetch](#Run.fetch) does.
|
|
534
538
|
|
|
535
539
|
Express example:
|
|
540
|
+
|
|
536
541
|
```ts
|
|
537
542
|
import express from "express";
|
|
538
543
|
import * as Run from "@marko/run/router";
|
|
@@ -569,12 +574,10 @@ express()
|
|
|
569
574
|
.listen(3000);
|
|
570
575
|
```
|
|
571
576
|
|
|
572
|
-
|
|
573
|
-
|
|
574
577
|
## TypeScript
|
|
575
578
|
|
|
576
|
-
|
|
577
579
|
### Global Namespace
|
|
580
|
+
|
|
578
581
|
`marko/run` provides a global namespace `MarkoRun` with the following types:
|
|
579
582
|
|
|
580
583
|
**`MarkoRun.Handler`** - Type that represents a handler function to be exported by a +handler or +middleware file
|
|
@@ -582,38 +585,44 @@ express()
|
|
|
582
585
|
**`MarkoRun.Route`** - Type of the route's params and metadata
|
|
583
586
|
|
|
584
587
|
**`MarkoRun.Context`** - Type of the request context object in a handler and `$global` in your Marko files. This type can be extended using TypeScript's module and interface merging by declaring a `Context` interface on the `@marko/run` module within your applcation code
|
|
588
|
+
|
|
585
589
|
```ts
|
|
586
590
|
declare module "@marko/run" {
|
|
587
591
|
interface Context {
|
|
588
|
-
customPropery: MyCustomThing // will be globally defined on MarkoRun.Context
|
|
592
|
+
customPropery: MyCustomThing; // will be globally defined on MarkoRun.Context
|
|
589
593
|
}
|
|
590
594
|
}
|
|
591
595
|
```
|
|
592
596
|
|
|
593
|
-
**`MarkoRun.Platform`** - Type of the platform object provided by the adapter in use. This interface can be extended in that same way as `Context` (see above) by declaring a `Platform` interface:
|
|
597
|
+
**`MarkoRun.Platform`** - Type of the platform object provided by the adapter in use. This interface can be extended in that same way as `Context` (see above) by declaring a `Platform` interface:
|
|
598
|
+
|
|
594
599
|
```ts
|
|
595
600
|
declare module "@marko/run" {
|
|
596
601
|
interface Platform {
|
|
597
|
-
customPropery: MyCustomThing // will be globally defined on MarkoRun.Platform
|
|
602
|
+
customPropery: MyCustomThing; // will be globally defined on MarkoRun.Platform
|
|
598
603
|
}
|
|
599
604
|
}
|
|
600
605
|
```
|
|
601
606
|
|
|
602
|
-
|
|
603
607
|
### Generated Types
|
|
608
|
+
|
|
604
609
|
If a [TSConfig](https://www.typescriptlang.org/tsconfig) file is discovered in the project root, the Vite plugin will automatically generate a .d.ts file which provides more specific types for each of your middleware, handlers, layouts, and pages. This file will be generated at `.marko-run/routes.d.ts` whenever the project is built - including dev.
|
|
610
|
+
|
|
605
611
|
> **Note** TypeScript will not include this file by default. You should use the [Marko VSCode plugin](https://marketplace.visualstudio.com/items?itemName=Marko-JS.marko-vscode) and [add it in your tsconfig](https://www.typescriptlang.org/tsconfig#include).
|
|
606
612
|
|
|
607
613
|
These types are replaced with more specific versions per routable file:
|
|
608
614
|
|
|
609
615
|
**`MarkoRun.Handler`**
|
|
616
|
+
|
|
610
617
|
- Overrides context with specific MarkoRun.Context
|
|
611
618
|
|
|
612
619
|
**`MarkoRun.Route`**
|
|
613
|
-
|
|
620
|
+
|
|
621
|
+
- Adds specific parameters and meta types
|
|
614
622
|
- In middleware and layouts which are used in many routes, this type will be a union of all possible routes that the file will see
|
|
615
623
|
|
|
616
624
|
**`MarkoRun.Context`**
|
|
625
|
+
|
|
617
626
|
- In middleware and layouts which are used in many routes, this type will be a union of all possible routes that the file will see.
|
|
618
627
|
- When an adapter is used, it can provide types for the platform
|
|
619
628
|
|