From 97f31ba0444fcd291cb377834aabc1c90d05a484 Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Thu, 21 Mar 2024 20:43:17 +0100 Subject: [PATCH] inits dev toolbar app --- packages/cloudflare/package.json | 9 +++- .../cloudflare/src/dev-toolbar-app/tabs.astro | 20 +++++++++ .../cloudflare/src/dev-toolbar-app/test.astro | 15 +++++++ .../src/entrypoints/dev-toolbar-app.ts | 42 +++++++++++++++++++ packages/cloudflare/src/index.ts | 22 +++++++++- 5 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 packages/cloudflare/src/dev-toolbar-app/tabs.astro create mode 100644 packages/cloudflare/src/dev-toolbar-app/test.astro create mode 100644 packages/cloudflare/src/entrypoints/dev-toolbar-app.ts diff --git a/packages/cloudflare/package.json b/packages/cloudflare/package.json index c220664c3..7a6972f8e 100644 --- a/packages/cloudflare/package.json +++ b/packages/cloudflare/package.json @@ -20,11 +20,14 @@ "./entrypoints/server.directory.js": "./dist/entrypoints/server.directory.js", "./image-service": "./dist/entrypoints/image-service.js", "./image-endpoint": "./dist/entrypoints/image-endpoint.js", + "./dev-toolbar-app": "./dist/entrypoints/dev-toolbar-app.js", + "./dev-toolbar-app/tabs.astro": "./dist/dev-toolbar-app/tabs.astro", + "./dev-toolbar-app/test.astro": "./dist/dev-toolbar-app/test.astro", "./package.json": "./package.json" }, "files": ["dist"], "scripts": { - "build": "tsc", + "build": "tsc && copyfiles -u 1 src/dev-toolbar-app/*.astro dist/", "test": "astro-scripts test \"test/**/*.test.js\"" }, "dependencies": { @@ -46,7 +49,9 @@ "astro": "^4.5.8", "cheerio": "1.0.0-rc.12", "@astrojs/test-utils": "workspace:*", - "astro-scripts": "workspace:*" + "astro-scripts": "workspace:*", + "rimraf": "^5.0.5", + "copyfiles": "^2.4.1" }, "publishConfig": { "provenance": true diff --git a/packages/cloudflare/src/dev-toolbar-app/tabs.astro b/packages/cloudflare/src/dev-toolbar-app/tabs.astro new file mode 100644 index 000000000..dcea1de11 --- /dev/null +++ b/packages/cloudflare/src/dev-toolbar-app/tabs.astro @@ -0,0 +1,20 @@ +--- +export const partial = true; +--- + +
+ + +
+ + diff --git a/packages/cloudflare/src/dev-toolbar-app/test.astro b/packages/cloudflare/src/dev-toolbar-app/test.astro new file mode 100644 index 000000000..63a3f1080 --- /dev/null +++ b/packages/cloudflare/src/dev-toolbar-app/test.astro @@ -0,0 +1,15 @@ +--- +export const partial = true; + +const { env } = Astro.locals.runtime; +--- + +
+  {JSON.stringify(env, null, 2)}
+
+ + diff --git a/packages/cloudflare/src/entrypoints/dev-toolbar-app.ts b/packages/cloudflare/src/entrypoints/dev-toolbar-app.ts new file mode 100644 index 000000000..a62271576 --- /dev/null +++ b/packages/cloudflare/src/entrypoints/dev-toolbar-app.ts @@ -0,0 +1,42 @@ +import type { DevToolbarApp } from 'astro'; + +const plugin: DevToolbarApp = { + id: 'cloudflare-app', + name: 'Cloudflare DevToolbarApp', + icon: '', + init(canvas, eventTarget) { + const page = ` + +
+
+ `; + + const pageEl = document.createRange().createContextualFragment(page); + const cfWindow = document.createElement('astro-dev-toolbar-window'); + cfWindow.append(pageEl); + + canvas.appendChild(cfWindow); + + eventTarget.addEventListener('app-toggled', (event) => { + // @ts-ignore + if (event.detail.state === true) { + console.log('The app is now enabled!'); + // @ts-ignore + loadTabs(cfWindow); + } + }); + }, +}; + +export default plugin; diff --git a/packages/cloudflare/src/index.ts b/packages/cloudflare/src/index.ts index 8c7a2ee93..ef9b3b706 100644 --- a/packages/cloudflare/src/index.ts +++ b/packages/cloudflare/src/index.ts @@ -63,7 +63,14 @@ export default function createIntegration(args?: Options): AstroIntegration { return { name: '@astrojs/cloudflare', hooks: { - 'astro:config:setup': ({ command, config, updateConfig, logger }) => { + 'astro:config:setup': ({ + command, + config, + updateConfig, + logger, + addDevToolbarApp, + injectRoute, + }) => { updateConfig({ build: { client: new URL(`.${config.base}/`, config.outDir), @@ -81,6 +88,19 @@ export default function createIntegration(args?: Options): AstroIntegration { }, image: setImageConfig(args?.imageService ?? 'DEFAULT', config.image, command, logger), }); + + // MARK: Dev Toolbar App + if (command === 'dev') { + addDevToolbarApp('@astrojs/cloudflare/dev-toolbar-app'); + injectRoute({ + pattern: '/_dev-toolbar-app/cloudflare/tabs', + entrypoint: '@astrojs/cloudflare/dev-toolbar-app/tabs.astro', + }); + injectRoute({ + pattern: '/_dev-toolbar-app/cloudflare/home', + entrypoint: '@astrojs/cloudflare/dev-toolbar-app/test.astro', + }); + } }, 'astro:config:done': ({ setAdapter, config }) => { _config = config;