Refactor Bundler. Fix bugs.
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
import type { Plugin } from "esbuild";
|
||||
import type { PageFiles } from "../../../types";
|
||||
type Params = {
|
||||
entryToPage: Map<string, PageFiles & {
|
||||
tsx: string;
|
||||
}>;
|
||||
post_build_fn?: (params: {
|
||||
artifacts: any[];
|
||||
}) => Promise<void> | void;
|
||||
};
|
||||
export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn, }: Params): Plugin;
|
||||
export {};
|
||||
@@ -0,0 +1,57 @@
|
||||
import { log } from "../../../utils/log";
|
||||
import grabArtifactsFromBundledResults from "../grab-artifacts-from-bundled-result";
|
||||
let buildStart = 0;
|
||||
let build_starts = 0;
|
||||
const MAX_BUILD_STARTS = 10;
|
||||
export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn, }) {
|
||||
const artifactTracker = {
|
||||
name: "artifact-tracker",
|
||||
setup(build) {
|
||||
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);
|
||||
global.RECOMPILING = false;
|
||||
}
|
||||
});
|
||||
build.onEnd((result) => {
|
||||
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({
|
||||
result,
|
||||
entryToPage,
|
||||
});
|
||||
// console.log("artifacts", artifacts);
|
||||
if (artifacts?.[0] && artifacts.length > 0) {
|
||||
for (let i = 0; i < artifacts.length; i++) {
|
||||
const artifact = artifacts[i];
|
||||
if (artifact?.local_path && global.BUNDLER_CTX_MAP) {
|
||||
global.BUNDLER_CTX_MAP[artifact.local_path] =
|
||||
artifact;
|
||||
}
|
||||
}
|
||||
post_build_fn?.({ artifacts });
|
||||
// writeFileSync(
|
||||
// HYDRATION_DST_DIR_MAP_JSON_FILE,
|
||||
// JSON.stringify(artifacts, null, 4),
|
||||
// );
|
||||
}
|
||||
const elapsed = (performance.now() - buildStart).toFixed(0);
|
||||
log.success(`[Built] in ${elapsed}ms`);
|
||||
global.RECOMPILING = false;
|
||||
build_starts = 0;
|
||||
});
|
||||
},
|
||||
};
|
||||
return artifactTracker;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import type { Plugin } from "esbuild";
|
||||
import type { PageFiles } from "../../../types";
|
||||
type Params = {
|
||||
entryToPage: Map<string, PageFiles & {
|
||||
tsx: string;
|
||||
}>;
|
||||
};
|
||||
export default function virtualFilesPlugin({ entryToPage }: Params): Plugin;
|
||||
export {};
|
||||
@@ -0,0 +1,28 @@
|
||||
import path from "path";
|
||||
import { log } from "../../../utils/log";
|
||||
export default function virtualFilesPlugin({ entryToPage }) {
|
||||
const virtualPlugin = {
|
||||
name: "virtual-hydration",
|
||||
setup(build) {
|
||||
build.onResolve({ filter: /^hydration-virtual:/ }, (args) => {
|
||||
const final_path = args.path.replace(/hydration-virtual:/, "");
|
||||
return {
|
||||
path: final_path,
|
||||
namespace: "hydration-virtual",
|
||||
};
|
||||
});
|
||||
build.onLoad({ filter: /.*/, namespace: "hydration-virtual" }, (args) => {
|
||||
const target = entryToPage.get(args.path);
|
||||
if (!target?.tsx)
|
||||
return null;
|
||||
const contents = target.tsx;
|
||||
return {
|
||||
contents: contents || "",
|
||||
loader: "tsx",
|
||||
resolveDir: path.dirname(target.local_path),
|
||||
};
|
||||
});
|
||||
},
|
||||
};
|
||||
return virtualPlugin;
|
||||
}
|
||||
Reference in New Issue
Block a user