Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4 from githubnext/aw/block-builds
Browse files Browse the repository at this point in the history
update production block to use iframe on `blocks` repo
  • Loading branch information
Wattenberger authored Nov 22, 2021
2 parents 09bb8e7 + d167bfd commit 9cbcaac
Show file tree
Hide file tree
Showing 10 changed files with 781 additions and 595 deletions.
3 changes: 1 addition & 2 deletions build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ async function main() {

const blockBuildFuncs = pkg.blocks.map((block) => {
return esbuild.build({
entryPoints: [`./` + block.entry],
entryPoints: [`.` + block.entry],
bundle: true,
outdir: `dist/${block.id}`,
format: "iife",
globalName: "BlockBundle",
minify: true,
platform: "node",
});
});

Expand Down
17 changes: 15 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
{
"name": "custom-block-template",
"version": "0.0.0",
"watch": {
"build-blocks": {
"patterns": [
"src"
],
"extensions": "js,jsx,ts,tsx,css"
}
},
"scripts": {
"dev": "vite --port 4000",
"watch": "npm-watch",
"dev": "concurrently --kill-others \"npm run local\" \"npm run watch\"",
"local": "vite --port 4000",
"build": "tsc && vite build",
"build-blocks": "node ./build.ts",
"serve": "vite preview"
},
"blocks": [
Expand All @@ -29,7 +40,7 @@
}
],
"dependencies": {
"@githubnext/utils": "^0.10.0",
"@githubnext/utils": "^0.11.0",
"@loadable/component": "^5.15.0",
"@octokit/rest": "^18.12.0",
"git-url-parse": "^11.6.0",
Expand All @@ -47,7 +58,9 @@
"@types/react-dom": "^17.0.0",
"@vitejs/plugin-react": "^1.0.0",
"autoprefixer": "^10.4.0",
"concurrently": "^6.4.0",
"esbuild": "^0.13.14",
"npm-watch": "^0.11.0",
"postcss": "^8.3.11",
"tailwindcss": "^2.2.19",
"typescript": "^4.3.2",
Expand Down
1 change: 0 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import "@githubnext/utils/dist/index.css";
import GitUrlParse from "git-url-parse";
import { useEffect, useMemo, useState } from "react";
import { AppInner } from "./components/app-inner";
Expand Down
9 changes: 6 additions & 3 deletions src/blocks/example-file-block/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import {
FileBlockProps,
useTailwindCdn,
// useTailwindCdn,
getLanguageFromFilename,
} from "@githubnext/utils";
import "./index.css";

export default function (props: FileBlockProps) {
const { context, content } = props;
const { context, content, metadata, onUpdateMetadata } = props;
const language = getLanguageFromFilename(context.file);

useTailwindCdn();
// useTailwindCdn();

return (
<div className="p-4 code">
<p className="text-sm">
File: {context.path} {language}
</p>
<div className="py-6">
Metadata example: this button has been clicked <button onClick={() => onUpdateMetadata({ number: (metadata.number || 0) + 1 })}>{metadata.number || 0} times</button>
</div>
<pre className="p-4 text-gray-600">{content}</pre>
</div>
);
Expand Down
1 change: 1 addition & 0 deletions src/components/app-inner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { FileBlock } from "./file-block";
import { FolderBlock } from "./folder-block";

interface Block {
id: string;
type: string;
title: string;
description: string;
Expand Down
14 changes: 5 additions & 9 deletions src/components/file-block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useFileContent } from "../hooks";
import { AppInnerProps } from "./app-inner";
import { ErrorState } from "./error-state";
import { LoadingState } from "./loading-state";
import { SandboxedBlock } from "@githubnext/utils";
import { ProductionBlock } from "./production-block"
import { LocalBlock } from "./local-block";

export function FileBlock(props: Omit<AppInnerProps, "onReset" | "blockType">) {
Expand Down Expand Up @@ -40,10 +40,9 @@ export function FileBlock(props: Omit<AppInnerProps, "onReset" | "blockType">) {
console.log(path);
const importType = path.endsWith(".css") ? "inline" : "raw";
const contents = await import(
// /* @vite-ignore */ `../../..${path}?${importType}`
/* @vite-ignore */ `../dist/file-block.js?raw`
/* @vite-ignore */ `../../..${path}?${importType}`
// /* @vite-ignore */ `../dist/file-block.js?raw`
);
console.log(contents);
return contents.default;
}, []);

Expand All @@ -52,19 +51,16 @@ export function FileBlock(props: Omit<AppInnerProps, "onReset" | "blockType">) {
if (status === "success" && data) {
return doMimicProductionEnvironment ? (
<div className="sandbox-wrapper h-full w-full">
<SandboxedBlock
getFileContent={getFileContent}
<ProductionBlock
contents={data.content}
context={{
...data.context,
file: name,
}}
dependencies={{}}
block={block}
metadata={metadata}
session={{ token: "" }}
/>
{/* <SandboxedBlock
{/* <ProductionBlock
getFileContent={getFileContent}
contents={data.content}
context={{
Expand Down
7 changes: 2 additions & 5 deletions src/components/folder-block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useFolderContent } from "../hooks";
import { AppInnerProps } from "./app-inner";
import { ErrorState } from "./error-state";
import { LoadingState } from "./loading-state";
import { SandboxedBlock } from "@githubnext/utils";
import { ProductionBlock } from "./production-block";
import { LocalBlock } from "./local-block";

export function FolderBlock(
Expand Down Expand Up @@ -43,17 +43,14 @@ export function FolderBlock(
if (status === "success" && data) {
return doMimicProductionEnvironment ? (
<div className="sandbox-wrapper h-full w-full">
<SandboxedBlock
getFileContent={getFileContent}
<ProductionBlock
tree={data.tree}
context={{
...data.context,
folder: name,
}}
dependencies={{}}
block={block}
metadata={metadata}
session={{ token: "" }}
/>
</div>
) : (
Expand Down
2 changes: 1 addition & 1 deletion src/components/local-block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const LocalBlock = (props: LocalBlockProps) => {
const getContents = async () => {
const content = await loadable(() => import(
`../../..${block.entry}`
).then(module => module.Block))
))
setBlock(content)
}
useEffect(() => { getContents() }, [block.entry])
Expand Down
69 changes: 69 additions & 0 deletions src/components/production-block.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { useEffect, useRef, useState } from "react";
// @ts-ignore
import loadable from "@loadable/component";
import { FileContext, FolderContext, RepoFiles } from "@githubnext/utils";

interface ProductionBlockProps {
block: {
id: string;
type: string;
title: string;
description: string;
entry: string;
extensions?: string[];
};
contents?: string;
tree?: RepoFiles;
metadata?: any;
context: FileContext | FolderContext
}
export const ProductionBlock = (props: ProductionBlockProps) => {
const {
block,
contents,
tree,
metadata = {},
context,
} = props;

const [bundleCode, setBundleCode] = useState("");
const [iframeIsLoaded, setIframeIsLoaded] = useState(false);
const iframeElement = useRef<HTMLIFrameElement>(null);

const getContents = async () => {
const content = await import(
`../../../dist/${block.id}/index.js?raw`
).then((m) => m.default);
setBundleCode(content)
}
useEffect(() => { getContents() }, [block.entry])

const onUpdateMetadata = (newMetadata: any) => {
window.postMessage({
type: "update-metadata",
context,
metadata: newMetadata,
}, "*")
}

useEffect(() => {
if (!bundleCode || !iframeIsLoaded) return
const iframeWindow = iframeElement.current?.contentWindow
if (!iframeWindow) return

iframeWindow.postMessage({
type: "block-props",
block, contents, tree, metadata, context, bundleCode
}, "*")
}, [bundleCode, contents, tree, metadata, context, iframeIsLoaded])

if (!bundleCode) return null
const url = "http://localhost:3000/block-testing"

return (
<iframe className="w-full h-full border-none" src={url} title="Block testing" onLoad={() => setIframeIsLoaded(true)} ref={iframeElement}

/>
)

}
Loading

0 comments on commit 9cbcaac

Please sign in to comment.