From 5504e0f892df72e40a984094f914bb9468e17503 Mon Sep 17 00:00:00 2001 From: Rusty Key Date: Wed, 4 Sep 2024 18:20:19 +0200 Subject: [PATCH 1/3] add support for multiple entries to esbuild --- commands/reshowcase | 68 ++++++++++---- example/Buttons/Huge.re | 29 ++++++ example/Buttons/Normal.re | 29 ++++++ example/Demo.re | 180 -------------------------------------- example/dune | 12 +++ src/ReshowcaseUi.re | 16 ++-- 6 files changed, 128 insertions(+), 206 deletions(-) create mode 100644 example/Buttons/Huge.re create mode 100644 example/Buttons/Normal.re delete mode 100644 example/Demo.re diff --git a/commands/reshowcase b/commands/reshowcase index 331af47..50f6295 100755 --- a/commands/reshowcase +++ b/commands/reshowcase @@ -107,10 +107,32 @@ const watchPlugin = { }; // entryPoint passed to htmlPlugin must be relative to the current working directory -const entryPathRelativeToCwd = path.relative(process.cwd(), entryPath); +const demoEntry = path.relative(process.cwd(), "/Users/rusty/code/tools/reshowcase/_build/default/example/example/example/Demo.js"); + +function readJSFilesRecursively(dir, fileList = []) { + const files = fs.readdirSync(dir); + + files.forEach(file => { + const filePath = path.join(dir, file); + const fileName = path.basename(filePath); + const stat = fs.statSync(filePath); + + if (stat.isDirectory() && !fileName.startsWith(".")) { + readJSFilesRecursively(filePath, fileList); + } else if (filePath.endsWith('.js') && path.normalize(demoEntry) != path.normalize(filePath)) { + fileList.push(filePath); + } + }); + + return fileList; +}; + +const startingDir = path.dirname(demoEntry); + +const storiesEntries = readJSFilesRecursively(startingDir); const defaultConfig = { - entryPoints: [entryPath], + entryPoints: [demoEntry, ...storiesEntries], entryNames: "[name]-[hash]", assetNames: "[name]-[hash]", chunkNames: "[name]-[hash]", @@ -148,25 +170,35 @@ const defaultConfig = { files: [ { filename: "index.html", - entryPoints: [entryPathRelativeToCwd], + entryPoints: [demoEntry], htmlTemplate: path.join(__dirname, "./ui-template.html"), scriptLoading: "module", }, - { - filename: "./demo/index.html", - entryPoints: [entryPathRelativeToCwd], - htmlTemplate: process.argv.find((item) => - item.startsWith("--template=") - ) - ? path.join( - process.cwd(), - process.argv - .find((item) => item.startsWith("--template=")) - .replace(/--template=/, "") - ) - : path.join(__dirname, "./demo-template.html"), - scriptLoading: "module", - }, + ...(storiesEntries.map(storyEntry => { + const relativePath = path.relative(path.dirname(demoEntry), storyEntry).replace(/\.js$/, ""); + console.log(relativePath); + return { + filename: `${relativePath}/index.html`, + entryPoints: [storyEntry], + htmlTemplate: path.join(__dirname, "./ui-template.html"), + scriptLoading: "module", + } + })) + // { + // filename: "./demo/index.html", + // entryPoints: [entryPathRelativeToCwd], + // htmlTemplate: process.argv.find((item) => + // item.startsWith("--template=") + // ) + // ? path.join( + // process.cwd(), + // process.argv + // .find((item) => item.startsWith("--template=")) + // .replace(/--template=/, "") + // ) + // : path.join(__dirname, "./demo-template.html"), + // scriptLoading: "module", + // }, ], }), ...(isBuild ? [] : [watchPlugin]), diff --git a/example/Buttons/Huge.re b/example/Buttons/Huge.re new file mode 100644 index 0000000..fbc0d92 --- /dev/null +++ b/example/Buttons/Huge.re @@ -0,0 +1,29 @@ +module Component = { + [@react.component] + let make = () => { + ; + }; +}; + +switch (ReactDOM.querySelector("body")) { +| Some(node) => + let root = ReactDOM.Client.createRoot(node); + ReactDOM.Client.render(root, ); +| None => + Js.Console.error( + "RR.renderToElementWithId : no element of id '" + ++ "body" + ++ "' found in the HTML.", + ) +}; diff --git a/example/Buttons/Normal.re b/example/Buttons/Normal.re new file mode 100644 index 0000000..06a143e --- /dev/null +++ b/example/Buttons/Normal.re @@ -0,0 +1,29 @@ +module Component = { + [@react.component] + let make = () => { + ; + }; +}; + +switch (ReactDOM.querySelector("body")) { +| Some(node) => + let root = ReactDOM.Client.createRoot(node); + ReactDOM.Client.render(root, ); +| None => + Js.Console.error( + "RR.renderToElementWithId : no element of id '" + ++ "body" + ++ "' found in the HTML.", + ) +}; diff --git a/example/Demo.re b/example/Demo.re deleted file mode 100644 index 877d3da..0000000 --- a/example/Demo.re +++ /dev/null @@ -1,180 +0,0 @@ -open Reshowcase.Entry; - -module Cn = { - let ifTrue = (cn, x) => x ? cn : ""; -}; - -let spaceConcat = (x1, x2) => - switch (x1, x2) { - | ("", x) - | (x, "") => x - | (x1, x2) => x1 ++ " " ++ x2 - }; - -let (+++) = spaceConcat; - -module Css = { - let button = [%cx - {| - color: #fff; - border: none; - padding: 10px; - border-radius: 10px; - font-family: inherit; - font-size: inherit; - |} - ]; - - let buttonHuge = [%cx {| - padding: 20px; - font-size: 30px; - |}]; - - let buttonDisabled = [%cx {| - cursor: default; - opacity: 0.5; - |}]; - - let buttonColor = color => { - let color = `hex(color); - [%cx {| - background-color: $(color); - |}]; - }; - - let h1Size = size => { - let fontSize = `px(size); - [%cx {| - font-size: $(fontSize); - |}]; - }; - - let code = [%cx - {| - white-space: pre; - padding: 0; - background-color: #f5f6f6; - |} - ]; -}; - -demo(({addDemo: _, addCategory}) => - addCategory("Buttons", ({addDemo, addCategory: _}) => { - addDemo("Normal", ({string, bool, _}) => { - let disabled = bool("Disabled", false); - let color = - string( - "Color", - ~options=[| - ("Red", "E02020"), - ("Green", "6DD400"), - ("Blue", "0091FF"), - |], - "0091FF", - ); - ; - }); - addDemo("Huge", ({string, bool, _}) => { - let disabled = bool("Disabled", false); - let color = - string( - "Color", - ~options=[| - ("Red", "E02020"), - ("Green", "6DD400"), - ("Blue", "0091FF"), - |], - "0091FF", - ); - ; - }); - }) -); - -demo(({addDemo: _, addCategory}) => - addCategory("Typography", ({addDemo: _, addCategory}) => { - addCategory("Headings", ({addDemo, addCategory: _}) => { - addDemo("H1", ({string, int, _}) => { - let size = - int("Font size", {min: 0, max: 100, initial: 30, step: 1}); - -

