forked from RSSNext/Follow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: vite config and re-optimize chunks
Signed-off-by: Innei <i@innei.in>
- Loading branch information
Showing
17 changed files
with
358 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
} | ||
} | ||
} | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
} | ||
}) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>`, | ||
) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 [] | ||
} | ||
}, | ||
} | ||
} |
Oops, something went wrong.