@solidjs/start 2.0.0-alpha.1 → 2.0.0-alpha.3

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 (119) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/dist/config/constants.d.ts +1 -1
  3. package/dist/config/constants.js +1 -1
  4. package/dist/config/dev-server.js +18 -0
  5. package/dist/config/env.d.ts +19 -0
  6. package/dist/config/env.js +81 -0
  7. package/dist/config/fs-routes/fs-watcher.js +14 -20
  8. package/dist/config/fs-routes/index.js +2 -2
  9. package/dist/config/fs-routes/router.js +7 -8
  10. package/dist/config/index.d.ts +16 -1
  11. package/dist/config/index.js +30 -29
  12. package/dist/config/lazy.js +35 -5
  13. package/dist/config/manifest.js +40 -6
  14. package/dist/config/vite-utils.d.ts +6 -1
  15. package/dist/config/vite-utils.js +7 -3
  16. package/dist/directives/bubble-function-declaration.d.ts +3 -0
  17. package/dist/directives/bubble-function-declaration.js +23 -0
  18. package/dist/directives/compile.d.ts +9 -0
  19. package/dist/directives/compile.js +40 -0
  20. package/dist/directives/generate-unique-name.d.ts +3 -0
  21. package/dist/directives/generate-unique-name.js +16 -0
  22. package/dist/directives/get-descriptive-name.d.ts +2 -0
  23. package/dist/directives/get-descriptive-name.js +37 -0
  24. package/dist/directives/get-import-identifier.d.ts +4 -0
  25. package/dist/directives/get-import-identifier.js +19 -0
  26. package/dist/directives/get-root-statement-path.d.ts +2 -0
  27. package/dist/directives/get-root-statement-path.js +12 -0
  28. package/dist/directives/index.d.ts +14 -0
  29. package/dist/directives/index.js +169 -0
  30. package/dist/directives/is-statement-top-level.d.ts +3 -0
  31. package/dist/directives/is-statement-top-level.js +9 -0
  32. package/dist/directives/paths.d.ts +9 -0
  33. package/dist/directives/paths.js +36 -0
  34. package/dist/directives/plugin.d.ts +21 -0
  35. package/dist/directives/plugin.js +302 -0
  36. package/dist/directives/remove-unused-variables.d.ts +3 -0
  37. package/dist/directives/remove-unused-variables.js +53 -0
  38. package/dist/directives/types.d.ts +10 -0
  39. package/dist/directives/xxhash32.d.ts +6 -0
  40. package/dist/directives/xxhash32.js +193 -0
  41. package/dist/{server/server-runtime.d.ts → fns/client.d.ts} +1 -1
  42. package/dist/fns/client.js +106 -0
  43. package/dist/fns/handler.d.ts +5 -0
  44. package/dist/{server/server-functions-handler.js → fns/handler.js} +55 -96
  45. package/dist/fns/registration.d.ts +2 -0
  46. package/dist/fns/registration.js +12 -0
  47. package/dist/fns/serialization.d.ts +6 -0
  48. package/dist/fns/serialization.js +208 -0
  49. package/dist/fns/server.d.ts +7 -0
  50. package/dist/{server/server-fns-runtime.js → fns/server.js} +7 -1
  51. package/dist/fns/shared.d.ts +17 -0
  52. package/dist/fns/shared.js +105 -0
  53. package/dist/http/index.d.ts +1 -1
  54. package/dist/middleware/index.d.ts +20 -0
  55. package/dist/middleware/index.js +21 -1
  56. package/dist/server/StartServer.jsx +2 -0
  57. package/dist/server/assets/PatchVirtualDevStyles.d.ts +4 -0
  58. package/dist/server/assets/PatchVirtualDevStyles.jsx +28 -0
  59. package/dist/server/collect-styles.js +9 -21
  60. package/dist/server/handler.d.ts +61 -3
  61. package/dist/server/handler.js +18 -15
  62. package/dist/server/spa/StartServer.jsx +2 -0
  63. package/dist/server/spa/handler.d.ts +30 -1
  64. package/dist/server/types.d.ts +1 -1
  65. package/dist/shared/dev-overlay/CodeView.jsx +4 -4
  66. package/dist/shared/dev-overlay/DevOverlayDialog.jsx +34 -34
  67. package/dist/shared/dev-overlay/index.jsx +2 -2
  68. package/dist/shared/dev-overlay/styles.css +40 -40
  69. package/package.json +27 -20
  70. package/src/config/constants.ts +1 -1
  71. package/src/config/dev-server.ts +12 -0
  72. package/src/config/env.ts +105 -0
  73. package/src/config/fs-routes/fs-watcher.ts +17 -24
  74. package/src/config/fs-routes/index.ts +2 -2
  75. package/src/config/fs-routes/router.ts +7 -8
  76. package/src/config/index.ts +44 -40
  77. package/src/config/lazy.ts +36 -5
  78. package/src/config/manifest.ts +40 -9
  79. package/src/config/vite-utils.ts +8 -4
  80. package/src/directives/bubble-function-declaration.ts +33 -0
  81. package/src/directives/compile.ts +54 -0
  82. package/src/directives/generate-unique-name.ts +22 -0
  83. package/src/directives/get-descriptive-name.ts +39 -0
  84. package/src/directives/get-import-identifier.ts +34 -0
  85. package/src/directives/get-root-statement-path.ts +14 -0
  86. package/src/directives/index.ts +232 -0
  87. package/src/directives/is-statement-top-level.ts +13 -0
  88. package/src/directives/paths.ts +63 -0
  89. package/src/directives/plugin.ts +389 -0
  90. package/src/directives/remove-unused-variables.ts +55 -0
  91. package/src/directives/types.ts +12 -0
  92. package/src/directives/xxhash32.ts +214 -0
  93. package/src/env.d.ts +1 -1
  94. package/src/fns/client.ts +141 -0
  95. package/src/fns/handler.spec.ts +85 -0
  96. package/src/{server/server-functions-handler.ts → fns/handler.ts} +65 -117
  97. package/src/fns/registration.ts +19 -0
  98. package/src/fns/serialization.ts +250 -0
  99. package/src/{server/server-fns-runtime.ts → fns/server.ts} +17 -2
  100. package/src/fns/shared.ts +116 -0
  101. package/src/middleware/index.ts +21 -1
  102. package/src/server/StartServer.tsx +2 -0
  103. package/src/server/assets/PatchVirtualDevStyles.tsx +31 -0
  104. package/src/server/collect-styles.ts +9 -26
  105. package/src/server/handler.ts +20 -16
  106. package/src/server/spa/StartServer.tsx +2 -0
  107. package/src/server/types.ts +1 -1
  108. package/src/shared/dev-overlay/CodeView.tsx +5 -7
  109. package/src/shared/dev-overlay/DevOverlayDialog.tsx +34 -34
  110. package/src/shared/dev-overlay/get-source-map.ts +1 -1
  111. package/src/shared/dev-overlay/index.tsx +4 -4
  112. package/src/shared/dev-overlay/styles.css +40 -40
  113. package/tsconfig.json +1 -0
  114. package/dist/server/fetchEvent.spec.js +0 -65
  115. package/dist/server/server-fns-runtime.d.ts +0 -1
  116. package/dist/server/server-functions-handler.d.ts +0 -2
  117. package/dist/server/server-runtime.js +0 -194
  118. package/src/server/server-runtime.ts +0 -240
  119. /package/dist/{server/fetchEvent.spec.d.ts → directives/types.js} +0 -0
