@dreamkit/dev 0.0.14-next.4 → 0.0.14-next.6

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.
@@ -1 +1 @@
1
- {"version":3,"file":"DreamkitDevServer.d.ts","sourceRoot":"","sources":["../src/DreamkitDevServer.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,qBAAqB,EACrB,wBAAwB,EACzB,MAAM,cAAc,CAAC;AAItB,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAMpD,OAAO,EAAE,GAAG,EAAW,KAAK,EAA2B,MAAM,eAAe,CAAC;AAM7E,OAAO,EAAE,SAAS,EAAS,MAAM,UAAU,CAAC;AAE5C,OAAO,EAAmC,aAAa,EAAE,MAAM,MAAM,CAAC;AAGtE,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3C,MAAM,MAAM,0BAA0B,GAAG;IACvC,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG,qBAAqB,GACtD,0BAA0B,CAAC;AAC7B,MAAM,MAAM,kBAAkB,GAAG,wBAAwB,GACvD,0BAA0B,CAAC;AAE7B,qBAAa,iBAAiB;IAC5B,MAAM,CAAC,WAAW,SAAmB;IACrC,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC;IAClB,QAAQ,CAAC,KAAK,EAAE,cAAc,CAAC;IAC/B,SAAS,CAAC,aAAa,EAAE,aAAa,GAAG,SAAS,CAAC;IACnD,SAAS,CAAC,OAAO,EAAE,WAAW,GAAG,SAAS,CAAC;IAC3C,SAAS,CAAC,mBAAmB,EAAE,SAAS,GAAG,SAAS,CAAC;IACrD,QAAQ,CAAC,OAAO,EAAE,kBAAkB,CAAC;gBACzB,SAAS,EAAE,oBAAoB;IA+B3C,MAAM,CAAC,QAAQ,IAAI,iBAAiB;IAKpC,MAAM,CAAC,YAAY,CAAC,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AE,WAAW,GAAG,SAAS,CAAC;IAC3C,SAAS,CAAC,mBAAmB,EAAE,SAAS,GAAG,SAAS,CAAC;IACrD,QAAQ,CAAC,OAAO,EAAE,kBAAkB,CAAC;gBACzB,SAAS,EAAE,oBAAoB;IA+B3C,MAAM,CAAC,QAAQ,IAAI,iBAAiB;IAKpC,MAAM,CAAC,YAAY,CAAC,QAAQ,EAAE,iBAAiB;IAGzC,KAAK,CAAC,IAAI,EAAE,MAAM;IAWlB,YAAY,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC;IAIzC,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC;IAIxC,gBAAgB;IA0BhB,iBAAiB,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAIjD,OAAO,CAAC,uBAAuB,UAAO;IAiDtC,KAAK;IAGL,IAAI;CAMX"}
@@ -94,6 +94,7 @@ export class DreamkitDevServer {
94
94
  ? $route
95
95
  : $route["clone"]({
96
96
  ...value.$options,
97
+ filePath: item.filePath.replaceAll("\\", "/"),
97
98
  path: item.path,
98
99
  }).create(value.$options.component);
99
100
  }
@@ -0,0 +1,9 @@
1
+ import { DreamkitDevServer } from "../DreamkitDevServer.js";
2
+ export declare function generateCode(server: DreamkitDevServer, input?: {
3
+ filePath: string;
4
+ read?: () => string | Promise<string>;
5
+ }): Promise<Record<string, {
6
+ path: string;
7
+ changed: boolean;
8
+ }>>;
9
+ //# sourceMappingURL=generate-code.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generate-code.d.ts","sourceRoot":"","sources":["../../src/actions/generate-code.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAY5D,wBAAsB,YAAY,CAChC,MAAM,EAAE,iBAAiB,EACzB,KAAK,CAAC,EAAE;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;CAAE;UAE9B,MAAM;aAAW,OAAO;IA2C9D"}
@@ -0,0 +1,78 @@
1
+ import { getImportLocations, getExports, parseCallsChain, parseFile, } from "../utils/ast.js";
2
+ import { writeFileIfDifferent } from "../utils/fs.js";
3
+ import { replaceText } from "../utils/string.js";
4
+ import { extractPathParams } from "@dreamkit/app/utils/routing.js";
5
+ import { readFile } from "fs/promises";
6
+ export async function generateCode(server, input) {
7
+ const result = {};
8
+ for (const route of server.app.routes) {
9
+ const filePath = route.$options.filePath;
10
+ if (input && filePath !== input.filePath)
11
+ continue;
12
+ if (!filePath || !route.$options.path)
13
+ continue;
14
+ const pathParams = extractPathParams(route.$options.path).map((param) => param.name);
15
+ if (!pathParams.length)
16
+ continue;
17
+ const code = input?.read
18
+ ? await input.read()
19
+ : (await readFile(filePath)).toString();
20
+ let action;
21
+ try {
22
+ action = getTransformAction(code);
23
+ }
24
+ catch (_) {
25
+ continue;
26
+ }
27
+ let next;
28
+ if (action) {
29
+ const newParamNames = pathParams.map((name) => JSON.stringify(name));
30
+ if (action.type === "replace") {
31
+ if (!checkChanges(code, action.start, action.end, pathParams))
32
+ next = replaceText(code, action.start, action.end, `(${newParamNames.join(", ")})`);
33
+ }
34
+ else if (action.type === "add") {
35
+ next = replaceText(code, action.start, action.start, `.pathParams(${newParamNames})`);
36
+ }
37
+ }
38
+ if (next && (await writeFileIfDifferent(filePath, next, code))) {
39
+ result[filePath] = { path: filePath, changed: true };
40
+ }
41
+ }
42
+ return result;
43
+ }
44
+ function checkChanges(code, start, end, newPathParams) {
45
+ const currentArgs = code.slice(start, end);
46
+ const json = `[${currentArgs.slice(1, -1)}]`;
47
+ let currentParamNames = [];
48
+ try {
49
+ currentParamNames = JSON.parse(json).sort();
50
+ }
51
+ catch (_) { }
52
+ return (currentParamNames.sort().join(",") === [...newPathParams].sort().join(","));
53
+ }
54
+ function getTransformAction(code) {
55
+ const ast = parseFile(code);
56
+ const ref = getImportLocations(ast, "dreamkit", ["$route"]);
57
+ for (const item of getExports(ast)) {
58
+ if (item.dec.type === "CallExpression") {
59
+ const chain = parseCallsChain(item.dec);
60
+ if (ref.$route.includes(chain.root.loc)) {
61
+ const pathParams = chain.calls.find((call) => call.name === "pathParams");
62
+ if (!pathParams) {
63
+ return {
64
+ type: "add",
65
+ start: chain.root.end,
66
+ };
67
+ }
68
+ else {
69
+ return {
70
+ type: "replace",
71
+ start: pathParams.property.end,
72
+ end: pathParams.node.end,
73
+ };
74
+ }
75
+ }
76
+ }
77
+ }
78
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../../src/actions/generate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAI5D,wBAAsB,QAAQ,CAC5B,MAAM,EAAE,iBAAiB,GACxB,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC,CAAC,CAS7D"}
1
+ {"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../../src/actions/generate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAK5D,wBAAsB,QAAQ,CAC5B,MAAM,EAAE,iBAAiB,GACxB,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC,CAAC,CAY7D"}
@@ -1,12 +1,16 @@
1
+ import { generateCode } from "./generate-code.js";
1
2
  import { generateMeta } from "./generate-meta.js";
2
3
  import { generateSettings } from "./generate-settings.js";
3
4
  export async function generate(server) {
4
- const [meta, settings] = await Promise.all([
5
+ const [meta, settings, code] = await Promise.all([
5
6
  generateMeta(server),
6
7
  generateSettings(server),
8
+ generateCode(server),
7
9
  ]);
10
+ await generateCode(server);
8
11
  return {
9
12
  meta,
10
13
  ...settings,
14
+ ...code,
11
15
  };
12
16
  }
@@ -1 +1 @@
1
- {"version":3,"file":"dev-server.d.ts","sourceRoot":"","sources":["../../../src/adapters/solid-start/dev-server.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,iBAAiB,EAClB,MAAM,4BAA4B,CAAC;AAGpC,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAMzD,OAAO,EAML,QAAQ,EAET,MAAM,sBAAsB,CAAC;AAsB9B,wBAAsB,uBAAuB,CAAC,OAAO,EAAE;IACrD,IAAI,EAAE,MAAM,CAAC;CACd,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAqDhC;AAuCD,wBAAgB,uBAAuB,CACrC,QAAQ,EAAE,QAAQ,EAClB,SAAS,GAAE,qBAA0B,qBAuLtC"}
1
+ {"version":3,"file":"dev-server.d.ts","sourceRoot":"","sources":["../../../src/adapters/solid-start/dev-server.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,iBAAiB,EAClB,MAAM,4BAA4B,CAAC;AAIpC,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAMzD,OAAO,EAML,QAAQ,EAET,MAAM,sBAAsB,CAAC;AAsB9B,wBAAsB,uBAAuB,CAAC,OAAO,EAAE;IACrD,IAAI,EAAE,MAAM,CAAC;CACd,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAqDhC;AAuCD,wBAAgB,uBAAuB,CACrC,QAAQ,EAAE,QAAQ,EAClB,SAAS,GAAE,qBAA0B,qBA4LtC"}
@@ -1,4 +1,5 @@
1
1
  import { DreamkitDevServer, } from "../../DreamkitDevServer.js";
2
+ import { generateCode } from "../../actions/generate-code.js";
2
3
  import { generateMeta } from "../../actions/generate-meta.js";
3
4
  import { generateSettings } from "../../actions/generate-settings.js";
4
5
  import { tryGenerate } from "../../utils/ast.js";
@@ -149,10 +150,15 @@ export function createDreamkitDevServer(vinxiApp, inOptions = {}) {
149
150
  await generateSettings(server);
150
151
  }, 300);
151
152
  server.app
152
- .on("change", (data) => {
153
+ .on("change", async (data) => {
153
154
  log("change detected", { action: data.action, id: data.id });
154
155
  if (isRoute(data.value)) {
155
156
  tryGenerateMeta();
157
+ if (data.action === "add" && data.value.$options.filePath) {
158
+ await generateCode(server, {
159
+ filePath: data.value.$options.filePath,
160
+ });
161
+ }
156
162
  }
157
163
  else if (isSettings(data.value)) {
158
164
  tryGenerateSettings();
@@ -177,6 +183,7 @@ export function createDreamkitDevServer(vinxiApp, inOptions = {}) {
177
183
  }
178
184
  });
179
185
  await server.prepare(true);
186
+ await generateCode(server);
180
187
  for (const router of vinxiApp.config.routers) {
181
188
  const internalRouter = router.internals.routes;
182
189
  if (internalRouter) {
@@ -117,6 +117,7 @@ function createServerAction(program, name, call, exported = false) {
117
117
  arguments: [
118
118
  t.objectExpression([
119
119
  t.objectProperty(t.identifier("context"), t.nullLiteral()),
120
+ t.objectProperty(t.identifier("onCall"), t.nullLiteral()),
120
121
  ]),
121
122
  ],
122
123
  },
@@ -7,15 +7,25 @@ export declare function parseCode(input: string | string[]): t.Statement[];
7
7
  export type ParseFileResult = ParseResult<t.File>;
8
8
  export declare function parseFile(code: string): ParseResult<t.File>;
9
9
  export declare function tryGenerate(ast: ParseFileResult | undefined, log?: boolean | string): import("@babel/generator").GeneratorResult | undefined;
10
+ export type InputChain = {
11
+ rootName: string;
12
+ calls: {
13
+ name: string;
14
+ arguments: any[];
15
+ }[];
16
+ };
10
17
  export type Chain = {
11
18
  rootName: string;
19
+ root: t.Identifier;
12
20
  calls: {
21
+ node: t.CallExpression;
22
+ property: t.Identifier;
13
23
  name: string;
14
24
  arguments: any[];
15
25
  }[];
16
26
  };
17
27
  export declare function parseCallsChain(input: t.CallExpression): Chain;
18
- export declare function createCallChains(chain: Chain): t.CallExpression | t.Identifier;
28
+ export declare function createCallChains(chain: InputChain): t.CallExpression | t.Identifier;
19
29
  export declare function removeChainCalls(input: t.CallExpression, names: string[]): t.CallExpression;
20
30
  export declare function getFirstChain(ref: t.Node): {
21
31
  identifier: string;
@@ -26,6 +36,20 @@ export declare function findExportedNames(ast: ParseFileResult): string[];
26
36
  export declare function findExportedNamesFromFile(path: string): Promise<string[]>;
27
37
  export declare function defineTransform<T>(transform: Transform<T>): Transform<T>;
28
38
  export declare function createConst(name: string, value: t.Expression): t.VariableDeclaration;
39
+ export declare function getExports(ast: ParseFileResult): {
40
+ name: string;
41
+ dec: t.Node;
42
+ }[];
43
+ export declare function getImportSpecs(ast: ParseFileResult): {
44
+ source: string;
45
+ spec: {
46
+ imported: string;
47
+ local: string;
48
+ }[];
49
+ }[];
50
+ export declare function getImportLocations<T extends string>(ast: ParseFileResult, source: string, specifiers: T[]): {
51
+ [K in T]: t.SourceLocation[];
52
+ };
29
53
  export declare function appendChainCall(input: t.Expression | string, method: string, args: t.Expression[]): t.CallExpression;
30
54
  export declare function prependChainCall(input: t.Expression | string, method: string, args?: t.Expression[]): t.CallExpression;
31
55
  export declare function addImports<T extends string>(program: NodePath<t.Program> & {
@@ -1 +1 @@
1
- {"version":3,"file":"ast.d.ts","sourceRoot":"","sources":["../../src/utils/ast.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAClC,OAAO,KAAK,KAAK,MAAM,iBAAiB,CAAC;AAIzC,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,iBAIjD;AAED,MAAM,MAAM,eAAe,GAAG,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;AAElD,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,uBAKrC;AAED,wB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AC,EAAE,GACd;KACA,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,cAAc,EAAE;CAC7B,CAqBA;AAED,wBAAgB,eAAe,CAC7B,KAAK,EAAE,CAAC,CAAC,UAAU,GAAG,MAAM,EAC5B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,CAAC,CAAC,UAAU,EAAE,oBAiBrB;AAED,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,CAAC,CAAC,UAAU,GAAG,MAAM,EAC5B,MAAM,EAAE,MAAM,EACd,IAAI,GAAE,CAAC,CAAC,UAAU,EAAO,oBAqB1B;AAED,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM,EACzC,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG;IAC7B,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CACpD,EACD,UAAU,EAAE,CAAC,EAAE,EACf,MAAM,EAAE,MAAM,GACb;KAAG,CAAC,IAAI,CAAC,GAAG,MAAM;CAAE,CAwBtB"}
package/lib/utils/ast.js CHANGED
@@ -1,4 +1,4 @@
1
- import { generator, parser } from "./babel.js";
1
+ import { generator, parser, traverse } from "./babel.js";
2
2
  import * as t from "@babel/types";
3
3
  import * as lexer from "es-module-lexer";
4
4
  import { transform } from "esbuild";
@@ -31,16 +31,20 @@ export function tryGenerate(ast, log) {
31
31
  export function parseCallsChain(input) {
32
32
  let ref = input;
33
33
  let rootName;
34
+ let root;
34
35
  let calls = [];
35
36
  while (true) {
36
37
  if (ref.callee.type === "MemberExpression") {
37
38
  if (ref.callee.property.type !== "Identifier")
38
39
  throw new Error("Invalid chain");
39
40
  calls.push({
41
+ node: ref,
40
42
  name: ref.callee.property.name,
43
+ property: ref.callee.property,
41
44
  arguments: ref.arguments,
42
45
  });
43
46
  if (ref.callee.object.type === "Identifier") {
47
+ root = ref.callee.object;
44
48
  rootName = ref.callee.object.name;
45
49
  break;
46
50
  }
@@ -52,7 +56,7 @@ export function parseCallsChain(input) {
52
56
  }
53
57
  }
54
58
  }
55
- return { rootName, calls: calls.reverse() };
59
+ return { root, rootName, calls: calls.reverse() };
56
60
  }
57
61
  export function createCallChains(chain) {
58
62
  let ref = t.identifier(chain.rootName);
@@ -103,22 +107,29 @@ export async function analyzeModule(path) {
103
107
  }
104
108
  export function findExportedNames(ast) {
105
109
  return ast.program.body
106
- .map((stm) => {
110
+ .flatMap((stm) => {
107
111
  if (stm.type === "ExportDefaultDeclaration") {
108
112
  return "default";
109
113
  }
110
- else if (stm.type === "ExportNamedDeclaration" && stm.declaration) {
111
- if (stm.declaration.type === "VariableDeclaration") {
112
- const [dec] = stm.declaration.declarations;
113
- if (dec.id.type === "Identifier")
114
- return dec.id.name;
115
- }
116
- else if (stm.declaration.type === "FunctionDeclaration" ||
117
- stm.declaration.type === "ClassDeclaration") {
118
- if (stm.declaration.id?.type === "Identifier") {
119
- return stm.declaration.id.name;
114
+ else if (stm.type === "ExportNamedDeclaration") {
115
+ if (stm.declaration) {
116
+ if (stm.declaration.type === "VariableDeclaration") {
117
+ const [dec] = stm.declaration.declarations;
118
+ if (dec.id.type === "Identifier")
119
+ return dec.id.name;
120
+ }
121
+ else if (stm.declaration.type === "FunctionDeclaration" ||
122
+ stm.declaration.type === "ClassDeclaration") {
123
+ if (stm.declaration.id?.type === "Identifier") {
124
+ return stm.declaration.id.name;
125
+ }
120
126
  }
121
127
  }
128
+ else if (stm.specifiers) {
129
+ return stm.specifiers
130
+ .map((spec) => spec.exported.type === "Identifier" && spec.exported.name)
131
+ .filter(Boolean);
132
+ }
122
133
  }
123
134
  })
124
135
  .filter((v) => typeof v === "string");
@@ -135,6 +146,69 @@ export function createConst(name, value) {
135
146
  t.variableDeclarator(t.identifier(name), value),
136
147
  ]);
137
148
  }
149
+ export function getExports(ast) {
150
+ const result = [];
151
+ traverse(ast, {
152
+ ExportDefaultDeclaration(path) {
153
+ let dec = path.node.declaration;
154
+ result.push({ name: "default", dec });
155
+ },
156
+ ExportNamedDeclaration(path) {
157
+ if (path.node.declaration?.type === "VariableDeclaration") {
158
+ const [dec] = path.node.declaration.declarations;
159
+ const { init } = dec;
160
+ if (dec.id.type === "Identifier" && init) {
161
+ result.push({ name: dec.id.name, dec: init });
162
+ }
163
+ }
164
+ },
165
+ });
166
+ return result;
167
+ }
168
+ export function getImportSpecs(ast) {
169
+ const imports = [];
170
+ traverse(ast, {
171
+ ImportDeclaration(path) {
172
+ imports.push({
173
+ source: path.node.source.value,
174
+ spec: path.node.specifiers
175
+ .map((spec) => {
176
+ if (spec.type === "ImportSpecifier" &&
177
+ spec.imported.type === "Identifier") {
178
+ return { imported: spec.imported.name, local: spec.local.name };
179
+ }
180
+ })
181
+ .filter((v) => !!v),
182
+ });
183
+ },
184
+ });
185
+ return imports;
186
+ }
187
+ export function getImportLocations(ast, source, specifiers) {
188
+ const imports = getImportSpecs(ast).filter((item) => item.source === source);
189
+ const locations = {};
190
+ traverse(ast, {
191
+ Program(programPath) {
192
+ for (const name of specifiers) {
193
+ locations[name] = [];
194
+ let bindingName;
195
+ for (const item of imports) {
196
+ const spec = item.spec.find((spec) => spec.imported === name);
197
+ if (spec)
198
+ bindingName = spec.local;
199
+ }
200
+ if (!bindingName)
201
+ continue;
202
+ const binds = programPath.scope.bindings[bindingName];
203
+ for (const path of binds.referencePaths) {
204
+ if (path.node.loc)
205
+ locations[name].push(path.node.loc);
206
+ }
207
+ }
208
+ },
209
+ });
210
+ return locations;
211
+ }
138
212
  export function appendChainCall(input, method, args) {
139
213
  if (typeof input === "string")
140
214
  input = t.identifier(input);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dreamkit/dev",
3
- "version": "0.0.14-next.4",
3
+ "version": "0.0.14-next.6",
4
4
  "description": "Development tools for DreamKit.",
5
5
  "homepage": "https://dreamkit.dev",
6
6
  "repository": {
@@ -46,10 +46,10 @@
46
46
  "rollup": "^4.26.0",
47
47
  "vite-plugin-solid": "^2.10.2",
48
48
  "vite-tsconfig-paths": "^5.1.2",
49
- "@dreamkit/app": "0.0.7-next.3",
49
+ "@dreamkit/app": "0.0.7-next.5",
50
50
  "@dreamkit/kind": "0.0.2",
51
- "@dreamkit/node-app": "0.0.2-next.2",
52
- "@dreamkit/schema": "0.0.5-next.1",
51
+ "@dreamkit/node-app": "0.0.2-next.4",
52
+ "@dreamkit/schema": "0.0.5-next.2",
53
53
  "@dreamkit/utils": "0.0.5-next.0"
54
54
  },
55
55
  "devDependencies": {