@dreamkit/dev 0.0.4 → 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.
- package/lib/DreamkitDevServer.d.ts.map +1 -1
- package/lib/DreamkitDevServer.js +5 -6
- package/lib/plugins/dreamkit.d.ts.map +1 -1
- package/lib/plugins/dreamkit.js +1 -9
- package/lib/transforms/to-solid-link.d.ts +2 -0
- package/lib/transforms/to-solid-link.d.ts.map +1 -0
- package/lib/transforms/to-solid-link.js +38 -0
- package/lib/transforms/to-solid-route.d.ts.map +1 -1
- package/lib/transforms/to-solid-route.js +29 -41
- package/lib/utils/ast.d.ts +9 -0
- package/lib/utils/ast.d.ts.map +1 -1
- package/lib/utils/ast.js +48 -0
- package/lib/utils/transform.d.ts +16 -8
- package/lib/utils/transform.d.ts.map +1 -1
- package/lib/utils/transform.js +21 -4
- package/package.json +1 -1
|
@@ -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;AAKtB,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"DreamkitDevServer.d.ts","sourceRoot":"","sources":["../src/DreamkitDevServer.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,qBAAqB,EACrB,wBAAwB,EACzB,MAAM,cAAc,CAAC;AAKtB,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AASpD,OAAO,EAAE,GAAG,EAAW,KAAK,EAAU,MAAM,eAAe,CAAC;AAE5D,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,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,QAAQ,CAAC,OAAO,EAAE,kBAAkB,CAAC;gBACzB,SAAS,EAAE,oBAAoB;IA6BrC,KAAK,CAAC,IAAI,EAAE,MAAM;IAMlB,YAAY,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC;IAIzC,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC;IAKxC,gBAAgB;IAyBhB,iBAAiB,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAIjD,OAAO,CAAC,uBAAuB,UAAO;IAqCtC,IAAI;CAIX"}
|
package/lib/DreamkitDevServer.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { dreamkitPluginOptions, } from "./options.js";
|
|
2
|
-
import { generateIfChanges } from "./utils/ast.js";
|
|
3
2
|
import { log } from "./utils/log.js";
|
|
4
|
-
import { resolvePath } from "./utils/path.js";
|
|
3
|
+
import { getExt, resolvePath } from "./utils/path.js";
|
|
5
4
|
import { findFileRoutes } from "./utils/router.js";
|
|
6
5
|
import { VirtualShaking } from "./utils/shaking.js";
|
|
7
|
-
import { createTransformUrl,
|
|
6
|
+
import { createTransformUrl, getUrlTransforms, transformAndGenerate, } from "./utils/transform.js";
|
|
8
7
|
import { App, isRoute, $route } from "@dreamkit/app";
|
|
9
8
|
import { existsSync } from "fs";
|
|
10
9
|
import { createServer, createViteRuntime } from "vite";
|
|
@@ -48,7 +47,8 @@ export class DreamkitDevServer {
|
|
|
48
47
|
async fetch(path) {
|
|
49
48
|
//const mod = $server.moduleGraph.getModuleById(shaking.entry);
|
|
50
49
|
//if (mod) $server.moduleGraph.invalidateModule(mod);
|
|
51
|
-
|
|
50
|
+
const ext = getExt(path);
|
|
51
|
+
return await this.runtime.executeUrl(`${path}?${Date.now()}&ext=${ext}`);
|
|
52
52
|
}
|
|
53
53
|
async fetchDefault(path) {
|
|
54
54
|
const result = await this.fetch(path);
|
|
@@ -100,8 +100,7 @@ export class DreamkitDevServer {
|
|
|
100
100
|
enforce: "pre",
|
|
101
101
|
load: (id) => this.entry.tryLoad(id),
|
|
102
102
|
transform(code, id) {
|
|
103
|
-
|
|
104
|
-
return generateIfChanges(ast);
|
|
103
|
+
return transformAndGenerate(code, { toSolidLink: true }, ...getUrlTransforms(id));
|
|
105
104
|
},
|
|
106
105
|
handleHotUpdate: async ({ file, read, modules }) => {
|
|
107
106
|
return this.entry.tryUpdate(file, read, modules);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dreamkit.d.ts","sourceRoot":"","sources":["../../src/plugins/dreamkit.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"dreamkit.d.ts","sourceRoot":"","sources":["../../src/plugins/dreamkit.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AAItD,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAE9B,wBAAgB,cAAc,CAAC,SAAS,GAAE,qBAA0B,GAAG,MAAM,CA+C5E"}
|
package/lib/plugins/dreamkit.js
CHANGED
|
@@ -23,15 +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 = [];
|
|
27
|
-
if (code.includes("'dreamkit'") || code.includes('"dreamkit"'))
|
|
28
|
-
transforms.push({
|
|
29
|
-
replaceImportSpec: {
|
|
30
|
-
source: "dreamkit",
|
|
31
|
-
spec: ["Link"],
|
|
32
|
-
newSource: "dreamkit/adapters/solid.js",
|
|
33
|
-
},
|
|
34
|
-
});
|
|
26
|
+
const transforms = [{ toSolidLink: true }];
|
|
35
27
|
if (dreamkitPickEntry) {
|
|
36
28
|
transforms.push({
|
|
37
29
|
pickExport: [dreamkitPickEntry],
|
|
@@ -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":"
|
|
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,10 +1,10 @@
|
|
|
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
3
|
import { replaceImportSpec } from "./replace-import-spec.js";
|
|
4
4
|
import * as t from "@babel/types";
|
|
5
5
|
export function toSolidRoute(ast) {
|
|
6
6
|
let changes = 0;
|
|
7
|
-
let
|
|
7
|
+
let deps;
|
|
8
8
|
let routeImportName;
|
|
9
9
|
const routeSpec = "$route";
|
|
10
10
|
const routeSource = "dreamkit";
|
|
@@ -36,10 +36,10 @@ export function toSolidRoute(ast) {
|
|
|
36
36
|
// export default route.params({}).create(() => {});
|
|
37
37
|
// [output]
|
|
38
38
|
// import { $route as _$route } from "dreamkit/adapters/solid.js"
|
|
39
|
-
// import
|
|
40
|
-
// const selfRoute = _$route.params({});
|
|
39
|
+
// import { useLocation, useRouter, useParams} from "@solidjs/router"
|
|
40
|
+
// const selfRoute = _$route.params({}).clone({ deps: ? });
|
|
41
41
|
// export const route = selfRoute.createRouteDefinition();
|
|
42
|
-
// export default delfRoute.
|
|
42
|
+
// export default delfRoute.create(() => {})
|
|
43
43
|
if (
|
|
44
44
|
// route.?.?.?.create()
|
|
45
45
|
dec.type === "CallExpression" &&
|
|
@@ -48,16 +48,18 @@ export function toSolidRoute(ast) {
|
|
|
48
48
|
dec.callee.property.type == "Identifier" &&
|
|
49
49
|
dec.callee.property.name === "create") {
|
|
50
50
|
changes++;
|
|
51
|
-
if (!
|
|
52
|
-
|
|
51
|
+
if (!deps)
|
|
52
|
+
deps = importDeps(programPath);
|
|
53
53
|
const selfRoute = programPath.scope.generateUid("selfRoute");
|
|
54
54
|
ast.program.body.splice(ast.program.body.indexOf(path.node), 1,
|
|
55
55
|
// const selfRoute = route.?.?.?
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
// export
|
|
60
|
-
|
|
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", []))),
|
|
61
|
+
// export default selfRoute.create(() => {})
|
|
62
|
+
t.exportDefaultDeclaration(appendChainCall(t.identifier(selfRoute), "create", dec.arguments)));
|
|
61
63
|
}
|
|
62
64
|
},
|
|
63
65
|
// [input]
|
|
@@ -66,8 +68,8 @@ export function toSolidRoute(ast) {
|
|
|
66
68
|
// export default function Users() { useRoute(route); }
|
|
67
69
|
// [output]
|
|
68
70
|
// import { $route as _$route } from "dreamkit/adapters/solid.js"
|
|
69
|
-
// import
|
|
70
|
-
// const selfRoute = _$route.params({}).path('/path');
|
|
71
|
+
// import { useLocation, useRouter, useParams } from "@solidjs/router"
|
|
72
|
+
// const selfRoute = _$route.params({}).path('/path').clone({ deps: ? });
|
|
71
73
|
// export const route = selfRoute.createRouteDefinition();
|
|
72
74
|
// export default selfRoute.create(function Users() { useRoute(selfRoute); });
|
|
73
75
|
ExportNamedDeclaration(path) {
|
|
@@ -83,8 +85,8 @@ export function toSolidRoute(ast) {
|
|
|
83
85
|
// [output]
|
|
84
86
|
// const _selfRoute = $route.?;
|
|
85
87
|
// export const route = _selfRoute.createRouteDefinition();
|
|
86
|
-
if (!
|
|
87
|
-
|
|
88
|
+
if (!deps)
|
|
89
|
+
deps = importDeps(programPath);
|
|
88
90
|
let exportFunction;
|
|
89
91
|
const selfRoute = programPath.scope.generateUid("selfRoute");
|
|
90
92
|
const index = ast.program.body.indexOf(path.node);
|
|
@@ -103,11 +105,13 @@ export function toSolidRoute(ast) {
|
|
|
103
105
|
}
|
|
104
106
|
return true;
|
|
105
107
|
});
|
|
106
|
-
ast.program.body.splice(index, 1,
|
|
108
|
+
ast.program.body.splice(index, 1, createConst(selfRoute, prependChainCall(dec.init, "clone", [createDepsObject(deps)])), t.exportNamedDeclaration(createConst("route", appendChainCall(selfRoute, "createRouteDefinition", []))), ...(exportFunction
|
|
107
109
|
? [
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
110
|
+
t.exportDefaultDeclaration(appendChainCall(selfRoute, "create", [
|
|
111
|
+
exportFunction.type === "FunctionDeclaration"
|
|
112
|
+
? t.functionExpression(exportFunction.id, exportFunction.params, exportFunction.body)
|
|
113
|
+
: exportFunction,
|
|
114
|
+
])),
|
|
111
115
|
]
|
|
112
116
|
: []));
|
|
113
117
|
}
|
|
@@ -118,27 +122,11 @@ export function toSolidRoute(ast) {
|
|
|
118
122
|
});
|
|
119
123
|
return addFileChanges(ast, changes);
|
|
120
124
|
}
|
|
121
|
-
function
|
|
122
|
-
return t.
|
|
123
|
-
t.
|
|
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))))),
|
|
124
128
|
]);
|
|
125
129
|
}
|
|
126
|
-
function
|
|
127
|
-
return
|
|
128
|
-
t.variableDeclarator(t.identifier("route"), t.callExpression(t.memberExpression(t.identifier(name), t.identifier("createRouteDefinition")), [])),
|
|
129
|
-
]));
|
|
130
|
-
}
|
|
131
|
-
function createDefaultExportRoute(name, depsImportName, component) {
|
|
132
|
-
return t.exportDefaultDeclaration(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier(name), t.identifier("clone")), [
|
|
133
|
-
t.objectExpression([
|
|
134
|
-
t.objectProperty(t.identifier("deps"), t.identifier(depsImportName)),
|
|
135
|
-
]),
|
|
136
|
-
]), t.identifier("create")), [component]));
|
|
137
|
-
}
|
|
138
|
-
function addImports(ast, program) {
|
|
139
|
-
const deps = program.scope.generateUid("deps");
|
|
140
|
-
ast.program.body.unshift(t.importDeclaration([t.importNamespaceSpecifier(t.identifier(deps))], t.stringLiteral("dreamkit/adapters/solid-deps.js")));
|
|
141
|
-
return {
|
|
142
|
-
deps,
|
|
143
|
-
};
|
|
130
|
+
function importDeps(program) {
|
|
131
|
+
return addImports(program, ["useLocation", "useNavigate", "useParams"], "@solidjs/router");
|
|
144
132
|
}
|
package/lib/utils/ast.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import type { Transform } from "./transform.js";
|
|
1
2
|
import { ParseResult } from "@babel/parser";
|
|
3
|
+
import { NodePath } from "@babel/traverse";
|
|
2
4
|
import * as t from "@babel/types";
|
|
3
5
|
import * as lexer from "es-module-lexer";
|
|
4
6
|
export declare function parseCode(input: string | string[]): t.Statement[];
|
|
@@ -14,4 +16,11 @@ export declare function getFirstChain(ref: t.Node): {
|
|
|
14
16
|
export declare function analyzeModule(path: string): Promise<readonly [imports: readonly lexer.ImportSpecifier[], exports: readonly lexer.ExportSpecifier[], facade: boolean, hasModuleSyntax: boolean]>;
|
|
15
17
|
export declare function findExportedNames(ast: ParseFileResult): string[];
|
|
16
18
|
export declare function findExportedNamesFromFile(path: string): Promise<string[]>;
|
|
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
|
+
};
|
|
17
26
|
//# sourceMappingURL=ast.d.ts.map
|
package/lib/utils/ast.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ast.d.ts","sourceRoot":"","sources":["../../src/utils/ast.ts"],"names":[],"mappings":"AACA,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"}
|
|
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,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,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAExE;AAED,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,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";
|
|
@@ -76,3 +77,50 @@ export async function findExportedNamesFromFile(path) {
|
|
|
76
77
|
const [, exports] = await analyzeModule(path);
|
|
77
78
|
return exports.map((item) => item.n);
|
|
78
79
|
}
|
|
80
|
+
export function defineTransform(transform) {
|
|
81
|
+
return transform;
|
|
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
|
+
}
|
package/lib/utils/transform.d.ts
CHANGED
|
@@ -8,18 +8,26 @@ declare const $transforms: {
|
|
|
8
8
|
pickExport: typeof pickExport;
|
|
9
9
|
exportDefault: typeof exportDefault;
|
|
10
10
|
noExport: typeof noExport;
|
|
11
|
-
toSolidRoute: typeof toSolidRoute;
|
|
12
11
|
replaceImportSpec: typeof replaceImportSpec;
|
|
12
|
+
toSolidRoute: typeof toSolidRoute;
|
|
13
|
+
toSolidLink: Transform<unknown>;
|
|
14
|
+
};
|
|
15
|
+
export type TransformObject = {
|
|
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]>;
|
|
17
|
+
};
|
|
18
|
+
export type TransformRun<T = any> = (ast: ParseFileResult, options: T) => number;
|
|
19
|
+
export type Transform<T = any> = {
|
|
20
|
+
onlyIf?: (code: string, options: T) => boolean;
|
|
21
|
+
run: TransformRun<T>;
|
|
13
22
|
};
|
|
14
|
-
export
|
|
15
|
-
|
|
23
|
+
export declare function transformCode(code: string, ...input: TransformObject[]): ParseFileResult | undefined;
|
|
24
|
+
export declare function transformAndGenerate(code: string, ...input: TransformObject[]): import("@babel/generator").GeneratorResult | {
|
|
25
|
+
code: undefined;
|
|
26
|
+
map: undefined;
|
|
16
27
|
};
|
|
17
|
-
export declare function transformCode(code: string, ...input: Transform[]): ParseFileResult | undefined;
|
|
18
28
|
export declare function transformCodeByUrl(id: string, code: string): ParseFileResult | undefined;
|
|
19
29
|
type TrueIfUndefined<T> = [undefined] extends [T] ? true : T;
|
|
20
|
-
export declare function createTransformUrl(id: string, ...transforms:
|
|
21
|
-
|
|
22
|
-
}[]): string;
|
|
23
|
-
export declare function getUrlTransforms(id: string): Transform[];
|
|
30
|
+
export declare function createTransformUrl(id: string, ...transforms: TransformObject[]): string;
|
|
31
|
+
export declare function getUrlTransforms(id: string): TransformObject[];
|
|
24
32
|
export {};
|
|
25
33
|
//# sourceMappingURL=transform.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../../src/utils/transform.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AACtD,OAAO,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAC1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,sCAAsC,CAAC;
|
|
1
|
+
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../../src/utils/transform.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AACtD,OAAO,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAC1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,sCAAsC,CAAC;AAEzE,OAAO,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AAC/D,OAAO,EAAgC,eAAe,EAAE,MAAM,UAAU,CAAC;AAEzE,QAAA,MAAM,WAAW;;;;;;;CAOhB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;KAC3B,CAAC,IAAI,MAAM,OAAO,WAAW,CAAC,CAAC,EAAE,eAAe,CAC/C,UAAU,CACR,CAAC,OAAO,WAAW,CAAC,CAAC,CAAC,CAAC,SAAS,SAAS,CAAC,GAAG,CAAC,GAC1C,CAAC,OAAO,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAC9B,CAAC,OAAO,WAAW,CAAC,CAAC,CAAC,CAAC,SAAS,YAAY,CAAC,GAAG,CAAC,GAC/C,CAAC,OAAO,WAAW,CAAC,CAAC,CAAC,CAAC,GACvB,KAAK,CACZ,CAAC,CAAC,CAAC,CACL;CACF,CAAC;AAEF,MAAM,MAAM,YAAY,CAAC,CAAC,GAAG,GAAG,IAAI,CAClC,GAAG,EAAE,eAAe,EACpB,OAAO,EAAE,CAAC,KACP,MAAM,CAAC;AAEZ,MAAM,MAAM,SAAS,CAAC,CAAC,GAAG,GAAG,IAAI;IAC/B,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,KAAK,OAAO,CAAC;IAC/C,GAAG,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC;CACtB,CAAC;AAEF,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,KAAK,EAAE,eAAe,EAAE,+BAsBtE;AAED,wBAAgB,oBAAoB,CAClC,IAAI,EAAE,MAAM,EACZ,GAAG,KAAK,EAAE,eAAe,EAAE;;;EAI5B;AAED,wBAAgB,kBAAkB,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,+BAG1D;AAED,KAAK,eAAe,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC;AAE7D,wBAAgB,kBAAkB,CAChC,EAAE,EAAE,MAAM,EACV,GAAG,UAAU,EAAE,eAAe,EAAE,UAQjC;AAED,wBAAgB,gBAAgB,CAAC,EAAE,EAAE,MAAM,GAAG,eAAe,EAAE,CAK9D"}
|
package/lib/utils/transform.js
CHANGED
|
@@ -2,27 +2,44 @@ 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 { toSolidLink } from "../transforms/to-solid-link.js";
|
|
5
6
|
import { toSolidRoute } from "../transforms/to-solid-route.js";
|
|
6
|
-
import { parseFile } from "./ast.js";
|
|
7
|
+
import { generateIfChanges, parseFile } from "./ast.js";
|
|
7
8
|
const $transforms = {
|
|
8
9
|
pickExport,
|
|
9
10
|
exportDefault,
|
|
10
11
|
noExport,
|
|
11
|
-
toSolidRoute,
|
|
12
12
|
replaceImportSpec,
|
|
13
|
+
toSolidRoute,
|
|
14
|
+
toSolidLink,
|
|
13
15
|
};
|
|
14
16
|
export function transformCode(code, ...input) {
|
|
15
17
|
let ast;
|
|
16
18
|
for (const item of input) {
|
|
17
|
-
for (let [name,
|
|
19
|
+
for (let [name, options] of Object.entries(item)) {
|
|
18
20
|
if (name in $transforms) {
|
|
21
|
+
const $transform = $transforms[name];
|
|
22
|
+
let run;
|
|
23
|
+
if ("run" in $transform) {
|
|
24
|
+
if (ast === undefined && $transform.onlyIf)
|
|
25
|
+
if (!$transform.onlyIf(code, options))
|
|
26
|
+
continue;
|
|
27
|
+
run = $transform.run;
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
run = $transform;
|
|
31
|
+
}
|
|
19
32
|
ast = ast || parseFile(code);
|
|
20
|
-
|
|
33
|
+
run(ast, options);
|
|
21
34
|
}
|
|
22
35
|
}
|
|
23
36
|
}
|
|
24
37
|
return ast;
|
|
25
38
|
}
|
|
39
|
+
export function transformAndGenerate(code, ...input) {
|
|
40
|
+
const ast = transformCode(code, ...input);
|
|
41
|
+
return generateIfChanges(ast) || { code: undefined, map: undefined };
|
|
42
|
+
}
|
|
26
43
|
export function transformCodeByUrl(id, code) {
|
|
27
44
|
const transforms = getUrlTransforms(id);
|
|
28
45
|
return transformCode(code, ...transforms);
|