@matra/site-generation 0.4.1-preview.0 → 0.4.1-preview.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +5 -0
- package/README.md +5 -0
- package/package.json +1 -1
- package/src/site.mjs +99 -7
package/README.ja.md
CHANGED
|
@@ -44,6 +44,11 @@ console.log(`Generated ${outputs.length} files`)
|
|
|
44
44
|
`buildSitePreview(directory)`は同じビルドを実行し、ローカル配信用に`files`、
|
|
45
45
|
`outputDirectory`、`routes`(ルートから出力相対パスへのmap)、`basePath`、`notFound`を返します。
|
|
46
46
|
|
|
47
|
+
設定の`site.hooks.module`に信頼する`.mjs`を指定すると、`pages`・`markdown`・`assets`フックを
|
|
48
|
+
ファイル方式と組み合わせられます。入力、出力、監視は[フックの契約](../../spec/site-generation.ja.md#cliのページmarkdownフック)
|
|
49
|
+
を参照してください。フックはsandboxやtimeoutなしで実行されます。
|
|
50
|
+
`assets`と`pages`の戻り値の共通`data`は`0.4.1-preview.1`から利用できます。
|
|
51
|
+
|
|
47
52
|
## JavaScriptからの直接登録
|
|
48
53
|
|
|
49
54
|
ページdataがすでにJavaScriptにあり、中間fileを作らずMatraに直接渡す場合は
|
package/README.md
CHANGED
|
@@ -45,6 +45,11 @@ same versioned schema.
|
|
|
45
45
|
`buildSitePreview(directory)` performs the same build and additionally returns its `files`,
|
|
46
46
|
`outputDirectory`, `routes` (route to relative output path), `basePath`, and `notFound` for local serving.
|
|
47
47
|
|
|
48
|
+
Set `site.hooks.module` to a trusted `.mjs` file to combine `pages`, `markdown`, and `assets` hooks with
|
|
49
|
+
file-based generation. See the [hook contract](../../spec/site-generation.md#cli-page-and-markdown-hooks)
|
|
50
|
+
for inputs, outputs, and watching. Hooks execute without an OS sandbox or timeout.
|
|
51
|
+
The `assets` hook and shared `data` returned by `pages` require `0.4.1-preview.1` or later.
|
|
52
|
+
|
|
48
53
|
## Direct JavaScript API
|
|
49
54
|
|
|
50
55
|
Use `generateSite()` when page data is already available in JavaScript and
|
package/package.json
CHANGED
package/src/site.mjs
CHANGED
|
@@ -72,6 +72,7 @@ export async function buildSiteInProcess(root, onInputs, config, settings) {
|
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
let templates = new Map()
|
|
75
|
+
let hookAssets = []
|
|
75
76
|
if (settings.hooks) {
|
|
76
77
|
const hooks = await loadHooks(root, settings, onInputs)
|
|
77
78
|
if (hooks.markdown) {
|
|
@@ -98,7 +99,11 @@ export async function buildSiteInProcess(root, onInputs, config, settings) {
|
|
|
98
99
|
assertPlainData(registrations, "pages hook result")
|
|
99
100
|
const returned = Array.isArray(registrations) ? { pages: registrations } : registrations
|
|
100
101
|
if (!isRecord(returned)) throw new TypeError("pages hook must return an array or object")
|
|
101
|
-
rejectUnknownKeys(returned, new Set(["pages", "templates"]), "pages hook result")
|
|
102
|
+
rejectUnknownKeys(returned, new Set(["pages", "templates", "data"]), "pages hook result")
|
|
103
|
+
if (returned.data !== undefined) {
|
|
104
|
+
if (!isRecord(returned.data)) throw new TypeError("pages hook data must be an object")
|
|
105
|
+
settings.data = { ...structuredClone(returned.data), ...settings.data }
|
|
106
|
+
}
|
|
102
107
|
direct = normalizeDirectSite(
|
|
103
108
|
{
|
|
104
109
|
root,
|
|
@@ -133,8 +138,9 @@ export async function buildSiteInProcess(root, onInputs, config, settings) {
|
|
|
133
138
|
sortAndValidateRoutes(site.pages)
|
|
134
139
|
templates = direct.templates
|
|
135
140
|
}
|
|
141
|
+
if (hooks.assets) hookAssets = await collectHookAssets(hooks.assets, root, settings, onInputs)
|
|
136
142
|
}
|
|
137
|
-
const result = await generateLoadedSite({ ...site, components, templates })
|
|
143
|
+
const result = await generateLoadedSite({ ...site, components, templates, hookAssets })
|
|
138
144
|
return {
|
|
139
145
|
...result,
|
|
140
146
|
outputDirectory: settings.outputDirectory,
|
|
@@ -205,7 +211,7 @@ async function loadHooks(root, settings, onInputs) {
|
|
|
205
211
|
} catch (error) {
|
|
206
212
|
throw new Error(`hook module ${module}: ${error?.message ?? String(error)}`, { cause: error })
|
|
207
213
|
}
|
|
208
|
-
for (const name of ["pages", "markdown"]) {
|
|
214
|
+
for (const name of ["pages", "markdown", "assets"]) {
|
|
209
215
|
if (hooks[name] !== undefined && typeof hooks[name] !== "function") {
|
|
210
216
|
throw new TypeError(`hook module ${module}: ${name} must be a function`)
|
|
211
217
|
}
|
|
@@ -213,6 +219,85 @@ async function loadHooks(root, settings, onInputs) {
|
|
|
213
219
|
return hooks
|
|
214
220
|
}
|
|
215
221
|
|
|
222
|
+
async function collectHookAssets(hook, root, settings, onInputs) {
|
|
223
|
+
try {
|
|
224
|
+
const result = await hook({ root })
|
|
225
|
+
if (types.isProxy(result) || !Array.isArray(result)) {
|
|
226
|
+
throw new TypeError("assets must return an array")
|
|
227
|
+
}
|
|
228
|
+
const arrayDescriptors = Object.getOwnPropertyDescriptors(result)
|
|
229
|
+
if (
|
|
230
|
+
Reflect.ownKeys(arrayDescriptors).length !== result.length + 1 ||
|
|
231
|
+
Object.keys(arrayDescriptors).some(key => key !== "length" && !/^(0|[1-9]\d*)$/.test(key))
|
|
232
|
+
) {
|
|
233
|
+
throw new TypeError("assets must return a dense array without extra properties")
|
|
234
|
+
}
|
|
235
|
+
const entries = Array.from({ length: result.length }, (_, index) => {
|
|
236
|
+
const descriptor = arrayDescriptors[index]
|
|
237
|
+
if (!descriptor?.enumerable || !Object.hasOwn(descriptor, "value")) {
|
|
238
|
+
throw new TypeError(`assets[${index}] must be a data property`)
|
|
239
|
+
}
|
|
240
|
+
const entry = descriptor.value
|
|
241
|
+
const label = `assets[${index}]`
|
|
242
|
+
if (types.isProxy(entry) || !isRecord(entry))
|
|
243
|
+
throw new TypeError(`${label} must be an object`)
|
|
244
|
+
const descriptors = Object.getOwnPropertyDescriptors(entry)
|
|
245
|
+
const keys = Reflect.ownKeys(descriptors)
|
|
246
|
+
for (const key of keys) {
|
|
247
|
+
const property = descriptors[key]
|
|
248
|
+
if (typeof key !== "string" || !property.enumerable || !Object.hasOwn(property, "value")) {
|
|
249
|
+
throw new TypeError(`${label} must contain enumerable string-keyed data properties`)
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
const values = Object.fromEntries(keys.map(key => [key, descriptors[key].value]))
|
|
253
|
+
rejectUnknownKeys(values, new Set(["path", "source", "content"]), label)
|
|
254
|
+
const safePath = (value, name) => {
|
|
255
|
+
if (
|
|
256
|
+
typeof value !== "string" ||
|
|
257
|
+
value.includes("\\") ||
|
|
258
|
+
value.split("/").some(part => !isSafeSegment(part))
|
|
259
|
+
) {
|
|
260
|
+
throw new TypeError(`${label}.${name} must be a safe project-relative path`)
|
|
261
|
+
}
|
|
262
|
+
return value
|
|
263
|
+
}
|
|
264
|
+
const path = safePath(values.path, "path")
|
|
265
|
+
if (Object.hasOwn(values, "source") === Object.hasOwn(values, "content")) {
|
|
266
|
+
throw new TypeError(`${label} must specify exactly one of source or content`)
|
|
267
|
+
}
|
|
268
|
+
if (Object.hasOwn(values, "source")) {
|
|
269
|
+
const source = resolve(root, safePath(values.source, "source"))
|
|
270
|
+
assertInputOutputSeparation(source, settings.outputDirectory)
|
|
271
|
+
return { output: join(...path.split("/")), source }
|
|
272
|
+
}
|
|
273
|
+
if (
|
|
274
|
+
typeof values.content !== "string" &&
|
|
275
|
+
(!(values.content instanceof Uint8Array) || types.isProxy(values.content))
|
|
276
|
+
) {
|
|
277
|
+
throw new TypeError(`${label}.content must be a string or Uint8Array`)
|
|
278
|
+
}
|
|
279
|
+
return {
|
|
280
|
+
output: join(...path.split("/")),
|
|
281
|
+
content: typeof values.content === "string" ? values.content : Buffer.from(values.content),
|
|
282
|
+
}
|
|
283
|
+
})
|
|
284
|
+
for (const entry of entries) {
|
|
285
|
+
if (!entry.source) continue
|
|
286
|
+
onInputs?.(entry.source)
|
|
287
|
+
await assertSafeHookPath(root, entry.source)
|
|
288
|
+
if (!(await lstat(entry.source)).isFile()) {
|
|
289
|
+
throw new Error(`asset source must be a regular file: ${entry.source}`)
|
|
290
|
+
}
|
|
291
|
+
entry.content = await readFile(entry.source)
|
|
292
|
+
}
|
|
293
|
+
return entries
|
|
294
|
+
} catch (error) {
|
|
295
|
+
throw new Error(`assets hook (${settings.hooks.module}): ${error?.message ?? String(error)}`, {
|
|
296
|
+
cause: error,
|
|
297
|
+
})
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
216
301
|
async function assertSafeHookPath(root, path) {
|
|
217
302
|
let current = root
|
|
218
303
|
for (const part of relative(root, path).split(sep).filter(Boolean)) {
|
|
@@ -230,6 +315,7 @@ async function generateLoadedSite({
|
|
|
230
315
|
pages,
|
|
231
316
|
components,
|
|
232
317
|
templates = new Map(),
|
|
318
|
+
hookAssets = [],
|
|
233
319
|
}) {
|
|
234
320
|
settings.components = []
|
|
235
321
|
const names = new Set()
|
|
@@ -253,14 +339,20 @@ async function generateLoadedSite({
|
|
|
253
339
|
...(settings.publicDirectory
|
|
254
340
|
? await collectAssets(settings.publicDirectory, "", settings.observeDirectory)
|
|
255
341
|
: []),
|
|
342
|
+
...hookAssets,
|
|
256
343
|
]
|
|
257
344
|
const outputPlan = planOutput(pages, assets, settings)
|
|
258
345
|
settings.routes = new Map(pages.map(page => [page.path, page.route]))
|
|
259
346
|
settings.routesByRoute = new Set(pages.map(page => page.route))
|
|
260
|
-
settings.assetFiles = new Map(
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
347
|
+
settings.assetFiles = new Map()
|
|
348
|
+
for (const asset of assets) {
|
|
349
|
+
if (!asset.source) continue
|
|
350
|
+
if (settings.assetFiles.has(asset.source)) {
|
|
351
|
+
throw new Error(`Asset source is registered more than once: ${asset.source}`)
|
|
352
|
+
}
|
|
353
|
+
settings.assetFiles.set(asset.source, `/${asset.output.split(sep).join("/")}`)
|
|
354
|
+
}
|
|
355
|
+
settings.assetOutputs = new Set(assets.map(asset => `/${asset.output.split(sep).join("/")}`))
|
|
264
356
|
await validateOutput(root, settings.outputDirectory, { files: [] })
|
|
265
357
|
const transform =
|
|
266
358
|
settings.markdown === "unified" ? await loadUnifiedPreset(root) : settings.markdown
|