cayo 0.9.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.
- package/LICENSE.md +21 -0
- package/README.md +6 -0
- package/cayo.js +295 -0
- package/jsconfig.json.txt +34 -0
- package/lib/cli.js +69 -0
- package/lib/codegen.js +77 -0
- package/lib/components/Cayo.svelte +24 -0
- package/lib/config.js +155 -0
- package/lib/files.js +84 -0
- package/lib/prerender.js +181 -0
- package/lib/renderer.js +45 -0
- package/lib/runtime.js +6 -0
- package/lib/utils.js +120 -0
- package/lib/vite.config.js +16 -0
- package/notes.md +3 -0
- package/package.json +39 -0
- package/test/cayo.config.js +35 -0
- package/test/public/assets/cow.js +1 -0
- package/test/public/images/app-icon.png +0 -0
- package/test/src/__layout.svelte +20 -0
- package/test/src/components/Cool.cayo.svelte +4 -0
- package/test/src/components/Some.svelte +1 -0
- package/test/src/index.js +5 -0
- package/test/src/main2.js +1 -0
- package/test/src/pages/hey.svelte +8 -0
- package/test/src/pages/howdy.svelte +11 -0
- package/test/src/pages/index.svelte +33 -0
- package/test/src/pages/some/page.svelte +2 -0
- package/tests/asset-dir/cayo.config.js +38 -0
- package/tests/asset-dir/package-lock.json +1435 -0
- package/tests/asset-dir/package.json +19 -0
- package/tests/asset-dir/public/images/app-icon.png +0 -0
- package/tests/asset-dir/src/__layout.svelte +20 -0
- package/tests/asset-dir/src/components/CayoExample.svelte +5 -0
- package/tests/asset-dir/src/components/Some.cayo.svelte +6 -0
- package/tests/asset-dir/src/index.js +5 -0
- package/tests/asset-dir/src/pages/index.svelte +19 -0
- package/tests/base-path/cayo.config.js +36 -0
- package/tests/base-path/package-lock.json +1435 -0
- package/tests/base-path/package.json +19 -0
- package/tests/base-path/public/assets/cow.js +1 -0
- package/tests/base-path/public/images/app-icon.png +0 -0
- package/tests/base-path/src/__layout.svelte +20 -0
- package/tests/base-path/src/components/CayoExample.svelte +5 -0
- package/tests/base-path/src/components/Some.cayo.svelte +6 -0
- package/tests/base-path/src/index.js +5 -0
- package/tests/base-path/src/pages/howdy.svelte +12 -0
- package/tests/base-path/src/pages/index.svelte +20 -0
- package/tests/basic/notcayo.config.js +35 -0
- package/tests/basic/package-lock.json +1435 -0
- package/tests/basic/package.json +19 -0
- package/tests/basic/public/assets/cow.js +1 -0
- package/tests/basic/public/images/app-icon.png +0 -0
- package/tests/basic/src/__layout.svelte +20 -0
- package/tests/basic/src/components/Cool.cayo.svelte +4 -0
- package/tests/basic/src/components/Some.svelte +1 -0
- package/tests/basic/src/index.js +5 -0
- package/tests/basic/src/main2.js +1 -0
- package/tests/basic/src/pages/hey.svelte +8 -0
- package/tests/basic/src/pages/howdy.svelte +11 -0
- package/tests/basic/src/pages/index.svelte +33 -0
- package/tests/basic/src/pages/some/some.svelte +2 -0
- package/tests/basic cases (old)/src/__index.svelte +18 -0
- package/tests/basic cases (old)/src/components/Cool.cayo.svelte +4 -0
- package/tests/basic cases (old)/src/components/Some.svelte +1 -0
- package/tests/basic cases (old)/src/components/dir/Cool.cayo.svelte +4 -0
- package/tests/basic cases (old)/src/main.js +1 -0
- package/tests/basic cases (old)/src/main2.js +1 -0
- package/tests/basic cases (old)/src/pages/hey.svelte +2 -0
- package/tests/basic cases (old)/src/pages/howdy.svelte +11 -0
- package/tests/basic cases (old)/src/pages/index.svelte +27 -0
- package/tests/nested-pages/cayo.config.js +35 -0
- package/tests/nested-pages/package-lock.json +1435 -0
- package/tests/nested-pages/package.json +19 -0
- package/tests/nested-pages/public/assets/cow.js +1 -0
- package/tests/nested-pages/public/images/app-icon.png +0 -0
- package/tests/nested-pages/src/__layout.svelte +20 -0
- package/tests/nested-pages/src/components/Cool.cayo.svelte +4 -0
- package/tests/nested-pages/src/index.js +5 -0
- package/tests/nested-pages/src/main2.js +1 -0
- package/tests/nested-pages/src/pages/index.svelte +18 -0
- package/tests/nested-pages/src/pages/some/other/page.svelte +7 -0
- package/tests/nested-pages/src/pages/some/page.svelte +6 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Matthew I. Alicea
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/cayo.js
ADDED
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import yargs from 'yargs-parser';
|
|
4
|
+
import chokidar from 'chokidar';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import chalk from 'chalk';
|
|
7
|
+
import merge from 'deepmerge';
|
|
8
|
+
import { createServer, createLogger, build as viteBuild } from 'vite';
|
|
9
|
+
import { loadConfig, checkConfigPaths } from './lib/config.js';
|
|
10
|
+
import { prerender } from './lib/prerender.js';
|
|
11
|
+
import {
|
|
12
|
+
writePageFiles,
|
|
13
|
+
writeComponentFile,
|
|
14
|
+
cleanCayoPath,
|
|
15
|
+
writeTemplateCSS,
|
|
16
|
+
} from './lib/files.js';
|
|
17
|
+
import {
|
|
18
|
+
hash,
|
|
19
|
+
getPageModules,
|
|
20
|
+
getComponentModules,
|
|
21
|
+
createTemplateManifest,
|
|
22
|
+
createPageManifest,
|
|
23
|
+
createComponentManifest,
|
|
24
|
+
} from './lib/utils.js';
|
|
25
|
+
|
|
26
|
+
const logger = createLogger('info', {
|
|
27
|
+
prefix: chalk.magenta.bold('[cayo]'),
|
|
28
|
+
// allowClearScreen: true,
|
|
29
|
+
});
|
|
30
|
+
const errorLogger = createLogger('warn', {
|
|
31
|
+
prefix: chalk.red.bold('[cayo]'),
|
|
32
|
+
// allowClearScreen: true,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const data = {
|
|
36
|
+
template: undefined,
|
|
37
|
+
pages: undefined,
|
|
38
|
+
components: undefined,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// / Handle arguments
|
|
42
|
+
function resolveArgs(argv) {
|
|
43
|
+
const cmd = argv._[2];
|
|
44
|
+
|
|
45
|
+
const options = {
|
|
46
|
+
projectRoot: typeof argv.projectRoot === 'string' ? argv.projectRoot : undefined,
|
|
47
|
+
configPath: typeof argv.config === 'string' ? argv.config : undefined,
|
|
48
|
+
mode: cmd === 'build' ? 'production' : 'development',
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
switch (cmd) {
|
|
52
|
+
case 'build':
|
|
53
|
+
case 'dev':
|
|
54
|
+
case 'help':
|
|
55
|
+
return { cmd: cmd, options };
|
|
56
|
+
default:
|
|
57
|
+
return { cmd: 'help', options };
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function printHelp() {
|
|
62
|
+
// TODO: help info
|
|
63
|
+
console.log('help');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Run
|
|
67
|
+
export async function cli(args) {
|
|
68
|
+
const argv = yargs(args);
|
|
69
|
+
const command = resolveArgs(argv);
|
|
70
|
+
|
|
71
|
+
switch(command.cmd) {
|
|
72
|
+
case 'dev':
|
|
73
|
+
case 'build':
|
|
74
|
+
run(command);
|
|
75
|
+
break;
|
|
76
|
+
case 'help':
|
|
77
|
+
default:
|
|
78
|
+
printHelp();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const commands = new Map([
|
|
83
|
+
['build', async (config) => {
|
|
84
|
+
await prerenderPages(config).then((pages)=> {
|
|
85
|
+
build(config, pages);
|
|
86
|
+
});
|
|
87
|
+
}],
|
|
88
|
+
['dev', (config) => {
|
|
89
|
+
prerenderPages(config);
|
|
90
|
+
watch(config);
|
|
91
|
+
serve(config);
|
|
92
|
+
}]
|
|
93
|
+
]);
|
|
94
|
+
|
|
95
|
+
async function run(command) {
|
|
96
|
+
const { cmd, options } = command;
|
|
97
|
+
|
|
98
|
+
logger.info(
|
|
99
|
+
`\n ${chalk.magenta.bold(`cayo.${cmd}`)}${chalk.dim(` starting`)}`,
|
|
100
|
+
{ timestamp: false }
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
const config = await loadConfig(options);
|
|
105
|
+
checkConfigPaths(config, errorLogger);
|
|
106
|
+
|
|
107
|
+
// Create a fresh cayo folder for this run
|
|
108
|
+
cleanCayoPath(config.cayoPath);
|
|
109
|
+
|
|
110
|
+
getTemplate(config)
|
|
111
|
+
.then(() => getPages(config))
|
|
112
|
+
.then(() => getCayoComponents(config))
|
|
113
|
+
.then((components) => {
|
|
114
|
+
handleCayoComponents(components, config);
|
|
115
|
+
})
|
|
116
|
+
.then(() => {
|
|
117
|
+
const runCommand = commands.get(cmd);
|
|
118
|
+
runCommand(config);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
} catch (err) {
|
|
122
|
+
console.error(err);
|
|
123
|
+
process.exit(1);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async function getTemplate(config) {
|
|
128
|
+
const { src, cayoPath, templateFileName } = config;
|
|
129
|
+
return createTemplateManifest(path.resolve(src, `${templateFileName}.svelte`), cayoPath)
|
|
130
|
+
.then(async () => {
|
|
131
|
+
const { Template } = await import(path.resolve(cayoPath, `./__cayo/template.js?v=${hash()}`))
|
|
132
|
+
data.template = Template;
|
|
133
|
+
return data.template;
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async function getPages(config) {
|
|
138
|
+
const { pages, cayoPath } = config;
|
|
139
|
+
return createPageManifest(pages, cayoPath)
|
|
140
|
+
.then(async () => await import(path.resolve(cayoPath, `./__cayo/pages.js?v=${hash()}`)))
|
|
141
|
+
.then(({ pages }) => {
|
|
142
|
+
data.pages = getPageModules(pages, config);
|
|
143
|
+
return data.pages;
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async function getCayoComponents(config) {
|
|
148
|
+
const { src, cayoPath } = config;
|
|
149
|
+
return createComponentManifest(src, cayoPath)
|
|
150
|
+
.then(async () => await import(path.resolve(cayoPath, `./__cayo/components.js?v=${hash()}`)))
|
|
151
|
+
.then(({ components }) => {
|
|
152
|
+
data.components = getComponentModules(components, config);
|
|
153
|
+
return data.components;
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function watch(config) {
|
|
158
|
+
|
|
159
|
+
const watcher = chokidar.watch(config.src, {
|
|
160
|
+
// awaitWriteFinish: {
|
|
161
|
+
// stabilityThreshold: 1,
|
|
162
|
+
// pollInterval: 250
|
|
163
|
+
// },
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
const configWatcher = chokidar.watch(path.resolve(config.src, '../cayo.config.js'));
|
|
167
|
+
configWatcher.on('change', (filePath) => {
|
|
168
|
+
logger.info(
|
|
169
|
+
chalk.yellow(`> config updated... restart dev server to use new config.`),
|
|
170
|
+
{ timestamp: true, clear: true, }
|
|
171
|
+
);
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
const logChange = (type) => {
|
|
175
|
+
logger.info(
|
|
176
|
+
`> ${type} updated`,
|
|
177
|
+
{ timestamp: true, clear: true, }
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
watcher.on('change', async (filePath) => {
|
|
182
|
+
if (filePath.endsWith('.svelte')) {
|
|
183
|
+
if (filePath.endsWith(`${config.templateFileName}.svelte`)) {
|
|
184
|
+
logChange('template')
|
|
185
|
+
getTemplate(config)
|
|
186
|
+
.then(() => prerenderPages(config));
|
|
187
|
+
|
|
188
|
+
} else if (filePath.startsWith(config.pages)) {
|
|
189
|
+
logChange('page');
|
|
190
|
+
getPages(config)
|
|
191
|
+
.then((pages) => {
|
|
192
|
+
let pageModule = Object.entries(pages).find(([, { modulePath }]) => modulePath === filePath);
|
|
193
|
+
let page = pageModule ? { [`${pageModule[0]}`]: pageModule[1] } : {}
|
|
194
|
+
prerenderPages(config, page);
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
} else if (filePath.includes(`.${config.cayoComponentInfix}`)) {
|
|
198
|
+
logChange('cayo component');
|
|
199
|
+
getCayoComponents(config)
|
|
200
|
+
.then((components) => {
|
|
201
|
+
let componentModule = Object.entries(components).find(([, { modulePath }]) => modulePath === filePath);
|
|
202
|
+
handleCayoComponent(componentModule[0], componentModule[1].modulePath, config);
|
|
203
|
+
})
|
|
204
|
+
// TODO: watch component changes
|
|
205
|
+
// } else if (componentFileChanged) {
|
|
206
|
+
// find out which pages are affected
|
|
207
|
+
// find out which components are affected (imports)?
|
|
208
|
+
} else {
|
|
209
|
+
prerenderPages(config);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
// watcher.close();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async function serve(config) {
|
|
217
|
+
|
|
218
|
+
const internalConfig = {
|
|
219
|
+
root: config.cayoPath,
|
|
220
|
+
publicDir: config.publicDir,
|
|
221
|
+
mode: config.mode,
|
|
222
|
+
base: config.base,
|
|
223
|
+
configFile: false,
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const mergedConfig = merge(config.viteConfig, internalConfig);
|
|
227
|
+
|
|
228
|
+
const server = await createServer({
|
|
229
|
+
...mergedConfig,
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
await server.listen()
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
async function build(config, pages) {
|
|
236
|
+
|
|
237
|
+
// Create inputs for rollup based on the pages
|
|
238
|
+
const inputs = {};
|
|
239
|
+
|
|
240
|
+
for (const [, page] of Object.entries(pages)) {
|
|
241
|
+
if (page.filePath === 'index') {
|
|
242
|
+
inputs['main'] = path.resolve(config.cayoPath, 'index.html');
|
|
243
|
+
} else {
|
|
244
|
+
inputs[page.filePath] = path.resolve(config.cayoPath, `${page.filePath}/index.html`);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const internalConfig = {
|
|
249
|
+
root: config.cayoPath,
|
|
250
|
+
publicDir: config.publicDir,
|
|
251
|
+
base: config.base,
|
|
252
|
+
mode: config.mode,
|
|
253
|
+
build: {
|
|
254
|
+
outDir: config.build.outDir,
|
|
255
|
+
assetsDir: config.build.assetsDir,
|
|
256
|
+
emptyOutDir: true,
|
|
257
|
+
rollupOptions: {
|
|
258
|
+
input: { ...inputs },
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
const mergedConfig = merge(config.viteConfig, internalConfig);
|
|
264
|
+
|
|
265
|
+
return await viteBuild({
|
|
266
|
+
...mergedConfig,
|
|
267
|
+
})
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
async function prerenderPages(config, pages = data.pages) {
|
|
271
|
+
const { template: Template, components } = data;
|
|
272
|
+
const { prerendered, template } = prerender(Template, pages, components, config, logger);
|
|
273
|
+
|
|
274
|
+
for (const [, page] of Object.entries(prerendered)) {
|
|
275
|
+
await writePageFiles(page, config.cayoPath, logger, config);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// Does nothing, if no CSS generated by template file render
|
|
279
|
+
await writeTemplateCSS(template.css, config.cayoPath, logger, config);
|
|
280
|
+
|
|
281
|
+
return prerendered;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
async function handleCayoComponent(name, modulePath, config) {
|
|
285
|
+
writeComponentFile(name, modulePath, config.cayoPath, logger);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
async function handleCayoComponents(components = data.components, config) {
|
|
289
|
+
Object.keys(components).forEach(name => {
|
|
290
|
+
let component = components[name];
|
|
291
|
+
handleCayoComponent(name, component.modulePath, config);
|
|
292
|
+
})
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
cli(process.argv);
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"moduleResolution": "node",
|
|
4
|
+
"target": "esnext",
|
|
5
|
+
"module": "esnext",
|
|
6
|
+
/**
|
|
7
|
+
* svelte-preprocess cannot figure out whether you have
|
|
8
|
+
* a value or a type, so tell TypeScript to enforce using
|
|
9
|
+
* `import type` instead of `import` for Types.
|
|
10
|
+
*/
|
|
11
|
+
"importsNotUsedAsValues": "error",
|
|
12
|
+
"isolatedModules": true,
|
|
13
|
+
"resolveJsonModule": true,
|
|
14
|
+
/**
|
|
15
|
+
* To have warnings / errors of the Svelte compiler at the
|
|
16
|
+
* correct position, enable source maps by default.
|
|
17
|
+
*/
|
|
18
|
+
"sourceMap": true,
|
|
19
|
+
"esModuleInterop": true,
|
|
20
|
+
"skipLibCheck": true,
|
|
21
|
+
"forceConsistentCasingInFileNames": true,
|
|
22
|
+
"baseUrl": ".",
|
|
23
|
+
/**
|
|
24
|
+
* Typecheck JS in `.svelte` and `.js` files by default.
|
|
25
|
+
* Disable this if you'd like to use dynamic types.
|
|
26
|
+
*/
|
|
27
|
+
"checkJs": false
|
|
28
|
+
},
|
|
29
|
+
/**
|
|
30
|
+
* Use global.d.ts instead of compilerOptions.types
|
|
31
|
+
* to avoid limiting type declarations.
|
|
32
|
+
*/
|
|
33
|
+
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
|
|
34
|
+
}
|
package/lib/cli.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { loadConfig } from './config.js';
|
|
2
|
+
import yargs from 'yargs-parser';
|
|
3
|
+
import { devServer } from './dev.js';
|
|
4
|
+
import { build } from './build.js';
|
|
5
|
+
import fs from 'fs-extra';
|
|
6
|
+
|
|
7
|
+
// TODO: move this elsewhere
|
|
8
|
+
if (!fs.existsSync(dotPath)) {
|
|
9
|
+
await fs.mkdir(dotPath)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Load config
|
|
13
|
+
|
|
14
|
+
// Handle arguments
|
|
15
|
+
function resolveArgs(argv) {
|
|
16
|
+
|
|
17
|
+
const cmd = argv._[2]
|
|
18
|
+
const options = {
|
|
19
|
+
//TODO: support options from command line
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
switch (cmd) {
|
|
23
|
+
case 'dev':
|
|
24
|
+
return { cmd: 'dev', options };
|
|
25
|
+
case 'build':
|
|
26
|
+
return { cmd: 'build', options };
|
|
27
|
+
default:
|
|
28
|
+
return { cmd: 'help', options };
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function printHelp() {
|
|
33
|
+
// TODO: help info
|
|
34
|
+
console.log('help');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function run(command) {
|
|
38
|
+
const { cmd, options } = command;
|
|
39
|
+
try {
|
|
40
|
+
// const { projectRoot, configPath } = options;
|
|
41
|
+
const config = await loadConfig(options);
|
|
42
|
+
|
|
43
|
+
// TODO: actually run command now
|
|
44
|
+
} catch (err) {
|
|
45
|
+
console.error(err);
|
|
46
|
+
process.exit(1);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Run
|
|
51
|
+
export async function cli(args) {
|
|
52
|
+
const argv = yargs(args);
|
|
53
|
+
const command = resolveArgs(argv);
|
|
54
|
+
// TODO: do something with options
|
|
55
|
+
|
|
56
|
+
switch(command.cmd) {
|
|
57
|
+
case 'dev':
|
|
58
|
+
// run server
|
|
59
|
+
devServer();
|
|
60
|
+
break;
|
|
61
|
+
case 'build':
|
|
62
|
+
// run build
|
|
63
|
+
build();
|
|
64
|
+
break;
|
|
65
|
+
case 'help':
|
|
66
|
+
default:
|
|
67
|
+
printHelp();
|
|
68
|
+
}
|
|
69
|
+
}
|
package/lib/codegen.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
|
|
3
|
+
// Generate runtime helper that finds and parses a component instance's prop data
|
|
4
|
+
export function generateGetProps() {
|
|
5
|
+
return (
|
|
6
|
+
`
|
|
7
|
+
function getProps(cayoId) {
|
|
8
|
+
const componentElement = document.querySelector(\`[data-cayo-id="\${cayoId}"]\`);
|
|
9
|
+
const json = componentElement.dataset.cayoProps;
|
|
10
|
+
return JSON.parse(json);
|
|
11
|
+
}
|
|
12
|
+
`
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function generateCayoRuntime(components, config) {
|
|
17
|
+
let code = '';
|
|
18
|
+
let instances = '';
|
|
19
|
+
|
|
20
|
+
if (Object.keys(components).length !== 0) {
|
|
21
|
+
// TODO: add this path to config (internal only)
|
|
22
|
+
// const componentPath = path.resolve(config.cayoPath, './__cayo/components');
|
|
23
|
+
const componentPath = '/__cayo/components';
|
|
24
|
+
Object.entries(components).forEach(([name, ids]) => {
|
|
25
|
+
// Add component dependency import
|
|
26
|
+
code += `import { ${name} } from '${componentPath}/${name}.js';\n`;
|
|
27
|
+
// Generate component instances
|
|
28
|
+
ids.forEach(id => {
|
|
29
|
+
instances += generateComponentInstance(id, name)
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Add getProps (used by component instances)
|
|
34
|
+
code += generateGetProps();
|
|
35
|
+
} else {
|
|
36
|
+
instances += ` // No cayo component instances found in page HTML`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Add main render, which runs the component instantiations
|
|
40
|
+
code += generateRender(instances);
|
|
41
|
+
|
|
42
|
+
return { code };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function generateRender(contents) {
|
|
46
|
+
return (
|
|
47
|
+
`
|
|
48
|
+
export default function render() {
|
|
49
|
+
${contents}
|
|
50
|
+
}
|
|
51
|
+
`
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Generate the code to wrap component instances in an event listener wrapper
|
|
56
|
+
export function generateComponentInstanceWrapper(contents) {
|
|
57
|
+
return (
|
|
58
|
+
`
|
|
59
|
+
document.addEventListener('DOMContentLoaded', function() {
|
|
60
|
+
${contents}
|
|
61
|
+
});
|
|
62
|
+
`
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Generate the code for a component instance
|
|
67
|
+
export function generateComponentInstance(cayoId, componentName) {
|
|
68
|
+
return (
|
|
69
|
+
`
|
|
70
|
+
new ${componentName}({
|
|
71
|
+
target: document.querySelector('[data-cayo-id="${cayoId}"]'),
|
|
72
|
+
hydrate: true,
|
|
73
|
+
props: getProps('${cayoId}'),
|
|
74
|
+
});
|
|
75
|
+
`
|
|
76
|
+
);
|
|
77
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import * as crypto from 'crypto';
|
|
3
|
+
// import { Boundary } from '@crownframework/svelte-error-boundary';
|
|
4
|
+
|
|
5
|
+
export let name;
|
|
6
|
+
|
|
7
|
+
// TODO: Consider error handling for required stuff
|
|
8
|
+
// if (!name) throw new Error('No name for component');
|
|
9
|
+
// try {
|
|
10
|
+
// if (!name) throw new Error(`\x1b[31mCayo Component: 'name' prop is required and can't be an empty string`);
|
|
11
|
+
// } catch (err) {
|
|
12
|
+
// new Error(err);
|
|
13
|
+
// }
|
|
14
|
+
|
|
15
|
+
function hash() {
|
|
16
|
+
return crypto.randomBytes(3).toString('hex');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const json = JSON.stringify($$restProps);
|
|
20
|
+
</script>
|
|
21
|
+
|
|
22
|
+
<div data-cayo-id={name ? `${name}-${hash()}` : ''} data-cayo-props={json}>Cayo loading...<slot/></div>
|
|
23
|
+
<slot/>
|
|
24
|
+
|
package/lib/config.js
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { normalizePath } from './utils.js';
|
|
5
|
+
import { default as viteConfig } from './vite.config.js';
|
|
6
|
+
import chalk from 'chalk';
|
|
7
|
+
import merge from 'deepmerge';
|
|
8
|
+
|
|
9
|
+
async function validateConfig(userConfig, base) {
|
|
10
|
+
const ConfigSchema = z.object({
|
|
11
|
+
projectRoot: z
|
|
12
|
+
.string()
|
|
13
|
+
.optional()
|
|
14
|
+
.default('.')
|
|
15
|
+
.transform(val => normalizePath(base, val)),
|
|
16
|
+
src: z
|
|
17
|
+
.string()
|
|
18
|
+
.optional()
|
|
19
|
+
.default('./src')
|
|
20
|
+
.transform(val => normalizePath(base, val)),
|
|
21
|
+
pages: z
|
|
22
|
+
.string()
|
|
23
|
+
.optional()
|
|
24
|
+
.default('./src/pages')
|
|
25
|
+
.transform(val => normalizePath(base, val)),
|
|
26
|
+
publicDir: z
|
|
27
|
+
.string()
|
|
28
|
+
.optional()
|
|
29
|
+
.default('./public')
|
|
30
|
+
.transform(val => normalizePath(base, val)),
|
|
31
|
+
base: z
|
|
32
|
+
.string()
|
|
33
|
+
.optional(),
|
|
34
|
+
build: z
|
|
35
|
+
.object({
|
|
36
|
+
outDir: z
|
|
37
|
+
.string()
|
|
38
|
+
.optional()
|
|
39
|
+
.default('./dist')
|
|
40
|
+
.transform(val => normalizePath(base, val)),
|
|
41
|
+
assetsDir: z
|
|
42
|
+
.string()
|
|
43
|
+
.optional()
|
|
44
|
+
.default('assets'),
|
|
45
|
+
// .transform(val => normalizePath(base, val)),
|
|
46
|
+
legacy: z.
|
|
47
|
+
boolean()
|
|
48
|
+
.optional()
|
|
49
|
+
.default(false),
|
|
50
|
+
})
|
|
51
|
+
.optional()
|
|
52
|
+
.default({}),
|
|
53
|
+
css: z
|
|
54
|
+
.object({
|
|
55
|
+
internal: z
|
|
56
|
+
.boolean()
|
|
57
|
+
.optional()
|
|
58
|
+
.default(false),
|
|
59
|
+
})
|
|
60
|
+
.optional()
|
|
61
|
+
.default({}),
|
|
62
|
+
viteConfig: z
|
|
63
|
+
.any({}),
|
|
64
|
+
// .default(viteConfig),
|
|
65
|
+
cayoPath: z
|
|
66
|
+
.string()
|
|
67
|
+
.optional()
|
|
68
|
+
.default('.cayo/')
|
|
69
|
+
.transform(val => normalizePath(base, val)),
|
|
70
|
+
cayoComponentInfix: z
|
|
71
|
+
.string()
|
|
72
|
+
.optional()
|
|
73
|
+
.default('cayo'),
|
|
74
|
+
templateFileName: z
|
|
75
|
+
.string()
|
|
76
|
+
.default('__layout'),
|
|
77
|
+
mode: z
|
|
78
|
+
.string()
|
|
79
|
+
.optional()
|
|
80
|
+
.default('development'),
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
return await ConfigSchema.parseAsync(userConfig);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function checkConfigPaths(config, logger) {
|
|
87
|
+
let errorLogged = false;
|
|
88
|
+
const log = (option, value) => {
|
|
89
|
+
logger.warn(
|
|
90
|
+
chalk.red(`Config Error: ${option}: '${value}' does not exist`),
|
|
91
|
+
{ timestamp: true, clear: false, }
|
|
92
|
+
);
|
|
93
|
+
errorLogged = true;
|
|
94
|
+
}
|
|
95
|
+
if (!fs.existsSync(config.projectRoot))
|
|
96
|
+
log('config.projectRoot', config.projectRoot);
|
|
97
|
+
if (!fs.existsSync(path.resolve(config.projectRoot, config.src)))
|
|
98
|
+
log('config.src', config.projectRoot);
|
|
99
|
+
if (!fs.existsSync(path.resolve(config.projectRoot, config.pages)))
|
|
100
|
+
log('config.pages', config.pages);
|
|
101
|
+
if (!fs.existsSync(path.resolve(config.projectRoot, config.publicDir)))
|
|
102
|
+
log('config.publicDir', config.publicDir);
|
|
103
|
+
if (!fs.existsSync(path.resolve(config.src, `${config.templateFileName}.svelte`)))
|
|
104
|
+
log('config.templateFileName', `${config.templateFileName}(.svelte)`);
|
|
105
|
+
if (!fs.existsSync(path.resolve(config.projectRoot, config.publicDir)))
|
|
106
|
+
log('config.publicDir', config.publicDir);
|
|
107
|
+
|
|
108
|
+
if(errorLogged) throw new Error('Config Error: fix the issues above in your config file');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export async function loadConfig(options) {
|
|
112
|
+
const configFileName = 'cayo.config.js';
|
|
113
|
+
|
|
114
|
+
// Use options passed to the CLI for projectRoot and configPath
|
|
115
|
+
const root = options.projectRoot
|
|
116
|
+
? path.resolve(options.projectRoot)
|
|
117
|
+
: process.cwd();
|
|
118
|
+
|
|
119
|
+
const configPath = options.configPath
|
|
120
|
+
? path.resolve(root, options.configPath)
|
|
121
|
+
: path.resolve(root, `./${configFileName}`);
|
|
122
|
+
|
|
123
|
+
// Load config from user config file
|
|
124
|
+
let userConfig = { projectRoot: root, mode: options.mode };
|
|
125
|
+
if (fs.existsSync(configPath)) {
|
|
126
|
+
userConfig = {
|
|
127
|
+
...(await import(configPath)).default,
|
|
128
|
+
...userConfig,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const config = await validateConfig(userConfig, root);
|
|
133
|
+
if (config.viteConfig) {
|
|
134
|
+
const mergedViteConfig = merge(viteConfig, config.viteConfig, { arrayMerge: combineMerge });
|
|
135
|
+
config.viteConfig = mergedViteConfig;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return config;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Credit: https://github.com/TehShrike/deepmerge#arraymerge-example-combine-arrays
|
|
142
|
+
function combineMerge(target, source, options) {
|
|
143
|
+
const destination = target.slice()
|
|
144
|
+
|
|
145
|
+
source.forEach((item, index) => {
|
|
146
|
+
if (typeof destination[index] === 'undefined') {
|
|
147
|
+
destination[index] = options.cloneUnlessOtherwiseSpecified(item, options)
|
|
148
|
+
} else if (options.isMergeableObject(item)) {
|
|
149
|
+
destination[index] = merge(target[index], item, options)
|
|
150
|
+
} else if (target.indexOf(item) === -1) {
|
|
151
|
+
destination.push(item)
|
|
152
|
+
}
|
|
153
|
+
})
|
|
154
|
+
return destination
|
|
155
|
+
}
|