Skip to content

Commit

Permalink
fix: reduce bundle size
Browse files Browse the repository at this point in the history
  • Loading branch information
SegaraRai committed Aug 18, 2024
1 parent d0b7bae commit bbcfeda
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import fg from "fast-glob";
import { spawnSync } from "node:child_process";
import { readFileSync } from "node:fs";
import { env } from "node:process";
import { fileURLToPath } from "node:url";
import type { NuxtConfig } from "nuxt/schema";
import { createNitroSWPreset } from "./nitroSWPreset";

function fromProjectDir(path: string): string {
Expand All @@ -25,6 +27,26 @@ function getAppVersion(): string {
return `v${pkgVersion} (${commitHash})`;
}

function getUsedIcons(ignoreCollections: string[]): string[] {
const iconSet = new Set<string>();
const files = fg.sync("app/**/*.{ts,vue}", { absolute: true });
for (const file of files) {
const content = readFileSync(file, "utf-8");
const matches = content.matchAll(/\bi-([a-z\d-]+:[a-z\d-]+)/g);
for (const match of matches) {
const name = match[1]!;
const collection = name.split(":")[0]!;
if (ignoreCollections.includes(collection)) {
continue;
}

iconSet.add(name);
}
}

return Array.from(iconSet).sort();
}

const nitro =
env.NODE_ENV === "development" || env.NODE_ENV === "test"
? {}
Expand All @@ -41,6 +63,19 @@ const nitro =
},
};

const iconBundleOptions: NuxtConfig["icon"] =
env.NODE_ENV === "development" || env.NODE_ENV === "test"
? {
serverBundle: "local",
}
: {
serverBundle: false,
clientBundle: {
includeCustomCollections: true,
icons: getUsedIcons(["luonto"]),
},
};

// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
compatibilityDate: "2024-07-16",
Expand Down Expand Up @@ -86,12 +121,13 @@ export default defineNuxtConfig({
],
},
icon: {
serverBundle: "local",
provider: "server",
customCollections: [
{
prefix: "luonto",
dir: "icons/build/luonto",
},
],
...iconBundleOptions,
},
});

0 comments on commit bbcfeda

Please sign in to comment.