@dreamkit/dev 0.0.5 → 0.0.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.
@@ -100,7 +100,7 @@ export class DreamkitDevServer {
100
100
  enforce: "pre",
101
101
  load: (id) => this.entry.tryLoad(id),
102
102
  transform(code, id) {
103
- return transformAndGenerate(code, { toSolidImport: true }, ...getUrlTransforms(id));
103
+ return transformAndGenerate(code, { toSolidLink: true }, ...getUrlTransforms(id));
104
104
  },
105
105
  handleHotUpdate: async ({ file, read, modules }) => {
106
106
  return this.entry.tryUpdate(file, read, modules);
@@ -23,7 +23,7 @@ export function dreamkitPlugin(inOptions = {}) {
23
23
  const searchParams = new URLSearchParams(searchString);
24
24
  const dreamkitPickEntry = searchParams.get("dk-pick-entry");
25
25
  const picks = searchParams.getAll("pick");
26
- const transforms = [{ toSolidImport: true }];
26
+ const transforms = [{ toSolidLink: true }];
27
27
  if (dreamkitPickEntry) {
28
28
  transforms.push({
29
29
  pickExport: [dreamkitPickEntry],
@@ -0,0 +1,2 @@
1
+ export declare const toSolidLink: import("../utils/transform.js").Transform<unknown>;
2
+ //# sourceMappingURL=to-solid-link.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to-solid-link.d.ts","sourceRoot":"","sources":["../../src/transforms/to-solid-link.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,WAAW,oDAkDtB,CAAC"}
@@ -0,0 +1,38 @@
1
+ import { addFileChanges, addImports, defineTransform } from "../utils/ast.js";
2
+ import { traverse } from "../utils/babel.js";
3
+ import * as t from "@babel/types";
4
+ export const toSolidLink = defineTransform({
5
+ onlyIf: (code) => code.includes("Link"),
6
+ run: (ast) => {
7
+ let changes = 0;
8
+ const addRouterImports = (programPath) => addImports(programPath, ["A"], "@solidjs/router");
9
+ let routeImports;
10
+ let linkSpec;
11
+ traverse(ast, {
12
+ Program(programPath) {
13
+ programPath.traverse({
14
+ ImportDeclaration(path) {
15
+ for (const spec of path.node.specifiers) {
16
+ if (path.node.source.value === "dreamkit" &&
17
+ spec.type === "ImportSpecifier" &&
18
+ spec.imported.type === "Identifier" &&
19
+ spec.imported.name === "Link") {
20
+ linkSpec = spec.local.name;
21
+ }
22
+ }
23
+ },
24
+ JSXOpeningElement(path) {
25
+ if (path.node.name.type === "JSXIdentifier" &&
26
+ path.node.name.name === linkSpec) {
27
+ if (!routeImports)
28
+ routeImports = addImports(programPath, ["A"], "@solidjs/router");
29
+ changes++;
30
+ path.node.attributes.push(t.jsxAttribute(t.jsxIdentifier("component"), t.jsxExpressionContainer(t.identifier(routeImports.A))));
31
+ }
32
+ },
33
+ });
34
+ },
35
+ });
36
+ return addFileChanges(ast, changes);
37
+ },
38
+ });
@@ -1 +1 @@
1
- {"version":3,"file":"to-solid-route.d.ts","sourceRoot":"","sources":["../../src/transforms/to-solid-route.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAElC,wBAAgB,YAAY,CAAC,GAAG,EAAE,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,UA2IpD"}
1
+ {"version":3,"file":"to-solid-route.d.ts","sourceRoot":"","sources":["../../src/transforms/to-solid-route.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAElC,wBAAgB,YAAY,CAAC,GAAG,EAAE,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,UA8KpD"}
@@ -1,21 +1,29 @@
1
- import { addFileChanges, getFirstChain } from "../utils/ast.js";
1
+ import { addFileChanges, addImports, appendChainCall, createConst, getFirstChain, prependChainCall, } from "../utils/ast.js";
2
2
  import { traverse } from "../utils/babel.js";
3
+ import { replaceImportSpec } from "./replace-import-spec.js";
3
4
  import * as t from "@babel/types";
4
5
  export function toSolidRoute(ast) {
5
6
  let changes = 0;
7
+ let deps;
6
8
  let routeImportName;
7
9
  const routeSpec = "$route";
8
- const routeSource = "dreamkit/solid";
10
+ const routeSource = "dreamkit";
11
+ const routeNewSource = "dreamkit/adapters/solid.js";
12
+ replaceImportSpec(ast, {
13
+ newSource: routeNewSource,
14
+ source: routeSource,
15
+ spec: [routeSpec],
16
+ });
9
17
  traverse(ast, {
10
18
  Program(programPath) {
11
19
  traverse(programPath.node, {
12
20
  ImportDeclaration(importPath) {
13
21
  importPath.traverse({
14
22
  ImportSpecifier(path) {
15
- // import { route as ? } from "dreamkit/solid";
23
+ // import { route as ? } from "dreamkit/adapters/solid.js";
16
24
  const isRouteImport = path.node.imported.type === "Identifier" &&
17
25
  path.node.imported.name === routeSpec &&
18
- importPath.node.source.value === routeSource;
26
+ importPath.node.source.value === routeNewSource;
19
27
  if (isRouteImport)
20
28
  routeImportName = path.node.local.name;
21
29
  },
@@ -27,8 +35,9 @@ export function toSolidRoute(ast) {
27
35
  // import { $route } from "dreamkit";
28
36
  // export default route.params({}).create(() => {});
29
37
  // [output]
30
- // import { $route as _$route } from "dreamkit/solid"
31
- // const selfRoute = _$route.params({});
38
+ // import { $route as _$route } from "dreamkit/adapters/solid.js"
39
+ // import { useLocation, useRouter, useParams} from "@solidjs/router"
40
+ // const selfRoute = _$route.params({}).clone({ deps: ? });
32
41
  // export const route = selfRoute.createRouteDefinition();
33
42
  // export default delfRoute.create(() => {})
34
43
  if (
@@ -39,14 +48,18 @@ export function toSolidRoute(ast) {
39
48
  dec.callee.property.type == "Identifier" &&
40
49
  dec.callee.property.name === "create") {
41
50
  changes++;
51
+ if (!deps)
52
+ deps = importDeps(programPath);
42
53
  const selfRoute = programPath.scope.generateUid("selfRoute");
43
54
  ast.program.body.splice(ast.program.body.indexOf(path.node), 1,
44
55
  // const selfRoute = route.?.?.?
45
- createSelfRoute(selfRoute, dec.callee.object),
46
- // export const route = selfRoute.createRouteDefinition();
47
- createExportRouteDefinition(selfRoute),
56
+ createConst(selfRoute, prependChainCall(dec.callee.object, "clone", [
57
+ createDepsObject(deps),
58
+ ])),
59
+ // export const route = selfRoute.createRouteDefinition()
60
+ t.exportNamedDeclaration(createConst("route", appendChainCall(selfRoute, "createRouteDefinition", []))),
48
61
  // export default selfRoute.create(() => {})
49
- createDefaultExportRoute(selfRoute, dec.arguments[0]));
62
+ t.exportDefaultDeclaration(appendChainCall(t.identifier(selfRoute), "create", dec.arguments)));
50
63
  }
51
64
  },
52
65
  // [input]
@@ -54,8 +67,9 @@ export function toSolidRoute(ast) {
54
67
  // export const route = $route.params({}).path('/path');
55
68
  // export default function Users() { useRoute(route); }
56
69
  // [output]
57
- // import { $route as _$route } from "dreamkit/solid"
58
- // const selfRoute = _$route.params({}).path('/path');
70
+ // import { $route as _$route } from "dreamkit/adapters/solid.js"
71
+ // import { useLocation, useRouter, useParams } from "@solidjs/router"
72
+ // const selfRoute = _$route.params({}).path('/path').clone({ deps: ? });
59
73
  // export const route = selfRoute.createRouteDefinition();
60
74
  // export default selfRoute.create(function Users() { useRoute(selfRoute); });
61
75
  ExportNamedDeclaration(path) {
@@ -71,6 +85,8 @@ export function toSolidRoute(ast) {
71
85
  // [output]
72
86
  // const _selfRoute = $route.?;
73
87
  // export const route = _selfRoute.createRouteDefinition();
88
+ if (!deps)
89
+ deps = importDeps(programPath);
74
90
  let exportFunction;
75
91
  const selfRoute = programPath.scope.generateUid("selfRoute");
76
92
  const index = ast.program.body.indexOf(path.node);
@@ -89,11 +105,13 @@ export function toSolidRoute(ast) {
89
105
  }
90
106
  return true;
91
107
  });
92
- ast.program.body.splice(index, 1, createSelfRoute(selfRoute, dec.init), createExportRouteDefinition(selfRoute), ...(exportFunction
108
+ ast.program.body.splice(index, 1, createConst(selfRoute, prependChainCall(dec.init, "clone", [createDepsObject(deps)])), t.exportNamedDeclaration(createConst("route", appendChainCall(selfRoute, "createRouteDefinition", []))), ...(exportFunction
93
109
  ? [
94
- createDefaultExportRoute(selfRoute, exportFunction.type === "FunctionDeclaration"
95
- ? t.functionExpression(exportFunction.id, exportFunction.params, exportFunction.body)
96
- : exportFunction),
110
+ t.exportDefaultDeclaration(appendChainCall(selfRoute, "create", [
111
+ exportFunction.type === "FunctionDeclaration"
112
+ ? t.functionExpression(exportFunction.id, exportFunction.params, exportFunction.body)
113
+ : exportFunction,
114
+ ])),
97
115
  ]
98
116
  : []));
99
117
  }
@@ -104,16 +122,11 @@ export function toSolidRoute(ast) {
104
122
  });
105
123
  return addFileChanges(ast, changes);
106
124
  }
107
- function createSelfRoute(name, route) {
108
- return t.variableDeclaration("const", [
109
- t.variableDeclarator(t.identifier(name), route),
125
+ function createDepsObject(deps) {
126
+ return t.objectExpression([
127
+ t.objectProperty(t.identifier("deps"), t.objectExpression(Object.entries(deps).map(([key, value]) => t.objectProperty(t.identifier(key), t.identifier(value))))),
110
128
  ]);
111
129
  }
112
- function createExportRouteDefinition(name) {
113
- return t.exportNamedDeclaration(t.variableDeclaration("const", [
114
- t.variableDeclarator(t.identifier("route"), t.callExpression(t.memberExpression(t.identifier(name), t.identifier("createRouteDefinition")), [])),
115
- ]));
116
- }
117
- function createDefaultExportRoute(name, component) {
118
- return t.exportDefaultDeclaration(t.callExpression(t.memberExpression(t.identifier(name), t.identifier("create")), [component]));
130
+ function importDeps(program) {
131
+ return addImports(program, ["useLocation", "useNavigate", "useParams"], "@solidjs/router");
119
132
  }
@@ -1,5 +1,6 @@
1
1
  import type { Transform } from "./transform.js";
2
2
  import { ParseResult } from "@babel/parser";
3
+ import { NodePath } from "@babel/traverse";
3
4
  import * as t from "@babel/types";
4
5
  import * as lexer from "es-module-lexer";
5
6
  export declare function parseCode(input: string | string[]): t.Statement[];
@@ -16,4 +17,10 @@ export declare function analyzeModule(path: string): Promise<readonly [imports:
16
17
  export declare function findExportedNames(ast: ParseFileResult): string[];
17
18
  export declare function findExportedNamesFromFile(path: string): Promise<string[]>;
18
19
  export declare function defineTransform<T>(transform: Transform<T>): Transform<T>;
20
+ export declare function createConst(name: string, value: t.Expression): t.VariableDeclaration;
21
+ export declare function appendChainCall(input: t.Expression | string, method: string, args: t.Expression[]): t.CallExpression;
22
+ export declare function prependChainCall(input: t.Expression | string, method: string, args?: t.Expression[]): t.CallExpression;
23
+ export declare function addImports<T extends string>(program: NodePath<t.Program>, specifiers: T[], source: string): {
24
+ [K in T]: string;
25
+ };
19
26
  //# sourceMappingURL=ast.d.ts.map
@@ -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,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,wBAAgB,cAAc,CAAC,GAAG,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,UAGjE;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE,eAAe,UAElD;AAED,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,eAAe,GAAG,SAAS,EAChC,GAAG,CAAC,EAAE,OAAO,0DAOd;AAED,wBAAgB,aAAa,CAAC,GAAG,EAAE,CAAC,CAAC,IAAI;gBAGrB,MAAM;WACX,CAAC,CAAC,gBAAgB;cAgBhC;AAED,wBAAsB,aAAa,CAAC,IAAI,EAAE,MAAM,uJAQ/C;AAED,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,eAAe,YAkBrD;AAED,wBAAsB,yBAAyB,CAAC,IAAI,EAAE,MAAM,qBAG3D;AACD,wBAAgB,eAAe,CAAC,CAAC,EAA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,KAAK,EAAE,CAAC,CAAC,UAAU,yBAI5D;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,EAC5B,UAAU,EAAE,CAAC,EAAE,EACf,MAAM,EAAE,MAAM,GACb;KAAG,CAAC,IAAI,CAAC,GAAG,MAAM;CAAE,CAkBtB"}
package/lib/utils/ast.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { generator, parser } from "./babel.js";
2
+ import * as t from "@babel/types";
2
3
  import * as lexer from "es-module-lexer";
3
4
  import { transform } from "esbuild";
4
5
  import { readFile } from "fs/promises";
@@ -79,3 +80,47 @@ export async function findExportedNamesFromFile(path) {
79
80
  export function defineTransform(transform) {
80
81
  return transform;
81
82
  }
83
+ export function createConst(name, value) {
84
+ return t.variableDeclaration("const", [
85
+ t.variableDeclarator(t.identifier(name), value),
86
+ ]);
87
+ }
88
+ export function appendChainCall(input, method, args) {
89
+ if (typeof input === "string")
90
+ input = t.identifier(input);
91
+ if (input.type === "Identifier") {
92
+ return t.callExpression(t.memberExpression(input, t.identifier(method)), args);
93
+ }
94
+ else if (input.type === "CallExpression") {
95
+ return t.callExpression(t.memberExpression(input, t.identifier(method)), args);
96
+ }
97
+ else {
98
+ throw new Error("Unsupported route type");
99
+ }
100
+ }
101
+ export function prependChainCall(input, method, args = []) {
102
+ if (typeof input === "string")
103
+ input = t.identifier(input);
104
+ if (input.type === "Identifier") {
105
+ return t.callExpression(t.memberExpression(input, t.identifier(method)), args);
106
+ }
107
+ else if (input.type === "CallExpression") {
108
+ const cloned = t.cloneNode(input);
109
+ const firstChain = getFirstChain(cloned);
110
+ if (!firstChain)
111
+ throw new Error("Unsupported route type");
112
+ firstChain.value.object = t.callExpression(t.memberExpression(firstChain.value.object, t.identifier(method)), args);
113
+ return cloned;
114
+ }
115
+ else {
116
+ throw new Error("Unsupported route type");
117
+ }
118
+ }
119
+ export function addImports(program, specifiers, source) {
120
+ const id = specifiers.map((name) => program.scope.generateUid(name));
121
+ program.node.body.unshift(t.importDeclaration(specifiers.map((spec, index) => t.importSpecifier(t.identifier(id[index]), t.identifier(spec))), t.stringLiteral(source)));
122
+ return specifiers.reduce((result, name, index) => {
123
+ result[name] = id[index];
124
+ return result;
125
+ }, {});
126
+ }
@@ -10,7 +10,7 @@ declare const $transforms: {
10
10
  noExport: typeof noExport;
11
11
  replaceImportSpec: typeof replaceImportSpec;
12
12
  toSolidRoute: typeof toSolidRoute;
13
- toSolidImport: Transform<unknown>;
13
+ toSolidLink: Transform<unknown>;
14
14
  };
15
15
  export type TransformObject = {
16
16
  [K in keyof typeof $transforms]?: TrueIfUndefined<Parameters<(typeof $transforms)[K] extends Transform<any> ? (typeof $transforms)[K]["run"] : (typeof $transforms)[K] extends TransformRun<any> ? (typeof $transforms)[K] : never>[1]>;
@@ -2,7 +2,7 @@ import { exportDefault } from "../transforms/export-default.js";
2
2
  import { noExport } from "../transforms/no-export.js";
3
3
  import { pickExport } from "../transforms/pick-export.js";
4
4
  import { replaceImportSpec } from "../transforms/replace-import-spec.js";
5
- import { toSolidImport } from "../transforms/to-solid-import.js";
5
+ import { toSolidLink } from "../transforms/to-solid-link.js";
6
6
  import { toSolidRoute } from "../transforms/to-solid-route.js";
7
7
  import { generateIfChanges, parseFile } from "./ast.js";
8
8
  const $transforms = {
@@ -11,7 +11,7 @@ const $transforms = {
11
11
  noExport,
12
12
  replaceImportSpec,
13
13
  toSolidRoute,
14
- toSolidImport,
14
+ toSolidLink,
15
15
  };
16
16
  export function transformCode(code, ...input) {
17
17
  let ast;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dreamkit/dev",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "Development tools for DreamKit.",
5
5
  "homepage": "https://dreamkit.dev",
6
6
  "repository": {
@@ -1,2 +0,0 @@
1
- export declare const toSolidImport: import("../utils/transform.js").Transform<unknown>;
2
- //# sourceMappingURL=to-solid-import.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"to-solid-import.d.ts","sourceRoot":"","sources":["../../src/transforms/to-solid-import.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,aAAa,oDAgBxB,CAAC"}
@@ -1,18 +0,0 @@
1
- import { defineTransform } from "../utils/ast.js";
2
- import { replaceImportSpec } from "./replace-import-spec.js";
3
- export const toSolidImport = defineTransform({
4
- onlyIf: (code) => code.includes("'dreamkit'") || code.includes('"dreamkit"'),
5
- run: (ast) => replaceImportSpec(ast, {
6
- source: "dreamkit",
7
- newSource: "dreamkit/solid",
8
- spec: [
9
- "Link",
10
- "Input",
11
- "createAction",
12
- "useRoute",
13
- "defineLink",
14
- "LinkComponent",
15
- "$route",
16
- ],
17
- }),
18
- });