- {string("Text", "hello")->React.string} -

; - }); - addDemo("H2", ({string, _}) => -

{string("Text", "hello")->React.string}

- ); - }); - addCategory("Text", ({addDemo, addCategory: _}) => { - addDemo("Paragraph", ({string, _}) => -

{string("Text", "hello")->React.string}

- ); - addDemo("Italic", ({string, _}) => - {string("Text", "hello")->React.string} - ); - }); - }) -); - -demo(({addDemo, addCategory: _}) => - addDemo("Code example", _propsApi => - - {js|open Reshowcase.Entry; - -demo(({addDemo: _, addCategory}) => - addCategory("Typography", ({addDemo: _, addCategory}) => { - addCategory("Headings", ({addDemo, addCategory: _}) => { - addDemo("H1", ({string, int, _}) => { - let size = - int("Font size", {min: 0, max: 100, initial: 30, step: 1}); - -

- {string("Text", "hello")->React.string} -

; - }); - addDemo("H2", ({string, _}) => -

{string("Text", "hello")->React.string}

- ); - }); - addCategory("Text", ({addDemo, addCategory: _}) => { - addDemo("Paragraph", ({string, _}) => -

{string("Text", "hello")->React.string}

- ); - addDemo("Italic", ({string, _}) => - {string("Text", "hello")->React.string} - ); - }); - }) -);|js} - ->React.string -
- ) -); - -demo(({addDemo: _, addCategory}) => - addCategory("Test search", ({addDemo, addCategory: _}) => { - addDemo("OneTwoThreeFour", _ => React.null); - addDemo("OneTwoThreeFive", _ => React.null); - addDemo("OneTwoFourSeven", _ => React.null); - }) -); - -start(); diff --git a/example/dune b/example/dune index a2c3fa7..8ecf26d 100644 --- a/example/dune +++ b/example/dune @@ -1,3 +1,10 @@ +(include_subdirs unqualified) + +(env + (_ + (flags + (-w +a-27)))) + (melange.emit (target example) (alias example) @@ -5,3 +12,8 @@ (module_systems es6) (preprocess (pps melange.ppx reason-react-ppx styled-ppx))) + +(rule + (targets Demo.re) + (action + (run reshowcase generate))) diff --git a/src/ReshowcaseUi.re b/src/ReshowcaseUi.re index f4fb10d..2fadfb6 100644 --- a/src/ReshowcaseUi.re +++ b/src/ReshowcaseUi.re @@ -924,20 +924,20 @@ module DemoUnitFrame = { ]; [@react.component] - let make = - (~queryString: string, ~responsiveMode, ~onLoad: Js.t('a) => unit) => { - let iframePath = if (useFullframeUrl) {"demo/index.html"} else {"demo"}; + let make = (~demoName: string, ~responsiveMode, ~onLoad: Js.t('a) => unit) => { + let _iframePath = if (useFullframeUrl) {"demo/index.html"} else {"demo"};