hyperspan 1.0.0-alpha.0 → 1.0.0-alpha.2
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/package.json +2 -2
- package/src/server.ts +63 -55
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hyperspan",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.2",
|
|
4
4
|
"description": "Hyperspan CLI - for @hyperspan/framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"public": true,
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"test": "bun test"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@hyperspan/framework": "^0.
|
|
35
|
+
"@hyperspan/framework": "^1.0.0-alpha",
|
|
36
36
|
"commander": "^14.0.2",
|
|
37
37
|
"degit": "^2.8.4"
|
|
38
38
|
},
|
package/src/server.ts
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
|
-
import { Glob } from
|
|
2
|
-
import { createServer, getRunnableRoute, IS_PROD, parsePath } from '@hyperspan/framework';
|
|
1
|
+
import { Glob } from 'bun';
|
|
2
|
+
import { createServer, getRunnableRoute, IS_PROD, isValidRoutePath, parsePath } from '@hyperspan/framework';
|
|
3
3
|
import { CSS_PUBLIC_PATH, CSS_ROUTE_MAP } from '@hyperspan/framework/client/css';
|
|
4
|
-
import { join } from
|
|
4
|
+
import { join } from 'node:path';
|
|
5
5
|
|
|
6
6
|
import type { Hyperspan as HS } from '@hyperspan/framework';
|
|
7
7
|
type startConfig = {
|
|
8
8
|
development?: boolean;
|
|
9
|
-
}
|
|
9
|
+
};
|
|
10
10
|
|
|
11
11
|
const CWD = process.cwd();
|
|
12
12
|
|
|
13
13
|
export async function loadConfig(): Promise<HS.Config> {
|
|
14
|
-
const configFile = join(CWD,
|
|
15
|
-
const configModule = await import(configFile)
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
const configFile = join(CWD, 'hyperspan.config.ts');
|
|
15
|
+
const configModule = await import(configFile)
|
|
16
|
+
.then((module) => module.default)
|
|
17
|
+
.catch((error) => {
|
|
18
|
+
console.error(`[Hyperspan] Unable to load config file: ${error}`);
|
|
19
|
+
console.error(
|
|
20
|
+
`[Hyperspan] Please create a hyperspan.config.ts file in the root of your project.`
|
|
21
|
+
);
|
|
22
|
+
console.log(`[Hyperspan] Example:
|
|
19
23
|
import { createConfig } from '@hyperspan/framework';
|
|
20
24
|
|
|
21
25
|
export default createConfig({
|
|
@@ -23,8 +27,8 @@ export default createConfig({
|
|
|
23
27
|
publicDir: './public',
|
|
24
28
|
});
|
|
25
29
|
`);
|
|
26
|
-
|
|
27
|
-
|
|
30
|
+
process.exit(1);
|
|
31
|
+
});
|
|
28
32
|
return configModule;
|
|
29
33
|
}
|
|
30
34
|
|
|
@@ -38,10 +42,10 @@ export async function startServer(startConfig: startConfig = {}): Promise<HS.Ser
|
|
|
38
42
|
}
|
|
39
43
|
|
|
40
44
|
export async function addRoutes(server: HS.Server, startConfig: startConfig) {
|
|
41
|
-
const routesGlob = new Glob(
|
|
45
|
+
const routesGlob = new Glob('**/*.ts');
|
|
42
46
|
const routeFiles: string[] = [];
|
|
43
|
-
const appDir = server._config.appDir ||
|
|
44
|
-
const routesDir = join(CWD, appDir,
|
|
47
|
+
const appDir = server._config.appDir || './app';
|
|
48
|
+
const routesDir = join(CWD, appDir, 'routes');
|
|
45
49
|
const buildDir = join(CWD, '.build');
|
|
46
50
|
const cssPublicDir = join(CWD, server._config.publicDir, CSS_PUBLIC_PATH);
|
|
47
51
|
|
|
@@ -57,53 +61,57 @@ export async function addRoutes(server: HS.Server, startConfig: startConfig) {
|
|
|
57
61
|
routeFiles.push(filePath);
|
|
58
62
|
}
|
|
59
63
|
|
|
60
|
-
const routeMap: { route: string
|
|
61
|
-
const routes: HS.Route[] = await Promise.all(
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
// Wasteful perhaps to compile the JS also and then just discard it, but it's an easy way to do CSS compilation by route
|
|
70
|
-
const buildResult = await Bun.build({
|
|
71
|
-
entrypoints: [filePath],
|
|
72
|
-
outdir: buildDir,
|
|
73
|
-
naming: `app/routes/${path.endsWith('/') ? path + 'index' : path}-[hash].[ext]`,
|
|
74
|
-
minify: IS_PROD,
|
|
75
|
-
format: 'esm',
|
|
76
|
-
target: 'node',
|
|
77
|
-
env: 'APP_PUBLIC_*',
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
// Move CSS files to the public directory
|
|
81
|
-
for (const output of buildResult.outputs) {
|
|
82
|
-
if (output.path.endsWith('.css')) {
|
|
83
|
-
const cssFileName = output.path.split('/').pop()!;
|
|
84
|
-
await Bun.write(join(cssPublicDir, cssFileName), Bun.file(output.path));
|
|
85
|
-
cssFiles.push(cssFileName);
|
|
64
|
+
const routeMap: { route: string; file: string }[] = [];
|
|
65
|
+
const routes: HS.Route[] = await Promise.all(
|
|
66
|
+
routeFiles
|
|
67
|
+
.map(async (filePath) => {
|
|
68
|
+
const relativePath = filePath.split('app/routes/').pop();
|
|
69
|
+
const { path } = parsePath(relativePath ?? '/');
|
|
70
|
+
|
|
71
|
+
if (!isValidRoutePath(path)) {
|
|
72
|
+
return null;
|
|
86
73
|
}
|
|
87
|
-
}
|
|
88
74
|
|
|
89
|
-
|
|
90
|
-
|
|
75
|
+
let cssFiles: string[] = [];
|
|
76
|
+
|
|
77
|
+
// Build the route just for the CSS files
|
|
78
|
+
// Wasteful perhaps to compile the JS also and then just discard it, but it's an easy way to do CSS compilation by route
|
|
79
|
+
const buildResult = await Bun.build({
|
|
80
|
+
entrypoints: [filePath],
|
|
81
|
+
outdir: buildDir,
|
|
82
|
+
naming: `app/routes/${path.endsWith('/') ? path + 'index' : path}-[hash].[ext]`,
|
|
83
|
+
minify: IS_PROD,
|
|
84
|
+
format: 'esm',
|
|
85
|
+
target: 'node',
|
|
86
|
+
env: 'APP_PUBLIC_*',
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// Move CSS files to the public directory
|
|
90
|
+
for (const output of buildResult.outputs) {
|
|
91
|
+
if (output.path.endsWith('.css')) {
|
|
92
|
+
const cssFileName = output.path.split('/').pop()!;
|
|
93
|
+
await Bun.write(join(cssPublicDir, cssFileName), Bun.file(output.path));
|
|
94
|
+
cssFiles.push(cssFileName);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
91
97
|
|
|
92
|
-
|
|
93
|
-
|
|
98
|
+
const routeModule = await import(filePath);
|
|
99
|
+
const route = getRunnableRoute(routeModule);
|
|
94
100
|
|
|
95
|
-
|
|
96
|
-
route._config.
|
|
97
|
-
CSS_ROUTE_MAP.set(path, cssFiles);
|
|
98
|
-
}
|
|
101
|
+
// Set route path based on the file path
|
|
102
|
+
route._config.path = path;
|
|
99
103
|
|
|
100
|
-
|
|
104
|
+
if (cssFiles.length > 0) {
|
|
105
|
+
route._config.cssImports = cssFiles;
|
|
106
|
+
CSS_ROUTE_MAP.set(path, cssFiles);
|
|
107
|
+
}
|
|
101
108
|
|
|
102
|
-
|
|
103
|
-
}
|
|
109
|
+
routeMap.push({ route: path, file: filePath.replace(CWD, '') });
|
|
104
110
|
|
|
105
|
-
|
|
106
|
-
|
|
111
|
+
return route;
|
|
112
|
+
})
|
|
113
|
+
.filter((route) => route !== null)
|
|
114
|
+
);
|
|
107
115
|
|
|
108
116
|
if (startConfig.development) {
|
|
109
117
|
console.log('[Hyperspan] Loaded routes:');
|
|
@@ -111,4 +119,4 @@ export async function addRoutes(server: HS.Server, startConfig: startConfig) {
|
|
|
111
119
|
}
|
|
112
120
|
|
|
113
121
|
server._routes.push(...routes);
|
|
114
|
-
}
|
|
122
|
+
}
|