@solidjs/start 2.0.0-alpha.2 → 2.0.0-beta.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/CHANGELOG.md +35 -0
- package/dist/config/constants.d.ts +3 -3
- package/dist/config/constants.js +1 -1
- package/dist/config/dev-server.js +9 -1
- package/dist/config/env.d.ts +19 -0
- package/dist/config/env.js +81 -0
- package/dist/config/fs-routes/fs-watcher.js +32 -28
- package/dist/config/fs-routes/index.js +2 -2
- package/dist/config/fs-routes/router.d.ts +8 -3
- package/dist/config/fs-routes/router.js +26 -13
- package/dist/config/fs-routes/tree-shake.js +0 -43
- package/dist/config/index.d.ts +16 -1
- package/dist/config/index.js +31 -31
- package/dist/config/lazy.js +4 -2
- package/dist/config/manifest.js +41 -7
- package/dist/config/vite-utils.d.ts +1 -1
- package/dist/directives/bubble-function-declaration.d.ts +3 -0
- package/dist/directives/bubble-function-declaration.js +23 -0
- package/dist/directives/compile.d.ts +9 -0
- package/dist/directives/compile.js +40 -0
- package/dist/directives/generate-unique-name.d.ts +3 -0
- package/dist/directives/generate-unique-name.js +16 -0
- package/dist/directives/get-descriptive-name.d.ts +2 -0
- package/dist/directives/get-descriptive-name.js +37 -0
- package/dist/directives/get-import-identifier.d.ts +4 -0
- package/dist/directives/get-import-identifier.js +19 -0
- package/dist/directives/get-root-statement-path.d.ts +2 -0
- package/dist/directives/get-root-statement-path.js +12 -0
- package/dist/directives/index.d.ts +14 -0
- package/dist/directives/index.js +170 -0
- package/dist/directives/is-statement-top-level.d.ts +3 -0
- package/dist/directives/is-statement-top-level.js +9 -0
- package/dist/directives/paths.d.ts +9 -0
- package/dist/directives/paths.js +36 -0
- package/dist/directives/plugin.d.ts +21 -0
- package/dist/directives/plugin.js +302 -0
- package/dist/directives/remove-unused-variables.d.ts +3 -0
- package/dist/directives/remove-unused-variables.js +66 -0
- package/dist/directives/types.d.ts +10 -0
- package/dist/directives/xxhash32.d.ts +6 -0
- package/dist/directives/xxhash32.js +193 -0
- package/dist/{server/server-runtime.d.ts → fns/client.d.ts} +1 -1
- package/dist/fns/client.js +106 -0
- package/dist/fns/handler.d.ts +5 -0
- package/dist/{server/server-functions-handler.js → fns/handler.js} +55 -96
- package/dist/fns/registration.d.ts +2 -0
- package/dist/fns/registration.js +12 -0
- package/dist/fns/serialization.d.ts +6 -0
- package/dist/fns/serialization.js +208 -0
- package/dist/fns/server.d.ts +7 -0
- package/dist/{server/server-fns-runtime.js → fns/server.js} +7 -1
- package/dist/fns/shared.d.ts +17 -0
- package/dist/fns/shared.js +105 -0
- package/dist/http/index.d.ts +1 -1
- package/dist/middleware/index.d.ts +17 -0
- package/dist/middleware/index.js +15 -0
- package/dist/server/handler.d.ts +61 -3
- package/dist/server/handler.js +18 -16
- package/dist/server/spa/handler.d.ts +30 -1
- package/dist/server/types.d.ts +1 -1
- package/dist/shared/dev-overlay/CodeView.jsx +4 -4
- package/dist/shared/dev-overlay/DevOverlayDialog.jsx +34 -34
- package/dist/shared/dev-overlay/index.jsx +2 -2
- package/dist/shared/dev-overlay/styles.css +40 -40
- package/dist/utils/debounce.d.ts +4 -0
- package/dist/utils/debounce.js +10 -0
- package/package.json +28 -21
- package/src/config/constants.ts +3 -3
- package/src/config/env.ts +105 -0
- package/src/config/fs-routes/fs-watcher.ts +38 -49
- package/src/config/fs-routes/index.ts +2 -2
- package/src/config/fs-routes/router.ts +29 -13
- package/src/config/fs-routes/tree-shake.ts +1 -48
- package/src/config/index.ts +45 -41
- package/src/config/lazy.ts +3 -2
- package/src/config/manifest.ts +45 -10
- package/src/config/vite-utils.ts +1 -1
- package/src/directives/bubble-function-declaration.ts +33 -0
- package/src/directives/compile.ts +54 -0
- package/src/directives/generate-unique-name.ts +22 -0
- package/src/directives/get-descriptive-name.ts +39 -0
- package/src/directives/get-import-identifier.ts +34 -0
- package/src/directives/get-root-statement-path.ts +14 -0
- package/src/directives/index.ts +233 -0
- package/src/directives/is-statement-top-level.ts +13 -0
- package/src/directives/paths.ts +63 -0
- package/src/directives/plugin.ts +389 -0
- package/src/directives/remove-unused-variables.ts +70 -0
- package/src/directives/types.ts +12 -0
- package/src/directives/xxhash32.ts +214 -0
- package/src/env.d.ts +1 -1
- package/src/fns/client.ts +141 -0
- package/src/fns/handler.spec.ts +85 -0
- package/src/{server/server-functions-handler.ts → fns/handler.ts} +65 -117
- package/src/fns/registration.ts +19 -0
- package/src/fns/serialization.ts +250 -0
- package/src/{server/server-fns-runtime.ts → fns/server.ts} +17 -2
- package/src/fns/shared.ts +116 -0
- package/src/middleware/index.ts +17 -0
- package/src/server/handler.ts +19 -17
- package/src/server/types.ts +1 -1
- package/src/shared/dev-overlay/CodeView.tsx +5 -7
- package/src/shared/dev-overlay/DevOverlayDialog.tsx +34 -34
- package/src/shared/dev-overlay/get-source-map.ts +1 -1
- package/src/shared/dev-overlay/index.tsx +4 -4
- package/src/shared/dev-overlay/styles.css +40 -40
- package/src/utils/debounce.ts +10 -0
- package/tsconfig.json +1 -0
- package/dist/server/fetchEvent.spec.js +0 -65
- package/dist/server/server-fns-runtime.d.ts +0 -1
- package/dist/server/server-functions-handler.d.ts +0 -2
- package/dist/server/server-runtime.js +0 -194
- package/src/server/server-runtime.ts +0 -240
- /package/dist/{server/fetchEvent.spec.d.ts → directives/types.js} +0 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createFilter,
|
|
3
|
+
type EnvironmentModuleGraph,
|
|
4
|
+
type FilterPattern,
|
|
5
|
+
type Plugin,
|
|
6
|
+
type ViteDevServer,
|
|
7
|
+
} from "vite";
|
|
8
|
+
import { compile, type CompileOptions } from "./compile.ts";
|
|
9
|
+
|
|
10
|
+
export interface ServerFunctionsFilter {
|
|
11
|
+
include?: FilterPattern;
|
|
12
|
+
exclude?: FilterPattern;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface ServerFunctionsOptions {
|
|
16
|
+
manifest: string;
|
|
17
|
+
runtime: {
|
|
18
|
+
server: string;
|
|
19
|
+
client: string;
|
|
20
|
+
};
|
|
21
|
+
filter?: ServerFunctionsFilter;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const DEFAULT_INCLUDE = "src/**/*.{jsx,tsx,ts,js,mjs,cjs}";
|
|
25
|
+
const DEFAULT_EXCLUDE = "node_modules/**/*.{jsx,tsx,ts,js,mjs,cjs}";
|
|
26
|
+
const DIRECTIVE = "use server";
|
|
27
|
+
|
|
28
|
+
type Manifest = Record<CompileOptions["mode"], Set<string>>;
|
|
29
|
+
|
|
30
|
+
function createManifest(): Manifest {
|
|
31
|
+
return {
|
|
32
|
+
server: new Set(),
|
|
33
|
+
client: new Set(),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface DeferredPromise<T> {
|
|
38
|
+
reference: Promise<T>;
|
|
39
|
+
resolve: (value: T) => void;
|
|
40
|
+
reject: (value: any) => void;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function createDeferredPromise<T>(): DeferredPromise<T> {
|
|
44
|
+
let resolve: DeferredPromise<T>["resolve"];
|
|
45
|
+
let reject: DeferredPromise<T>["reject"];
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
reference: new Promise((res, rej) => {
|
|
49
|
+
resolve = res;
|
|
50
|
+
reject = rej;
|
|
51
|
+
}),
|
|
52
|
+
resolve(value) {
|
|
53
|
+
resolve(value);
|
|
54
|
+
},
|
|
55
|
+
reject(value) {
|
|
56
|
+
reject(value);
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
class Debouncer<T> {
|
|
62
|
+
promise: DeferredPromise<T>;
|
|
63
|
+
|
|
64
|
+
private timeout: ReturnType<typeof setTimeout> | undefined;
|
|
65
|
+
|
|
66
|
+
constructor(private source: () => T) {
|
|
67
|
+
this.promise = createDeferredPromise();
|
|
68
|
+
this.defer();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
defer(): void {
|
|
72
|
+
if (this.timeout) {
|
|
73
|
+
clearTimeout(this.timeout);
|
|
74
|
+
this.timeout = undefined;
|
|
75
|
+
}
|
|
76
|
+
this.timeout = setTimeout(() => {
|
|
77
|
+
this.promise.resolve(this.source());
|
|
78
|
+
}, 1000);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function mergeManifestRecord(
|
|
83
|
+
source: Set<string>,
|
|
84
|
+
target: Set<string>,
|
|
85
|
+
): { invalidPreload: boolean; invalidated: string[] } {
|
|
86
|
+
const current = source.size;
|
|
87
|
+
for (const entry of target) {
|
|
88
|
+
source.add(entry);
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
invalidPreload: current !== source.size,
|
|
92
|
+
invalidated: [...source],
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function invalidateModule(moduleGraph: EnvironmentModuleGraph, path: string) {
|
|
97
|
+
const target = moduleGraph.getModuleById(path);
|
|
98
|
+
if (target) {
|
|
99
|
+
moduleGraph.invalidateModule(target);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function invalidateModules(
|
|
104
|
+
server: ViteDevServer | undefined,
|
|
105
|
+
result: ReturnType<typeof mergeManifestRecord>,
|
|
106
|
+
manifest: string,
|
|
107
|
+
): void {
|
|
108
|
+
if (server) {
|
|
109
|
+
if (result.invalidPreload) {
|
|
110
|
+
invalidateModule(server.environments.client.moduleGraph, manifest);
|
|
111
|
+
invalidateModule(server.environments.ssr.moduleGraph, manifest);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
export function serverFunctionsPlugin(options: ServerFunctionsOptions): Plugin[] {
|
|
118
|
+
const filter = createFilter(
|
|
119
|
+
options.filter?.include || DEFAULT_INCLUDE,
|
|
120
|
+
options.filter?.exclude || DEFAULT_EXCLUDE,
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
let env: CompileOptions["env"];
|
|
124
|
+
|
|
125
|
+
const manifest = createManifest();
|
|
126
|
+
|
|
127
|
+
const preload: Record<CompileOptions["mode"], Debouncer<string> | undefined> = {
|
|
128
|
+
server: undefined,
|
|
129
|
+
client: undefined,
|
|
130
|
+
};
|
|
131
|
+
let currentServer: ViteDevServer;
|
|
132
|
+
|
|
133
|
+
const clientOptions: Pick<CompileOptions, "directive" | "definitions"> = {
|
|
134
|
+
directive: DIRECTIVE,
|
|
135
|
+
definitions: {
|
|
136
|
+
register: {
|
|
137
|
+
kind: "named",
|
|
138
|
+
name: "createServerReference",
|
|
139
|
+
source: options.runtime.client,
|
|
140
|
+
},
|
|
141
|
+
clone: {
|
|
142
|
+
kind: "named",
|
|
143
|
+
name: "cloneServerReference",
|
|
144
|
+
source: options.runtime.client,
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
const serverOptions: Pick<CompileOptions, "directive" | "definitions"> = {
|
|
149
|
+
directive: DIRECTIVE,
|
|
150
|
+
definitions: {
|
|
151
|
+
register: {
|
|
152
|
+
kind: "named",
|
|
153
|
+
name: "createServerReference",
|
|
154
|
+
source: options.runtime.server,
|
|
155
|
+
},
|
|
156
|
+
clone: {
|
|
157
|
+
kind: "named",
|
|
158
|
+
name: "cloneServerReference",
|
|
159
|
+
source: options.runtime.server,
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
return [
|
|
165
|
+
{
|
|
166
|
+
name: "solid-start:server-functions/setup",
|
|
167
|
+
enforce: "pre",
|
|
168
|
+
configResolved(config) {
|
|
169
|
+
env = config.mode !== "production" ? "development" : "production";
|
|
170
|
+
},
|
|
171
|
+
configureServer(server) {
|
|
172
|
+
currentServer = server;
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
name: "solid-start:server-functions/preload",
|
|
177
|
+
enforce: "pre",
|
|
178
|
+
resolveId(source) {
|
|
179
|
+
if (source === options.manifest) {
|
|
180
|
+
return { id: options.manifest, moduleSideEffects: true };
|
|
181
|
+
}
|
|
182
|
+
return null;
|
|
183
|
+
},
|
|
184
|
+
async load(id, opts) {
|
|
185
|
+
const mode = opts?.ssr ? "server" : "client";
|
|
186
|
+
if (id === options.manifest) {
|
|
187
|
+
const current = new Debouncer(() =>
|
|
188
|
+
[...manifest[mode]].map(entry => `import "${entry}";`).join("\n"),
|
|
189
|
+
);
|
|
190
|
+
preload[mode] = current;
|
|
191
|
+
const result = await current.promise.reference;
|
|
192
|
+
return result;
|
|
193
|
+
}
|
|
194
|
+
return null;
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
name: "solid-start:server-functions/compiler",
|
|
199
|
+
enforce: 'pre',
|
|
200
|
+
async transform(code, fileId, opts) {
|
|
201
|
+
const mode = opts?.ssr ? "server" : "client";
|
|
202
|
+
const [id] = fileId.split("?");
|
|
203
|
+
if (!filter(id)) {
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const result = await compile(id!, code, {
|
|
208
|
+
...(mode === "server" ? serverOptions : clientOptions),
|
|
209
|
+
mode,
|
|
210
|
+
env,
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
if (result.valid) {
|
|
214
|
+
const preloader = preload[mode];
|
|
215
|
+
if (preloader) {
|
|
216
|
+
preloader.defer();
|
|
217
|
+
}
|
|
218
|
+
invalidateModules(
|
|
219
|
+
currentServer,
|
|
220
|
+
mergeManifestRecord(manifest.server, new Set([id!])),
|
|
221
|
+
options.manifest,
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
return {
|
|
225
|
+
code: result.code || "",
|
|
226
|
+
map: result.map,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
return null;
|
|
230
|
+
},
|
|
231
|
+
},
|
|
232
|
+
];
|
|
233
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type * as babel from "@babel/core";
|
|
2
|
+
import type * as t from "@babel/types";
|
|
3
|
+
|
|
4
|
+
export function isStatementTopLevel(path: babel.NodePath<t.Statement>): boolean {
|
|
5
|
+
let blockParent = path.scope.getBlockParent();
|
|
6
|
+
const programParent = path.scope.getProgramParent();
|
|
7
|
+
// a FunctionDeclaration binding refers to itself as the block parent
|
|
8
|
+
if (blockParent.path === path) {
|
|
9
|
+
blockParent = blockParent.parent;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return programParent === blockParent;
|
|
13
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type * as t from "@babel/types";
|
|
2
|
+
|
|
3
|
+
type TypeFilter<V extends t.Node> = (node: t.Node) => node is V;
|
|
4
|
+
|
|
5
|
+
export function isPathValid<V extends t.Node>(
|
|
6
|
+
path: unknown,
|
|
7
|
+
key: TypeFilter<V>,
|
|
8
|
+
): path is babel.NodePath<V> {
|
|
9
|
+
const node = (path as babel.NodePath).node;
|
|
10
|
+
return node ? key(node) : false;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type NestedExpression =
|
|
14
|
+
| t.ParenthesizedExpression
|
|
15
|
+
| t.TypeCastExpression
|
|
16
|
+
| t.TSAsExpression
|
|
17
|
+
| t.TSSatisfiesExpression
|
|
18
|
+
| t.TSNonNullExpression
|
|
19
|
+
| t.TSInstantiationExpression
|
|
20
|
+
| t.TSTypeAssertion;
|
|
21
|
+
|
|
22
|
+
export function isNestedExpression(node: t.Node): node is NestedExpression {
|
|
23
|
+
switch (node.type) {
|
|
24
|
+
case "ParenthesizedExpression":
|
|
25
|
+
case "TypeCastExpression":
|
|
26
|
+
case "TSAsExpression":
|
|
27
|
+
case "TSSatisfiesExpression":
|
|
28
|
+
case "TSNonNullExpression":
|
|
29
|
+
case "TSTypeAssertion":
|
|
30
|
+
case "TSInstantiationExpression":
|
|
31
|
+
return true;
|
|
32
|
+
default:
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
type TypeCheck<K> = K extends TypeFilter<infer U> ? U : never;
|
|
38
|
+
|
|
39
|
+
export function unwrapNode<K extends (value: t.Node) => boolean>(
|
|
40
|
+
node: t.Node,
|
|
41
|
+
key: K,
|
|
42
|
+
): TypeCheck<K> | undefined {
|
|
43
|
+
if (key(node)) {
|
|
44
|
+
return node as TypeCheck<K>;
|
|
45
|
+
}
|
|
46
|
+
if (isNestedExpression(node)) {
|
|
47
|
+
return unwrapNode(node.expression, key);
|
|
48
|
+
}
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function unwrapPath<V extends t.Node>(
|
|
53
|
+
path: unknown,
|
|
54
|
+
key: TypeFilter<V>,
|
|
55
|
+
): babel.NodePath<V> | undefined {
|
|
56
|
+
if (isPathValid(path, key)) {
|
|
57
|
+
return path;
|
|
58
|
+
}
|
|
59
|
+
if (isPathValid(path, isNestedExpression)) {
|
|
60
|
+
return unwrapPath(path.get("expression"), key);
|
|
61
|
+
}
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
import type * as babel from "@babel/core";
|
|
2
|
+
import type { Binding } from "@babel/traverse";
|
|
3
|
+
import * as t from "@babel/types";
|
|
4
|
+
import { bubbleFunctionDeclaration } from "./bubble-function-declaration.ts";
|
|
5
|
+
import { generateUniqueName } from "./generate-unique-name.ts";
|
|
6
|
+
import { getDescriptiveName } from "./get-descriptive-name.ts";
|
|
7
|
+
import { getImportIdentifier } from "./get-import-identifier.ts";
|
|
8
|
+
import { getRootStatementPath } from "./get-root-statement-path.ts";
|
|
9
|
+
import { isStatementTopLevel } from "./is-statement-top-level.ts";
|
|
10
|
+
import { isPathValid, unwrapPath } from "./paths.ts";
|
|
11
|
+
import { removeUnusedVariables } from "./remove-unused-variables.ts";
|
|
12
|
+
import type { ImportDefinition } from "./types.ts";
|
|
13
|
+
|
|
14
|
+
export interface StateContext {
|
|
15
|
+
env: "production" | "development";
|
|
16
|
+
mode: "server" | "client";
|
|
17
|
+
directive: string;
|
|
18
|
+
hash: string;
|
|
19
|
+
count: number;
|
|
20
|
+
imports: Map<string, t.Identifier>;
|
|
21
|
+
valid: boolean;
|
|
22
|
+
|
|
23
|
+
definitions: {
|
|
24
|
+
register: ImportDefinition;
|
|
25
|
+
clone: ImportDefinition;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
type ValidFunction = t.ArrowFunctionExpression | t.FunctionExpression;
|
|
30
|
+
|
|
31
|
+
function isValidFunction(node: t.Node): node is ValidFunction {
|
|
32
|
+
return t.isArrowFunctionExpression(node) || t.isFunctionExpression(node);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function isDirectiveValid(ctx: StateContext, directives: t.Directive[]) {
|
|
36
|
+
for (let i = 0, len = directives.length; i < len; i++) {
|
|
37
|
+
if (directives[i]!.value.value === ctx.directive) {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function cleanDirectives(path: babel.NodePath<t.BlockStatement | t.Program>, target: string): void {
|
|
45
|
+
const newDirectives: t.Directive[] = [];
|
|
46
|
+
for (let i = 0, len = path.node.directives.length; i < len; i++) {
|
|
47
|
+
const current = path.node.directives[i]!;
|
|
48
|
+
if (current.value.value !== target) {
|
|
49
|
+
newDirectives.push(current);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
path.node.directives = newDirectives;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function cleanFunctionDirectives(
|
|
56
|
+
ctx: StateContext,
|
|
57
|
+
path: babel.NodePath<t.FunctionDeclaration | ValidFunction>,
|
|
58
|
+
) {
|
|
59
|
+
const body = path.get("body");
|
|
60
|
+
|
|
61
|
+
if (isPathValid(body, t.isBlockStatement)) {
|
|
62
|
+
cleanDirectives(body, ctx.directive);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function isFunctionDirectiveValid(
|
|
67
|
+
ctx: StateContext,
|
|
68
|
+
path: babel.NodePath<t.FunctionDeclaration | ValidFunction>,
|
|
69
|
+
) {
|
|
70
|
+
const body = path.get("body");
|
|
71
|
+
|
|
72
|
+
if (isPathValid(body, t.isBlockStatement)) {
|
|
73
|
+
return isDirectiveValid(ctx, body.node.directives);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function createID(ctx: StateContext, name: string) {
|
|
80
|
+
const base = `${ctx.hash}-${ctx.count++}`;
|
|
81
|
+
if (ctx.env === "development") {
|
|
82
|
+
return `${base}-${name}`;
|
|
83
|
+
}
|
|
84
|
+
return base;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function transformFunction(
|
|
88
|
+
ctx: StateContext,
|
|
89
|
+
path: babel.NodePath<ValidFunction>,
|
|
90
|
+
direct: boolean,
|
|
91
|
+
) {
|
|
92
|
+
if (!direct) {
|
|
93
|
+
if (!isFunctionDirectiveValid(ctx, path)) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
cleanFunctionDirectives(ctx, path);
|
|
97
|
+
}
|
|
98
|
+
// First, get root statement
|
|
99
|
+
const rootStatement = getRootStatementPath(path);
|
|
100
|
+
|
|
101
|
+
// Create a unique ID for the function
|
|
102
|
+
const fnID = createID(ctx, getDescriptiveName(path, "anonymous"));
|
|
103
|
+
|
|
104
|
+
if (ctx.mode === "server") {
|
|
105
|
+
// Create a "source" function on the root-level
|
|
106
|
+
const sourceReference = t.callExpression(
|
|
107
|
+
getImportIdentifier(ctx.imports, path, ctx.definitions.register),
|
|
108
|
+
[t.stringLiteral(fnID), path.node],
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
const sourceID = generateUniqueName(path, "serverFn");
|
|
112
|
+
|
|
113
|
+
rootStatement.insertBefore(
|
|
114
|
+
t.variableDeclaration("const", [t.variableDeclarator(sourceID, sourceReference)]),
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
// Clone the source function to replace the server function
|
|
118
|
+
path.replaceWith(
|
|
119
|
+
t.callExpression(getImportIdentifier(ctx.imports, path, ctx.definitions.clone), [sourceID]),
|
|
120
|
+
);
|
|
121
|
+
} else {
|
|
122
|
+
// Otherwise, clone the function based on its ID
|
|
123
|
+
path.replaceWith(
|
|
124
|
+
t.callExpression(getImportIdentifier(ctx.imports, path, ctx.definitions.clone), [
|
|
125
|
+
t.stringLiteral(fnID),
|
|
126
|
+
]),
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
path.scope.crawl();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function traceBinding(path: babel.NodePath, name: string): Binding | undefined {
|
|
134
|
+
const current = path.scope.getBinding(name);
|
|
135
|
+
if (!current) {
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
switch (current.kind) {
|
|
139
|
+
case "const":
|
|
140
|
+
case "let":
|
|
141
|
+
case "var": {
|
|
142
|
+
if (isPathValid(current.path, t.isVariableDeclarator)) {
|
|
143
|
+
// Check if left is identifier
|
|
144
|
+
const left = unwrapPath(current.path.get("id"), t.isIdentifier);
|
|
145
|
+
if (left) {
|
|
146
|
+
const right = unwrapPath(current.path.get("init"), t.isIdentifier);
|
|
147
|
+
if (right) {
|
|
148
|
+
return traceBinding(path, right.node.name);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Only valid for functions
|
|
152
|
+
const func = unwrapPath(current.path.get("init"), isValidFunction);
|
|
153
|
+
if (func) {
|
|
154
|
+
return current;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
case "hoisted":
|
|
161
|
+
case "local":
|
|
162
|
+
case "module":
|
|
163
|
+
case "param":
|
|
164
|
+
case "unknown":
|
|
165
|
+
return undefined;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function transformBindingForServer(ctx: StateContext, binding: Binding) {
|
|
170
|
+
if (isPathValid(binding.path, t.isVariableDeclarator)) {
|
|
171
|
+
const right = unwrapPath(binding.path.get("init"), isValidFunction);
|
|
172
|
+
if (right) {
|
|
173
|
+
transformFunction(ctx, right, true);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
interface State extends babel.PluginPass {
|
|
179
|
+
opts: StateContext;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function transformModuleLevelDirective(ctx: StateContext, program: babel.NodePath<t.Program>) {
|
|
183
|
+
cleanDirectives(program, ctx.directive);
|
|
184
|
+
program.traverse({
|
|
185
|
+
FunctionDeclaration(child) {
|
|
186
|
+
// We only need to move top-level functions
|
|
187
|
+
if (isStatementTopLevel(child)) {
|
|
188
|
+
bubbleFunctionDeclaration(child);
|
|
189
|
+
}
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
program.scope.crawl();
|
|
193
|
+
if (ctx.mode === "server") {
|
|
194
|
+
// Trace bindings
|
|
195
|
+
const bindings = new Set<Binding>();
|
|
196
|
+
|
|
197
|
+
program.traverse({
|
|
198
|
+
ExportDefaultDeclaration(path) {
|
|
199
|
+
const id = unwrapPath(path.get("declaration"), t.isIdentifier);
|
|
200
|
+
if (id) {
|
|
201
|
+
const binding = traceBinding(path, id.node.name);
|
|
202
|
+
if (binding) {
|
|
203
|
+
bindings.add(binding);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
},
|
|
207
|
+
ExportNamedDeclaration(path) {
|
|
208
|
+
if (path.node.source || path.node.exportKind === "type") {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
for (const specifier of path.get("specifiers")) {
|
|
212
|
+
if (isPathValid(specifier, t.isExportSpecifier)) {
|
|
213
|
+
const binding = traceBinding(specifier, specifier.node.local.name);
|
|
214
|
+
|
|
215
|
+
if (binding) {
|
|
216
|
+
bindings.add(binding);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
const declarations = path.get("declaration");
|
|
221
|
+
|
|
222
|
+
if (isPathValid(declarations, t.isVariableDeclaration)) {
|
|
223
|
+
for (const declaration of declarations.get("declarations")) {
|
|
224
|
+
// Check if left is identifier
|
|
225
|
+
const left = unwrapPath(declaration.get("id"), t.isIdentifier);
|
|
226
|
+
if (left) {
|
|
227
|
+
const binding = traceBinding(left, left.node.name);
|
|
228
|
+
if (binding) {
|
|
229
|
+
bindings.add(binding);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
for (const binding of bindings) {
|
|
238
|
+
transformBindingForServer(ctx, binding);
|
|
239
|
+
}
|
|
240
|
+
} else {
|
|
241
|
+
// Trace bindings
|
|
242
|
+
const uniqueBindings = new Set<Binding>();
|
|
243
|
+
const exportedBindings = new Map<string, Binding>();
|
|
244
|
+
|
|
245
|
+
program.traverse({
|
|
246
|
+
ExportDefaultDeclaration(path) {
|
|
247
|
+
const id = unwrapPath(path.get("declaration"), t.isIdentifier);
|
|
248
|
+
if (id) {
|
|
249
|
+
const binding = traceBinding(path, id.node.name);
|
|
250
|
+
if (binding) {
|
|
251
|
+
uniqueBindings.add(binding);
|
|
252
|
+
exportedBindings.set("default", binding);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
},
|
|
256
|
+
ExportNamedDeclaration(path) {
|
|
257
|
+
if (path.node.source || path.node.exportKind === "type") {
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
for (const specifier of path.get("specifiers")) {
|
|
261
|
+
if (isPathValid(specifier, t.isExportSpecifier)) {
|
|
262
|
+
const binding = traceBinding(specifier, specifier.node.local.name);
|
|
263
|
+
|
|
264
|
+
if (binding) {
|
|
265
|
+
const key = t.isIdentifier(specifier.node.exported)
|
|
266
|
+
? specifier.node.exported.name
|
|
267
|
+
: specifier.node.exported.value;
|
|
268
|
+
uniqueBindings.add(binding);
|
|
269
|
+
exportedBindings.set(key, binding);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const declarations = path.get("declaration");
|
|
275
|
+
|
|
276
|
+
if (isPathValid(declarations, t.isVariableDeclaration)) {
|
|
277
|
+
for (const declaration of declarations.get("declarations")) {
|
|
278
|
+
// Check if left is identifier
|
|
279
|
+
const left = unwrapPath(declaration.get("id"), t.isIdentifier);
|
|
280
|
+
if (left) {
|
|
281
|
+
const binding = traceBinding(left, left.node.name);
|
|
282
|
+
if (binding) {
|
|
283
|
+
uniqueBindings.add(binding);
|
|
284
|
+
exportedBindings.set(left.node.name, binding);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
},
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
// generate ids for each unique binding
|
|
293
|
+
const sourceIDs = new Map<Binding, string>();
|
|
294
|
+
for (const binding of uniqueBindings) {
|
|
295
|
+
if (isPathValid(binding.path, t.isVariableDeclarator)) {
|
|
296
|
+
const init = unwrapPath(binding.path.get("init"), isValidFunction);
|
|
297
|
+
if (init) {
|
|
298
|
+
sourceIDs.set(binding, createID(ctx, getDescriptiveName(init, "anonymous")));
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// clear body
|
|
304
|
+
program.node.body = [];
|
|
305
|
+
|
|
306
|
+
const declarations: t.VariableDeclarator[] = [];
|
|
307
|
+
const specifiers: t.ExportSpecifier[] = [];
|
|
308
|
+
|
|
309
|
+
const declarationMap = new Map<Binding, t.Identifier>();
|
|
310
|
+
|
|
311
|
+
// Declare all client functions
|
|
312
|
+
for (const [exported, binding] of exportedBindings) {
|
|
313
|
+
let currentIdentifier = declarationMap.get(binding);
|
|
314
|
+
if (!currentIdentifier) {
|
|
315
|
+
currentIdentifier = generateUniqueName(program, "fn");
|
|
316
|
+
|
|
317
|
+
const fnID = sourceIDs.get(binding);
|
|
318
|
+
|
|
319
|
+
if (fnID) {
|
|
320
|
+
declarations.push(
|
|
321
|
+
t.variableDeclarator(
|
|
322
|
+
currentIdentifier,
|
|
323
|
+
t.callExpression(getImportIdentifier(ctx.imports, program, ctx.definitions.clone), [
|
|
324
|
+
t.stringLiteral(fnID),
|
|
325
|
+
]),
|
|
326
|
+
),
|
|
327
|
+
);
|
|
328
|
+
|
|
329
|
+
declarationMap.set(binding, currentIdentifier);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (currentIdentifier) {
|
|
334
|
+
specifiers.push(t.exportSpecifier(currentIdentifier, t.stringLiteral(exported)));
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const body: t.Statement[] = [];
|
|
339
|
+
|
|
340
|
+
if (declarations.length > 0) {
|
|
341
|
+
body.push(t.variableDeclaration("const", declarations));
|
|
342
|
+
}
|
|
343
|
+
if (specifiers.length > 0) {
|
|
344
|
+
body.push(t.exportNamedDeclaration(null, specifiers, null));
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
program.pushContainer("body", body);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
export function directivesPlugin(): babel.PluginObj<State> {
|
|
352
|
+
return {
|
|
353
|
+
name: "solid-start:directives",
|
|
354
|
+
visitor: {
|
|
355
|
+
Program(program, ctx) {
|
|
356
|
+
const isModuleLevel = isDirectiveValid(ctx.opts, program.node.directives);
|
|
357
|
+
if (isModuleLevel) {
|
|
358
|
+
transformModuleLevelDirective(ctx.opts, program);
|
|
359
|
+
ctx.opts.valid = true;
|
|
360
|
+
} else {
|
|
361
|
+
// First, bubble up function declarations
|
|
362
|
+
program.traverse({
|
|
363
|
+
FunctionDeclaration(child) {
|
|
364
|
+
// if (isFunctionDirectiveValid(ctx.opts, child)) {
|
|
365
|
+
bubbleFunctionDeclaration(child);
|
|
366
|
+
// }
|
|
367
|
+
},
|
|
368
|
+
});
|
|
369
|
+
program.scope.crawl();
|
|
370
|
+
// Now we transform each function
|
|
371
|
+
program.traverse({
|
|
372
|
+
ArrowFunctionExpression(path) {
|
|
373
|
+
transformFunction(ctx.opts, path, false);
|
|
374
|
+
},
|
|
375
|
+
FunctionExpression(path) {
|
|
376
|
+
transformFunction(ctx.opts, path, false);
|
|
377
|
+
},
|
|
378
|
+
});
|
|
379
|
+
program.scope.crawl();
|
|
380
|
+
|
|
381
|
+
if (ctx.opts.count > 0) {
|
|
382
|
+
ctx.opts.valid = true;
|
|
383
|
+
removeUnusedVariables(program);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
},
|
|
387
|
+
},
|
|
388
|
+
};
|
|
389
|
+
}
|