Skip to content

Commit

Permalink
feat: use vite-plugin-logseq
Browse files Browse the repository at this point in the history
  • Loading branch information
pengx17 committed Mar 24, 2022
1 parent 175394e commit 54aa154
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 84 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 1 addition & 13 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
72 changes: 1 addition & 71 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -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 = `<base href="${baseHref}">`;

const htmlWithBase = html.replace(`<head>`, `<head>${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",
Expand Down

0 comments on commit 54aa154

Please sign in to comment.