Skip to content

Commit

Permalink
Fix Embedded Page
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrozadotdev committed Nov 9, 2023
1 parent 792371e commit 285138b
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 21 deletions.
69 changes: 69 additions & 0 deletions client/packages/openblocks/embedded.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>PocketBlocks Embedded</title>
<script type="module" crossorigin src="/js/proxy.js"></script>
<style>
.demo-title {
text-align: center;
}
.demo {
overflow: auto;
padding: 10px;
}
.ops {
padding: 10px;
}
.ops button {
margin-right: 10px;
}
</style>
</head>
<body>
<h1 class="demo-title">PocketBlocks Embedded</h1>
<div id="app" class="demo"></div>
<script type="module" src="/src/embedded.ts"></script>
<script>
window.onload = async function () {
const url = new URL(location.href);
const appId = url.searchParams.get("appId");
const baseUrl = url.searchParams.get("baseUrl") || location.origin;
if (!appId) {
return;
}
const instance = await $pbl.bootstrapAppAt(
appId,
document.querySelector("#app"),
{
baseUrl,
moduleInputs: { userName: "Lucy" },
}
);

instance?.on("moduleOutputChange", (output) => {
console.info("output change:", output);
});

instance?.on("moduleEventTriggered", (eventName) => {
console.info("event triggered:", eventName);
});

document.querySelector("#app-ops")?.addEventListener("click", (e) => {
const target = e.target;
const key = target.dataset.key;
if (key === "setModuleInputs") {
instance?.setModuleInputs({
userName: "Tom",
});
}
if (key === "invokeMethod") {
instance?.invokeMethod("setSlider");
}
});
};
</script>
</body>
</html>
1 change: 1 addition & 0 deletions client/packages/openblocks/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
content="A developer-friendly open-source low code platform to build internal apps within minutes."
/>
<meta name="theme-color" content="#000000" />
<script type="module" crossorigin src="/js/proxy.js"></script>
<style>
html,
body {
Expand Down
1 change: 1 addition & 0 deletions client/packages/openblocks/public/js/proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//Just a file to trick Vite Build
10 changes: 10 additions & 0 deletions client/packages/openblocks/src/embedded.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as sdk from "./index.sdk";

// @ts-nocheck
import "./index.less";
import "virtual:globals";

// @ts-ignore
window.$openblocks_sdk = sdk;
// @ts-ignore
window.$pbl = sdk;
25 changes: 20 additions & 5 deletions client/packages/openblocks/vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export const viteConfig: UserConfig = {
outDir: "../../../proxy/public",
emptyOutDir: false,
rollupOptions: {
external: ["/js/proxy.js"],
input: {
main: path.resolve(__dirname, "index.html"),
embedded: path.resolve(__dirname, "embedded.html"),
},
output: {
chunkFileNames: "js/[hash].js",
assetFileNames: "assets/[name]-[hash][extname]",
Expand Down Expand Up @@ -118,25 +123,35 @@ export const viteConfig: UserConfig = {
globalDepPlugin(),
createHtmlPlugin({
minify: false,
inject: {
data: {
browserCheckScript: isDev ? "" : `<script src="/js/browser-check.js"></script>`,
proxyScript: isDev ? `<script type="module" crossorigin src="/js/proxy.js"></script>` : `<!-- PROXYSCRIPT -->`,
pages: [
{
filename: "index.html",
template: '/index.html',
injectOptions: {
data: {
browserCheckScript: isDev ? "" : `<script src="/js/browser-check.js"></script>`,
proxyScript: isDev ? `<script type="module" crossorigin src="/js/proxy.js"></script>` : `<!-- PROXYSCRIPT -->`,
},
}
},
},
]
}),
isVisualizerEnabled && visualizer(),
].filter(Boolean),
};

const browserCheckConfig: UserConfig = {
...viteConfig,
plugins: [...(viteConfig.plugins?.slice(0,5) || []), isVisualizerEnabled && visualizer()].filter(Boolean),
define: {
...viteConfig.define,
"process.env.NODE_ENV": JSON.stringify("production"),
},
build: {
...viteConfig.build,
rollupOptions: {
output: viteConfig.build?.rollupOptions?.output
},
manifest: false,
copyPublicDir: false,
emptyOutDir: true,
Expand Down
16 changes: 0 additions & 16 deletions client/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,6 @@ async function buildBuiltinPlugin(name) {
shell.rm(tarballFileName);
}

async function buildSDK() {
console.log();
console.log(chalk.cyan`SDK building...`);

const targetDir = `../proxy/public/sdk`;
shell.mkdir("-p", targetDir);

shell.exec(`yarn workspace openblocks-sdk build`, { fatal: true });

const distFolder = "./packages/openblocks-sdk/dist";

shell.exec(`cp ${distFolder}/* ${targetDir}`, { fatal: true });
}

shell.set("-e");

const start = Date.now();
Expand Down Expand Up @@ -87,7 +73,5 @@ if (process.env.REACT_APP_BUNDLE_BUILTIN_PLUGIN) {
}
}

await buildSDK()

console.log();
console.log(chalk.green`Done! time: ${((Date.now() - start) / 1000).toFixed(2)}s`);

0 comments on commit 285138b

Please sign in to comment.