@solidjs/start 1.2.1 → 2.0.0-alpha.1
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/CHANGELOG.md +14 -0
- package/README.md +2 -2
- package/dist/client/StartClient.d.ts +0 -1
- package/dist/client/StartClient.jsx +2 -4
- package/dist/client/index.d.ts +2 -3
- package/dist/client/index.jsx +2 -4
- package/dist/client/mount.d.ts +1 -7
- package/dist/client/mount.js +1 -91
- package/dist/client/spa/index.d.ts +1 -2
- package/dist/client/spa/index.jsx +1 -2
- package/dist/config/constants.d.ts +16 -0
- package/dist/config/constants.js +16 -0
- package/dist/config/dev-server.d.ts +2 -0
- package/dist/config/dev-server.js +108 -0
- package/dist/config/fs-router.d.ts +37 -0
- package/dist/config/fs-router.js +141 -0
- package/dist/config/fs-routes/fs-watcher.d.ts +3 -0
- package/dist/config/fs-routes/fs-watcher.js +49 -0
- package/dist/config/fs-routes/index.d.ts +7 -0
- package/dist/config/fs-routes/index.js +107 -0
- package/dist/config/fs-routes/router.d.ts +37 -0
- package/dist/config/fs-routes/router.js +136 -0
- package/dist/config/fs-routes/tree-shake.d.ts +2 -0
- package/dist/config/fs-routes/tree-shake.js +348 -0
- package/dist/config/index.d.ts +10 -0
- package/dist/config/index.js +214 -0
- package/dist/config/lazy.d.ts +3 -0
- package/dist/config/lazy.js +82 -0
- package/dist/config/manifest.d.ts +3 -0
- package/dist/config/manifest.js +74 -0
- package/dist/config/utils.d.ts +4 -0
- package/dist/config/utils.js +6 -0
- package/dist/config/vite-utils.d.ts +16 -0
- package/dist/config/vite-utils.js +81 -0
- package/dist/http/index.d.ts +81 -0
- package/dist/http/index.js +129 -0
- package/dist/index.d.ts +5 -5
- package/dist/index.jsx +5 -5
- package/dist/middleware/index.d.ts +6 -6
- package/dist/middleware/index.js +39 -0
- package/dist/{router/FileRoutes.js → router.jsx} +7 -8
- package/dist/server/StartServer.d.ts +3 -3
- package/dist/server/StartServer.jsx +9 -63
- package/dist/server/assets/index.d.ts +2 -0
- package/dist/server/assets/index.js +52 -0
- package/dist/server/assets/render.d.ts +19 -0
- package/dist/server/{renderAsset.jsx → assets/render.jsx} +1 -1
- package/dist/server/collect-styles.d.ts +3 -0
- package/dist/server/collect-styles.js +69 -0
- package/dist/server/fetchEvent.d.ts +4 -3
- package/dist/server/fetchEvent.js +12 -74
- package/dist/server/fetchEvent.spec.js +18 -197
- package/dist/server/handler.d.ts +6 -12
- package/dist/server/handler.js +193 -96
- package/dist/server/index.d.ts +9 -6
- package/dist/server/index.jsx +14 -6
- package/dist/server/manifest/client-manifest.d.ts +9 -0
- package/dist/server/manifest/client-manifest.js +6 -0
- package/dist/server/manifest/dev-client-manifest.d.ts +4 -0
- package/dist/server/manifest/dev-client-manifest.js +24 -0
- package/dist/server/manifest/dev-ssr-manifest.d.ts +5 -0
- package/dist/server/manifest/dev-ssr-manifest.js +23 -0
- package/dist/server/manifest/prod-client-manifest.d.ts +5 -0
- package/dist/server/manifest/prod-client-manifest.js +26 -0
- package/dist/server/manifest/prod-ssr-manifest.d.ts +6 -0
- package/dist/server/manifest/prod-ssr-manifest.js +85 -0
- package/dist/server/manifest/ssr-manifest.d.ts +4 -0
- package/dist/server/manifest/ssr-manifest.js +6 -0
- package/dist/{router → server}/routes.d.ts +5 -10
- package/dist/{router → server}/routes.js +32 -33
- package/dist/{runtime → server}/server-fns-runtime.d.ts +1 -1
- package/dist/{runtime → server}/server-fns-runtime.js +8 -7
- package/dist/server/server-functions-handler.d.ts +2 -0
- package/dist/{runtime/server-handler.js → server/server-functions-handler.js} +57 -97
- package/dist/server/server-runtime.d.ts +3 -0
- package/dist/{runtime → server}/server-runtime.js +17 -24
- package/dist/server/spa/StartServer.d.ts +1 -1
- package/dist/server/spa/StartServer.jsx +7 -9
- package/dist/server/spa/handler.d.ts +3 -3
- package/dist/server/spa/handler.js +9 -11
- package/dist/server/spa/index.d.ts +4 -3
- package/dist/server/spa/index.jsx +4 -3
- package/dist/server/types.d.ts +7 -9
- package/dist/server/util.d.ts +2 -0
- package/dist/server/util.js +8 -0
- package/dist/shared/ErrorBoundary.jsx +2 -2
- package/dist/shared/HttpHeader.jsx +4 -2
- package/dist/shared/{HttpStatusCode.jsx → HttpStatusCode.js} +3 -1
- package/dist/shared/dev-overlay/CodeView.d.ts +1 -1
- package/dist/shared/dev-overlay/CodeView.jsx +23 -34
- package/dist/shared/dev-overlay/DevOverlayDialog.d.ts +2 -2
- package/dist/shared/dev-overlay/DevOverlayDialog.jsx +73 -64
- package/dist/shared/dev-overlay/createStackFrame.js +3 -3
- package/dist/shared/dev-overlay/get-source-map.d.ts +1 -1
- package/dist/shared/dev-overlay/get-source-map.js +5 -5
- package/dist/shared/dev-overlay/icons.d.ts +10 -10
- package/dist/shared/dev-overlay/icons.jsx +91 -91
- package/dist/shared/dev-overlay/index.jsx +6 -9
- package/dist/shared/lazy.d.ts +3 -0
- package/dist/shared/lazy.js +42 -0
- package/dist/shared/serverFunction.d.ts +1 -1
- package/env.d.ts +2 -19
- package/package.json +44 -65
- package/scripts/build.js +15 -0
- package/scripts/validate-imports.js +316 -0
- package/src/client/StartClient.tsx +34 -0
- package/src/client/index.tsx +2 -0
- package/src/client/mount.ts +1 -0
- package/src/client/spa/index.tsx +9 -0
- package/src/config/constants.ts +19 -0
- package/src/config/dev-server.ts +145 -0
- package/{config/fs-router.js → src/config/fs-router.ts} +49 -29
- package/src/config/fs-routes/fs-watcher.ts +77 -0
- package/src/config/fs-routes/index.ts +136 -0
- package/src/config/fs-routes/router.ts +169 -0
- package/src/config/fs-routes/tree-shake.ts +371 -0
- package/src/config/index.ts +233 -0
- package/src/config/lazy.ts +100 -0
- package/src/config/manifest.ts +84 -0
- package/src/config/utils.ts +5 -0
- package/src/config/vite-utils.ts +109 -0
- package/src/env.d.ts +17 -0
- package/src/http/index.ts +216 -0
- package/src/index.tsx +7 -0
- package/src/internal.d.ts +14 -0
- package/src/middleware/index.ts +72 -0
- package/src/router.tsx +37 -0
- package/src/server/StartServer.tsx +56 -0
- package/src/server/assets/index.ts +70 -0
- package/src/server/assets/render.tsx +46 -0
- package/src/server/collect-styles.ts +87 -0
- package/src/server/fetchEvent.spec.ts +85 -0
- package/src/server/fetchEvent.ts +36 -0
- package/src/server/handler.ts +257 -0
- package/src/server/index.tsx +29 -0
- package/src/server/manifest/client-manifest.ts +8 -0
- package/src/server/manifest/dev-client-manifest.ts +21 -0
- package/src/server/manifest/dev-ssr-manifest.ts +23 -0
- package/src/server/manifest/prod-client-manifest.ts +21 -0
- package/src/server/manifest/prod-ssr-manifest.ts +109 -0
- package/src/server/manifest/ssr-manifest.ts +10 -0
- package/src/server/routes.ts +126 -0
- package/src/server/server-fns-runtime.ts +31 -0
- package/src/server/server-functions-handler.ts +339 -0
- package/src/server/server-runtime.ts +240 -0
- package/src/server/spa/StartServer.tsx +40 -0
- package/src/server/spa/handler.ts +29 -0
- package/src/server/spa/index.tsx +5 -0
- package/src/server/types.ts +82 -0
- package/src/server/util.ts +12 -0
- package/src/shared/ErrorBoundary.tsx +52 -0
- package/src/shared/GET.ts +8 -0
- package/src/shared/HttpHeader.tsx +42 -0
- package/src/shared/HttpStatusCode.ts +27 -0
- package/src/shared/clientOnly.ts +44 -0
- package/src/shared/dev-overlay/CodeView.tsx +92 -0
- package/src/shared/dev-overlay/DevOverlayDialog.tsx +316 -0
- package/src/shared/dev-overlay/createStackFrame.ts +74 -0
- package/src/shared/dev-overlay/download.ts +6 -0
- package/src/shared/dev-overlay/env.d.ts +4 -0
- package/src/shared/dev-overlay/get-source-map.ts +33 -0
- package/src/shared/dev-overlay/icons.tsx +456 -0
- package/src/shared/dev-overlay/index.tsx +63 -0
- package/src/shared/dev-overlay/styles.css +292 -0
- package/src/shared/lazy.ts +52 -0
- package/src/shared/serverFunction.ts +10 -0
- package/src/virtual.d.ts +27 -0
- package/tsconfig.json +23 -0
- package/vitest.config.ts +9 -0
- package/config/index.d.ts +0 -40
- package/config/index.js +0 -304
- package/config/server-components.js +0 -106
- package/dist/client/islands.d.ts +0 -7
- package/dist/client/islands.jsx +0 -10
- package/dist/middleware/index.jsx +0 -35
- package/dist/router/index.d.ts +0 -1
- package/dist/router/index.jsx +0 -2
- package/dist/router/lazyRoute.d.ts +0 -6
- package/dist/router/lazyRoute.js +0 -53
- package/dist/runtime/server-handler.d.ts +0 -2
- package/dist/runtime/server-runtime.d.ts +0 -3
- package/dist/server/islands/index.d.ts +0 -19
- package/dist/server/islands/index.jsx +0 -62
- package/dist/server/islands/utils.d.ts +0 -1
- package/dist/server/islands/utils.jsx +0 -85
- package/dist/server/pageEvent.d.ts +0 -2
- package/dist/server/pageEvent.js +0 -56
- package/dist/server/renderAsset.d.ts +0 -2
- /package/dist/{router/FileRoutes.d.ts → router.d.ts} +0 -0
- /package/dist/shared/{clientOnly.jsx → clientOnly.js} +0 -0
- /package/dist/shared/{serverFunction.jsx → serverFunction.js} +0 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { relative } from "node:path";
|
|
2
|
+
import { fileSystemWatcher } from "./fs-watcher.js";
|
|
3
|
+
import { treeShake } from "./tree-shake.js";
|
|
4
|
+
export const moduleId = "solid-start:routes";
|
|
5
|
+
export function fsRoutes({ routers }) {
|
|
6
|
+
globalThis.ROUTERS = routers;
|
|
7
|
+
return [
|
|
8
|
+
{
|
|
9
|
+
name: "solid-start-fs-routes",
|
|
10
|
+
enforce: "pre",
|
|
11
|
+
resolveId(id) {
|
|
12
|
+
if (id === moduleId)
|
|
13
|
+
return id;
|
|
14
|
+
},
|
|
15
|
+
async load(id) {
|
|
16
|
+
const root = this.environment.config.root;
|
|
17
|
+
const isBuild = this.environment.mode === "build";
|
|
18
|
+
if (id !== moduleId)
|
|
19
|
+
return;
|
|
20
|
+
const js = jsCode();
|
|
21
|
+
const router = globalThis.ROUTERS[this.environment.name];
|
|
22
|
+
const routes = await router.getRoutes();
|
|
23
|
+
let routesCode = JSON.stringify(routes ?? [], (k, v) => {
|
|
24
|
+
if (v === undefined)
|
|
25
|
+
return undefined;
|
|
26
|
+
if (k.startsWith("$$")) {
|
|
27
|
+
const buildId = `${v.src}?${v.pick.map((p) => `pick=${p}`).join("&")}`;
|
|
28
|
+
/**
|
|
29
|
+
* @type {{ [key: string]: string }}
|
|
30
|
+
*/
|
|
31
|
+
const refs = {};
|
|
32
|
+
for (var pick of v.pick) {
|
|
33
|
+
refs[pick] = js.addNamedImport(pick, buildId);
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
require: `_$() => ({ ${Object.entries(refs)
|
|
37
|
+
.map(([pick, namedImport]) => `'${pick}': ${namedImport}`)
|
|
38
|
+
.join(", ")} })$_`,
|
|
39
|
+
// src: isBuild ? relative(root, buildId) : buildId
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
else if (k.startsWith("$")) {
|
|
43
|
+
const buildId = `${v.src}?${v.pick.map((p) => `pick=${p}`).join("&")}`;
|
|
44
|
+
return {
|
|
45
|
+
src: relative(root, buildId),
|
|
46
|
+
build: isBuild ? `_$() => import(/* @vite-ignore */ '${buildId}')$_` : undefined,
|
|
47
|
+
import: `_$() => import(/* @vite-ignore */ '${buildId}')$_`,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
return v;
|
|
51
|
+
});
|
|
52
|
+
routesCode = routesCode.replaceAll('"_$(', "(").replaceAll(')$_"', ")");
|
|
53
|
+
const code = `
|
|
54
|
+
${js.getImportStatements()}
|
|
55
|
+
export default ${routesCode}`;
|
|
56
|
+
return code;
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
treeShake(),
|
|
60
|
+
fileSystemWatcher(routers),
|
|
61
|
+
];
|
|
62
|
+
}
|
|
63
|
+
function jsCode() {
|
|
64
|
+
const imports = new Map();
|
|
65
|
+
let vars = 0;
|
|
66
|
+
function addImport(p) {
|
|
67
|
+
let id = imports.get(p);
|
|
68
|
+
if (!id) {
|
|
69
|
+
id = {};
|
|
70
|
+
imports.set(p, id);
|
|
71
|
+
}
|
|
72
|
+
const d = "routeData" + vars++;
|
|
73
|
+
id["default"] = d;
|
|
74
|
+
return d;
|
|
75
|
+
}
|
|
76
|
+
function addNamedImport(name, p) {
|
|
77
|
+
let id = imports.get(p);
|
|
78
|
+
if (!id) {
|
|
79
|
+
id = {};
|
|
80
|
+
imports.set(p, id);
|
|
81
|
+
}
|
|
82
|
+
const d = "routeData" + vars++;
|
|
83
|
+
id[name] = d;
|
|
84
|
+
return d;
|
|
85
|
+
}
|
|
86
|
+
const getNamedExport = (p) => {
|
|
87
|
+
const id = imports.get(p);
|
|
88
|
+
delete id["default"];
|
|
89
|
+
return Object.keys(id).length > 0
|
|
90
|
+
? `{ ${Object.keys(id)
|
|
91
|
+
.map(k => `${k} as ${id[k]}`)
|
|
92
|
+
.join(", ")} }`
|
|
93
|
+
: "";
|
|
94
|
+
};
|
|
95
|
+
const getImportStatements = () => {
|
|
96
|
+
return `${[...imports.keys()]
|
|
97
|
+
.map(i => `import ${imports.get(i).default
|
|
98
|
+
? `${imports.get(i).default}${Object.keys(imports.get(i)).length > 1 ? ", " : ""}`
|
|
99
|
+
: ""} ${getNamedExport(i)} from '${i}';`)
|
|
100
|
+
.join("\n")}`;
|
|
101
|
+
};
|
|
102
|
+
return {
|
|
103
|
+
addImport,
|
|
104
|
+
addNamedImport,
|
|
105
|
+
getImportStatements,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { pathToRegexp } from "path-to-regexp";
|
|
2
|
+
export { pathToRegexp };
|
|
3
|
+
export declare const glob: (path: string) => string[];
|
|
4
|
+
export type FileSystemRouterConfig = {
|
|
5
|
+
dir: string;
|
|
6
|
+
extensions: string[];
|
|
7
|
+
};
|
|
8
|
+
type Route = {
|
|
9
|
+
path: string;
|
|
10
|
+
} & Record<string, any>;
|
|
11
|
+
export declare function cleanPath(src: string, config: FileSystemRouterConfig): string;
|
|
12
|
+
export declare function analyzeModule(src: string): readonly [imports: readonly import("es-module-lexer").ImportSpecifier[], exports: readonly import("es-module-lexer").ExportSpecifier[], facade: boolean, hasModuleSyntax: boolean];
|
|
13
|
+
export declare class BaseFileSystemRouter extends EventTarget {
|
|
14
|
+
routes: Route[];
|
|
15
|
+
config: FileSystemRouterConfig;
|
|
16
|
+
/**
|
|
17
|
+
*
|
|
18
|
+
* @param {} config
|
|
19
|
+
*/
|
|
20
|
+
constructor(config: FileSystemRouterConfig);
|
|
21
|
+
glob(): string;
|
|
22
|
+
buildRoutes(): Promise<any[]>;
|
|
23
|
+
isRoute(src: string): boolean;
|
|
24
|
+
toPath(src: string): void;
|
|
25
|
+
toRoute(src: string): Route | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* To be attached by vite plugin to the vite dev server
|
|
28
|
+
*/
|
|
29
|
+
update: undefined;
|
|
30
|
+
_addRoute(route: Route): void;
|
|
31
|
+
addRoute(src: string): Promise<void>;
|
|
32
|
+
reload(route: string): void;
|
|
33
|
+
updateRoute(src: string): Promise<void>;
|
|
34
|
+
removeRoute(src: string): void;
|
|
35
|
+
buildRoutesPromise?: Promise<any[]>;
|
|
36
|
+
getRoutes(): Promise<Route[]>;
|
|
37
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { init } from "es-module-lexer";
|
|
2
|
+
import { parse } from "es-module-lexer";
|
|
3
|
+
import esbuild from "esbuild";
|
|
4
|
+
import fg from "fast-glob";
|
|
5
|
+
import fs from "fs";
|
|
6
|
+
import micromatch from "micromatch";
|
|
7
|
+
import { posix } from "path";
|
|
8
|
+
import { pathToRegexp } from "path-to-regexp";
|
|
9
|
+
import { normalize } from "node:path";
|
|
10
|
+
export { pathToRegexp };
|
|
11
|
+
export const glob = (path) => fg.sync(path, { absolute: true });
|
|
12
|
+
export function cleanPath(src, config) {
|
|
13
|
+
return src
|
|
14
|
+
.slice(config.dir.length)
|
|
15
|
+
.replace(new RegExp(`\.(${(config.extensions ?? []).join("|")})$`), "");
|
|
16
|
+
}
|
|
17
|
+
export function analyzeModule(src) {
|
|
18
|
+
return parse(esbuild.transformSync(fs.readFileSync(src, "utf-8"), {
|
|
19
|
+
jsx: "transform",
|
|
20
|
+
format: "esm",
|
|
21
|
+
loader: "tsx",
|
|
22
|
+
}).code, src);
|
|
23
|
+
}
|
|
24
|
+
export class BaseFileSystemRouter extends EventTarget {
|
|
25
|
+
routes;
|
|
26
|
+
config;
|
|
27
|
+
/**
|
|
28
|
+
*
|
|
29
|
+
* @param {} config
|
|
30
|
+
*/
|
|
31
|
+
constructor(config) {
|
|
32
|
+
super();
|
|
33
|
+
this.routes = [];
|
|
34
|
+
this.config = config;
|
|
35
|
+
}
|
|
36
|
+
glob() {
|
|
37
|
+
return (posix.join(fg.convertPathToPattern(this.config.dir), "**/*") +
|
|
38
|
+
`.{${this.config.extensions.join(",")}}`);
|
|
39
|
+
}
|
|
40
|
+
async buildRoutes() {
|
|
41
|
+
await init;
|
|
42
|
+
for (var src of glob(this.glob())) {
|
|
43
|
+
await this.addRoute(src);
|
|
44
|
+
}
|
|
45
|
+
return this.routes;
|
|
46
|
+
}
|
|
47
|
+
isRoute(src) {
|
|
48
|
+
return Boolean(micromatch(src, this.glob())?.length);
|
|
49
|
+
}
|
|
50
|
+
toPath(src) {
|
|
51
|
+
throw new Error("Not implemented");
|
|
52
|
+
}
|
|
53
|
+
toRoute(src) {
|
|
54
|
+
let path = this.toPath(src);
|
|
55
|
+
if (path === undefined)
|
|
56
|
+
return;
|
|
57
|
+
const [_, exports] = analyzeModule(src);
|
|
58
|
+
if (!exports.find(e => e.n === "default")) {
|
|
59
|
+
console.warn("No default export", src);
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
$component: {
|
|
63
|
+
src: src,
|
|
64
|
+
pick: ["default", "$css"],
|
|
65
|
+
},
|
|
66
|
+
path,
|
|
67
|
+
// filePath: src
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* To be attached by vite plugin to the vite dev server
|
|
72
|
+
*/
|
|
73
|
+
update = undefined;
|
|
74
|
+
_addRoute(route) {
|
|
75
|
+
this.routes = this.routes.filter(r => r.path !== route.path);
|
|
76
|
+
this.routes.push(route);
|
|
77
|
+
}
|
|
78
|
+
async addRoute(src) {
|
|
79
|
+
src = normalize(src);
|
|
80
|
+
if (this.isRoute(src)) {
|
|
81
|
+
try {
|
|
82
|
+
const route = this.toRoute(src);
|
|
83
|
+
if (route) {
|
|
84
|
+
this._addRoute(route);
|
|
85
|
+
this.reload(route.path);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
catch (e) {
|
|
89
|
+
console.error(e);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
reload(route) {
|
|
94
|
+
this.dispatchEvent(new Event("reload", {
|
|
95
|
+
// @ts-ignore
|
|
96
|
+
detail: {
|
|
97
|
+
route,
|
|
98
|
+
},
|
|
99
|
+
}));
|
|
100
|
+
}
|
|
101
|
+
async updateRoute(src) {
|
|
102
|
+
src = normalize(src);
|
|
103
|
+
if (this.isRoute(src)) {
|
|
104
|
+
try {
|
|
105
|
+
const route = this.toRoute(src);
|
|
106
|
+
if (route) {
|
|
107
|
+
this._addRoute(route);
|
|
108
|
+
this.reload(route.path);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
catch (e) {
|
|
112
|
+
console.error(e);
|
|
113
|
+
}
|
|
114
|
+
// this.update?.();
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
removeRoute(src) {
|
|
118
|
+
src = normalize(src);
|
|
119
|
+
if (this.isRoute(src)) {
|
|
120
|
+
const path = this.toPath(src);
|
|
121
|
+
if (path === undefined) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
this.routes = this.routes.filter(r => r.path !== path);
|
|
125
|
+
this.dispatchEvent(new Event("reload", {}));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
buildRoutesPromise;
|
|
129
|
+
async getRoutes() {
|
|
130
|
+
if (!this.buildRoutesPromise) {
|
|
131
|
+
this.buildRoutesPromise = this.buildRoutes();
|
|
132
|
+
}
|
|
133
|
+
await this.buildRoutesPromise;
|
|
134
|
+
return this.routes;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
// All credit for this work goes to the amazing Next.js team.
|
|
2
|
+
// https://github.com/vercel/next.js/blob/canary/packages/next/build/babel/plugins/next-ssg-transform.ts
|
|
3
|
+
// This is adapted to work with routeData functions. It can be run in two modes, one which preserves the routeData and the Component in the same file, and one which creates a
|
|
4
|
+
import { basename } from "pathe";
|
|
5
|
+
function treeShakeTransform({ types: t }) {
|
|
6
|
+
function getIdentifier(path) {
|
|
7
|
+
const parentPath = path.parentPath;
|
|
8
|
+
if (parentPath.type === "VariableDeclarator") {
|
|
9
|
+
const pp = parentPath;
|
|
10
|
+
const name = pp.get("id");
|
|
11
|
+
return name.node.type === "Identifier" ? name : null;
|
|
12
|
+
}
|
|
13
|
+
if (parentPath.type === "AssignmentExpression") {
|
|
14
|
+
const pp = parentPath;
|
|
15
|
+
const name = pp.get("left");
|
|
16
|
+
return name.node.type === "Identifier" ? name : null;
|
|
17
|
+
}
|
|
18
|
+
if (path.node.type === "ArrowFunctionExpression") {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
return path.node.id && path.node.id.type === "Identifier" ? path.get("id") : null;
|
|
22
|
+
}
|
|
23
|
+
function isIdentifierReferenced(ident) {
|
|
24
|
+
const b = ident.scope.getBinding(ident.node.name);
|
|
25
|
+
if (b?.referenced) {
|
|
26
|
+
if (b.path.type === "FunctionDeclaration") {
|
|
27
|
+
return !b.constantViolations
|
|
28
|
+
.concat(b.referencePaths)
|
|
29
|
+
.every(ref => ref.findParent(p => p === b.path));
|
|
30
|
+
}
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
function markFunction(path, state) {
|
|
36
|
+
const ident = getIdentifier(path);
|
|
37
|
+
if (ident && ident.node && isIdentifierReferenced(ident)) {
|
|
38
|
+
state.refs.add(ident);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function markImport(path, state) {
|
|
42
|
+
const local = path.get("local");
|
|
43
|
+
if (isIdentifierReferenced(local)) {
|
|
44
|
+
state.refs.add(local);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
visitor: {
|
|
49
|
+
Program: {
|
|
50
|
+
enter(path, state) {
|
|
51
|
+
state.refs = new Set();
|
|
52
|
+
state.done = false;
|
|
53
|
+
path.traverse({
|
|
54
|
+
VariableDeclarator(variablePath, variableState) {
|
|
55
|
+
if (variablePath.node.id.type === "Identifier") {
|
|
56
|
+
const local = variablePath.get("id");
|
|
57
|
+
if (isIdentifierReferenced(local)) {
|
|
58
|
+
variableState.refs.add(local);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
else if (variablePath.node.id.type === "ObjectPattern") {
|
|
62
|
+
const pattern = variablePath.get("id");
|
|
63
|
+
const properties = pattern.get("properties");
|
|
64
|
+
properties.forEach(p => {
|
|
65
|
+
const local = p.get(p.node.type === "ObjectProperty"
|
|
66
|
+
? "value"
|
|
67
|
+
: p.node.type === "RestElement"
|
|
68
|
+
? "argument"
|
|
69
|
+
: (() => {
|
|
70
|
+
throw new Error("invariant");
|
|
71
|
+
})());
|
|
72
|
+
if (isIdentifierReferenced(local)) {
|
|
73
|
+
variableState.refs.add(local);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
else if (variablePath.node.id.type === "ArrayPattern") {
|
|
78
|
+
const pattern = variablePath.get("id");
|
|
79
|
+
const elements = pattern.get("elements");
|
|
80
|
+
elements.forEach(e => {
|
|
81
|
+
let local;
|
|
82
|
+
if (e.node && e.node.type === "Identifier") {
|
|
83
|
+
local = e;
|
|
84
|
+
}
|
|
85
|
+
else if (e.node && e.node.type === "RestElement") {
|
|
86
|
+
local = e.get("argument");
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (isIdentifierReferenced(local)) {
|
|
92
|
+
variableState.refs.add(local);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
ExportDefaultDeclaration(exportNamedPath) {
|
|
98
|
+
// if opts.keep is true, we don't remove the routeData export
|
|
99
|
+
if (state.opts.pick && !state.opts.pick.includes("default")) {
|
|
100
|
+
exportNamedPath.remove();
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
ExportNamedDeclaration(exportNamedPath) {
|
|
104
|
+
// if opts.keep is false, we don't remove the routeData export
|
|
105
|
+
if (!state.opts.pick) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const specifiers = exportNamedPath.get("specifiers");
|
|
109
|
+
if (specifiers.length) {
|
|
110
|
+
specifiers.forEach(s => {
|
|
111
|
+
if (t.isIdentifier(s.node.exported)
|
|
112
|
+
? s.node.exported.name
|
|
113
|
+
: state.opts.pick.includes(s.node.exported.value)) {
|
|
114
|
+
s.remove();
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
if (exportNamedPath.node.specifiers.length < 1) {
|
|
118
|
+
exportNamedPath.remove();
|
|
119
|
+
}
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const decl = exportNamedPath.get("declaration");
|
|
123
|
+
if (decl == null || decl.node == null) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
switch (decl.node.type) {
|
|
127
|
+
case "FunctionDeclaration": {
|
|
128
|
+
const name = decl.node.id?.name;
|
|
129
|
+
if (name && state.opts.pick && !state.opts.pick.includes(name)) {
|
|
130
|
+
exportNamedPath.remove();
|
|
131
|
+
}
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
case "VariableDeclaration": {
|
|
135
|
+
const inner = decl.get("declarations");
|
|
136
|
+
inner.forEach(d => {
|
|
137
|
+
if (d.node.id.type !== "Identifier") {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const name = d.node.id.name;
|
|
141
|
+
if (state.opts.pick && !state.opts.pick.includes(name)) {
|
|
142
|
+
d.remove();
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
default: {
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
FunctionDeclaration: markFunction,
|
|
153
|
+
FunctionExpression: markFunction,
|
|
154
|
+
ArrowFunctionExpression: markFunction,
|
|
155
|
+
ImportSpecifier: markImport,
|
|
156
|
+
ImportDefaultSpecifier: markImport,
|
|
157
|
+
ImportNamespaceSpecifier: markImport,
|
|
158
|
+
ImportDeclaration: (path, state) => {
|
|
159
|
+
if (path.node.source.value.endsWith(".css") &&
|
|
160
|
+
state.opts.pick &&
|
|
161
|
+
!state.opts.pick.includes("$css")) {
|
|
162
|
+
path.remove();
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
}, state);
|
|
166
|
+
const refs = state.refs;
|
|
167
|
+
let count = 0;
|
|
168
|
+
const sweepFunction = (sweepPath) => {
|
|
169
|
+
const ident = getIdentifier(sweepPath);
|
|
170
|
+
if (ident && ident.node && refs.has(ident) && !isIdentifierReferenced(ident)) {
|
|
171
|
+
++count;
|
|
172
|
+
if (t.isAssignmentExpression(sweepPath.parentPath) ||
|
|
173
|
+
t.isVariableDeclarator(sweepPath.parentPath)) {
|
|
174
|
+
sweepPath.parentPath.remove();
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
sweepPath.remove();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
function sweepImport(sweepPath) {
|
|
182
|
+
const local = sweepPath.get("local");
|
|
183
|
+
if (refs.has(local) && !isIdentifierReferenced(local)) {
|
|
184
|
+
++count;
|
|
185
|
+
sweepPath.remove();
|
|
186
|
+
if (sweepPath.parent.specifiers.length === 0) {
|
|
187
|
+
sweepPath.parentPath.remove();
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
do {
|
|
192
|
+
path.scope.crawl();
|
|
193
|
+
count = 0;
|
|
194
|
+
path.traverse({
|
|
195
|
+
VariableDeclarator(variablePath) {
|
|
196
|
+
if (variablePath.node.id.type === "Identifier") {
|
|
197
|
+
const local = variablePath.get("id");
|
|
198
|
+
if (refs.has(local) && !isIdentifierReferenced(local)) {
|
|
199
|
+
++count;
|
|
200
|
+
variablePath.remove();
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
else if (variablePath.node.id.type === "ObjectPattern") {
|
|
204
|
+
const pattern = variablePath.get("id");
|
|
205
|
+
const beforeCount = count;
|
|
206
|
+
const properties = pattern.get("properties");
|
|
207
|
+
properties.forEach(p => {
|
|
208
|
+
const local = p.get(p.node.type === "ObjectProperty"
|
|
209
|
+
? "value"
|
|
210
|
+
: p.node.type === "RestElement"
|
|
211
|
+
? "argument"
|
|
212
|
+
: (() => {
|
|
213
|
+
throw new Error("invariant");
|
|
214
|
+
})());
|
|
215
|
+
if (refs.has(local) && !isIdentifierReferenced(local)) {
|
|
216
|
+
++count;
|
|
217
|
+
p.remove();
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
if (beforeCount !== count && pattern.get("properties").length < 1) {
|
|
221
|
+
variablePath.remove();
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
else if (variablePath.node.id.type === "ArrayPattern") {
|
|
225
|
+
const pattern = variablePath.get("id");
|
|
226
|
+
const beforeCount = count;
|
|
227
|
+
const elements = pattern.get("elements");
|
|
228
|
+
elements.forEach(e => {
|
|
229
|
+
let local;
|
|
230
|
+
if (e.node && e.node.type === "Identifier") {
|
|
231
|
+
local = e;
|
|
232
|
+
}
|
|
233
|
+
else if (e.node && e.node.type === "RestElement") {
|
|
234
|
+
local = e.get("argument");
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
if (refs.has(local) && !isIdentifierReferenced(local)) {
|
|
240
|
+
++count;
|
|
241
|
+
e.remove();
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
if (beforeCount !== count && pattern.get("elements").length < 1) {
|
|
245
|
+
variablePath.remove();
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
},
|
|
249
|
+
FunctionDeclaration: sweepFunction,
|
|
250
|
+
FunctionExpression: sweepFunction,
|
|
251
|
+
ArrowFunctionExpression: sweepFunction,
|
|
252
|
+
ImportSpecifier: sweepImport,
|
|
253
|
+
ImportDefaultSpecifier: sweepImport,
|
|
254
|
+
ImportNamespaceSpecifier: sweepImport,
|
|
255
|
+
});
|
|
256
|
+
} while (count);
|
|
257
|
+
},
|
|
258
|
+
},
|
|
259
|
+
},
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
export function treeShake() {
|
|
263
|
+
let config;
|
|
264
|
+
const cache = {};
|
|
265
|
+
let server;
|
|
266
|
+
async function transform(id, code) {
|
|
267
|
+
const [path, queryString] = id.split("?");
|
|
268
|
+
const query = new URLSearchParams(queryString);
|
|
269
|
+
if (query.has("pick")) {
|
|
270
|
+
const babel = await import("@babel/core");
|
|
271
|
+
const transformed = await babel.transformAsync(code, {
|
|
272
|
+
plugins: [[treeShakeTransform, { pick: query.getAll("pick") }]],
|
|
273
|
+
parserOpts: {
|
|
274
|
+
plugins: ["jsx", "typescript"],
|
|
275
|
+
},
|
|
276
|
+
filename: basename(id),
|
|
277
|
+
ast: false,
|
|
278
|
+
sourceMaps: true,
|
|
279
|
+
configFile: false,
|
|
280
|
+
babelrc: false,
|
|
281
|
+
sourceFileName: id,
|
|
282
|
+
});
|
|
283
|
+
return transformed;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return {
|
|
287
|
+
name: "tree-shake",
|
|
288
|
+
enforce: "pre",
|
|
289
|
+
configResolved(resolvedConfig) {
|
|
290
|
+
config = resolvedConfig;
|
|
291
|
+
},
|
|
292
|
+
configureServer(s) {
|
|
293
|
+
server = s;
|
|
294
|
+
},
|
|
295
|
+
async handleHotUpdate(ctx) {
|
|
296
|
+
if (cache[ctx.file]) {
|
|
297
|
+
const mods = [];
|
|
298
|
+
const newCode = await ctx.read();
|
|
299
|
+
for (const [id, code] of Object.entries(cache[ctx.file])) {
|
|
300
|
+
const transformed = await transform(id, newCode);
|
|
301
|
+
if (!transformed)
|
|
302
|
+
continue;
|
|
303
|
+
const { code: transformedCode } = transformed;
|
|
304
|
+
if (transformedCode !== code) {
|
|
305
|
+
const mod = server.moduleGraph.getModuleById(id);
|
|
306
|
+
if (mod)
|
|
307
|
+
mods.push(mod);
|
|
308
|
+
}
|
|
309
|
+
cache[ctx.file] ??= {};
|
|
310
|
+
cache[ctx.file][id] = transformedCode;
|
|
311
|
+
// server.moduleGraph.setModuleSource(id, code);
|
|
312
|
+
}
|
|
313
|
+
return mods;
|
|
314
|
+
}
|
|
315
|
+
// const mods = [];
|
|
316
|
+
// [...server.moduleGraph.urlToModuleMap.entries()].forEach(([url, m]) => {
|
|
317
|
+
// if (m.file === ctx.file && m.id.includes("pick=")) {
|
|
318
|
+
// if (!m.id.includes("pick=loader")) {
|
|
319
|
+
// mods.push(m);
|
|
320
|
+
// }
|
|
321
|
+
// }
|
|
322
|
+
// });
|
|
323
|
+
// return mods;
|
|
324
|
+
// // this.router.updateRoute(ctx.path);
|
|
325
|
+
// }
|
|
326
|
+
},
|
|
327
|
+
async transform(code, id) {
|
|
328
|
+
const [path, queryString] = id.split("?");
|
|
329
|
+
if (!path)
|
|
330
|
+
return;
|
|
331
|
+
const query = new URLSearchParams(queryString);
|
|
332
|
+
const ext = path.split(".").pop();
|
|
333
|
+
if (!ext)
|
|
334
|
+
return;
|
|
335
|
+
if (query.has("pick") && ["js", "jsx", "ts", "tsx"].includes(ext)) {
|
|
336
|
+
const transformed = await transform(id, code);
|
|
337
|
+
if (!transformed?.code)
|
|
338
|
+
return;
|
|
339
|
+
cache[path] ??= {};
|
|
340
|
+
cache[path][id] = transformed.code;
|
|
341
|
+
return {
|
|
342
|
+
code: transformed.code,
|
|
343
|
+
map: transformed.map,
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
},
|
|
347
|
+
};
|
|
348
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type PluginOption } from "vite";
|
|
2
|
+
import { type Options as SolidOptions } from "vite-plugin-solid";
|
|
3
|
+
export interface SolidStartOptions {
|
|
4
|
+
solid?: Partial<SolidOptions>;
|
|
5
|
+
ssr?: boolean;
|
|
6
|
+
routeDir?: string;
|
|
7
|
+
extensions?: string[];
|
|
8
|
+
middleware?: string;
|
|
9
|
+
}
|
|
10
|
+
export declare function solidStart(options?: SolidStartOptions): Array<PluginOption>;
|