Update bundler. Handle non-existent file error.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { writeFileSync } from "fs";
|
||||
import { existsSync, statSync, writeFileSync } from "fs";
|
||||
import * as esbuild from "esbuild";
|
||||
import grabAllPages from "../../utils/grab-all-pages";
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
@@ -9,8 +9,13 @@ import { log } from "../../utils/log";
|
||||
import tailwindEsbuildPlugin from "../server/web-pages/tailwind-esbuild-plugin";
|
||||
import grabClientHydrationScript from "./grab-client-hydration-script";
|
||||
import grabArtifactsFromBundledResults from "./grab-artifacts-from-bundled-result";
|
||||
import path from "path";
|
||||
|
||||
const { HYDRATION_DST_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames();
|
||||
const { HYDRATION_DST_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE, ROOT_DIR } =
|
||||
grabDirNames();
|
||||
|
||||
let build_starts = 0;
|
||||
const MAX_BUILD_STARTS = 10;
|
||||
|
||||
type Params = {
|
||||
watch?: boolean;
|
||||
@@ -56,11 +61,27 @@ export default async function allPagesBundler(params?: Params) {
|
||||
let buildStart = 0;
|
||||
|
||||
build.onStart(() => {
|
||||
build_starts++;
|
||||
buildStart = performance.now();
|
||||
|
||||
if (build_starts == MAX_BUILD_STARTS) {
|
||||
const error_msg = `Build Failed. Please check all your components and imports.`;
|
||||
log.error(error_msg);
|
||||
// process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
build.onEnd((result) => {
|
||||
if (result.errors.length > 0) return;
|
||||
if (result.errors.length > 0) {
|
||||
for (const error of result.errors) {
|
||||
const loc = error.location;
|
||||
const location = loc
|
||||
? ` ${loc.file}:${loc.line}:${loc.column}`
|
||||
: "";
|
||||
log.error(`[Build]${location} ${error.text}`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const artifacts = grabArtifactsFromBundledResults({
|
||||
pages,
|
||||
@@ -86,6 +107,8 @@ export default async function allPagesBundler(params?: Params) {
|
||||
if (params?.exit_after_first_build) {
|
||||
process.exit();
|
||||
}
|
||||
|
||||
build_starts = 0;
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { watch, existsSync } from "fs";
|
||||
import { watch, existsSync, statSync } from "fs";
|
||||
import path from "path";
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
import rebuildBundler from "./rebuild-bundler";
|
||||
@@ -6,7 +6,9 @@ import { log } from "../../utils/log";
|
||||
|
||||
const { ROOT_DIR } = grabDirNames();
|
||||
|
||||
export default function watcher() {
|
||||
export default async function watcher() {
|
||||
await Bun.sleep(1000);
|
||||
|
||||
const pages_src_watcher = watch(
|
||||
ROOT_DIR,
|
||||
{
|
||||
@@ -16,6 +18,17 @@ export default function watcher() {
|
||||
async (event, filename) => {
|
||||
if (!filename) return;
|
||||
|
||||
const full_file_path = path.join(ROOT_DIR, filename);
|
||||
|
||||
if (full_file_path.match(/\/styles$/)) {
|
||||
global.RECOMPILING = true;
|
||||
await Bun.sleep(1000);
|
||||
await fullRebuild({
|
||||
msg: `Detected new \`styles\` directory. Rebuilding ...`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const excluded_match =
|
||||
/node_modules\/|^public\/|^\.bunext\/|^\.git\/|^dist\/|bun\.lockb$/;
|
||||
|
||||
@@ -52,8 +65,7 @@ export default function watcher() {
|
||||
|
||||
if (global.RECOMPILING) return;
|
||||
|
||||
const fullPath = path.join(ROOT_DIR, filename);
|
||||
const action = existsSync(fullPath) ? "created" : "deleted";
|
||||
const action = existsSync(full_file_path) ? "created" : "deleted";
|
||||
const type = filename.match(/\.css$/) ? "Sylesheet" : "Page";
|
||||
|
||||
await fullRebuild({
|
||||
|
||||
@@ -58,7 +58,7 @@ export default async function grabPageComponent({
|
||||
|
||||
if (!file_path) {
|
||||
const errMsg = `No File Path (\`file_path\`) or Request Object (\`req\`) provided not found`;
|
||||
// console.error(errMsg);
|
||||
// log.error(errMsg);
|
||||
throw new Error(errMsg);
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ export default async function grabPageComponent({
|
||||
|
||||
if (!bundledMap?.path) {
|
||||
const errMsg = `No Bundled File Path for this request path!`;
|
||||
console.error(errMsg);
|
||||
log.error(errMsg);
|
||||
throw new Error(errMsg);
|
||||
}
|
||||
|
||||
@@ -172,7 +172,7 @@ export default async function grabPageComponent({
|
||||
head: Head,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.error(`Error Grabbing Page Component: ${error.message}`);
|
||||
log.error(`Error Grabbing Page Component: ${error.message}`);
|
||||
|
||||
return await grabPageErrorComponent({
|
||||
error,
|
||||
|
||||
@@ -36,14 +36,18 @@ export default async function (params?: Params) {
|
||||
script += ` try {\n`;
|
||||
script += ` document.getElementById("__bunext_error_overlay")?.remove();\n`;
|
||||
script += ` const data = JSON.parse(event.data);\n`;
|
||||
// script += ` console.log("data", data);\n`;
|
||||
|
||||
script += ` const oldCSSLink = document.querySelector('link[rel="stylesheet"]');\n`;
|
||||
|
||||
script += ` if (data.target_map.css_path) {\n`;
|
||||
script += ` const oldLink = document.querySelector('link[rel="stylesheet"]');\n`;
|
||||
script += ` const newLink = document.createElement("link");\n`;
|
||||
script += ` newLink.rel = "stylesheet";\n`;
|
||||
script += ` newLink.href = \`/\${data.target_map.css_path}?t=\${Date.now()}\`;\n`;
|
||||
script += ` newLink.onload = () => oldLink?.remove();\n`;
|
||||
script += ` newLink.onload = () => oldCSSLink?.remove();\n`;
|
||||
script += ` document.head.appendChild(newLink);\n`;
|
||||
script += ` } else if (oldCSSLink) {\n`;
|
||||
script += ` oldCSSLink.remove();\n`;
|
||||
script += ` }\n`;
|
||||
|
||||
script += ` const newScriptPath = \`/\${data.target_map.path}?t=\${Date.now()}\`;\n\n`;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import isDevelopment from "../../../utils/is-development";
|
||||
import { log } from "../../../utils/log";
|
||||
import getCache from "../../cache/get-cache";
|
||||
import generateWebPageResponseFromComponentReturn from "./generate-web-page-response-from-component-return";
|
||||
import grabPageComponent from "./grab-page-component";
|
||||
@@ -38,7 +39,7 @@ export default async function handleWebPages({
|
||||
...componentRes,
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error(`Error Handling Web Page: ${error.message}`);
|
||||
log.error(`Error Handling Web Page: ${error.message}`);
|
||||
|
||||
const componentRes = await grabPageErrorComponent({
|
||||
error,
|
||||
|
||||
@@ -2,19 +2,59 @@ import * as esbuild from "esbuild";
|
||||
import postcss from "postcss";
|
||||
import tailwindcss from "@tailwindcss/postcss";
|
||||
import { readFile } from "fs/promises";
|
||||
import path from "path";
|
||||
import { existsSync } from "fs";
|
||||
import grabDirNames from "../../../utils/grab-dir-names";
|
||||
import { log } from "../../../utils/log";
|
||||
|
||||
const { ROOT_DIR } = grabDirNames();
|
||||
|
||||
let error_logged = false;
|
||||
|
||||
const tailwindEsbuildPlugin: esbuild.Plugin = {
|
||||
name: "tailwindcss",
|
||||
setup(build) {
|
||||
build.onLoad({ filter: /\.css$/ }, async (args) => {
|
||||
const source = await readFile(args.path, "utf-8");
|
||||
const result = await postcss([tailwindcss()]).process(source, {
|
||||
from: args.path,
|
||||
});
|
||||
try {
|
||||
const source = await readFile(args.path, "utf-8");
|
||||
const result = await postcss([tailwindcss()]).process(source, {
|
||||
from: args.path,
|
||||
});
|
||||
|
||||
error_logged = false;
|
||||
|
||||
return { contents: result.css, loader: "css" };
|
||||
} catch (error: any) {
|
||||
return { errors: [{ text: error.message }] };
|
||||
}
|
||||
});
|
||||
|
||||
build.onResolve({ filter: /\.css$/ }, async (args) => {
|
||||
const css_path = path.resolve(
|
||||
args.resolveDir,
|
||||
args.path.replace(/\@\//g, ROOT_DIR + "/"),
|
||||
);
|
||||
|
||||
const does_path_exist = existsSync(css_path);
|
||||
|
||||
if (!does_path_exist && !error_logged) {
|
||||
const err_msg = `CSS Error: ${css_path} file does not exist.`;
|
||||
|
||||
log.error(err_msg);
|
||||
error_logged = true;
|
||||
|
||||
// return {
|
||||
// errors: [
|
||||
// {
|
||||
// text: err_msg,
|
||||
// },
|
||||
// ],
|
||||
// pluginName: "tailwindcss",
|
||||
// };
|
||||
}
|
||||
|
||||
return {
|
||||
contents: result.css,
|
||||
loader: "css",
|
||||
path: css_path,
|
||||
};
|
||||
});
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user