cayo 0.9.10 → 1.0.0

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.
Files changed (122) hide show
  1. package/README.md +504 -5
  2. package/cayo.js +1 -298
  3. package/devlog.md +177 -0
  4. package/dist/cayo.svelte.js +80 -0
  5. package/dist/entry.svelte.js +16 -0
  6. package/dist/index.js +2 -0
  7. package/docs/config-reference.md +349 -0
  8. package/docs/how-cayo-works.md +28 -0
  9. package/docs/why-i-created-cayo.md +21 -0
  10. package/lib/cli/build.js +34 -0
  11. package/lib/cli/cli.js +152 -0
  12. package/lib/cli/dev.js +12 -0
  13. package/lib/cli/serve.js +25 -0
  14. package/lib/cli/watch.js +125 -0
  15. package/lib/core/bundle.js +155 -0
  16. package/lib/core/codegen.js +156 -0
  17. package/lib/core/compile/cayos.js +65 -0
  18. package/lib/core/compile/index.js +3 -0
  19. package/lib/core/compile/pages.js +61 -0
  20. package/lib/core/compile/template.js +24 -0
  21. package/lib/core/component.js +62 -0
  22. package/lib/core/config.js +206 -0
  23. package/lib/core/dependencies.js +167 -0
  24. package/lib/core/entry.js +24 -0
  25. package/lib/core/files.js +99 -0
  26. package/lib/core/logger.js +47 -0
  27. package/lib/core/page.js +59 -0
  28. package/lib/core/render/prerender.js +240 -0
  29. package/lib/core/render/renderer.js +52 -0
  30. package/lib/core/utils.js +60 -0
  31. package/package.json +21 -10
  32. package/scripts/build.js +60 -0
  33. package/src/cayo-warnings.js +37 -0
  34. package/src/cayo.svelte +36 -0
  35. package/src/entry.svelte +6 -0
  36. package/template/cayo.config.js +3 -0
  37. package/template/package-lock.json +4027 -0
  38. package/template/package.json +10 -0
  39. package/template/public/vite.svg +1 -0
  40. package/{tests/basic cases (old)/src/__index.svelte → template/src/__template.svelte} +3 -6
  41. package/template/src/components/counter.cayo.svelte +27 -0
  42. package/template/src/index.js +4 -0
  43. package/template/src/pages/index.svelte +15 -0
  44. package/template/src/style.css +13 -0
  45. package/lib/cli.js +0 -69
  46. package/lib/codegen.js +0 -79
  47. package/lib/components/Cayo.svelte +0 -25
  48. package/lib/config.js +0 -155
  49. package/lib/files.js +0 -84
  50. package/lib/prerender.js +0 -181
  51. package/lib/renderer.js +0 -49
  52. package/lib/runtime.js +0 -6
  53. package/lib/utils.js +0 -126
  54. package/lib/vite.config.js +0 -16
  55. package/notes.md +0 -3
  56. package/test/cayo.config.js +0 -35
  57. package/test/public/assets/cow.js +0 -1
  58. package/test/public/images/app-icon.png +0 -0
  59. package/test/src/__layout.svelte +0 -20
  60. package/test/src/components/Cool.cayo.svelte +0 -5
  61. package/test/src/components/Some.svelte +0 -1
  62. package/test/src/components/Test.cayo.svelte +0 -5
  63. package/test/src/index.js +0 -17
  64. package/test/src/main2.js +0 -1
  65. package/test/src/pages/hey.svelte +0 -8
  66. package/test/src/pages/howdy.svelte +0 -11
  67. package/test/src/pages/index.svelte +0 -38
  68. package/test/src/pages/some/page.svelte +0 -2
  69. package/tests/asset-dir/cayo.config.js +0 -38
  70. package/tests/asset-dir/package-lock.json +0 -1435
  71. package/tests/asset-dir/package.json +0 -19
  72. package/tests/asset-dir/public/images/app-icon.png +0 -0
  73. package/tests/asset-dir/src/__layout.svelte +0 -20
  74. package/tests/asset-dir/src/components/CayoExample.svelte +0 -5
  75. package/tests/asset-dir/src/components/Some.cayo.svelte +0 -6
  76. package/tests/asset-dir/src/index.js +0 -5
  77. package/tests/asset-dir/src/pages/index.svelte +0 -19
  78. package/tests/base-path/cayo.config.js +0 -36
  79. package/tests/base-path/package-lock.json +0 -1435
  80. package/tests/base-path/package.json +0 -19
  81. package/tests/base-path/public/assets/cow.js +0 -1
  82. package/tests/base-path/public/images/app-icon.png +0 -0
  83. package/tests/base-path/src/__layout.svelte +0 -20
  84. package/tests/base-path/src/components/CayoExample.svelte +0 -5
  85. package/tests/base-path/src/components/Some.cayo.svelte +0 -6
  86. package/tests/base-path/src/index.js +0 -5
  87. package/tests/base-path/src/pages/howdy.svelte +0 -12
  88. package/tests/base-path/src/pages/index.svelte +0 -20
  89. package/tests/basic/notcayo.config.js +0 -35
  90. package/tests/basic/package-lock.json +0 -1435
  91. package/tests/basic/package.json +0 -19
  92. package/tests/basic/public/assets/cow.js +0 -1
  93. package/tests/basic/public/images/app-icon.png +0 -0
  94. package/tests/basic/src/__layout.svelte +0 -20
  95. package/tests/basic/src/components/Cool.cayo.svelte +0 -4
  96. package/tests/basic/src/components/Some.svelte +0 -1
  97. package/tests/basic/src/index.js +0 -5
  98. package/tests/basic/src/main2.js +0 -1
  99. package/tests/basic/src/pages/hey.svelte +0 -8
  100. package/tests/basic/src/pages/howdy.svelte +0 -11
  101. package/tests/basic/src/pages/index.svelte +0 -33
  102. package/tests/basic/src/pages/some/some.svelte +0 -2
  103. package/tests/basic cases (old)/src/components/Cool.cayo.svelte +0 -4
  104. package/tests/basic cases (old)/src/components/Some.svelte +0 -1
  105. package/tests/basic cases (old)/src/components/dir/Cool.cayo.svelte +0 -4
  106. package/tests/basic cases (old)/src/main.js +0 -1
  107. package/tests/basic cases (old)/src/main2.js +0 -1
  108. package/tests/basic cases (old)/src/pages/hey.svelte +0 -2
  109. package/tests/basic cases (old)/src/pages/howdy.svelte +0 -11
  110. package/tests/basic cases (old)/src/pages/index.svelte +0 -27
  111. package/tests/nested-pages/cayo.config.js +0 -35
  112. package/tests/nested-pages/package-lock.json +0 -1435
  113. package/tests/nested-pages/package.json +0 -19
  114. package/tests/nested-pages/public/assets/cow.js +0 -1
  115. package/tests/nested-pages/public/images/app-icon.png +0 -0
  116. package/tests/nested-pages/src/__layout.svelte +0 -20
  117. package/tests/nested-pages/src/components/Cool.cayo.svelte +0 -4
  118. package/tests/nested-pages/src/index.js +0 -5
  119. package/tests/nested-pages/src/main2.js +0 -1
  120. package/tests/nested-pages/src/pages/index.svelte +0 -18
  121. package/tests/nested-pages/src/pages/some/other/page.svelte +0 -7
  122. package/tests/nested-pages/src/pages/some/page.svelte +0 -6
