@kithinji/pod 1.0.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/build.js +22 -0
- package/dist/main.js +4464 -0
- package/dist/main.js.map +7 -0
- package/dist/types/add/component/component.d.ts +5 -0
- package/dist/types/add/component/component.d.ts.map +1 -0
- package/dist/types/add/component/index.d.ts +2 -0
- package/dist/types/add/component/index.d.ts.map +1 -0
- package/dist/types/add/index.d.ts +4 -0
- package/dist/types/add/index.d.ts.map +1 -0
- package/dist/types/add/module/index.d.ts +2 -0
- package/dist/types/add/module/index.d.ts.map +1 -0
- package/dist/types/add/module/module.d.ts +3 -0
- package/dist/types/add/module/module.d.ts.map +1 -0
- package/dist/types/add/new/index.d.ts +2 -0
- package/dist/types/add/new/index.d.ts.map +1 -0
- package/dist/types/config/config.d.ts +18 -0
- package/dist/types/config/config.d.ts.map +1 -0
- package/dist/types/config/index.d.ts +2 -0
- package/dist/types/config/index.d.ts.map +1 -0
- package/dist/types/dev/index.d.ts +2 -0
- package/dist/types/dev/index.d.ts.map +1 -0
- package/dist/types/dev/project.d.ts +9 -0
- package/dist/types/dev/project.d.ts.map +1 -0
- package/dist/types/dev/server.d.ts +2 -0
- package/dist/types/dev/server.d.ts.map +1 -0
- package/dist/types/docker/docker.d.ts +2 -0
- package/dist/types/docker/docker.d.ts.map +1 -0
- package/dist/types/docker/index.d.ts +2 -0
- package/dist/types/docker/index.d.ts.map +1 -0
- package/dist/types/macros/expand_macros.d.ts +48 -0
- package/dist/types/macros/expand_macros.d.ts.map +1 -0
- package/dist/types/macros/index.d.ts +3 -0
- package/dist/types/macros/index.d.ts.map +1 -0
- package/dist/types/macros/macro_executer.d.ts +12 -0
- package/dist/types/macros/macro_executer.d.ts.map +1 -0
- package/dist/types/main.d.ts +13 -0
- package/dist/types/main.d.ts.map +1 -0
- package/dist/types/plugins/analyzers/graph.d.ts +25 -0
- package/dist/types/plugins/analyzers/graph.d.ts.map +1 -0
- package/dist/types/plugins/css/index.d.ts +7 -0
- package/dist/types/plugins/css/index.d.ts.map +1 -0
- package/dist/types/plugins/generators/generate_controller.d.ts +2 -0
- package/dist/types/plugins/generators/generate_controller.d.ts.map +1 -0
- package/dist/types/plugins/generators/generate_rsc.d.ts +2 -0
- package/dist/types/plugins/generators/generate_rsc.d.ts.map +1 -0
- package/dist/types/plugins/generators/generate_server_component.d.ts +2 -0
- package/dist/types/plugins/generators/generate_server_component.d.ts.map +1 -0
- package/dist/types/plugins/generators/tsx_server_stub.d.ts +2 -0
- package/dist/types/plugins/generators/tsx_server_stub.d.ts.map +1 -0
- package/dist/types/plugins/index.d.ts +4 -0
- package/dist/types/plugins/index.d.ts.map +1 -0
- package/dist/types/plugins/my.d.ts +10 -0
- package/dist/types/plugins/my.d.ts.map +1 -0
- package/dist/types/plugins/transformers/j2d.d.ts +11 -0
- package/dist/types/plugins/transformers/j2d.d.ts.map +1 -0
- package/dist/types/store/index.d.ts +2 -0
- package/dist/types/store/index.d.ts.map +1 -0
- package/dist/types/store/store.d.ts +14 -0
- package/dist/types/store/store.d.ts.map +1 -0
- package/dist/types/utils/cases.d.ts +4 -0
- package/dist/types/utils/cases.d.ts.map +1 -0
- package/dist/types/utils/create.d.ts +12 -0
- package/dist/types/utils/create.d.ts.map +1 -0
- package/dist/types/utils/index.d.ts +3 -0
- package/dist/types/utils/index.d.ts.map +1 -0
- package/package.json +44 -0
- package/src/add/component/component.ts +496 -0
- package/src/add/component/index.ts +1 -0
- package/src/add/index.ts +3 -0
- package/src/add/module/index.ts +1 -0
- package/src/add/module/module.ts +521 -0
- package/src/add/new/index.ts +135 -0
- package/src/config/config.ts +141 -0
- package/src/config/index.ts +1 -0
- package/src/dev/index.ts +1 -0
- package/src/dev/project.ts +45 -0
- package/src/dev/server.ts +190 -0
- package/src/docker/docker.ts +452 -0
- package/src/docker/index.ts +1 -0
- package/src/macros/expand_macros.ts +791 -0
- package/src/macros/index.ts +2 -0
- package/src/macros/macro_executer.ts +189 -0
- package/src/main.ts +95 -0
- package/src/plugins/analyzers/graph.ts +291 -0
- package/src/plugins/css/index.ts +25 -0
- package/src/plugins/generators/generate_controller.ts +308 -0
- package/src/plugins/generators/generate_rsc.ts +274 -0
- package/src/plugins/generators/generate_server_component.ts +279 -0
- package/src/plugins/generators/tsx_server_stub.ts +295 -0
- package/src/plugins/index.ts +3 -0
- package/src/plugins/my.ts +274 -0
- package/src/plugins/transformers/j2d.ts +1014 -0
- package/src/store/index.ts +1 -0
- package/src/store/store.ts +44 -0
- package/src/utils/cases.ts +15 -0
- package/src/utils/create.ts +26 -0
- package/src/utils/index.ts +2 -0
- package/tsconfig.json +27 -0
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import type { Plugin, OnLoadArgs, OnLoadResult } from "esbuild";
|
|
2
|
+
import * as fs from "fs/promises";
|
|
3
|
+
import { ReactConfig, transform } from "@swc/core";
|
|
4
|
+
import * as babel from "@babel/core";
|
|
5
|
+
import * as path from "path";
|
|
6
|
+
import { expandMacros } from "@/macros";
|
|
7
|
+
import { DependencyGraph, FileNode } from "./analyzers/graph";
|
|
8
|
+
import { generateController } from "./generators/generate_controller";
|
|
9
|
+
import { generateServerStub } from "./generators/tsx_server_stub";
|
|
10
|
+
import { j2d } from "./transformers/j2d";
|
|
11
|
+
import { generateServerComponent } from "./generators/generate_server_component";
|
|
12
|
+
import { generateRscStub } from "./generators/generate_rsc";
|
|
13
|
+
|
|
14
|
+
interface MyPluginParams {
|
|
15
|
+
isServerBuild: boolean;
|
|
16
|
+
graph?: DependencyGraph;
|
|
17
|
+
onClientFound: (path: string) => void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type FileDirective = "interactive" | "public" | null;
|
|
21
|
+
|
|
22
|
+
interface FileMetadata {
|
|
23
|
+
source: string;
|
|
24
|
+
path: string;
|
|
25
|
+
isTsx: boolean;
|
|
26
|
+
directive: FileDirective;
|
|
27
|
+
isPublicFile: boolean;
|
|
28
|
+
isInteractiveFile: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function swcTransform(
|
|
32
|
+
source: string,
|
|
33
|
+
pathStr: string,
|
|
34
|
+
tsx: boolean = false,
|
|
35
|
+
react?: ReactConfig
|
|
36
|
+
): Promise<OnLoadResult> {
|
|
37
|
+
const resolveDir = path.dirname(pathStr);
|
|
38
|
+
|
|
39
|
+
const swcResult = await transform(source, {
|
|
40
|
+
filename: pathStr,
|
|
41
|
+
jsc: {
|
|
42
|
+
parser: {
|
|
43
|
+
syntax: "typescript",
|
|
44
|
+
tsx,
|
|
45
|
+
decorators: true,
|
|
46
|
+
},
|
|
47
|
+
transform: {
|
|
48
|
+
legacyDecorator: true,
|
|
49
|
+
decoratorMetadata: true,
|
|
50
|
+
react,
|
|
51
|
+
},
|
|
52
|
+
target: "esnext",
|
|
53
|
+
},
|
|
54
|
+
isModule: true,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
contents: swcResult.code,
|
|
59
|
+
loader: "js",
|
|
60
|
+
resolveDir,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function parseFileMetadata(source: string, path: string): FileMetadata {
|
|
65
|
+
const isTsx = path.endsWith(".tsx");
|
|
66
|
+
const isInteractiveFile =
|
|
67
|
+
source.startsWith('"use interactive"') ||
|
|
68
|
+
source.startsWith("'use interactive'");
|
|
69
|
+
const isPublicFile =
|
|
70
|
+
source.startsWith('"use public"') || source.startsWith("'use public'");
|
|
71
|
+
|
|
72
|
+
let directive: FileDirective = null;
|
|
73
|
+
if (isInteractiveFile) directive = "interactive";
|
|
74
|
+
else if (isPublicFile) directive = "public";
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
source,
|
|
78
|
+
path,
|
|
79
|
+
isTsx,
|
|
80
|
+
directive,
|
|
81
|
+
isPublicFile,
|
|
82
|
+
isInteractiveFile,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
class ServerBuildTransformer {
|
|
87
|
+
async transformPublicFile(
|
|
88
|
+
source: string,
|
|
89
|
+
path: string
|
|
90
|
+
): Promise<OnLoadResult> {
|
|
91
|
+
const controllerCode = generateController(path, source);
|
|
92
|
+
|
|
93
|
+
if (controllerCode) {
|
|
94
|
+
source = `${source}\n\n${controllerCode}\n`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return swcTransform(source, path);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async transformRegularTypeScript(
|
|
101
|
+
source: string,
|
|
102
|
+
path: string,
|
|
103
|
+
isPublic: boolean
|
|
104
|
+
): Promise<OnLoadResult> {
|
|
105
|
+
if (isPublic) {
|
|
106
|
+
return this.transformPublicFile(source, path);
|
|
107
|
+
}
|
|
108
|
+
return swcTransform(source, path);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async transformServerTsx(
|
|
112
|
+
source: string,
|
|
113
|
+
path: string
|
|
114
|
+
): Promise<OnLoadResult> {
|
|
115
|
+
return swcTransform(source, path, true, {
|
|
116
|
+
runtime: "automatic",
|
|
117
|
+
importSource: "@kithinji/orca",
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async transformInteractiveTsxStub(
|
|
122
|
+
source: string,
|
|
123
|
+
path: string
|
|
124
|
+
): Promise<OnLoadResult> {
|
|
125
|
+
const stubSource = generateServerStub(path, source);
|
|
126
|
+
return swcTransform(stubSource, path);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async process(
|
|
130
|
+
metadata: FileMetadata,
|
|
131
|
+
onClientFound: (path: string) => void
|
|
132
|
+
): Promise<OnLoadResult> {
|
|
133
|
+
const expandedSource = await expandMacros(metadata.source, metadata.path);
|
|
134
|
+
|
|
135
|
+
const expandedMetadata = { ...metadata, source: expandedSource };
|
|
136
|
+
const { source, path, isTsx, isInteractiveFile, isPublicFile } =
|
|
137
|
+
expandedMetadata;
|
|
138
|
+
|
|
139
|
+
if (isTsx) {
|
|
140
|
+
if (isInteractiveFile) {
|
|
141
|
+
onClientFound(path);
|
|
142
|
+
return this.transformInteractiveTsxStub(source, path);
|
|
143
|
+
}
|
|
144
|
+
return this.transformServerTsx(source, path);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return this.transformRegularTypeScript(source, path, isPublicFile);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
class ClientBuildTransformer {
|
|
152
|
+
async transformInteractiveTsx(
|
|
153
|
+
source: string,
|
|
154
|
+
path: string
|
|
155
|
+
): Promise<OnLoadResult> {
|
|
156
|
+
const swcResult = await swcTransform(source, path, true, {
|
|
157
|
+
runtime: "preserve",
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
const babelResult = await babel.transformAsync(
|
|
161
|
+
swcResult.contents as string,
|
|
162
|
+
{
|
|
163
|
+
filename: path,
|
|
164
|
+
sourceType: "module",
|
|
165
|
+
plugins: [j2d],
|
|
166
|
+
parserOpts: {
|
|
167
|
+
plugins: ["jsx"],
|
|
168
|
+
},
|
|
169
|
+
configFile: false,
|
|
170
|
+
babelrc: false,
|
|
171
|
+
}
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
return {
|
|
175
|
+
contents: babelResult?.code || "",
|
|
176
|
+
loader: "js",
|
|
177
|
+
resolveDir: swcResult.resolveDir,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async transformServerComponent(
|
|
182
|
+
node: FileNode,
|
|
183
|
+
source: string,
|
|
184
|
+
path: string
|
|
185
|
+
): Promise<OnLoadResult> {
|
|
186
|
+
const scSource = generateServerComponent(path, source);
|
|
187
|
+
return swcTransform(scSource, path);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async transformPublicFileRsc(
|
|
191
|
+
node: FileNode,
|
|
192
|
+
source: string,
|
|
193
|
+
path: string
|
|
194
|
+
): Promise<OnLoadResult> {
|
|
195
|
+
const stubSource = generateRscStub(path, source);
|
|
196
|
+
return swcTransform(stubSource, path);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async transformSharedCode(
|
|
200
|
+
source: string,
|
|
201
|
+
path: string
|
|
202
|
+
): Promise<OnLoadResult> {
|
|
203
|
+
return swcTransform(source, path);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async process(node: FileNode, metadata: FileMetadata): Promise<OnLoadResult> {
|
|
207
|
+
const expandedSource = await expandMacros(metadata.source, metadata.path);
|
|
208
|
+
|
|
209
|
+
const expandedMetadata = { ...metadata, source: expandedSource };
|
|
210
|
+
const { source, path, isTsx, directive } = expandedMetadata;
|
|
211
|
+
|
|
212
|
+
if (isTsx) {
|
|
213
|
+
if (directive === "interactive") {
|
|
214
|
+
return this.transformInteractiveTsx(source, path);
|
|
215
|
+
} else if (directive === null) {
|
|
216
|
+
return this.transformServerComponent(node, source, path);
|
|
217
|
+
} else {
|
|
218
|
+
throw new Error(
|
|
219
|
+
`Unexpected directive "${directive}" for TSX file: ${path}`
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (directive === "public") {
|
|
225
|
+
return this.transformPublicFileRsc(node, source, path);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (directive === null) {
|
|
229
|
+
return this.transformSharedCode(source, path);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return {
|
|
233
|
+
contents: source,
|
|
234
|
+
loader: isTsx ? "tsx" : "ts",
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export function useMyPlugin(options: MyPluginParams): Plugin {
|
|
240
|
+
const serverTransformer = new ServerBuildTransformer();
|
|
241
|
+
const clientTransformer = new ClientBuildTransformer();
|
|
242
|
+
|
|
243
|
+
return {
|
|
244
|
+
name: "Orca",
|
|
245
|
+
setup(build) {
|
|
246
|
+
build.onLoad(
|
|
247
|
+
{ filter: /\.tsx?$/ },
|
|
248
|
+
async (args: OnLoadArgs): Promise<OnLoadResult> => {
|
|
249
|
+
const source = await fs.readFile(args.path, "utf8");
|
|
250
|
+
const metadata = parseFileMetadata(source, args.path);
|
|
251
|
+
|
|
252
|
+
if (options.isServerBuild) {
|
|
253
|
+
return serverTransformer.process(metadata, options.onClientFound);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (!options.graph) {
|
|
257
|
+
throw new Error(
|
|
258
|
+
"Dependency graph is required for client build but was not provided"
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const node = options.graph[args.path];
|
|
263
|
+
if (!node) {
|
|
264
|
+
throw new Error(
|
|
265
|
+
`File node not found in dependency graph: ${args.path}`
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return clientTransformer.process(node, metadata);
|
|
270
|
+
}
|
|
271
|
+
);
|
|
272
|
+
},
|
|
273
|
+
};
|
|
274
|
+
}
|