From 1efab4aa884f5fa5d313c8ba1fe31dc7a3029630 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Sat, 26 Mar 2022 05:09:19 -0500 Subject: [PATCH] feat: add spa integration --- packages/integrations/spa/client.js | 9 ++++++ packages/integrations/spa/package.json | 37 +++++++++++++++++++++++++ packages/integrations/spa/src/index.ts | 15 ++++++++++ packages/integrations/spa/tsconfig.json | 10 +++++++ 4 files changed, 71 insertions(+) create mode 100644 packages/integrations/spa/client.js create mode 100644 packages/integrations/spa/package.json create mode 100644 packages/integrations/spa/src/index.ts create mode 100644 packages/integrations/spa/tsconfig.json diff --git a/packages/integrations/spa/client.js b/packages/integrations/spa/client.js new file mode 100644 index 000000000..2fd1e99d8 --- /dev/null +++ b/packages/integrations/spa/client.js @@ -0,0 +1,9 @@ +import listen from 'micromorph/spa' + +export default () => { + listen({ + afterDiff() { + window.dispatchEvent(new CustomEvent('astro:locationchange')) + } + }); +} diff --git a/packages/integrations/spa/package.json b/packages/integrations/spa/package.json new file mode 100644 index 000000000..084ed3a15 --- /dev/null +++ b/packages/integrations/spa/package.json @@ -0,0 +1,37 @@ +{ + "name": "@astrojs/spa", + "description": "SPA Astro Integrations", + "version": "0.0.2", + "type": "module", + "types": "./dist/index.d.ts", + "author": "withastro", + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/withastro/astro.git", + "directory": "packages/integrations/turbolinks" + }, + "keywords": [ + "astro-component", + "performance" + ], + "bugs": "https://github.com/withastro/astro/issues", + "homepage": "https://astro.build", + "exports": { + ".": "./dist/index.js", + "./client.js": "./client.js", + "./package.json": "./package.json" + }, + "scripts": { + "build": "astro-scripts build \"src/**/*.ts\" && tsc", + "build:ci": "astro-scripts build \"src/**/*.ts\"", + "dev": "astro-scripts dev \"src/**/*.ts\"" + }, + "dependencies": { + "micromorph": "^0.1.2" + }, + "devDependencies": { + "astro": "workspace:*", + "astro-scripts": "workspace:*" + } +} diff --git a/packages/integrations/spa/src/index.ts b/packages/integrations/spa/src/index.ts new file mode 100644 index 000000000..e3013edf0 --- /dev/null +++ b/packages/integrations/spa/src/index.ts @@ -0,0 +1,15 @@ +import type { AstroIntegration } from 'astro'; + +export default function createPlugin(): AstroIntegration { + return { + name: '@astrojs/spa', + hooks: { + 'astro:config:setup': ({ injectScript }) => { + // This gets injected into the user's page, so we need to re-export Turbolinks + // from our own package so that package managers like pnpm don't get mad and + // can follow the import correctly. + injectScript('page', `import listen from "@astrojs/spa/client.js"; listen();`); + }, + }, + }; +} diff --git a/packages/integrations/spa/tsconfig.json b/packages/integrations/spa/tsconfig.json new file mode 100644 index 000000000..44baf375c --- /dev/null +++ b/packages/integrations/spa/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../../tsconfig.base.json", + "include": ["src"], + "compilerOptions": { + "allowJs": true, + "module": "ES2020", + "outDir": "./dist", + "target": "ES2020" + } +}