-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeigniter.vite.ts
More file actions
286 lines (251 loc) · 9.08 KB
/
codeigniter.vite.ts
File metadata and controls
286 lines (251 loc) · 9.08 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import fs from 'fs'
import path from 'path'
import type { Plugin, ResolvedConfig, ViteDevServer } from 'vite'
export interface EntryAccess {
access: 'public' | 'auth'
groups?: string[]
permissions?: string[]
}
export interface EntryConfig extends EntryAccess {
input: string
}
export interface CodeIgniterPluginConfig {
/** Named entries with access control metadata */
entries: Record<string, EntryConfig>
/** Output directory relative to project root (default: 'writable/app') */
buildPath?: string
/** Hot file path override (default: '<buildPath>/hot') */
hotFile?: string
}
interface CIManifestEntry {
input: string
access: string
groups: string[]
permissions: string[]
files: { js: string[]; css: string[] }
chunks: string[]
preloads: string[]
}
interface CIManifest {
entries: Record<string, CIManifestEntry>
/** Maps every output file to its effective access rule */
fileAccess: Record<string, EntryAccess>
}
/**
* Vite plugin for CodeIgniter 4 integration.
*
* - Dev mode: writes a `hot` file so the PHP helper knows the dev-server URL.
* - Build mode: generates `ci-manifest.json` alongside the standard Vite manifest,
* mapping each output file to its access-control rule derived from the entry config.
*/
export default function codeigniter(config: CodeIgniterPluginConfig): Plugin {
const buildPath = config.buildPath ?? 'writable/app'
const hotFilePath = config.hotFile ?? path.join(buildPath, 'hot')
let resolvedConfig: ResolvedConfig
return {
name: 'vite-plugin-codeigniter',
enforce: 'post',
/* ------------------------------------------------------------------ */
/* Merge rollup input from entries */
/* ------------------------------------------------------------------ */
config() {
const input: Record<string, string> = {}
for (const [name, entry] of Object.entries(config.entries)) {
input[name] = entry.input
}
return {
build: {
manifest: true,
rollupOptions: { input },
},
}
},
configResolved(resolved) {
resolvedConfig = resolved
},
/* ------------------------------------------------------------------ */
/* Dev: write hot file with dev-server URL */
/* ------------------------------------------------------------------ */
configureServer(server: ViteDevServer) {
// Write once the http server is actually listening so we get the real port
server.httpServer?.once('listening', () => {
const protocol = resolvedConfig.server.https ? 'https' : 'http'
const host =
typeof resolvedConfig.server.host === 'string'
? resolvedConfig.server.host
: 'localhost'
const address = server.httpServer!.address()
const port =
address && typeof address === 'object'
? address.port
: (resolvedConfig.server.port ?? 5173)
fs.mkdirSync(path.dirname(path.resolve(hotFilePath)), {
recursive: true,
})
fs.writeFileSync(
path.resolve(hotFilePath),
`${protocol}://${host}:${port}`,
)
})
const cleanup = () => {
try {
if (fs.existsSync(path.resolve(hotFilePath))) {
fs.rmSync(path.resolve(hotFilePath))
}
} catch {
/* ignore */
}
}
process.on('exit', cleanup)
process.on('SIGINT', () => {
cleanup()
process.exit()
})
process.on('SIGTERM', () => {
cleanup()
process.exit()
})
},
/* ------------------------------------------------------------------ */
/* Build: generate ci-manifest.json */
/* ------------------------------------------------------------------ */
closeBundle() {
const manifestPath = path.resolve(buildPath, '.vite', 'manifest.json')
if (!fs.existsSync(manifestPath)) return
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const manifest: Record<string, any> = JSON.parse(
fs.readFileSync(manifestPath, 'utf-8'),
)
// Recursively collect every output file reachable from a manifest key
function collectFiles(
key: string,
visited = new Set<string>(),
): { js: string[]; css: string[]; assets: string[]; preloads: string[] } {
if (visited.has(key)) return { js: [], css: [], assets: [], preloads: [] }
visited.add(key)
const entry = manifest[key]
if (!entry) return { js: [], css: [], assets: [], preloads: [] }
const js: string[] = []
const css: string[] = entry.css ? [...entry.css] : []
const assets: string[] = entry.assets ? [...entry.assets] : []
const preloads: string[] = []
// Imported chunks
if (entry.imports) {
for (const imp of entry.imports) {
const sub = manifest[imp]
if (sub) {
preloads.push(sub.file)
const nested = collectFiles(imp, visited)
js.push(...nested.js)
css.push(...nested.css)
assets.push(...nested.assets)
preloads.push(...nested.preloads)
}
}
}
// Dynamic imports are NOT preloaded but still tracked for access control
if (entry.dynamicImports) {
for (const imp of entry.dynamicImports) {
const sub = manifest[imp]
if (sub) {
js.push(sub.file)
const nested = collectFiles(imp, visited)
js.push(...nested.js)
css.push(...nested.css)
assets.push(...nested.assets)
}
}
}
return {
js: [...new Set(js)],
css: [...new Set(css)],
assets: [...new Set(assets)],
preloads: [...new Set(preloads)],
}
}
const ciManifest: CIManifest = { entries: {}, fileAccess: {} }
// Track which entries each file belongs to
const fileEntryMap = new Map<string, Set<string>>()
function trackFile(file: string, entryName: string) {
if (!fileEntryMap.has(file)) fileEntryMap.set(file, new Set())
fileEntryMap.get(file)!.add(entryName)
}
for (const [name, entryConfig] of Object.entries(config.entries)) {
const manifestEntry = manifest[entryConfig.input]
if (!manifestEntry) {
console.warn(
`[codeigniter] Entry "${name}" (${entryConfig.input}) not found in manifest`,
)
continue
}
const collected = collectFiles(entryConfig.input)
const mainJs = [manifestEntry.file]
// Merge CSS from the entry itself AND all imported chunks
const allCss = [...new Set([...(manifestEntry.css ?? []), ...collected.css])]
ciManifest.entries[name] = {
input: entryConfig.input,
access: entryConfig.access,
groups: entryConfig.groups ?? [],
permissions: entryConfig.permissions ?? [],
files: { js: mainJs, css: allCss },
chunks: collected.js,
preloads: collected.preloads,
}
// Track all files belonging to this entry
for (const f of [...mainJs, ...allCss, ...collected.js, ...collected.assets, ...collected.preloads]) {
trackFile(f, name)
}
}
// Determine effective access for each file:
// If ANY public entry uses it → public. Otherwise → most permissive auth entry.
for (const [file, entryNames] of fileEntryMap) {
let effectiveAccess: EntryAccess = {
access: 'auth',
groups: [],
permissions: [],
}
let hasPublic = false
for (const eName of entryNames) {
const entryCfg = config.entries[eName]
if (entryCfg.access === 'public') {
hasPublic = true
break
}
}
if (hasPublic) {
effectiveAccess = { access: 'public', groups: [], permissions: [] }
} else {
// All entries are auth — merge groups/permissions (union)
const groups = new Set<string>()
const perms = new Set<string>()
for (const eName of entryNames) {
const entryCfg = config.entries[eName]
for (const g of entryCfg.groups ?? []) groups.add(g)
for (const p of entryCfg.permissions ?? []) perms.add(p)
}
effectiveAccess = {
access: 'auth',
groups: [...groups],
permissions: [...perms],
}
}
ciManifest.fileAccess[file] = effectiveAccess
}
fs.writeFileSync(
path.resolve(buildPath, 'ci-manifest.json'),
JSON.stringify(ciManifest, null, 2),
)
// Remove hot file after build (production mode)
try {
if (fs.existsSync(path.resolve(hotFilePath))) {
fs.rmSync(path.resolve(hotFilePath))
}
} catch {
/* ignore */
}
console.log(
`\n[codeigniter] ci-manifest.json written with ${Object.keys(ciManifest.entries).length} entries\n`,
)
},
}
}