~repos /edge-city

#react#js#ssr

git clone https://pyrossh.dev/repos/edge-city.git

edge-city is a next level meta-framework for react that runs only on edge runtimes


3d6e7c7f pyrossh

2 years ago
Fix service imports
Files changed (1) hide show
  1. cli.js +22 -47
cli.js CHANGED
@@ -48,6 +48,13 @@ const clientEnvs = Object.keys(process.env)
48
48
  return acc
49
49
  }, {});
50
50
 
51
+ const parseExports = (src) => {
52
+ return src.split("\n").filter((l) => l.includes("export const") && l.includes("=>"))
53
+ .map((l) => /export const (.*) = async/g.exec(l))
54
+ .filter((n) => n && n[1])
55
+ .map((n) => n[1]);
56
+ }
57
+
51
58
  const bundleJs = async ({ entryPoints, isServer, outfile, ...options }, plg) => {
52
59
  const result = await esbuild.build({
53
60
  bundle: true,
@@ -166,7 +173,19 @@ const bundlePages = async () => {
166
173
  loader: "file",
167
174
  }
168
175
  });
176
+ build.onLoad({ filter: /\\*.service.js/, namespace: undefined }, async (args) => {
177
+ const src = fs.readFileSync(args.path, "utf8");
178
+ const svcName = args.path.replace(srcDir, "").replace("/services/", "").replace(".service.js", "");
179
+ const funcs = parseExports(src);
180
+ const newSrc = `
181
+ import { defineRpc } from "edge-city";
182
+ ${funcs.map((f) => `export const ${f} = defineRpc("${svcName}/${f}")`).join("\n")}
183
+ `
169
- build.on
184
+ return {
185
+ contents: newSrc,
186
+ loader: "js",
187
+ };
188
+ });
170
189
  }
171
190
  });
172
191
  for (const r of routes) {
@@ -180,10 +199,7 @@ const bundleServices = async () => {
180
199
  for (const s of services) {
181
200
  const dest = s.replace(srcDir, "").replace("/services", "").replace(".service.js", "");
182
201
  const src = fs.readFileSync(s, 'utf8');
183
- const funcs = src.split("\n").filter((l) => l.includes("export const") && l.includes("=>"))
184
- .map((l) => /export const (.*) = async/g.exec(l))
185
- .filter((n) => n && n[1])
202
+ const funcs = parseExports(src);
186
- .map((n) => n[1]);
187
203
  for (const p of funcs) {
188
204
  const buildStart = Date.now();
189
205
  const result = await bundleJs({
@@ -278,45 +294,4 @@ yargs(hideBin(process.argv))
278
294
  build(platform, false);
279
295
  })
280
296
  .demandCommand(1)
281
- .parse()
297
+ .parse();
282
-
283
- // const renderJs = async (srcFile) => {
284
- // try {
285
- // const jsText = await Bun.file(srcFile).text();
286
- // const result = await transpiler.transform(jsText);
287
- // // inject code which calls the api for that function
288
- // const lines = result.split("\n");
289
- // // lines.unshift(`import React from "react";`);
290
-
291
- // // replace all .service imports which rpc interface
292
- // let addRpcImport = false;
293
- // lines.forEach((ln) => {
294
- // if (ln.includes(".service")) {
295
- // addRpcImport = true;
296
- // const [importName, serviceName] = ln.match(/\@\/services\/(.*)\.service/);
297
- // const funcsText = ln.replace(`from "${importName}"`, "").replace("import", "").replace("{", "").replace("}", "").replace(";", "");
298
- // const funcsName = funcsText.split(",");
299
- // funcsName.forEach((fnName) => {
300
- // lines.push(`const ${fnName} = rpc("${serviceName}/${fnName.trim()}")`);
301
- // })
302
- // }
303
- // })
304
- // if (addRpcImport) {
305
- // lines.unshift(`import { rpc } from "edge-city";`);
306
- // }
307
- // // remove .css and .service imports
308
- // const filteredJsx = lines.filter((ln) => !ln.includes(`.css"`) && !ln.includes(`.service"`)).join("\n");
309
- // //.replaceAll("$jsx", "React.createElement");
310
- // return new Response(filteredJsx, {
311
- // headers: {
312
- // 'Content-Type': 'application/javascript',
313
- // },
314
- // status: 200,
315
- // });
316
- // } catch (err) {
317
- // return new Response(`Not Found`, {
318
- // headers: { 'Content-Type': 'text/html' },
319
- // status: 404,
320
- // });
321
- // }
322
- // }