Skip to content

Commit

Permalink
Run mapper with sexp parsing and remove esbuild plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
davesnx committed Dec 12, 2024
1 parent 83a6f20 commit cb5fda7
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 668 deletions.
72 changes: 4 additions & 68 deletions demo/client/build.mjs
Original file line number Diff line number Diff line change
@@ -1,68 +1,4 @@
// @ts-check
import esbuild from 'esbuild';
import Fs from 'fs/promises';
import Path from 'path';
import * as STier from "s-tier";
import * as Melange from "melange-ffi";

/** @type {Melange.Result<STier.Sexp, string>} */
let deserialized = STier.deserialize(
`(
(app/demo/universal/native/lib/Counter.re (app/demo/universal/js/Counter.js))
(app/demo/universal/native/lib/Note_editor.re (app/demo/universal/js/Note_editor.js))
(app/demo/universal/native/lib/Promise_renderer.re (app/demo/universal/js/Promise_renderer.js))
)`
);

let data /* @type {STier.Sexp} */ = Melange.Result.unwrap(deserialized)
const b = Melange.List.toArray(data._0);
const sexpManifest = b.map(item => [item._0.hd._0, item._0.tl.hd._0.hd._0]);

const melangeManifest = sexpManifest.reduce((acc, [re, js]) => {
acc[re] = `./${js}`;
return acc;
}, {});

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) {
const bundleContents = new Map();

// Store the contents of each output file
build.onEnd(async (result) => {
if (result.errors.length > 0) return;

const outputDir = build.initialOptions.outdir || Path.dirname(build.initialOptions.outfile);

const outputs = result.outputFiles || [];
for (const file of outputs) {
const relativePath = Path.relative(outputDir, file.path);
/* console.log(JSON.stringify(Object.keys(file))); */
console.log(" ", relativePath);
bundleContents.set(relativePath, file.hash);
Fs.writeFile(Path.join(outputDir, relativePath), file.text);
}

await writeRegisterFile(outputDir, melangeManifest);
});
}
};

async function build(input, output) {
let outdir = undefined;
Expand All @@ -87,12 +23,12 @@ async function build(input, output) {
logLevel: 'error',
outdir: outdir,
outfile: outfile,
plugins: [bootstrapPlugin],
write: false, // Need this to get outputFiles in onEnd
metafile: true, // Generate metadata about the build
plugins: [],
write: true,
metafile: true,
});

console.log('\nBuild completed successfully');
console.log('Build completed successfully for "' + input + '"');
return result;
} catch (error) {
console.error('\nBuild failed:', error);
Expand Down
7 changes: 4 additions & 3 deletions demo/client/dune
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@

(rule
(alias client)
(target bootstrap.js)
(deps
(alias_rec melange-app)
(:input runtime-with-client.jsx)
(:script build.mjs)
(:mapper
%{workspace_root}/packages/melange-file-mapper/melange_file_mapper.exe)
(:script build.mjs))
%{workspace_root}/packages/melange-file-mapper/client_components_mapper.exe))
(action
(progn
; (run %{mapper})
(run %{mapper} %{target})
(run node %{script} %{input} "app/demo/client/")))
(enabled_if
(= %{profile} "dev")))
1 change: 0 additions & 1 deletion demo/client/runtime-with-client.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const React = require("react");
const ReactDOM = require("react-dom/client");
const ReactServerDOM = require("react-server-dom-webpack/client");

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

class ErrorBoundary extends React.Component {
Expand Down
41 changes: 1 addition & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
"license": "ISC",
"dependencies": {
"esbuild": "^0.21.4",
"melange-ffi": "github:dmmulroy/melange-ffi",
"react": "^19.0.0-rc-69d4b800-20241021",
"react-dom": "^19.0.0-rc-69d4b800-20241021",
"react-server-dom-webpack": "^19.0.0-rc-69d4b800-20241021",
"s-tier": "^1.0.1"
"react-server-dom-webpack": "^19.0.0-rc-69d4b800-20241021"
}
}
Loading

0 comments on commit cb5fda7

Please sign in to comment.