Skip to content

Commit 3db80d9

Browse files
committed
add code splitting and lazy imports
1 parent 0e789d9 commit 3db80d9

File tree

4 files changed

+82
-55
lines changed

4 files changed

+82
-55
lines changed

src/App.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import Editor from "./components/Editor";
21
import { Navbar } from "./components/Navbar";
32
import { ThemeProvider } from "./components/theme-provider";
43
import { Toaster } from "@/components/ui/sonner";
4+
import { lazy } from "react";
5+
6+
const Editor = lazy(() => import("./components/Editor"));
57

68
function App() {
9+
710
return (
811
<ThemeProvider defaultTheme="dark" storageKey="strophe-theme">
912
<div className="font-mono">

src/components/Editor.tsx

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,13 @@
11
import { EditorContent, useEditor } from "@tiptap/react";
2-
import StarterKit from "@tiptap/starter-kit";
3-
import Placeholder from "@tiptap/extension-placeholder";
4-
import Link from "@tiptap/extension-link";
5-
import { createLowlight, common } from "lowlight";
6-
import CodeBlockLowlight from "@tiptap/extension-code-block-lowlight";
72
import { useAtom } from "jotai";
83
import { $storage, contentAtom } from "@/lib/stores";
9-
import TaskList from "@tiptap/extension-task-list";
10-
import TaskItem from "@tiptap/extension-task-item";
11-
import Image from "@tiptap/extension-image";
12-
import { nodePasteRule, type PasteRuleFinder } from "@tiptap/core";
13-
import EditorMenu from "./EditorMenu";
14-
import { useEffect } from "react";
15-
import { toast } from "sonner";
164

17-
const getExtensions = () => {
18-
const ImageFinder: PasteRuleFinder = /data:image\//g;
5+
import { lazy, useEffect } from "react";
6+
import { toast } from "sonner"
7+
import { loadExtensions } from "@/lib/extensions";
198

20-
const ImageExtended = Image.extend({
21-
name: "ImageExtended",
22-
addPasteRules() {
23-
return [
24-
nodePasteRule({
25-
find: ImageFinder,
26-
type: this.type,
27-
getAttributes(match) {
28-
if (match.input) {
29-
return {
30-
src: match.input,
31-
};
32-
}
33-
},
34-
}),
35-
];
36-
},
37-
});
38-
39-
const lowlight = createLowlight(common);
409

41-
// define your extension array
42-
const extensions = [
43-
StarterKit.configure({
44-
codeBlock: false,
45-
}),
46-
CodeBlockLowlight.configure({
47-
lowlight,
48-
}),
49-
Placeholder.configure({ placeholder: "Start typing..." }),
50-
Link.configure({ openOnClick: true, autolink: true }),
51-
TaskList,
52-
TaskItem.configure({
53-
nested: true,
54-
}),
55-
ImageExtended,
56-
];
57-
58-
return extensions;
59-
};
10+
const EditorMenu = lazy(() => import("./EditorMenu"));
6011

6112
const Editor = () => {
6213
const [content, setContent] = useAtom(contentAtom);
@@ -71,7 +22,7 @@ const Editor = () => {
7122
};
7223

7324
const editor = useEditor({
74-
extensions: getExtensions(),
25+
extensions: loadExtensions(),
7526
content: safeParse(content),
7627
onUpdate: ({ editor }) => {
7728
setContent(JSON.stringify(editor.getJSON()));

src/lib/extensions.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import StarterKit from "@tiptap/starter-kit";
2+
import Placeholder from "@tiptap/extension-placeholder";
3+
import Link from "@tiptap/extension-link";
4+
import { createLowlight, common } from "lowlight";
5+
import CodeBlockLowlight from "@tiptap/extension-code-block-lowlight";
6+
import TaskList from "@tiptap/extension-task-list";
7+
import TaskItem from "@tiptap/extension-task-item";
8+
import Image from "@tiptap/extension-image";
9+
import { nodePasteRule, type PasteRuleFinder } from "@tiptap/core";
10+
11+
export const loadExtensions = () => {
12+
const ImageFinder: PasteRuleFinder = /data:image\//g;
13+
14+
const ImageExtended = Image.extend({
15+
name: "ImageExtended",
16+
addPasteRules() {
17+
return [
18+
nodePasteRule({
19+
find: ImageFinder,
20+
type: this.type,
21+
getAttributes(match) {
22+
if (match.input) {
23+
return {
24+
src: match.input,
25+
};
26+
}
27+
},
28+
}),
29+
];
30+
},
31+
});
32+
33+
const lowlight = createLowlight(common);
34+
35+
// define your extension array
36+
const extensions = [
37+
StarterKit.configure({
38+
codeBlock: false,
39+
}),
40+
CodeBlockLowlight.configure({
41+
lowlight,
42+
}),
43+
Placeholder.configure({ placeholder: "Start typing..." }),
44+
Link.configure({ openOnClick: true, autolink: true }),
45+
TaskList,
46+
TaskItem.configure({
47+
nested: true,
48+
}),
49+
ImageExtended,
50+
];
51+
52+
return extensions;
53+
};

vite.config.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@ import { viteStaticCopy } from 'vite-plugin-static-copy';
55

66
// https://vite.dev/config/
77
export default defineConfig({
8+
build: {
9+
rollupOptions: {
10+
output: {
11+
manualChunks: (id) => {
12+
if (id.includes("react")) {
13+
return "react";
14+
}
15+
if (id.includes("tiptap")) {
16+
return "tiptap";
17+
}
18+
if (id.includes("highlight.js")) {
19+
return "highlight";
20+
}
21+
if (id.includes("prosemirror")) {
22+
return "prosemirror";
23+
}
24+
}
25+
}
26+
}
27+
},
828
plugins: [
929
react(),
1030
viteStaticCopy({

0 commit comments

Comments
 (0)