-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmodule.ts
More file actions
140 lines (124 loc) · 3.99 KB
/
Copy pathmodule.ts
File metadata and controls
140 lines (124 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import fs from "node:fs";
import path from "node:path";
import {
extractClientKeys,
extractKeys,
extractServerKeys,
extractSharedKeys,
findSchemaPath,
formatBuildError,
resolveLayout,
} from "@arkenv/build";
import { defineNuxtModule } from "@nuxt/kit";
import type { NuxtModule } from "@nuxt/schema";
import { name, peerDependencies, version } from "../package.json";
export type ModuleOptions = {
schemaPath?: string;
layout?: "simple" | "strict";
};
const module: NuxtModule<ModuleOptions> = defineNuxtModule<ModuleOptions>({
meta: {
name,
version,
configKey: "arkenv",
compatibility: {
nuxt: peerDependencies?.nuxt,
},
},
defaults: {},
setup(options, nuxt) {
const schemaPath = options.schemaPath
? path.resolve(nuxt.options.rootDir, options.schemaPath)
: findSchemaPath(nuxt.options.rootDir);
if (!schemaPath || !fs.existsSync(schemaPath)) {
return;
}
const { layout: resolvedLayout, baseDir } = resolveLayout(
schemaPath,
options.layout,
);
// Register schema paths to watch so Nuxt restarts and updates runtimeConfig when they change
if (nuxt.options.dev) {
const watchPaths =
resolvedLayout === "strict" && baseDir
? [
path.join(baseDir, "internal", "shared.ts"),
path.join(baseDir, "client.ts"),
path.join(baseDir, "server.ts"),
].filter(fs.existsSync)
: [schemaPath];
nuxt.options.watch = nuxt.options.watch || [];
for (const p of watchPaths) {
nuxt.options.watch.push(p);
}
}
// 3. Register env keys to runtimeConfig
let serverKeys: string[] = [];
let clientKeys: string[] = [];
let sharedKeys: string[] = [];
if (resolvedLayout === "strict" && baseDir) {
const clientPath = path.join(baseDir, "client.ts");
const sharedPath = path.join(baseDir, "internal", "shared.ts");
const serverPath = path.join(baseDir, "server.ts");
const clientContent = fs.existsSync(clientPath)
? fs.readFileSync(clientPath, "utf-8")
: "";
const sharedContent = fs.existsSync(sharedPath)
? fs.readFileSync(sharedPath, "utf-8")
: "";
const serverContent = fs.existsSync(serverPath)
? fs.readFileSync(serverPath, "utf-8")
: "";
clientKeys = extractClientKeys(clientContent);
sharedKeys = extractSharedKeys(sharedContent);
serverKeys = extractServerKeys(serverContent);
} else {
const fileContent = fs.readFileSync(schemaPath, "utf-8");
const extracted = extractKeys(fileContent);
serverKeys = extracted.serverKeys;
clientKeys = extracted.clientKeys;
sharedKeys = extracted.sharedKeys;
}
nuxt.options.runtimeConfig = nuxt.options.runtimeConfig || {};
nuxt.options.runtimeConfig.public = nuxt.options.runtimeConfig.public || {};
// Server keys (private)
for (const key of serverKeys) {
if (nuxt.options.runtimeConfig[key] === undefined) {
nuxt.options.runtimeConfig[key] = process.env[key] || "";
}
}
// Client & Shared keys (public)
for (const key of [...clientKeys, ...sharedKeys]) {
if (nuxt.options.runtimeConfig.public[key] === undefined) {
nuxt.options.runtimeConfig.public[key] = process.env[key] || "";
}
}
// 4. Vite extendConfig to block server-only imports on client side
nuxt.hook("vite:extendConfig", (config, { isClient }) => {
if (isClient) {
// biome-ignore lint/suspicious/noExplicitAny: bypassed because Nuxt's Vite config type is overly restrictive
const anyConfig = config as any;
anyConfig.plugins = anyConfig.plugins || [];
anyConfig.plugins.push({
name: "arkenv-nuxt-client-security",
resolveId(id: string) {
const isServerModule =
id === "@arkenv/nuxt/server" ||
id === "@arkenv/nuxt/standard/server" ||
/[/\\]@arkenv[/\\]nuxt[/\\](?:src|dist)[/\\](?:standard[/\\])?server(?:\.(?:js|mjs|cjs|ts))?$/.test(
id,
);
if (isServerModule) {
throw new Error(
formatBuildError(
"Importing server-only environment schema on the client is not allowed!",
),
);
}
},
});
}
});
},
});
export default module;