|
| 1 | +import { defineNuxtModule, addTemplate } from "nuxt/kit"; |
| 2 | + |
| 3 | +export interface FeatureFlags { |
| 4 | + [key: string]: boolean; |
| 5 | +} |
| 6 | + |
| 7 | +export type ModuleOptions = FeatureFlags; |
| 8 | + |
| 9 | +export default defineNuxtModule<ModuleOptions>({ |
| 10 | + meta: { |
| 11 | + name: "feature-flags", |
| 12 | + configKey: "featureFlags", |
| 13 | + }, |
| 14 | + defaults: {}, |
| 15 | + async setup(options, nuxt) { |
| 16 | + const featureFlags = options || {}; |
| 17 | + const defines: Record<string, string> = {}; |
| 18 | + for (const [key, value] of Object.entries(featureFlags)) { |
| 19 | + defines[`import.meta.vfFeatures.${key}`] = JSON.stringify(value); |
| 20 | + } |
| 21 | + |
| 22 | + const typeContent = generateTypeDeclarations(featureFlags); |
| 23 | + |
| 24 | + /** |
| 25 | + * For client (Nuxt Vite) |
| 26 | + ---------------------------------------------------------------------------- */ |
| 27 | + |
| 28 | + nuxt.options.vite = nuxt.options.vite || {}; |
| 29 | + nuxt.options.vite.define = { |
| 30 | + ...nuxt.options.vite.define, |
| 31 | + ...defines, |
| 32 | + }; |
| 33 | + |
| 34 | + addTemplate({ |
| 35 | + filename: "types/feature-flags.d.ts", |
| 36 | + getContents: () => typeContent, |
| 37 | + write: true, |
| 38 | + }); |
| 39 | + |
| 40 | + nuxt.hook("prepare:types", async ({ references }) => { |
| 41 | + references.push({ path: "./types/feature-flags.d.ts" }); |
| 42 | + }); |
| 43 | + |
| 44 | + /** |
| 45 | + * For server (Nitro) |
| 46 | + ---------------------------------------------------------------------------- */ |
| 47 | + |
| 48 | + nuxt.options.nitro = nuxt.options.nitro || {}; |
| 49 | + nuxt.options.nitro.esbuild = nuxt.options.nitro.esbuild || {}; |
| 50 | + nuxt.options.nitro.esbuild.options = nuxt.options.nitro.esbuild.options || {}; |
| 51 | + nuxt.options.nitro.esbuild.options.define = { ...nuxt.options.nitro.esbuild.options.define, ...defines }; |
| 52 | + nuxt.options.nitro.replace = { ...nuxt.options.nitro.replace, ...defines }; |
| 53 | + |
| 54 | + addTemplate({ |
| 55 | + filename: "types/nitro-feature-flags.d.ts", |
| 56 | + getContents: () => typeContent, |
| 57 | + write: true, |
| 58 | + }); |
| 59 | + |
| 60 | + nuxt.hook("nitro:config", (nitroConfig) => { |
| 61 | + nitroConfig.typescript = nitroConfig.typescript || {}; |
| 62 | + nitroConfig.typescript.tsConfig = nitroConfig.typescript.tsConfig || {}; |
| 63 | + nitroConfig.typescript.tsConfig.compilerOptions = nitroConfig.typescript.tsConfig.compilerOptions || {}; |
| 64 | + nitroConfig.typescript.tsConfig.compilerOptions.types = nitroConfig.typescript.tsConfig.compilerOptions.types || []; |
| 65 | + if (Array.isArray(nitroConfig.typescript.tsConfig.compilerOptions.types)) { |
| 66 | + nitroConfig.typescript.tsConfig.compilerOptions.types.push(".nuxt/types/nitro-feature-flags"); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + nuxt.hook("prepare:types", async ({ references }) => { |
| 71 | + references.push({ path: "./types/nitro-feature-flags.d.ts" }); |
| 72 | + }); |
| 73 | + }, |
| 74 | +}); |
| 75 | + |
| 76 | +function generateTypeDeclarations(featureFlags: FeatureFlags): string { |
| 77 | + const flagTypes = Object.entries(featureFlags) |
| 78 | + .map(([key, value]) => ` readonly ${key}: ${typeof value};`) |
| 79 | + .join("\n"); |
| 80 | + |
| 81 | + return `// Auto-generated feature flags type definitions |
| 82 | +// Do not edit this file directly |
| 83 | +
|
| 84 | +declare global { |
| 85 | + interface ImportMetaFeatureFlags { |
| 86 | +${flagTypes} |
| 87 | + } |
| 88 | +
|
| 89 | + interface ImportMeta { |
| 90 | + readonly vfFeatures: ImportMetaFeatureFlags; |
| 91 | + } |
| 92 | +} |
| 93 | +
|
| 94 | +export {}; |
| 95 | +`; |
| 96 | +} |
0 commit comments