Skip to content

Commit

Permalink
Generate the boostrap file
Browse files Browse the repository at this point in the history
  • Loading branch information
davesnx committed Dec 12, 2024
1 parent 7893d00 commit 6b453ec
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 40 deletions.
39 changes: 23 additions & 16 deletions demo/client/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,28 @@ import esbuild from 'esbuild';
import Fs from 'fs/promises';
import Path from 'path';

// Plugin to generate bootstrap.js with bundled content as strings
const melangeManifest = {
"Counter": "./app/demo/universal/js/Counter.js",
"Note_editor": "./app/demo/universal/js/Note_editor.js",
"Promise_renderer": "./app/demo/universal/js/Promise_renderer.js",
};

let writeRegisterFile = async (outputDir, melangeManifest) => {
const registerClientComponents = Object.entries(melangeManifest).map(([name, path]) => `register("${name}", React.lazy(() => import("${path}")))`).join(';\n');
let content = `// Generated by esbuild plugin
window.__client_manifest_map = window.__client_manifest_map || {};
const register = (name, render) => {
window.__client_manifest_map[name] = render;
};
${registerClientComponents}
`;

await Fs.writeFile(Path.join(outputDir, 'bootstrap.js'), content);
}

const bootstrapPlugin = {
name: 'bootstrap',
setup(build) {
Expand All @@ -23,21 +44,7 @@ const bootstrapPlugin = {
Fs.writeFile(Path.join(outputDir, relativePath), file.text);
}

// Generate bootstrap.js
const bootstrapContent = `
// Generated by esbuild bootstrap plugin
export const bundledFiles = {
${Array.from(bundleContents.entries())
.map(([filepath, content]) => ` "${filepath}": ${JSON.stringify(content)}`)
.join(',\n')}
};
`;

// Write bootstrap.js to the output directory
await Fs.writeFile(
Path.join(outputDir, 'bootstrap.js'),
bootstrapContent
);
await writeRegisterFile(outputDir, melangeManifest);
});
}
};
Expand Down
26 changes: 2 additions & 24 deletions demo/client/runtime-with-client.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,8 @@ const React = require("react");
const ReactDOM = require("react-dom/client");
const ReactServerDOM = require("react-server-dom-webpack/client");

/* bootstrap.js */

window.__client_manifest_map = {};

const register = (name, render) => {
window.__client_manifest_map[name] = render;
};

register(
"Counter",
React.lazy(() => import("./app/demo/universal/js/Counter.js"))
);

register(
"Note_editor",
React.lazy(() => import("./app/demo/universal/js/Note_editor.js")),
);

/* register(
"Promise_renderer",
React.lazy(() => import("./app/demo/universal/js/Promise_renderer.js")),
); */

/* end bootstrap.js */
/* This file is generated by the esbuild plugin the _build folder */
require("./bootstrap.js");

class ErrorBoundary extends React.Component {
constructor(props) {
Expand Down

0 comments on commit 6b453ec

Please sign in to comment.