perf: compress and split main entry bundle size
Signed-off-by: Innei <i@innei.in>
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import { IN_ELECTRON } from "@follow/shared/constants"
|
||||
import { env } from "@follow/shared/env"
|
||||
import { useEffect, useLayoutEffect } from "react"
|
||||
import { Outlet } from "react-router-dom"
|
||||
|
||||
@@ -7,16 +6,13 @@ import { queryClient } from "~/lib/query-client"
|
||||
|
||||
import { useAppIsReady } from "./atoms/app"
|
||||
import { useUISettingKey } from "./atoms/settings/ui"
|
||||
import { applyAfterReadyCallbacks } from "./initialize/queue.js"
|
||||
import { applyAfterReadyCallbacks } from "./initialize/queue"
|
||||
import { appLog } from "./lib/log"
|
||||
import { cn, getOS } from "./lib/utils"
|
||||
import { Titlebar } from "./modules/app/Titlebar.js"
|
||||
import { Titlebar } from "./modules/app/Titlebar"
|
||||
import { RootProviders } from "./providers/root-providers"
|
||||
import { handlers } from "./tipc"
|
||||
|
||||
if (import.meta.env.DEV) {
|
||||
console.info("[renderer] env loaded:", env)
|
||||
}
|
||||
function App() {
|
||||
useEffect(() => {
|
||||
const cleanup = handlers?.invalidateQuery.listen((queryKey) => {
|
||||
@@ -97,4 +93,5 @@ const AppSkeleton = () => {
|
||||
</div>
|
||||
)
|
||||
}
|
||||
export default App
|
||||
|
||||
export { App as Component }
|
||||
|
||||
@@ -10,9 +10,12 @@ import { useModalStack } from "~/components/ui/modal"
|
||||
import { FeedForm } from "~/modules/discover/feed-form"
|
||||
import { ListForm } from "~/modules/discover/list-form"
|
||||
import { usePresentUserProfileModal } from "~/modules/profile/hooks"
|
||||
import { useSettingModal } from "~/modules/settings/modal/hooks"
|
||||
|
||||
export const ExtensionExposeProvider = () => {
|
||||
const { present } = useModalStack()
|
||||
const showSettings = useSettingModal()
|
||||
|
||||
useLayoutEffect(() => {
|
||||
registerGlobalContext({
|
||||
showSetting: (path) => window.router.showSettings(path),
|
||||
@@ -28,6 +31,11 @@ export const ExtensionExposeProvider = () => {
|
||||
},
|
||||
})
|
||||
}, [])
|
||||
useEffect(() => {
|
||||
// @ts-expect-error
|
||||
window.router ||= {}
|
||||
window.router.showSettings = showSettings
|
||||
}, [showSettings])
|
||||
|
||||
const { t } = useTranslation()
|
||||
|
||||
|
||||
@@ -4,25 +4,47 @@ import { PersistQueryClientProvider } from "@tanstack/react-query-persist-client
|
||||
import { LazyMotion, MotionConfig } from "framer-motion"
|
||||
import { Provider } from "jotai"
|
||||
import type { FC, PropsWithChildren } from "react"
|
||||
import { lazy, Suspense } from "react"
|
||||
import { HotkeysProvider } from "react-hotkeys-hook"
|
||||
|
||||
import { LottieRenderContainer } from "~/components/ui/lottie-container"
|
||||
import { ModalStackProvider } from "~/components/ui/modal"
|
||||
import { Toaster } from "~/components/ui/sonner"
|
||||
import { HotKeyScopeMap } from "~/constants"
|
||||
import { HotKeyScopeMap } from "~/constants/hotkeys"
|
||||
import { jotaiStore } from "~/lib/jotai"
|
||||
import { persistConfig, queryClient } from "~/lib/query-client"
|
||||
import { FeatureFlagDebugger } from "~/modules/ab/providers"
|
||||
|
||||
import { ContextMenuProvider } from "./context-menu-provider"
|
||||
import { EventProvider } from "./event-provider"
|
||||
import { ExtensionExposeProvider } from "./extension-expose-provider"
|
||||
import { I18nProvider } from "./i18n-provider"
|
||||
import { InvalidateQueryProvider } from "./invalidate-query-provider"
|
||||
import { SettingSync } from "./setting-sync"
|
||||
import { StableRouterProvider } from "./stable-router-provider"
|
||||
import { UserProvider } from "./user-provider"
|
||||
|
||||
const LazyLottieRenderContainer = lazy(() =>
|
||||
import("../components/ui/lottie-container").then((res) => ({
|
||||
default: res.LottieRenderContainer,
|
||||
})),
|
||||
)
|
||||
const LazyContextMenuProvider = lazy(() =>
|
||||
import("./context-menu-provider").then((res) => ({
|
||||
default: res.ContextMenuProvider,
|
||||
})),
|
||||
)
|
||||
const LazyModalStackProvider = lazy(() =>
|
||||
import("../components/ui/modal/stacked/provider").then((res) => ({
|
||||
default: res.ModalStackProvider,
|
||||
})),
|
||||
)
|
||||
|
||||
const LazyExtensionExposeProvider = lazy(() =>
|
||||
import("./extension-expose-provider").then((res) => ({
|
||||
default: res.ExtensionExposeProvider,
|
||||
})),
|
||||
)
|
||||
const LazyFeatureFlagDebugger = lazy(() =>
|
||||
import("../modules/ab/providers").then((res) => ({
|
||||
default: res.FeatureFlagDebugger,
|
||||
})),
|
||||
)
|
||||
const loadFeatures = () => import("../framer-lazy-feature").then((res) => res.default)
|
||||
export const RootProviders: FC<PropsWithChildren> = ({ children }) => (
|
||||
<LazyMotion features={loadFeatures} strict key="framer">
|
||||
@@ -37,15 +59,20 @@ export const RootProviders: FC<PropsWithChildren> = ({ children }) => (
|
||||
<HotkeysProvider initiallyActiveScopes={HotKeyScopeMap.Home}>
|
||||
<Provider store={jotaiStore}>
|
||||
<I18nProvider>
|
||||
<ExtensionExposeProvider />
|
||||
<EventProvider />
|
||||
|
||||
<UserProvider />
|
||||
<SettingSync />
|
||||
<ModalStackProvider />
|
||||
<ContextMenuProvider />
|
||||
<LottieRenderContainer />
|
||||
<Suspense>
|
||||
<LazyExtensionExposeProvider />
|
||||
<LazyModalStackProvider />
|
||||
<LazyContextMenuProvider />
|
||||
<LazyLottieRenderContainer />
|
||||
<LazyFeatureFlagDebugger />
|
||||
</Suspense>
|
||||
|
||||
<StableRouterProvider />
|
||||
<FeatureFlagDebugger />
|
||||
<SettingSync />
|
||||
|
||||
{import.meta.env.DEV && <Devtools />}
|
||||
{children}
|
||||
<Toaster />
|
||||
|
||||
@@ -3,7 +3,6 @@ import type { NavigateFunction } from "react-router-dom"
|
||||
import { useLocation, useNavigate, useParams, useSearchParams } from "react-router-dom"
|
||||
|
||||
import { setNavigate, setRoute } from "~/atoms/route"
|
||||
import { useSettingModal } from "~/modules/settings/modal/hooks"
|
||||
|
||||
declare global {
|
||||
export const router: {
|
||||
@@ -33,19 +32,16 @@ export const StableRouterProvider = () => {
|
||||
const nav = useNavigate()
|
||||
const location = useLocation()
|
||||
|
||||
const showSettings = useSettingModal()
|
||||
|
||||
// NOTE: This is a hack to expose the navigate function to the window object, avoid to import `router` circular issue.
|
||||
useLayoutEffect(() => {
|
||||
window.router.navigate = nav
|
||||
window.router.showSettings = showSettings
|
||||
setRoute({
|
||||
params,
|
||||
searchParams,
|
||||
location,
|
||||
})
|
||||
setNavigate({ fn: nav })
|
||||
}, [searchParams, params, location, nav, showSettings])
|
||||
}, [searchParams, params, location, nav])
|
||||
|
||||
// Posthog tracking
|
||||
useEffect(() => {
|
||||
|
||||
@@ -2,7 +2,6 @@ import { IN_ELECTRON } from "@follow/shared/constants"
|
||||
import { wrapCreateBrowserRouter } from "@sentry/react"
|
||||
import { createBrowserRouter, createHashRouter } from "react-router-dom"
|
||||
|
||||
import App from "./App"
|
||||
import { ErrorElement } from "./components/common/ErrorElement"
|
||||
import { NotFound } from "./components/common/NotFound"
|
||||
import { buildGlobRoutes } from "./lib/route-builder"
|
||||
@@ -19,7 +18,7 @@ if (window.SENTRY_RELEASE) {
|
||||
export const router = routerCreator([
|
||||
{
|
||||
path: "/",
|
||||
element: <App />,
|
||||
lazy: () => import("./App"),
|
||||
children: tree,
|
||||
errorElement: <ErrorElement />,
|
||||
},
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
import generate from "@babel/generator"
|
||||
import { parse } from "@babel/parser"
|
||||
import traverse from "@babel/traverse"
|
||||
import * as t from "@babel/types"
|
||||
import type { Plugin } from "vite"
|
||||
|
||||
// @ts-expect-error
|
||||
const traverseApply: typeof traverse = traverse.default || traverse
|
||||
// @ts-expect-error
|
||||
const generateApply: typeof generate = generate.default || generate
|
||||
|
||||
export function shortAliasPlugin(): Plugin {
|
||||
const aliasMap = new Map<string, string>()
|
||||
let aliasCounter = 0
|
||||
|
||||
function generateShortAlias(): string {
|
||||
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
let result = "_"
|
||||
let counter = aliasCounter++
|
||||
|
||||
do {
|
||||
result += chars[counter % chars.length]
|
||||
counter = Math.floor(counter / chars.length)
|
||||
} while (counter > 0)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
return {
|
||||
name: "short-alias",
|
||||
renderChunk(code, chunk) {
|
||||
if (chunk.fileName.startsWith("vendor/")) return code
|
||||
|
||||
const ast = parse(code, {
|
||||
sourceType: "module",
|
||||
plugins: ["typescript"],
|
||||
})
|
||||
|
||||
traverseApply(ast, {
|
||||
ImportDeclaration(path) {
|
||||
path.node.specifiers.forEach((specifier) => {
|
||||
if (t.isImportSpecifier(specifier)) {
|
||||
const imported = specifier.imported as t.Identifier
|
||||
const local = specifier.local as t.Identifier
|
||||
|
||||
if (local.name !== imported.name && local.name.length > 3) {
|
||||
let shortAlias = aliasMap.get(local.name)
|
||||
if (!shortAlias) {
|
||||
shortAlias = generateShortAlias()
|
||||
aliasMap.set(local.name, shortAlias)
|
||||
}
|
||||
|
||||
path.scope.rename(local.name, shortAlias)
|
||||
local.name = shortAlias
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
const output = generateApply(ast, { retainLines: true }, code)
|
||||
return output.code
|
||||
},
|
||||
}
|
||||
}
|
||||
+1
-4
@@ -76,7 +76,7 @@ export default ({ mode }) => {
|
||||
devPrint(),
|
||||
createDependencyChunksPlugin([
|
||||
// React framework
|
||||
["react", "react-dom", "react-router-dom", "react-error-boundary"],
|
||||
["react", "react-dom", "react-router-dom", "react-error-boundary", "react-dom/server"],
|
||||
// Data Statement
|
||||
["zustand", "jotai", "use-context-selector", "immer", "dexie"],
|
||||
// Remark
|
||||
@@ -116,9 +116,6 @@ export default ({ mode }) => {
|
||||
["@sentry/react", "posthog-js"],
|
||||
["zod", "react-hook-form", "@hookform/resolvers"],
|
||||
]),
|
||||
// FIXME this plugin to rename symbol will cost lots time about ~30s on M2 Max machine.
|
||||
// And reduce main chunk js ~10K Brotli. Does that make sense?
|
||||
// shortAliasPlugin(),
|
||||
|
||||
process.env.ANALYZER && analyzer(),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user