tutu/vite.config.ts

102 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-07-14 12:28:44 +00:00
import { defineConfig } from "vite";
import solid from "vite-plugin-solid";
import solidStyled from "vite-plugin-solid-styled";
import suid from "@suid/vite-plugin";
import { VitePWA } from "vite-plugin-pwa";
2024-07-22 13:57:04 +00:00
import version from "vite-plugin-package-version";
2024-10-22 09:48:17 +00:00
import manifest from "./manifest.config";
2024-10-22 14:55:53 +00:00
import { GetManualChunk } from "rollup";
/**
* Put all strings (/i18n/{key}.<json|js|ts>) into separated chunks based on the key.
*/
const chunkStrs: GetManualChunk = (id, { getModuleInfo }) => {
const match = /.*\/i18n\/(.*)\.[jt]s.*$/.exec(id);
if (match) {
const key = match[1];
const dependentEntryPoints = [];
// we use a Set here so we handle each module at most once. This
// prevents infinite loops in case of circular dependencies
const idsToHandle = new Set(getModuleInfo(id)!.dynamicImporters);
for (const moduleId of idsToHandle) {
const { isEntry, dynamicImporters, importers } = getModuleInfo(moduleId)!;
if (isEntry || dynamicImporters.length > 0)
dependentEntryPoints.push(moduleId);
// The Set iterator is intelligent enough to iterate over
// elements that are added during iteration
for (const importerId of importers) idsToHandle.add(importerId);
}
// If there is a unique entry, we put it into a chunk based on the
// entry name
if (dependentEntryPoints.length === 1) {
return `${key}.${
dependentEntryPoints[0].split("/").slice(-1)[0].split(".")[0]
}.strings`;
}
// For multiple entries, we put it into a "shared" chunk
if (dependentEntryPoints.length > 1) {
return `${key}.shared.strings`;
}
}
};
const manualChunks: GetManualChunk = (id, meta) => {
return chunkStrs(id, meta);
};
2024-07-14 12:28:44 +00:00
export default defineConfig(({ mode }) => ({
plugins: [
suid(),
solid(),
solidStyled({
filter: {
include: "src/**/*.{tsx,jsx}",
exclude: "node_modules/**/*.{ts,js,tsx,jsx}",
},
}),
VitePWA({
strategies: "injectManifest",
2024-07-14 12:28:44 +00:00
registerType: "autoUpdate",
2024-07-23 06:16:35 +00:00
devOptions: {
enabled: !["production", "staging"].includes(mode),
2024-07-23 06:16:35 +00:00
},
srcDir: "src/serviceworker",
filename: "main.ts",
2024-10-22 09:48:17 +00:00
manifest: manifest,
pwaAssets: {
config: true,
},
2024-07-14 12:28:44 +00:00
}),
2024-07-22 13:57:04 +00:00
version(),
2024-07-14 12:28:44 +00:00
],
2024-07-23 06:16:35 +00:00
server: {
https: {
// localhost.direct: https://github.com/Upinel/localhost.direct
key: "tools/certs/localhost.direct.key",
cert: "tools/certs/localhost.direct.crt",
passphrase: "localhost",
},
},
2024-07-22 13:57:04 +00:00
define: {
"import.meta.env.BUILT_AT": `"${new Date().toISOString()}"`,
},
2024-07-14 12:28:44 +00:00
css: {
devSourcemap: true,
},
build: {
target: ["firefox98", "safari15.4", "ios15.4", "chrome84", "edge87"],
2024-07-14 13:26:55 +00:00
sourcemap: true,
2024-10-22 14:55:53 +00:00
rollupOptions: {
output: {
manualChunks,
},
},
2024-07-14 12:28:44 +00:00
},
}));