diff --git a/package.json b/package.json index 8682ff3..bb6223b 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "semantic-release": "19.0.2", "typescript": "4.6.2", "vite": "2.8.6", + "vite-plugin-logseq": "^1.0.0", "vite-plugin-windicss": "1.8.3", "windicss": "3.5.1" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4c1f75a..b2c6026 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -22,6 +22,7 @@ specifiers: semantic-release: 19.0.2 typescript: 4.6.2 vite: 2.8.6 + vite-plugin-logseq: ^1.0.0 vite-plugin-windicss: 1.8.3 windicss: 3.5.1 @@ -49,6 +50,7 @@ devDependencies: semantic-release: 19.0.2 typescript: 4.6.2 vite: 2.8.6 + vite-plugin-logseq: 1.0.0 vite-plugin-windicss: 1.8.3_vite@2.8.6 windicss: 3.5.1 @@ -2687,6 +2689,13 @@ packages: sourcemap-codec: 1.4.8 dev: true + /magic-string/0.26.1: + resolution: {integrity: sha512-ndThHmvgtieXe8J/VGPjG+Apu7v7ItcD5mhEIvOscWjPF/ccOiLxHaSuCAS2G+3x4GKsAbT8u7zdyamupui8Tg==} + engines: {node: '>=12'} + dependencies: + sourcemap-codec: 1.4.8 + dev: true + /map-obj/1.0.1: resolution: {integrity: sha1-2TPOuSBdgr3PSIb2dCvcK03qFG0=} engines: {node: '>=0.10.0'} @@ -4059,6 +4068,12 @@ packages: spdx-expression-parse: 3.0.1 dev: true + /vite-plugin-logseq/1.0.0: + resolution: {integrity: sha512-dqoNsmx4nunhJGwZVyShTzDWaR723fJ0cg4MOYAyewGRhI8ttQnhQagc9Ro17zQVPEQGclG9jBqcRsQb1/hCAQ==} + dependencies: + magic-string: 0.26.1 + dev: true + /vite-plugin-windicss/1.8.3_vite@2.8.6: resolution: {integrity: sha512-RIw2GD6H6cKNE8wZXVOBs4L1uTicVS0FaAkeqXvy1oyuXLC4SXmvnzEuoK0+qFuWJjW0ECNwE8eU+ZZhzNQKUg==} peerDependencies: diff --git a/src/main.tsx b/src/main.tsx index 1ce08c1..96cdfc8 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -57,16 +57,4 @@ function main() { }); } -if (isDev && import.meta.hot) { - const maybePlugin = top?.LSPluginCore.registeredPlugins.get(pluginId); - import.meta.hot.accept(() => {}); - import.meta.hot.dispose(() => { - top?.LSPluginCore.reload(pluginId); - console.log(`✨Plugin ${pluginId} reloaded ✨`); - }); - if (!maybePlugin?.loaded) { - logseq.ready(main).catch(console.error); - } -} else { - logseq.ready(main).catch(console.error); -} +logseq.ready(main).catch(console.error); diff --git a/vite.config.ts b/vite.config.ts index cb822f0..0d23c62 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,82 +1,12 @@ import { defineConfig, Plugin, ResolvedConfig } from "vite"; import reactPlugin from "@vitejs/plugin-react"; import WindiCSS from "vite-plugin-windicss"; -import { writeFile, mkdir } from "fs/promises"; - -import path from "path"; - -// TODO: maybe publish this Vite plugin? -const logseqDevPlugin: () => Plugin = () => { - let config: ResolvedConfig; - - const delay = (ms: number) => - new Promise((resolve) => setTimeout(resolve, ms)); - - const tapHtml = async (config: ResolvedConfig) => { - await delay(1000); - const { default: fetch } = await import("node-fetch"); - return await ( - await fetch(`http://${config.server.host}:${config.server.port}`) - ).text(); - }; - - return { - name: "vite:logseq-dev-plugin", - enforce: "post", - apply: "serve", - config: async (config) => { - if ( - !config.server.host || - !config.server.port || - !config.server.strictPort - ) { - throw new Error( - "logseq-dev-plugin requires server.host, server.port, and server.strictPort to be set" - ); - } - config.base = `http://${config.server.host}:${config.server.port}`; - return config; - }, - configResolved(resolvedConfig) { - // store the resolved config - config = resolvedConfig; - }, - - buildStart: async () => { - tapHtml(config).then(async (html) => { - const baseHref = `http://${config.server.host}:${config.server.port}`; - const baseString = ``; - - const htmlWithBase = html.replace(``, `${baseString}`); - - await mkdir(config.build.outDir, { recursive: true }); - await writeFile( - path.resolve(config.build.outDir, "index.html"), - htmlWithBase, - { - encoding: "utf-8", - } - ); - console.info("Wrote development index.html"); - }); - }, - }; -}; +import logseqDevPlugin from "vite-plugin-logseq"; // https://vitejs.dev/config/ export default defineConfig({ plugins: [logseqDevPlugin(), reactPlugin(), WindiCSS()], - clearScreen: false, // Makes HMR available for development - server: { - cors: true, - host: "localhost", - hmr: { - host: "localhost", - }, - port: 4567, - strictPort: true, - }, build: { target: "esnext", minify: "esbuild",