@@ -0,0 +1,40 @@
1
+ import * as babel from "@babel/core";
2
+ import path from "node:path";
3
+ import { directivesPlugin } from "./plugin.js";
4
+ import xxHash32 from "./xxhash32.js";
5
+ export async function compile(id, code, options) {
6
+ const context = {
7
+ ...options,
8
+ valid: false,
9
+ hash: xxHash32(id).toString(16),
10
+ count: 0,
11
+ imports: new Map(),
12
+ };
13
+ const pluginOption = [directivesPlugin, context];
14
+ const plugins = [
15
+ "jsx",
16
+ ];
17
+ if (/\.[mc]?tsx?$/i.test(id)) {
18
+ plugins.push("typescript");
19
+ }
20
+ const result = await babel.transformAsync(code, {
21
+ plugins: [pluginOption],
22
+ parserOpts: {
23
+ plugins,
24
+ },
25
+ filename: path.basename(id),
26
+ ast: false,
27
+ sourceMaps: true,
28
+ configFile: false,
29
+ babelrc: false,
30
+ sourceFileName: id,
31
+ });
32
+ if (result) {
33
+ return {
34
+ valid: context.valid,
35
+ code: result.code || "",
36
+ map: result.map,
37
+ };
38
+ }
39
+ throw new Error("invariant");
40
+ }
@@ -0,0 +1,3 @@
1
+ import type * as babel from "@babel/core";
2
+ import * as t from "@babel/types";
3
+ export declare function generateUniqueName(path: babel.NodePath, name: string): t.Identifier;
@@ -0,0 +1,16 @@
1
+ import * as t from "@babel/types";
2
+ export function generateUniqueName(path, name) {
3
+ let uid;
4
+ let i = 1;
5
+ do {
6
+ uid = name + "_" + i;
7
+ i++;
8
+ } while (path.scope.hasLabel(uid) ||
9
+ path.scope.hasBinding(uid) ||
10
+ path.scope.hasGlobal(uid) ||
11
+ path.scope.hasReference(uid));
12
+ const program = path.scope.getProgramParent();
13
+ program.references[uid] = true;
14
+ program.uids[uid] = true;
15
+ return t.identifier(uid);
16
+ }
@@ -0,0 +1,2 @@
1
+ import type { NodePath } from "@babel/core";
2
+ export declare function getDescriptiveName(path: NodePath, defaultName: string): string;
@@ -0,0 +1,37 @@
1
+ export function getDescriptiveName(path, defaultName) {
2
+ let current = path;
3
+ while (current) {
4
+ switch (current.node.type) {
5
+ case "FunctionDeclaration":
6
+ case "FunctionExpression": {
7
+ if (current.node.id) {
8
+ return current.node.id.name;
9
+ }
10
+ break;
11
+ }
12
+ case "VariableDeclarator": {
13
+ if (current.node.id.type === "Identifier") {
14
+ return current.node.id.name;
15
+ }
16
+ break;
17
+ }
18
+ case "ClassPrivateMethod":
19
+ case "ClassMethod":
20
+ case "ObjectMethod": {
21
+ switch (current.node.key.type) {
22
+ case "Identifier":
23
+ return current.node.key.name;
24
+ case "PrivateName":
25
+ return current.node.key.id.name;
26
+ default:
27
+ break;
28
+ }
29
+ break;
30
+ }
31
+ default:
32
+ break;
33
+ }
34
+ current = current.parentPath;
35
+ }
36
+ return defaultName;
37
+ }
@@ -0,0 +1,4 @@
1
+ import type * as babel from "@babel/core";
2
+ import * as t from "@babel/types";
3
+ import type { ImportDefinition } from "./types.ts";
4
+ export declare function getImportIdentifier(imports: Map<string, t.Identifier>, path: babel.NodePath, registration: ImportDefinition): t.Identifier;
@@ -0,0 +1,19 @@
1
+ import * as t from "@babel/types";
2
+ import { generateUniqueName } from "./generate-unique-name.js";
3
+ export function getImportIdentifier(imports, path, registration) {
4
+ const name = registration.kind === "named" ? registration.name : "default";
5
+ const target = `${registration.source}[${name}]`;
6
+ const current = imports.get(target);
7
+ if (current) {
8
+ return current;
9
+ }
10
+ const programParent = path.scope.getProgramParent();
11
+ const uid = generateUniqueName(programParent.path, name);
12
+ programParent.registerDeclaration(programParent.path.unshiftContainer("body", t.importDeclaration([
13
+ registration.kind === "named"
14
+ ? t.importSpecifier(uid, t.identifier(registration.name))
15
+ : t.importDefaultSpecifier(uid),
16
+ ], t.stringLiteral(registration.source)))[0]);
17
+ imports.set(target, uid);
18
+ return uid;
19
+ }
@@ -0,0 +1,2 @@
1
+ import type * as babel from "@babel/core";
2
+ export declare function getRootStatementPath(path: babel.NodePath): babel.NodePath;
@@ -0,0 +1,12 @@
1
+ import * as t from "@babel/types";
2
+ export function getRootStatementPath(path) {
3
+ let current = path.parentPath;
4
+ while (current) {
5
+ const next = current.parentPath;
6
+ if (next && t.isProgram(next.node)) {
7
+ return current;
8
+ }
9
+ current = next;
10
+ }
11
+ return path;
12
+ }
@@ -0,0 +1,14 @@
1
+ import { type FilterPattern, type Plugin } from "vite";
2
+ export interface ServerFunctionsFilter {
3
+ include?: FilterPattern;
4
+ exclude?: FilterPattern;
5
+ }
6
+ export interface ServerFunctionsOptions {
7
+ manifest: string;
8
+ runtime: {
9
+ server: string;
10
+ client: string;
11
+ };
12
+ filter?: ServerFunctionsFilter;
13
+ }
14
+ export declare function serverFunctionsPlugin(options: ServerFunctionsOptions): Plugin[];
@@ -0,0 +1,169 @@
1
+ import { createFilter, } from "vite";
2
+ import { compile } from "./compile.js";
3
+ const DEFAULT_INCLUDE = "src/**/*.{jsx,tsx,ts,js,mjs,cjs}";
4
+ const DEFAULT_EXCLUDE = "node_modules/**/*.{jsx,tsx,ts,js,mjs,cjs}";
5
+ const DIRECTIVE = "use server";
6
+ function createManifest() {
7
+ return {
8
+ server: new Set(),
9
+ client: new Set(),
10
+ };
11
+ }
12
+ function createDeferredPromise() {
13
+ let resolve;
14
+ let reject;
15
+ return {
16
+ reference: new Promise((res, rej) => {
17
+ resolve = res;
18
+ reject = rej;
19
+ }),
20
+ resolve(value) {
21
+ resolve(value);
22
+ },
23
+ reject(value) {
24
+ reject(value);
25
+ },
26
+ };
27
+ }
28
+ class Debouncer {
29
+ source;
30
+ promise;
31
+ timeout;
32
+ constructor(source) {
33
+ this.source = source;
34
+ this.promise = createDeferredPromise();
35
+ this.defer();
36
+ }
37
+ defer() {
38
+ if (this.timeout) {
39
+ clearTimeout(this.timeout);
40
+ this.timeout = undefined;
41
+ }
42
+ this.timeout = setTimeout(() => {
43
+ this.promise.resolve(this.source());
44
+ }, 1000);
45
+ }
46
+ }
47
+ function mergeManifestRecord(source, target) {
48
+ const current = source.size;
49
+ for (const entry of target) {
50
+ source.add(entry);
51
+ }
52
+ return {
53
+ invalidPreload: current !== source.size,
54
+ invalidated: [...source],
55
+ };
56
+ }
57
+ function invalidateModule(moduleGraph, path) {
58
+ const target = moduleGraph.getModuleById(path);
59
+ if (target) {
60
+ moduleGraph.invalidateModule(target);
61
+ }
62
+ }
63
+ function invalidateModules(server, result, manifest) {
64
+ if (server) {
65
+ if (result.invalidPreload) {
66
+ invalidateModule(server.environments.client.moduleGraph, manifest);
67
+ invalidateModule(server.environments.ssr.moduleGraph, manifest);
68
+ }
69
+ }
70
+ }
71
+ export function serverFunctionsPlugin(options) {
72
+ const filter = createFilter(options.filter?.include || DEFAULT_INCLUDE, options.filter?.exclude || DEFAULT_EXCLUDE);
73
+ let env;
74
+ const manifest = createManifest();
75
+ const preload = {
76
+ server: undefined,
77
+ client: undefined,
78
+ };
79
+ let currentServer;
80
+ const clientOptions = {
81
+ directive: DIRECTIVE,
82
+ definitions: {
83
+ register: {
84
+ kind: "named",
85
+ name: "createServerReference",
86
+ source: options.runtime.client,
87
+ },
88
+ clone: {
89
+ kind: "named",
90
+ name: "cloneServerReference",
91
+ source: options.runtime.client,
92
+ },
93
+ },
94
+ };
95
+ const serverOptions = {
96
+ directive: DIRECTIVE,
97
+ definitions: {
98
+ register: {
99
+ kind: "named",
100
+ name: "createServerReference",
101
+ source: options.runtime.server,
102
+ },
103
+ clone: {
104
+ kind: "named",
105
+ name: "cloneServerReference",
106
+ source: options.runtime.server,
107
+ },
108
+ },
109
+ };
110
+ return [
111
+ {
112
+ name: "solid-start:server-functions/setup",
113
+ enforce: "pre",
114
+ configResolved(config) {
115
+ env = config.mode !== "production" ? "development" : "production";
116
+ },
117
+ configureServer(server) {
118
+ currentServer = server;
119
+ },
120
+ },
121
+ {
122
+ name: "solid-start:server-functions/preload",
123
+ enforce: "pre",
124
+ resolveId(source) {
125
+ if (source === options.manifest) {
126
+ return { id: options.manifest, moduleSideEffects: true };
127
+ }
128
+ return null;
129
+ },
130
+ async load(id, opts) {
131
+ const mode = opts?.ssr ? "server" : "client";
132
+ if (id === options.manifest) {
133
+ const current = new Debouncer(() => [...manifest[mode]].map(entry => `import "${entry}";`).join("\n"));
134
+ preload[mode] = current;
135
+ const result = await current.promise.reference;
136
+ return result;
137
+ }
138
+ return null;
139
+ },
140
+ },
141
+ {
142
+ name: "solid-start:server-functions/compiler",
143
+ async transform(code, fileId, opts) {
144
+ const mode = opts?.ssr ? "server" : "client";
145
+ const [id] = fileId.split("?");
146
+ if (!filter(id)) {
147
+ return null;
148
+ }
149
+ const result = await compile(id, code, {
150
+ ...(mode === "server" ? serverOptions : clientOptions),
151
+ mode,
152
+ env,
153
+ });
154
+ if (result.valid) {
155
+ const preloader = preload[mode];
156
+ if (preloader) {
157
+ preloader.defer();
158
+ }
159
+ invalidateModules(currentServer, mergeManifestRecord(manifest.server, new Set([id])), options.manifest);
160
+ return {
161
+ code: result.code || "",
162
+ map: result.map,
163
+ };
164
+ }
165
+ return null;
166
+ },
167
+ },
168
+ ];
169
+ }
@@ -0,0 +1,3 @@
1
+ import type * as babel from "@babel/core";
2
+ import type * as t from "@babel/types";
3
+ export declare function isStatementTopLevel(path: babel.NodePath<t.Statement>): boolean;
@@ -0,0 +1,9 @@
1
+ export function isStatementTopLevel(path) {
2
+ let blockParent = path.scope.getBlockParent();
3
+ const programParent = path.scope.getProgramParent();
4
+ // a FunctionDeclaration binding refers to itself as the block parent
5
+ if (blockParent.path === path) {
6
+ blockParent = blockParent.parent;
7
+ }
8
+ return programParent === blockParent;
9
+ }
@@ -0,0 +1,9 @@
1
+ import type * as t from "@babel/types";
2
+ type TypeFilter<V extends t.Node> = (node: t.Node) => node is V;
3
+ export declare function isPathValid<V extends t.Node>(path: unknown, key: TypeFilter<V>): path is babel.NodePath<V>;
4
+ export type NestedExpression = t.ParenthesizedExpression | t.TypeCastExpression | t.TSAsExpression | t.TSSatisfiesExpression | t.TSNonNullExpression | t.TSInstantiationExpression | t.TSTypeAssertion;
5
+ export declare function isNestedExpression(node: t.Node): node is NestedExpression;
6
+ type TypeCheck<K> = K extends TypeFilter<infer U> ? U : never;
7
+ export declare function unwrapNode<K extends (value: t.Node) => boolean>(node: t.Node, key: K): TypeCheck<K> | undefined;
8
+ export declare function unwrapPath<V extends t.Node>(path: unknown, key: TypeFilter<V>): babel.NodePath<V> | undefined;
9
+ export {};
@@ -0,0 +1,36 @@
1
+ export function isPathValid(path, key) {
2
+ const node = path.node;
3
+ return node ? key(node) : false;
4
+ }
5
+ export function isNestedExpression(node) {
6
+ switch (node.type) {
7
+ case "ParenthesizedExpression":
8
+ case "TypeCastExpression":
9
+ case "TSAsExpression":
10
+ case "TSSatisfiesExpression":
11
+ case "TSNonNullExpression":
12
+ case "TSTypeAssertion":
13
+ case "TSInstantiationExpression":
14
+ return true;
15
+ default:
16
+ return false;
17
+ }
18
+ }
19
+ export function unwrapNode(node, key) {
20
+ if (key(node)) {
21
+ return node;
22
+ }
23
+ if (isNestedExpression(node)) {
24
+ return unwrapNode(node.expression, key);
25
+ }
26
+ return undefined;
27
+ }
28
+ export function unwrapPath(path, key) {
29
+ if (isPathValid(path, key)) {
30
+ return path;
31
+ }
32
+ if (isPathValid(path, isNestedExpression)) {
33
+ return unwrapPath(path.get("expression"), key);
34
+ }
35
+ return undefined;
36
+ }
@@ -0,0 +1,21 @@
1
+ import type * as babel from "@babel/core";
2
+ import * as t from "@babel/types";
3
+ import type { ImportDefinition } from "./types.ts";
4
+ export interface StateContext {
5
+ env: "production" | "development";
6
+ mode: "server" | "client";
7
+ directive: string;
8
+ hash: string;
9
+ count: number;
10
+ imports: Map<string, t.Identifier>;
11
+ valid: boolean;
12
+ definitions: {
13
+ register: ImportDefinition;
14
+ clone: ImportDefinition;
15
+ };
16
+ }
17
+ interface State extends babel.PluginPass {
18
+ opts: StateContext;
19
+ }
20
+ export declare function directivesPlugin(): babel.PluginObj<State>;
21
+ export {};