don’t require a build to run in development mode

This commit is contained in:
Hans Pagel committed 2023-02-06 17:46:24 +01:00
1 parent 8011bdb482
commit 706e13eddc
4 files changed
+65 -53

No files matched your search

+5 -3
View File
@@ -10,9 +10,9 @@ import documentLoader from "@server/commands/documentLoader";
import env from "@server/env";
import { Integration } from "@server/models";
import presentEnv from "@server/presenters/env";
import manifest from "@server/utils/manifest";
import { getTeamFromContext } from "@server/utils/passport";
import prefetchTags from "@server/utils/prefetchTags";
import readManifestFile from "@server/utils/readManifestFile";
const isProduction = env.ENVIRONMENT === "production";
const isTest = env.ENVIRONMENT === "test";
@@ -27,7 +27,7 @@ const readIndexFile = async (): Promise<Buffer> => {
}
return (indexHtmlCache = await readFile(
path.join(__dirname, "../static/index.html")
path.join(__dirname, "../../../server/static/index.html")
));
};
@@ -59,7 +59,9 @@ export const renderApp = async (
const serviceWorker = `<script src="${process.env.CDN_URL}/app/registerSW.js"></script>`;
const entry = "app/index.tsx";
const scriptTags = isProduction
? `<script type="module" src="${process.env.CDN_URL}/${manifest[entry]["file"]}"></script>`
? `<script type="module" src="${process.env.CDN_URL}/${
readManifestFile()[entry]["file"]
}"></script>`
: `<script type="module">
import RefreshRuntime from 'http://localhost:3001/@react-refresh'
RefreshRuntime.injectIntoGlobalHook(window)
-24
View File
@@ -1,24 +0,0 @@
import fs from "fs";
import path from "path";
type Chunk = {
file: string;
src: string;
isEntry?: boolean;
};
type ManifestStructure = Record<string, Chunk>;
const file = path.resolve("./build/app/manifest.json");
let manifest = "{}";
try {
manifest = fs.readFileSync(file, "utf8") as string;
} catch (err) {
console.warn(
`Can not find ${file}. Try executing "yarn vite:build" before running in production mode.`
);
}
export default JSON.parse(manifest) as ManifestStructure;
+32 -26
View File
@@ -1,7 +1,9 @@
import * as React from "react";
import ReactDOMServer from "react-dom/server";
import env from "@server/env";
import manifest from "./manifest";
import readManifestFile from "./readManifestFile";
const isProduction = env.ENVIRONMENT === "production";
const prefetchTags = [];
@@ -15,37 +17,41 @@ if (process.env.AWS_S3_UPLOAD_BUCKET_URL) {
);
}
Object.keys(manifest).forEach((filename) => {
const file = manifest[filename]["file"];
if (isProduction) {
const manifest = readManifestFile();
if (typeof file !== "string") {
return;
}
if (!env.CDN_URL) {
return;
}
Object.keys(manifest).forEach((filename) => {
const file = manifest[filename]["file"];
if (file.endsWith(".js")) {
// Preload resources you have high-confidence will be used in the current
// page.Prefetch resources likely to be used for future navigations
// TODO: Doesn’t apply to any file after switching to Vite.
// What files should we really preload?
const shouldPreload =
file.includes("/main") ||
file.includes("/runtime") ||
file.includes("preload-");
if (typeof file !== "string") {
return;
}
if (!env.CDN_URL) {
return;
}
if (shouldPreload) {
if (file.endsWith(".js")) {
// Preload resources you have high-confidence will be used in the current
// page.Prefetch resources likely to be used for future navigations
// TODO: Doesn’t apply to any file after switching to Vite.
// What files should we really preload?
const shouldPreload =
file.includes("/main") ||
file.includes("/runtime") ||
file.includes("preload-");
if (shouldPreload) {
prefetchTags.push(
<link rel="preload" href={file} key={file} as="script" />
);
}
} else if (file.endsWith(".css")) {
prefetchTags.push(
<link rel="preload" href={file} key={file} as="script" />
<link rel="prefetch" href={file} key={file} as="style" />
);
}
} else if (file.endsWith(".css")) {
prefetchTags.push(
<link rel="prefetch" href={file} key={file} as="style" />
);
}
});
});
}
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type 'Element[]' is not assignable to ... Remove this comment to see the full error message
export default ReactDOMServer.renderToString(prefetchTags);
+28
View File
@@ -0,0 +1,28 @@
import fs from "fs";
import path from "path";
export type Chunk = {
file: string;
src: string;
isEntry?: boolean;
};
export type ManifestStructure = Record<string, Chunk>;
export const readManifestFile = (file = "./build/app/manifest.json") => {
const absoluteFilePath = path.resolve(file);
let manifest = "{}";
try {
manifest = fs.readFileSync(absoluteFilePath, "utf8") as string;
} catch (err) {
console.warn(
`Can not find ${absoluteFilePath}. Try executing "yarn vite:build" before running in production mode.`
);
}
return JSON.parse(manifest) as ManifestStructure;
};
export default readManifestFile;