@@ -0,0 +1,349 @@
1
+ # Config Reference
2
+
3
+ ## Config File
4
+
5
+ Cayo is not "zero config" because it does require a particular [project structure](../README.md#project-structure), but it does support having _no config file_. However, adding a `cayo.config.js` file to your project allows you to configure most of the things that Cayo needs & expects, such as paths for pages, components, and even the template filename.
6
+
7
+ ### Example
8
+ ```js
9
+ // cayo.config.js
10
+ export default {
11
+ // config options
12
+ pages: 'outputs', // default: 'pages'
13
+ components: 'cayos', // default: 'components'
14
+ build: {
15
+ outDir: 'build', // default: 'dist'
16
+ },
17
+ svelte: {
18
+ // Svelte options, e.g., for defining preprocessors for svelte.preprocess
19
+ },
20
+ vite: {
21
+ // Vite options, like server.port
22
+ server: {
23
+ port: 1234
24
+ },
25
+ rollupOptions: {
26
+ // Rollup options, like plugins
27
+ }
28
+ }
29
+ }
30
+ ```
31
+
32
+ Check out [more config examples](#more-examples).
33
+
34
+ ## Cayo Options
35
+
36
+ ### projectRoot
37
+ - **Type**: `string`
38
+ - **Default**: `'.'`
39
+
40
+ The project root directory, where Cayo will look for the required project structure. Can be an absolute path or a directory relative to the current working directory.
41
+
42
+ ---
43
+
44
+ ### src
45
+ - **Type**: `string`
46
+ - **Default**: `'src'`
47
+
48
+ Specify the directory where your source files will be, relative to the [project root](#projectroot). This folder will be watched for changes during dev.
49
+
50
+ ---
51
+
52
+ ### pages
53
+ - **Type**: `string`
54
+ - **Default**: `'pages'`
55
+
56
+ Specify the directory where your page components will be, relative to `src`. These can also just be thought of as "inputs" for the HTML that is generated as output.
57
+
58
+ ---
59
+
60
+ ### components
61
+ - **Type**: `string`
62
+ - **Default**: `'components'`
63
+
64
+ Specify the directory where your Cayo Components will be, relative to `src`. Cayo will expect any `<name>.cayo.svelte` that is to be rendered on a page to be in this directory. Other components are not required to be in this directory, but can be.
65
+
66
+ ---
67
+
68
+ ### publicDir
69
+ - **Type**: `string`
70
+ - **Default**: `'public'`
71
+
72
+ > **Note**<br>
73
+ > Use this option _instead_ of Vite's `publicDir` option. We pass it to Vite, but also use it internally within Cayo processes.
74
+
75
+ Specify the directory to be used as the public directory, which is used to serve static assets during development and copied into the `build.outDir` during build.
76
+
77
+ This is where your static assets will go, such as images, `favicon.ico`, and any static JS dependencies.
78
+
79
+ You will be able to reference any of these assets with a leading `/` in your markup.
80
+
81
+ **For example:** assuming you haven't changed the default, and you have this in your project root:
82
+
83
+ ```
84
+ src/
85
+ └ pages/
86
+ └ index.svelte
87
+ public/
88
+ └ some.png
89
+ ```
90
+
91
+ ```svelte
92
+ <!-- index.svelte -->
93
+ <img src="/some.png">
94
+ ```
95
+
96
+ Read about how the [public directory works in Vite](https://vitejs.dev/guide/assets.html#the-public-directory).
97
+
98
+ ---
99
+
100
+ ### base
101
+ - **Type**: `string`
102
+ - **Default**: `'/'`
103
+
104
+ > **Note**<br>
105
+ > Use this option _instead_ of Vite's `base` option. We pass it to Vite, but also use it internally within Cayo processes.
106
+
107
+ Base public path when served in development or production.
108
+
109
+ <!-- TODO: add more details -->
110
+
111
+ Since this option is passed to Vite, read more about it in the [Vite docs](https://vitejs.dev/config/shared-options.html#base).
112
+
113
+ ---
114
+
115
+ ### build.outDir
116
+ - **Type**: `string`
117
+ - **Default**: `'dist'`
118
+
119
+ Specify the output directory, relative to the [project root](#projectroot).
120
+
121
+ ---
122
+
123
+ ### build.assetsDir
124
+ - **Type**: `string`
125
+ - **Default**: `'assets'`
126
+
127
+ Specify where the assets generated by Cayo should be placed within your output, relative to `build.outDir`.
128
+
129
+ ---
130
+
131
+ ### css.internal
132
+ - **Type**: `boolean`
133
+ - **Default**: `false`
134
+
135
+ If set to `true`, this will inject a page's CSS inside `<style>` elements, in place of `<link>` elements.
136
+
137
+ This will only affect the component-scoped CSS you define in a Svelte component (or page), and any CSS imported in an entry file.
138
+
139
+ ---
140
+
141
+ ### cayoPath
142
+ - **Type**: `string`
143
+ - **Default**: `'.cayo'`
144
+
145
+ Specify the Cayo output directory that is used for Cayo processes. Cayo outputs files into this folder, and it's contents are either served during `cayo dev` or used to build your project during `cayo build`.
146
+
147
+ ---
148
+
149
+ ### cayoComponentInfix
150
+ - **Type**: `string`
151
+ - **Default**: `'cayo'`
152
+
153
+ Specify the Cayo Component infix that is used to identify by Cayo's processes (Note: `<name>.<infix>.<extension>`). By default, a Cayo looks like: `some.cayo.svelte`. If you changed this option's value to `'fancy'`, your Cayo would need to be named `some.fancy.svelte`.
154
+
155
+ ---
156
+
157
+ ### templateName
158
+ - **Type**: `string`
159
+ - **Default**: `'__template'`
160
+
161
+ Specify the name of the template file. This is **_not_** a path; Cayo will always expect this filename to be at the root of the `src` directory (e.g., `src/__template.svelte`). The extension, `.svelte` should not be included in this option's value.
162
+
163
+ ---
164
+
165
+ ### mode
166
+ - **Type**: `string | false`
167
+ - **Default**: `'development'`
168
+
169
+ By default, the mode will be `development` during `cayo dev` and `production` during `cayo build`. This option can be used to override the default mode, and will be used for both commands.
170
+
171
+ ---
172
+
173
+ ### debug
174
+ - **Type**: `boolean`
175
+ - **Default**: `false`
176
+
177
+ Useful for seeing more output when running `cayo`.
178
+
179
+ > **Note**<br>
180
+ > This option is experimental and may be removed in the future.
181
+
182
+ ---
183
+
184
+ ## Svelte Options
185
+
186
+ There are a few options that Cayo passes to Svelte (consumed by `rollup-plugin-svelte`):
187
+ - [preprocess](#sveltepreprocess)
188
+ - [compilerOptions](#sveltecompileroptions)
189
+ <!-- TODO: - [extensions](#svelteextensions) -->
190
+
191
+ Svelte options are passed via the Cayo config:
192
+ ```js
193
+ // cayo.config.js
194
+ export default {
195
+ svelte: {
196
+ preprocess: [
197
+ // define custom preprocessors for Svelte to use
198
+ ],
199
+ compilerOptions: {
200
+ // define compiler options for Svelte to use
201
+ }
202
+ }
203
+ }
204
+ ```
205
+
206
+ ---
207
+
208
+ ### svelte.preprocess
209
+ - **Type**: `<SveltePreprocessor> | Array<SveltePreprocessor>`
210
+ - **Default**: `[]`
211
+
212
+ Passed to the Svelte Rollup plugin, but is appended to an array of Cayo's internally defined preprocessors (which is only [`svelte-preprocess`](https://github.com/sveltejs/svelte-preprocess)). [See plugin options](https://github.com/sveltejs/rollup-plugin-svelte#usage).
213
+
214
+ ---
215
+
216
+ ### svelte.compilerOptions
217
+ - **Type**: `object` _(of options)_
218
+ - **Default**: `{}`
219
+
220
+ There are a few options that Cayo needs to control in order to work, so changing them will have no effect: `compilerOptions.generate` and `compilerOptions.hydratable`. All other options will be passed to the Svelte Rollup plugin. [See plugin options](https://github.com/sveltejs/rollup-plugin-svelte#usage).
221
+
222
+ ---
223
+
224
+ ## Vite Options
225
+
226
+ Not all Vite options are directly used by Cayo. Most of them are passed to the dev process, which uses `vite dev`. `vite.build.rollupOptions` is the only Vite-specific option passed during both `cayo dev` and `cayo build`.
227
+
228
+ > **Note**<br>
229
+ > Cayo shares some options with Vite, such as `publicDir` and `base` and `root` (as `projectRoot`). These should be defined at top level of your Cayo config, not in the `vite` option object, but will be passed to Vite as needed.
230
+
231
+ Vite options are passed via the Cayo config:
232
+ ```js
233
+ // cayo.config.js
234
+ export default {
235
+ vite: {
236
+ server: {
237
+ // server options, like port
238
+ }
239
+ build: {
240
+ // rollupOptions is the only passed vite.build option
241
+ rollupOptions: {
242
+ // define Rollup options to be used for dev and build, like plugins
243
+ },
244
+ // other Vite options...
245
+ }
246
+ }
247
+ ```
248
+
249
+ During `cayo dev`, Cayo defines a few Vite options internally, which cannot be overridden:
250
+ - `configFile: false`
251
+
252
+ ## Rollup Options
253
+
254
+ Options for Rollup should be passed to Cayo via the `vite.build.rollupOptions` object. The only Rollup config option that Cayo uses internally is `vite.build.rollupOptions.plugins`, but the entire `rollupOptions` object is passed to Vite while running the dev server and during build.
255
+
256
+ ## More Examples
257
+
258
+ ### Customize `svelte-preprocess`
259
+ To add options to Svelte Preprocess, install `svelte-preprocess` locally, and then pass the preprocessor(s) in your Cayo config with options:
260
+ ```js
261
+ // cayo.config.js
262
+ import sveltePreprocess from 'svelte-preprocess';
263
+ export default {
264
+ svelte {
265
+ preprocess: [
266
+ sveltePreprocess({
267
+ // Learn more about the options you can pass here:
268
+ // https://github.com/sveltejs/svelte-preprocess/blob/main/docs/preprocessing.md
269
+ markupTagName: 'content',
270
+ replace: [['Jane', 'Cool'], ['Doe', 'Cat']]
271
+ }),
272
+ ],
273
+ }
274
+ }
275
+ ```
276
+ ---
277
+
278
+ ### Add `mdsvex`
279
+ Adding other Svelte preprocessors works similar to how it would in any other Vite + Svelte project.
280
+
281
+ For example, let's refactor mdsvex's own docs example for [customizing extensions](https://mdsvex.pngwn.io/docs#extensions).
282
+
283
+ To import markdown files as components, add `.md` to both the Svelte compiler and mdsvex extensions:
284
+ ```js
285
+ // cayo.config.js
286
+ import { mdsvex } from "mdsvex";
287
+ export default {
288
+ // Putting these options inside the svelte option object is the only difference
289
+ svelte: {
290
+ extensions: ['.svelte', '.svx', '.md'],
291
+ preprocess: [
292
+ mdsvex({ extensions: ['.svx', '.md'] }),
293
+ ],
294
+ }
295
+ }
296
+ ```
297
+
298
+ ---
299
+
300
+ ### Conditional Config
301
+ Cayo doesn't have built-in features to support a conditional config. E.g., say you different config options for `cayo dev` and `cayo build`.
302
+
303
+ However, you can set up your dev environment to support this yourself. Here's an example that uses the `NODE_ENV` environment variable to swap configs when running `cayo`:
304
+
305
+ **Define scripts that set the environment while running `cayo dev` and `cayo build`**
306
+
307
+ `package.json`:
308
+ ```json
309
+ "scripts": {
310
+ "dev": "export NODE_ENV=development && cayo dev",
311
+ "build": "export NODE_ENV=production && cayo build"
312
+ }
313
+ ```
314
+
315
+ **Add conditions in your Cayo config based on the value of NODE_ENV**
316
+
317
+ `cayo.config.js`:
318
+ ```js
319
+ let config;
320
+ if (process.env.NODE_ENV === 'development') {
321
+ config = {
322
+ // development options for `cayo dev`
323
+ }
324
+ } else if (process.env.NODE_ENV === 'production') {
325
+ config = {
326
+ // production options for `cayo build`
327
+ }
328
+ }
329
+
330
+ export default config;
331
+ ```
332
+
333
+ Now when you run `npm run dev`:
334
+ - `cayo dev` will run but also set `NODE_ENV` to `'development'`
335
+ - the dev config will be used
336
+
337
+ With this scripts setup, you could also use `process.env.NODE_ENV` in your components. Say you wanted to have a conditional [Template](../README.md#template-file):
338
+
339
+ `__template.svelte`:
340
+ ```svelte
341
+ {#if process.env.NODE_ENV === 'development'}
342
+ <div class="dev">
343
+ I'm running in development mode
344
+ </div>
345
+ %cayo.body%
346
+ {:else}
347
+ %cayo.body%
348
+ {/if}
349
+ ```
@@ -0,0 +1,28 @@
1
+ # How Cayo Works
2
+
3
+ Some additional explanation of how things work within Cayo. This isn't need-to-know info in order to use Cayo, but if you're curious...
4
+
5
+ ## Render Hook
6
+
7
+ The generated hook function looks something like this in `cayo-runtime.js` files:
8
+ ```js
9
+ // cayo-runtime.js
10
+ export default renderCayos(cb) {
11
+ var target = cb ? cb : (node) => node;
12
+ let cayos = {};
13
+ // Code that adds cayo instances to the `cayos` object
14
+ // Example, based on counter.cayo.svelte from earlier
15
+ // 'counter-f00b4r' is a generated unique identifier for the instance
16
+ cayos['counter-f00b4r'] = {};
17
+ cayos['counter-f00b4r'].target = document.querySelector('[data-cayo-id="counter-f00b4r"]');
18
+ cayos['counter-f00b4r'].instance = new ${componentName}({
19
+ target: target(cayos['counter-f00b4r'].target),
20
+ hydrate: true,
21
+ props: getProps(cayos['counter-f00b4r'].target),
22
+ });
23
+
24
+ return cayos;
25
+ }
26
+ ```
27
+
28
+ **Note:** `getProps()` is also generated in `cayo-runtime.js`. It handles parsing the props as a string, to use the props to hydrate the Cayo instance.
@@ -0,0 +1,21 @@
1
+ ## Why I Created Cayo
2
+
3
+ With so many other great tools out there, it does seem kind of silly to create a new, pretty niche tool with less features. It really came down to me having limitations to how I can build web pages on the particular web platform we use at work. Most of the pages I work with are content pages, with no need for any data flow to a database, and in general have small bits of interactivity (carousels, modals, etc). The "big stuff" is handled all within the source code of the web platform's framework. I don't have access to that source code, but I do have access to a CMS that essentially lets me upload HTML snippets them as a static page, or as a static _part_ of page. I call that the "little stuff."
4
+
5
+ Also, I love Svelte. I wanted to use Svelte to write those HTML snippets. Since Svelte really is a compiler at heart, it seemed like the perfect fit. I realized I could use the Svelte API to server side render (SSR) them to HTML strings in any node environment, like in a CLI.
6
+
7
+ Another requirement was: little-to-no-JS. For most of the content I build to then be imported into the CMS, it's completely static. Occasionally I need some JS to make a carousel do its thing. But, my JS can only be imported via a `<script>` within my HTML snippet—that means my JS gets loaded last, after anything already being loaded by the web platform in the <head>. Ultimately, I've gotta be careful about when/what JS is running in my HTML snippets, as their JS always gets delayed.
8
+
9
+ That's where Cayo Components come in. I wanted to be able to give the power of Svelte to those limited cases where I do need interactivity (JS). For the "islands of reactivity", or _cayos_, instead of SSR-ing these components, I bundle them up as JS for client side rendering (CSR). CSR components just require the Svelte vendor code to run, which is pretty small. And the output bundle is typically pretty small (but does scales with the complexity of the component itself), so I wasn't too worried about the overhead to have Svelte components run client side.
10
+
11
+ ### Okay, but can't SvelteKit and Astro do all of those things?
12
+
13
+ Mostly, yes! And that's why Cayo is not recommended for most projects; those other frameworks are great for most people.
14
+
15
+ SvelteKit does have an adapter that is for using it as a "static site generator" (SSG), but I found that it still doesn't quite get you to a zero-JS output. Because SvelteKit is much more than an SSG, it isn't tailored to be one, so your output still includes some JS files and a hydration script on your page, even if you don't have _any_ JS for that page to make use of all that overhead/setup. For most people, that doesn't really matter! For my use case, I'd need to run scripts to comb through all that unnecessary overhead and remove it from the markup. That's much easier than creating an entirely new tool, but since SvelteKit is still in beta at the time of writing Cayo, I didn't want to find myself wrestling with SvelteKit as it's in flux.
16
+
17
+ SvelteKit also doesn't support partial hydration within a page. It does however support prerendering page A, and hyrdrating page B, but that's not quite the same as island architecture.
18
+
19
+ Astro, however, does check nearly every box for me. It supports island architecture (and their Astro Components have more features than Cayo Components). Their output doesn't include all the extra that SvelteKit includes when it doesn't need it. _At the time I set out to create Cayo_, the only thing missing for me was Vite—I wanted to be able to make use of Vite's plugin ecosystem. Now, it does use Vite. (So, go use Astro.)
20
+
21
+ I studied Svelte, SvelteKit, and Astro docs and source code for hours throughout building Cayo; it really wouldn't exist without being inspired by those teams and ingenuity behind those projects.
@@ -0,0 +1,34 @@
1
+ import path from 'path';
2
+ import { build as viteBuild } from 'vite';
3
+
4
+ export async function build(_cayo) {
5
+ const { config } = _cayo;
6
+ const root = config.cayoPath;
7
+ const inputs = {};
8
+ for (const [, page] of _cayo.pages) {
9
+ let key = page.url;
10
+ let source = `./${page.url}/index.html`
11
+ if (page.url === '/') {
12
+ key = 'index';
13
+ source = './index.html'
14
+ }
15
+
16
+ inputs[key] = path.resolve(root, source);
17
+ }
18
+
19
+ return await viteBuild({
20
+ root: config.cayoPath,
21
+ base: config.base,
22
+ publicDir: config.publicDir,
23
+ build: {
24
+ ...config.build,
25
+ emptyOutDir: true,
26
+ rollupOptions: {
27
+ ...config.vite.rollupOptions,
28
+ input: {
29
+ ...inputs,
30
+ },
31
+ }
32
+ }
33
+ })
34
+ }
package/lib/cli/cli.js ADDED
@@ -0,0 +1,152 @@
1
+ import yargs from 'yargs-parser';
2
+ import fs from 'fs-extra';
3
+ import chalk from 'chalk';
4
+ import path from 'path';
5
+ import { fileURLToPath } from 'url';
6
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
7
+
8
+ import {
9
+ writePageFiles,
10
+ cleanCayoPath,
11
+ } from '#core/files.js';
12
+
13
+ import { loadConfig } from '#core/config.js';
14
+ import * as compile from '#core/compile/index.js';
15
+ import logger from '#core/logger.js';
16
+ import { debugStats } from '#core/utils.js';
17
+
18
+ import { dev } from './dev.js';
19
+ import { build } from './build.js';
20
+
21
+ // / Handle arguments
22
+ function resolveArgs(argv) {
23
+ const cmd = argv._[2];
24
+
25
+ const options = {
26
+ projectRoot: typeof argv.projectRoot === 'string' ? argv.projectRoot : undefined,
27
+ configPath: typeof argv.config === 'string' ? argv.config : undefined,
28
+ mode: cmd === 'build' ? 'production' : 'development',
29
+ }
30
+
31
+ switch (cmd) {
32
+ case 'build':
33
+ case 'dev':
34
+ case 'help':
35
+ return { cmd: cmd, options };
36
+ default:
37
+ return { cmd: 'unknown', options };
38
+ }
39
+ }
40
+
41
+ // Run
42
+ export async function cli(args) {
43
+ const argv = yargs(args);
44
+ const command = resolveArgs(argv);
45
+ run(command);
46
+ }
47
+
48
+ const commands = new Map([
49
+ ['build', async (_cayo) => {
50
+ build(_cayo);
51
+ // await prerenderPages(config).then((pages)=> {
52
+ // build(config, pages);
53
+ // });
54
+ }],
55
+ ['dev', (_cayo) => {
56
+ dev(_cayo);
57
+ // prerenderPages(config);
58
+ // watch(config);
59
+ // serve(config);
60
+ }],
61
+ ['help', (_cayo) => {
62
+ help(_cayo, logger);
63
+ }],
64
+ ['unknown', (_cayo) => {
65
+ help(_cayo, logger, true);
66
+ }],
67
+ ]);
68
+
69
+ function help(_cayo, logger, isUnknownCommand) {
70
+ // TODO: help info
71
+ if (isUnknownCommand) console.log(`unknown command. here's some help:`)
72
+ console.log('help');
73
+ }
74
+
75
+ async function run(command) {
76
+ const { cmd, options } = command;
77
+ logger.log.info(
78
+ `\n ${chalk.magenta.bold(`cayo.${cmd}`)}${chalk.dim(` starting`)}`,
79
+ { timestamp: false }
80
+ );
81
+
82
+ // Initialize Cayo Config
83
+ let cayoConfig;
84
+ try {
85
+ cayoConfig = await loadConfig(options);
86
+ } catch (err) {
87
+ logger.error(err);
88
+ process.exit(1);
89
+ }
90
+
91
+ try {
92
+ const __VERSION__ = (await fs.readJSON(path.resolve(__dirname, '../../package.json'))).version;
93
+ // Initialize Cayo CLI runtime data
94
+ const _cayo = {
95
+ template: null,
96
+ pages: new Map(),
97
+ components: new Map(),
98
+ stats: {
99
+ dependencies: {
100
+ pages: {},
101
+ components: {},
102
+ entries: {},
103
+ assets: {},
104
+ },
105
+ cayoComponents: { },
106
+ compiled: {
107
+ paths: new Set(),
108
+ },
109
+ },
110
+ config: cayoConfig,
111
+ logger,
112
+ __VERSION__,
113
+ }
114
+
115
+ // Create a fresh cayo folder for this run
116
+ cleanCayoPath(_cayo.config.cayoPath);
117
+
118
+ // Compile & Render all user page and their dependent component files
119
+ try {
120
+ // Compile template that will be used by all pages
121
+ await compile.template(_cayo);
122
+
123
+ await compile.pages(null, _cayo);
124
+ for (const [key, page] of _cayo.pages) {
125
+ page.dependencies = _cayo.stats.dependencies.pages[key];
126
+ await page.render(_cayo, { load: true });
127
+ }
128
+
129
+ for (const [, page] of _cayo.pages) {
130
+ await writePageFiles(page, _cayo);
131
+ }
132
+
133
+ } catch (err) {
134
+ logger.error(err);
135
+ if (cmd === 'build') {
136
+ process.exit(1);
137
+ }
138
+ }
139
+
140
+ // for debugging
141
+ if (_cayo.config.debug) {
142
+ debugStats(_cayo);
143
+ }
144
+
145
+ // Run the command
146
+ commands.get(cmd)(_cayo);
147
+
148
+ } catch (err) {
149
+ logger.error(err);
150
+ process.exit(1);
151
+ }
152
+ }
package/lib/cli/dev.js ADDED
@@ -0,0 +1,12 @@
1
+ import { serve } from './serve.js';
2
+ import { watch } from './watch.js';
3
+
4
+ export function dev(_cayo) {
5
+ // for (const [,page] of _cayo.pages) {
6
+ // page.render(true, _cayo.stats.cayoComponents).then(() =>
7
+ // writePageFiles(page, config.cayoPath, config)
8
+ // );
9
+ // }
10
+ watch(_cayo);
11
+ serve(_cayo.config);
12
+ }
@@ -0,0 +1,25 @@
1
+ import { createServer } from 'vite';
2
+ import merge from 'deepmerge';
3
+
4
+ export async function serve(config) {
5
+
6
+ const serveConfig = {
7
+ root: config.cayoPath,
8
+ publicDir: config.publicDir,
9
+ mode: config.mode,
10
+ base: config.base,
11
+ configFile: false,
12
+ }
13
+
14
+ const server = await createServer({
15
+ ...merge(config.vite, serveConfig),
16
+ });
17
+
18
+ await server.listen();
19
+ server.printUrls();
20
+
21
+ // logger.log.info(
22
+ // chalk.green('Server running at') + chalk.dim(`${page.name}`),
23
+ // { timestamp: true }
24
+ // );
25
+ }