refactor: vite config and re-optimize chunks

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-10-05 00:37:08 +08:00
parent 8c6753d4a6
commit e05d2bab75
17 changed files with 358 additions and 240 deletions
-3
View File
@@ -20,7 +20,6 @@
"@lottiefiles/dotlottie-react": "0.9.0",
"@microflash/remark-callout-directives": "4.3.1",
"@mozilla/readability": "^0.5.0",
"@radix-ui/react-alert-dialog": "1.1.1",
"@radix-ui/react-avatar": "1.1.0",
"@radix-ui/react-checkbox": "1.1.1",
"@radix-ui/react-context-menu": "2.2.1",
@@ -39,7 +38,6 @@
"@radix-ui/react-toast": "1.2.1",
"@radix-ui/react-tooltip": "1.1.2",
"@sentry/react": "8.32.0",
"@sentry/vite-plugin": "2.22.4",
"@shikijs/transformers": "1.20.0",
"@t3-oss/env-core": "^0.11.1",
"@tanstack/query-sync-storage-persister": "5.56.2",
@@ -68,7 +66,6 @@
"immer": "10.1.1",
"jotai": "2.10.0",
"lethargy": "1.0.9",
"linkedom": "^0.18.5",
"lodash-es": "4.17.21",
"masonic": "4.0.1",
"nanoid": "5.0.7",
@@ -1,33 +0,0 @@
import Versions from "../../components/Versions"
export function Component() {
if (!import.meta.env.DEV) return null
const ipcHandle = (): void => window.electron?.ipcRenderer.send("ping")
return (
<>
<div className="creator">Powered by electron-vite</div>
<div className="text">
<span className="i-mgc-rss-cute-fi" />
Build an Electron app with <span className="react">React</span>
&nbsp;and <span className="ts">TypeScript</span>
</div>
<p className="tip">
Please try pressing <code>F12</code> to open the devTool
</p>
<div className="actions">
<div className="action">
<a href="https://electron-vite.org/" target="_blank" rel="noreferrer">
Documentation
</a>
</div>
<div className="action">
<a target="_blank" rel="noreferrer" onClick={ipcHandle}>
Send IPC
</a>
</div>
</div>
<Versions />
</>
)
}
+8 -134
View File
@@ -1,96 +1,19 @@
import fs, { readFileSync } from "node:fs"
import path, { resolve } from "node:path"
import { readFileSync } from "node:fs"
import { resolve } from "node:path"
import * as babel from "@babel/core"
import generate from "@babel/generator"
import { parse } from "@babel/parser"
import * as t from "@babel/types"
import { sentryVitePlugin } from "@sentry/vite-plugin"
import react from "@vitejs/plugin-react"
import { set } from "lodash-es"
import { prerelease } from "semver"
import type { Plugin, UserConfig } from "vite"
import type { UserConfig } from "vite"
import { customI18nHmrPlugin } from "../plugins/vite/i18n-hmr"
import { localesPlugin } from "../plugins/vite/locales"
import { twMacro } from "../plugins/vite/tw-macro"
import i18nCompleteness from "../plugins/vite/utils/i18n-completeness"
import { getGitHash } from "../scripts/lib"
import i18nCompleteness from "./i18n-completeness"
const pkg = JSON.parse(readFileSync("package.json", "utf8"))
const isCI = process.env.CI === "true" || process.env.CI === "1"
function localesPlugin(): Plugin {
return {
name: "locales-merge",
enforce: "post",
generateBundle(_options, bundle) {
const localesDir = path.resolve(__dirname, "../locales")
const namespaces = fs.readdirSync(localesDir).filter((dir) => dir !== ".DS_Store")
const languageResources = {}
namespaces.forEach((namespace) => {
const namespacePath = path.join(localesDir, namespace)
const files = fs.readdirSync(namespacePath).filter((file) => file.endsWith(".json"))
files.forEach((file) => {
const lang = path.basename(file, ".json")
const filePath = path.join(namespacePath, file)
const content = JSON.parse(fs.readFileSync(filePath, "utf-8"))
if (!languageResources[lang]) {
languageResources[lang] = {}
}
const obj = {}
const keys = Object.keys(content as object)
for (const accessorKey of keys) {
set(obj, accessorKey, (content as any)[accessorKey])
}
languageResources[lang][namespace] = obj
})
})
Object.entries(languageResources).forEach(([lang, resources]) => {
const fileName = `locales/${lang}.js`
const content = `export default ${JSON.stringify(resources)};`
this.emitFile({
type: "asset",
fileName,
source: content,
})
})
// Remove original JSON chunks
Object.keys(bundle).forEach((key) => {
if (key.startsWith("locales/") && key.endsWith(".json")) {
delete bundle[key]
}
})
},
}
}
function customI18nHmrPlugin(): Plugin {
return {
name: "custom-i18n-hmr",
handleHotUpdate({ file, server }) {
if (file.endsWith(".json") && file.includes("locales")) {
server.ws.send({
type: "custom",
event: "i18n-update",
data: {
file,
content: readFileSync(file, "utf-8"),
},
})
// return empty array to prevent the default HMR
return []
}
},
}
}
export const viteRenderBaseConfig = {
resolve: {
@@ -130,7 +53,7 @@ export const viteRenderBaseConfig = {
}),
localesPlugin(),
viteTwToRawString(),
twMacro(),
customI18nHmrPlugin(),
],
define: {
@@ -147,52 +70,3 @@ export const viteRenderBaseConfig = {
I18N_COMPLETENESS_MAP: JSON.stringify({ ...i18nCompleteness, en: 100 }),
},
} satisfies UserConfig
function viteTwToRawString(): Plugin {
return {
name: "vite-plugin-tw-to-raw-string",
transform(code, id) {
// Only Process .tsx .ts .jsx .js files
if (!/\.[jt]sx?$/.test(id)) {
return null
}
// Parse the code using Babel's parser with TypeScript support
const ast = parse(code, {
sourceType: "module",
plugins: ["jsx", "typescript"], // Add typescript support
})
babel.traverse(ast, {
TaggedTemplateExpression(path) {
if (t.isIdentifier(path.node.tag, { name: "tw" })) {
const { quasi } = path.node
if (t.isTemplateLiteral(quasi)) {
// Create a new template literal by combining quasis and expressions
const quasis = quasi.quasis.map((q) => q.value.raw)
// Replace the tagged template expression with the new template literal as a string
path.replaceWith(
t.templateLiteral(
quasis.map((q, i) =>
t.templateElement({ raw: q, cooked: q }, i === quasis.length - 1),
),
quasi.expressions,
),
)
}
}
},
})
// Generate the transformed code from the modified AST
// @ts-expect-error
const output = generate.default(ast, {}, code)
return {
code: output.code,
map: null, // Source map generation can be added if necessary
}
},
}
}
+3 -3
View File
@@ -1,9 +1,9 @@
// @ts-check
import { defineConfig } from "eslint-config-hyoban"
import checkI18nJson from "./plugins/eslint-check-i18n-json.js"
import noDebug from "./plugins/eslint-no-debug.js"
import recursiveSort from "./plugins/eslint-recursive-sort.js"
import checkI18nJson from "./plugins/eslint/eslint-check-i18n-json.js"
import noDebug from "./plugins/eslint/eslint-no-debug.js"
import recursiveSort from "./plugins/eslint/eslint-recursive-sort.js"
export default defineConfig(
{
+39
View File
@@ -0,0 +1,39 @@
import path from "node:path"
import fs from "fs-extra"
import type { Plugin } from "vite"
export default function customAssetOutput(options: {
dependencies: Record<string, { sourceDir: string; targetDir: string }>
}): Plugin {
const { dependencies = {} } = options
return {
name: "vite-plugin-custom-asset-output",
async generateBundle(_, bundle) {
for (const [dependencyName, config] of Object.entries(dependencies)) {
const { sourceDir, targetDir } = config
for (const fileName in bundle) {
const file = bundle[fileName]
if (
file.type === "asset" &&
file.name &&
file.name.startsWith(`${dependencyName}/${sourceDir}`)
) {
const newFileName = file.name.replace(`${dependencyName}/${sourceDir}`, targetDir)
file.fileName = newFileName
await fs.ensureDir(path.dirname(newFileName))
bundle[newFileName] = file
delete bundle[fileName]
}
}
}
},
}
}
+33
View File
@@ -0,0 +1,33 @@
import type { Plugin, UserConfig } from "vite"
export function createDependencyChunksPlugin(dependencies: string[] | string[][]): Plugin {
return {
name: "dependency-chunks",
config(config: UserConfig) {
config.build = config.build || {}
config.build.rollupOptions = config.build.rollupOptions || {}
config.build.rollupOptions.output = config.build.rollupOptions.output || {}
const { output } = config.build.rollupOptions
const outputConfig = Array.isArray(output) ? output[0] : output
outputConfig.manualChunks = outputConfig.manualChunks || {}
outputConfig.assetFileNames = "assets/[name].[hash][extname]"
outputConfig.chunkFileNames = (chunkInfo) => {
return chunkInfo.name.startsWith("vendor/") ? "[name].[hash].js" : "assets/[name].[hash].js"
}
const manualChunks = Array.isArray(output) ? output[0].manualChunks : output.manualChunks
if (typeof manualChunks !== "object") return
dependencies.forEach((dep, index) => {
if (Array.isArray(dep)) {
const chunkName = `vendor/a${index}`
manualChunks[chunkName] = dep
} else {
manualChunks[`vendor/${dep}`] = [dep]
}
})
},
}
}
+29
View File
@@ -0,0 +1,29 @@
import type { PluginOption } from "vite"
import type { env as EnvType } from "../../packages/shared/src/env"
export function htmlInjectPlugin(env: typeof EnvType): PluginOption {
return {
name: "html-transform",
enforce: "post",
transformIndexHtml(html) {
return html.replace(
"<!-- FOLLOW VITE BUILD INJECT -->",
`<script id="env_injection" type="module">
${function injectEnv(env: any) {
for (const key in env) {
if (env[key] === undefined) continue
globalThis["__followEnv"] ??= {}
globalThis["__followEnv"][key] = env[key]
}
}.toString()}
injectEnv(${JSON.stringify({
VITE_API_URL: env.VITE_API_URL,
VITE_WEB_URL: env.VITE_WEB_URL,
VITE_IMGPROXY_URL: env.VITE_IMGPROXY_URL,
})})
</script>`,
)
},
}
}
+24
View File
@@ -0,0 +1,24 @@
import { readFileSync } from "node:fs"
import type { Plugin } from "vite"
export function customI18nHmrPlugin(): Plugin {
return {
name: "custom-i18n-hmr",
handleHotUpdate({ file, server }) {
if (file.endsWith(".json") && file.includes("locales")) {
server.ws.send({
type: "custom",
event: "i18n-update",
data: {
file,
content: readFileSync(file, "utf-8"),
},
})
// return empty array to prevent the default HMR
return []
}
},
}
}
+64
View File
@@ -0,0 +1,64 @@
import fs from "node:fs"
import path, { dirname } from "node:path"
import { fileURLToPath } from "node:url"
import { set } from "lodash-es"
import type { Plugin } from "vite"
export function localesPlugin(): Plugin {
return {
name: "locales-merge",
enforce: "post",
generateBundle(_options, bundle) {
const __dirname = dirname(fileURLToPath(import.meta.url))
const localesDir = path.resolve(__dirname, "../../locales")
const namespaces = fs.readdirSync(localesDir).filter((dir) => dir !== ".DS_Store")
const languageResources = {}
namespaces.forEach((namespace) => {
const namespacePath = path.join(localesDir, namespace)
const files = fs.readdirSync(namespacePath).filter((file) => file.endsWith(".json"))
files.forEach((file) => {
const lang = path.basename(file, ".json")
const filePath = path.join(namespacePath, file)
const content = JSON.parse(fs.readFileSync(filePath, "utf-8"))
if (!languageResources[lang]) {
languageResources[lang] = {}
}
const obj = {}
const keys = Object.keys(content as object)
for (const accessorKey of keys) {
set(obj, accessorKey, (content as any)[accessorKey])
}
languageResources[lang][namespace] = obj
})
})
Object.entries(languageResources).forEach(([lang, resources]) => {
const fileName = `locales/${lang}.js`
const content = `export default ${JSON.stringify(resources)};`
this.emitFile({
type: "asset",
fileName,
source: content,
})
})
// Remove original JSON chunks
Object.keys(bundle).forEach((key) => {
if (key.startsWith("locales/") && key.endsWith(".json")) {
delete bundle[key]
}
})
},
}
}
+57
View File
@@ -0,0 +1,57 @@
import type { Plugin } from "vite"
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 importRegex = /import\s*(\{[^}]+\})\s*from\s*(['"])([^'"]+)\2/g
return code.replaceAll(importRegex, (match, imports, quote, source) => {
// Only process imports with curly braces
if (!imports.startsWith("{") || !imports.endsWith("}")) {
return match
}
const newImports = imports
.slice(1, -1) // Remove the curly braces
.split(",")
.map((imp) => {
const [original, alias] = imp.trim().split(/\s+as\s+/)
if (alias) {
// If an alias already exists, keep it or shorten it if it's too long
if (alias.length > 3) {
let shortAlias = aliasMap.get(alias)
if (!shortAlias) {
shortAlias = generateShortAlias()
aliasMap.set(alias, shortAlias)
}
return `${original} as ${shortAlias}`
}
return imp.trim()
}
return imp.trim()
})
.join(", ")
return `import {${newImports}} from ${quote}${source}${quote}`
})
},
}
}
+54
View File
@@ -0,0 +1,54 @@
import * as babel from "@babel/core"
import generate from "@babel/generator"
import { parse } from "@babel/parser"
import * as t from "@babel/types"
import type { Plugin } from "vite"
export function twMacro(): Plugin {
return {
name: "vite-plugin-tw-to-raw-string",
transform(code, id) {
// Only Process .tsx .ts .jsx .js files
if (!/\.[jt]sx?$/.test(id)) {
return null
}
// Parse the code using Babel's parser with TypeScript support
const ast = parse(code, {
sourceType: "module",
plugins: ["jsx", "typescript"], // Add typescript support
})
babel.traverse(ast, {
TaggedTemplateExpression(path) {
if (t.isIdentifier(path.node.tag, { name: "tw" })) {
const { quasi } = path.node
if (t.isTemplateLiteral(quasi)) {
// Create a new template literal by combining quasis and expressions
const quasis = quasi.quasis.map((q) => q.value.raw)
// Replace the tagged template expression with the new template literal as a string
path.replaceWith(
t.templateLiteral(
quasis.map((q, i) =>
t.templateElement({ raw: q, cooked: q }, i === quasis.length - 1),
),
quasi.expressions,
),
)
}
}
},
})
// Generate the transformed code from the modified AST
// @ts-expect-error
const output = generate.default(ast, {}, code)
return {
code: output.code,
map: null, // Source map generation can be added if necessary
}
},
}
}
@@ -1,5 +1,6 @@
import fs from "node:fs"
import path from "node:path"
import path, { dirname } from "node:path"
import { fileURLToPath } from "node:url"
type LanguageCompletion = Record<string, number>
@@ -55,6 +56,7 @@ function calculateCompleteness(localesDir: string): LanguageCompletion {
return completeness
}
const __dirname = dirname(fileURLToPath(import.meta.url))
const i18n = calculateCompleteness(path.resolve(__dirname, "../locales"))
const i18n = calculateCompleteness(path.resolve(__dirname, "../../../locales"))
export default i18n
-33
View File
@@ -351,9 +351,6 @@ importers:
'@mozilla/readability':
specifier: ^0.5.0
version: 0.5.0(patch_hash=fgkvsbckled47trggkhdkimzbm)
'@radix-ui/react-alert-dialog':
specifier: 1.1.1
version: 1.1.1(@types/react-dom@18.3.0)(@types/react@18.3.10)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
'@radix-ui/react-avatar':
specifier: 1.1.0
version: 1.1.0(@types/react-dom@18.3.0)(@types/react@18.3.10)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
@@ -408,9 +405,6 @@ importers:
'@sentry/react':
specifier: 8.32.0
version: 8.32.0(react@18.3.1)
'@sentry/vite-plugin':
specifier: 2.22.4
version: 2.22.4(encoding@0.1.13)
'@shikijs/transformers':
specifier: 1.20.0
version: 1.20.0
@@ -2398,19 +2392,6 @@ packages:
'@radix-ui/primitive@1.1.0':
resolution: {integrity: sha512-4Z8dn6Upk0qk4P74xBhZ6Hd/w0mPEzOOLxy4xiPXOXqjF7jZS0VAKk7/x/H6FyY2zCkYJqePf1G5KmkmNJ4RBA==}
'@radix-ui/react-alert-dialog@1.1.1':
resolution: {integrity: sha512-wmCoJwj7byuVuiLKqDLlX7ClSUU0vd9sdCeM+2Ls+uf13+cpSJoMgwysHq1SGVVkJj5Xn0XWi1NoRCdkMpr6Mw==}
peerDependencies:
'@types/react': '*'
'@types/react-dom': '*'
react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
react-dom: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
peerDependenciesMeta:
'@types/react':
optional: true
'@types/react-dom':
optional: true
'@radix-ui/react-arrow@1.1.0':
resolution: {integrity: sha512-FmlW1rCg7hBpEBwFbjHwCW6AmWLQM6g/v0Sn8XbP9NvmSZ2San1FpQeyPtufzOMSIx7Y4dzjlHoifhp+7NkZhw==}
peerDependencies:
@@ -11099,20 +11080,6 @@ snapshots:
'@radix-ui/primitive@1.1.0': {}
'@radix-ui/react-alert-dialog@1.1.1(@types/react-dom@18.3.0)(@types/react@18.3.10)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)':
dependencies:
'@radix-ui/primitive': 1.1.0
'@radix-ui/react-compose-refs': 1.1.0(@types/react@18.3.10)(react@18.3.1)
'@radix-ui/react-context': 1.1.0(@types/react@18.3.10)(react@18.3.1)
'@radix-ui/react-dialog': 1.1.1(@types/react-dom@18.3.0)(@types/react@18.3.10)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
'@radix-ui/react-primitive': 2.0.0(@types/react-dom@18.3.0)(@types/react@18.3.10)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
'@radix-ui/react-slot': 1.1.0(@types/react@18.3.10)(react@18.3.1)
react: 18.3.1
react-dom: 18.3.1(react@18.3.1)
optionalDependencies:
'@types/react': 18.3.10
'@types/react-dom': 18.3.0
'@radix-ui/react-arrow@1.1.0(@types/react-dom@18.3.0)(@types/react@18.3.10)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)':
dependencies:
'@radix-ui/react-primitive': 2.0.0(@types/react-dom@18.3.0)(@types/react@18.3.10)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
+43 -32
View File
@@ -9,38 +9,14 @@ import { analyzer } from "vite-bundle-analyzer"
import mkcert from "vite-plugin-mkcert"
import { viteRenderBaseConfig } from "./configs/vite.render.config"
import type { env as EnvType } from "./src/env"
import type { env as EnvType } from "./packages/shared/src/env"
import { createDependencyChunksPlugin } from "./plugins/vite/deps"
import { htmlInjectPlugin } from "./plugins/vite/html-inject"
const __dirname = fileURLToPath(new URL(".", import.meta.url))
const isCI = process.env.CI === "true" || process.env.CI === "1"
const ROOT = "./apps/renderer"
function htmlPlugin(env: typeof EnvType): PluginOption {
return {
name: "html-transform",
enforce: "post",
transformIndexHtml(html) {
return html.replace(
"<!-- FOLLOW VITE BUILD INJECT -->",
`<script id="env_injection" type="module">
${function injectEnv(env: any) {
for (const key in env) {
if (env[key] === undefined) continue
globalThis["__followEnv"] ??= {}
globalThis["__followEnv"][key] = env[key]
}
}.toString()}
injectEnv(${JSON.stringify({
VITE_API_URL: env.VITE_API_URL,
VITE_WEB_URL: env.VITE_WEB_URL,
VITE_IMGPROXY_URL: env.VITE_IMGPROXY_URL,
})})
</script>`,
)
},
}
}
const devPrint = (): PluginOption => ({
name: "dev-print",
configureServer(server: ViteDevServer) {
@@ -78,10 +54,6 @@ export default ({ mode }) => {
main: resolve(ROOT, "/index.html"),
__debug_proxy: resolve(ROOT, "/__debug_proxy.html"),
},
output: {
// 10KB
experimentalMinChunkSize: 10_000,
},
},
},
server: {
@@ -99,12 +71,51 @@ export default ({ mode }) => {
modernTargets: ">0.3%, last 2 versions, Firefox ESR, not dead",
modernPolyfills: ["es.array.find-last-index", "es.array.find-last"],
}),
htmlPlugin(typedEnv),
htmlInjectPlugin(typedEnv),
mkcert(),
devPrint(),
createDependencyChunksPlugin([
["react", "react-dom"],
["zustand", "jotai", "use-context-selector", "immer", "dexie"],
[
"remark-directive",
"remark-gfm",
"remark-parse",
"remark-stringify",
"remark-rehype",
"@microflash/remark-callout-directives",
"remark-gh-alerts",
],
[
"rehype-parse",
"rehype-sanitize",
"rehype-stringify",
"rehype-infer-description-meta",
"hast-util-to-jsx-runtime",
"hast-util-to-text",
],
["vfile", "unified"],
["lodash-es"],
["framer-motion"],
["clsx", "tailwind-merge", "class-variance-authority"],
["@radix-ui/react-dialog", "re-resizable"],
["i18next", "i18next-browser-languagedetector", "react-i18next"],
[
"@tanstack/react-query",
"@tanstack/react-query-persist-client",
"@tanstack/query-sync-storage-persister",
],
["blurhash", "react-blurhash"],
["tldts"],
["shiki", "@shikijs/transformers"],
["@sentry/react", "posthog-js"],
["zod", "react-hook-form", "@hookform/resolvers"],
]),
// shortAliasPlugin(),
process.env.ANALYZER && analyzer(),
],
define: {
...viteRenderBaseConfig.define,
ELECTRON: "false",