Skip to content
This repository has been archived by the owner on Apr 10, 2023. It is now read-only.

Commit

Permalink
fix(view): avoid circular dependency and simplify makeRender and make…
Browse files Browse the repository at this point in the history
…View api
  • Loading branch information
axmad386 committed May 17, 2022
1 parent e8c3bc1 commit 76f06f4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/View/Factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Response from "../Support/Facades/Response";
import type { ObjectOf } from "../Types";
import type Application from "../Foundation/Application";
import { pathToFileURL } from "url";
import { RuntimeException } from "../Foundation/Exception";
import RuntimeException from "../Foundation/Exception/RuntimeException";

class Factory {
protected app: Application;
Expand Down
3 changes: 2 additions & 1 deletion src/View/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import Factory from "./Factory";
import ViewServiceProvider from "./ViewServiceProvider";
export { ViewServiceProvider };
export { ViewServiceProvider, Factory };
5 changes: 4 additions & 1 deletion src/entry-client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const makeView = async (modules: any, viewPath: string) => {
const makeView = async (
modules: any,
viewPath = "/app/view/resources/view"
) => {
let View = null;
await Promise.all(
Object.keys(modules).map(async (m) => {
Expand Down
11 changes: 7 additions & 4 deletions src/entry-server.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import type { Request } from "./Http/Request";
import {readFileSync} from "fs";
import path from "path";
const defaultViewPath = config("view.paths")[0]||"/app/resources/view";

export const makeRender =
(modules: any, viewPath: string) =>
(modules: any, viewPath=defaultViewPath) =>
async (url: any, props: any, req: Request, cb: (props: any) => any) => {
const manifest = process.env.NODE_ENV=="production"? JSON.parse(readFileSync(base_path("client/manifest.json"), "utf-8")):{};
let View: any = null;
let preloadLinks = "";
await Promise.all(
Object.keys(modules).map(async (m) => {
const fullViewPath = `${viewPath}/${url}.svelte`;
if (m == fullViewPath) {
if (m == fullViewPath){
const module = await modules[m]();
if(module.onServer){
const serverProps = await module.onServer(req);
props = { ...props, ...serverProps };
}
View = module.default;
if(process.env.NODE_ENV=="production"){
preloadLinks = renderPreloadLinks("app"+fullViewPath.split("app")[1], manifest);
preloadLinks = renderPreloadLinks(fullViewPath.replace(/^\//, ""), manifest);
}
}
})
Expand All @@ -29,9 +31,10 @@ export const makeRender =
};

const renderPreloadLinks = (viewPath: string, manifest:any) => {

let links = "";
const seen = new Set();
const {imports, css, file} = manifest[viewPath];
const {imports, css, file} = manifest[path.join(viewPath)];
if(file && !seen.has(file)){
if(!seen.has(file)){
seen.add(file);
Expand Down

0 comments on commit 76f06f4

Please sign in to comment.