@backmatter/next 0.0.6
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/dist/config.cjs +405 -0
- package/dist/loader.cjs +349 -0
- package/package.json +60 -0
- package/src/cached-doc.ts +55 -0
- package/src/config.ts +160 -0
- package/src/create-backmatter.ts +15 -0
- package/src/devbar.tsx +280 -0
- package/src/index.ts +9 -0
- package/src/loader.ts +59 -0
- package/src/mdx-client.tsx +29 -0
- package/src/mdx-compile.ts +37 -0
- package/src/mdx.ts +13 -0
- package/src/middleware.ts +4 -0
- package/src/page-render.ts +111 -0
- package/src/preview-live.tsx +108 -0
- package/src/preview-refresh.tsx +17 -0
- package/src/preview-route.ts +138 -0
- package/src/preview-ui.ts +15 -0
- package/src/preview.ts +4 -0
- package/src/proxy.ts +24 -0
- package/src/revalidate.ts +64 -0
- package/src/routes.ts +226 -0
package/dist/config.cjs
ADDED
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
function __accessProp(key) {
|
|
8
|
+
return this[key];
|
|
9
|
+
}
|
|
10
|
+
var __toESMCache_node;
|
|
11
|
+
var __toESMCache_esm;
|
|
12
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
13
|
+
var canCache = mod != null && typeof mod === "object";
|
|
14
|
+
if (canCache) {
|
|
15
|
+
var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
|
|
16
|
+
var cached = cache.get(mod);
|
|
17
|
+
if (cached)
|
|
18
|
+
return cached;
|
|
19
|
+
}
|
|
20
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
21
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
22
|
+
if (mod && typeof mod === "object" || typeof mod === "function") {
|
|
23
|
+
for (let key of __getOwnPropNames(mod))
|
|
24
|
+
if (!__hasOwnProp.call(to, key))
|
|
25
|
+
__defProp(to, key, {
|
|
26
|
+
get: __accessProp.bind(mod, key),
|
|
27
|
+
enumerable: true
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
if (canCache)
|
|
31
|
+
cache.set(mod, to);
|
|
32
|
+
return to;
|
|
33
|
+
};
|
|
34
|
+
var __toCommonJS = (from) => {
|
|
35
|
+
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
36
|
+
if (entry)
|
|
37
|
+
return entry;
|
|
38
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
39
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
40
|
+
for (var key of __getOwnPropNames(from))
|
|
41
|
+
if (!__hasOwnProp.call(entry, key))
|
|
42
|
+
__defProp(entry, key, {
|
|
43
|
+
get: __accessProp.bind(from, key),
|
|
44
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
__moduleCache.set(from, entry);
|
|
48
|
+
return entry;
|
|
49
|
+
};
|
|
50
|
+
var __moduleCache;
|
|
51
|
+
var __returnValue = (v) => v;
|
|
52
|
+
function __exportSetter(name, newValue) {
|
|
53
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
54
|
+
}
|
|
55
|
+
var __export = (target, all) => {
|
|
56
|
+
for (var name in all)
|
|
57
|
+
__defProp(target, name, {
|
|
58
|
+
get: all[name],
|
|
59
|
+
enumerable: true,
|
|
60
|
+
configurable: true,
|
|
61
|
+
set: __exportSetter.bind(all, name)
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// src/config.ts
|
|
66
|
+
var exports_config = {};
|
|
67
|
+
__export(exports_config, {
|
|
68
|
+
LOADER: () => LOADER,
|
|
69
|
+
generateRegistry: () => generateRegistry,
|
|
70
|
+
registryMatchers: () => registryMatchers,
|
|
71
|
+
resolveLoaderOptions: () => resolveLoaderOptions,
|
|
72
|
+
warnMissingInstrumentation: () => warnMissingInstrumentation,
|
|
73
|
+
withBackmatter: () => withBackmatter
|
|
74
|
+
});
|
|
75
|
+
module.exports = __toCommonJS(exports_config);
|
|
76
|
+
var import_node_fs3 = require("node:fs");
|
|
77
|
+
var import_node_path3 = __toESM(require("node:path"));
|
|
78
|
+
|
|
79
|
+
// ../core/src/node/generate.ts
|
|
80
|
+
var import_node_fs2 = require("node:fs");
|
|
81
|
+
var import_node_path2 = __toESM(require("node:path"));
|
|
82
|
+
|
|
83
|
+
// ../core/src/config.ts
|
|
84
|
+
var DEFAULT_DIRS = {
|
|
85
|
+
models: "models",
|
|
86
|
+
blocks: "blocks",
|
|
87
|
+
mdx: "mdx",
|
|
88
|
+
layouts: "layouts"
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// ../core/src/node/discover.ts
|
|
92
|
+
var import_node_fs = require("node:fs");
|
|
93
|
+
var import_node_path = __toESM(require("node:path"));
|
|
94
|
+
var SOURCE_EXT = /\.(ts|tsx|js|jsx|mjs)$/;
|
|
95
|
+
var IGNORED = /(\.test\.|\.spec\.|\.d\.ts$|\.stories\.)/;
|
|
96
|
+
function toIdentifier(stem) {
|
|
97
|
+
const camel = stem.split(/[^A-Za-z0-9]+/).filter(Boolean).map((part, i) => i === 0 ? part : part[0].toUpperCase() + part.slice(1)).join("");
|
|
98
|
+
const safe = camel.replace(/^[^A-Za-z_$]/, (c) => `_${c}`);
|
|
99
|
+
return safe || "_";
|
|
100
|
+
}
|
|
101
|
+
function listSourceFiles(dir) {
|
|
102
|
+
if (!import_node_fs.existsSync(dir) || !import_node_fs.statSync(dir).isDirectory())
|
|
103
|
+
return [];
|
|
104
|
+
return import_node_fs.readdirSync(dir, { withFileTypes: true }).filter((e) => e.isFile() && SOURCE_EXT.test(e.name) && !IGNORED.test(e.name)).filter((e) => !e.name.startsWith("_") && !e.name.startsWith(".")).map((e) => import_node_path.default.join(dir, e.name)).sort();
|
|
105
|
+
}
|
|
106
|
+
function listModelFiles(root, modelsDir = "models") {
|
|
107
|
+
const files = listSourceFiles(import_node_path.default.resolve(root, modelsDir));
|
|
108
|
+
const seen = new Map;
|
|
109
|
+
return files.map((file) => {
|
|
110
|
+
const stem = import_node_path.default.basename(file).replace(SOURCE_EXT, "");
|
|
111
|
+
const importName = toIdentifier(stem);
|
|
112
|
+
const clash = seen.get(importName);
|
|
113
|
+
if (clash) {
|
|
114
|
+
throw new Error(`models/${import_node_path.default.basename(clash)} and models/${import_node_path.default.basename(file)} both map to "${importName}"`);
|
|
115
|
+
}
|
|
116
|
+
seen.set(importName, file);
|
|
117
|
+
return { file, stem, importName };
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
var JSX_TAG = /^[A-Z][A-Za-z0-9]*$/;
|
|
121
|
+
var FALLBACK_STEM = /^([A-Z][A-Za-z0-9]*)\.fallback$/;
|
|
122
|
+
function listPascalCaseFiles(root, dir) {
|
|
123
|
+
const files = [];
|
|
124
|
+
const skipped = [];
|
|
125
|
+
for (const file of listSourceFiles(import_node_path.default.resolve(root, dir))) {
|
|
126
|
+
const name = import_node_path.default.basename(file).replace(SOURCE_EXT, "");
|
|
127
|
+
if (FALLBACK_STEM.test(name))
|
|
128
|
+
continue;
|
|
129
|
+
if (JSX_TAG.test(name))
|
|
130
|
+
files.push({ file, name });
|
|
131
|
+
else
|
|
132
|
+
skipped.push(import_node_path.default.relative(root, file));
|
|
133
|
+
}
|
|
134
|
+
return { files, skipped };
|
|
135
|
+
}
|
|
136
|
+
function listBlockFallbackFiles(root, blocksDir = "blocks") {
|
|
137
|
+
const files = [];
|
|
138
|
+
for (const file of listSourceFiles(import_node_path.default.resolve(root, blocksDir))) {
|
|
139
|
+
const stem = import_node_path.default.basename(file).replace(SOURCE_EXT, "");
|
|
140
|
+
const match = FALLBACK_STEM.exec(stem);
|
|
141
|
+
if (match)
|
|
142
|
+
files.push({ file, name: match[1] });
|
|
143
|
+
}
|
|
144
|
+
return files.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
145
|
+
}
|
|
146
|
+
function listLayoutFiles(root, layoutsDir = "layouts") {
|
|
147
|
+
return listModelFiles(root, layoutsDir);
|
|
148
|
+
}
|
|
149
|
+
function listBlockFiles(root, blocksDir = "blocks") {
|
|
150
|
+
const { files, skipped } = listPascalCaseFiles(root, blocksDir);
|
|
151
|
+
return { blocks: files, skipped };
|
|
152
|
+
}
|
|
153
|
+
function listMdxFiles(root, mdxDir = "mdx") {
|
|
154
|
+
const { files, skipped } = listPascalCaseFiles(root, mdxDir);
|
|
155
|
+
return { blocks: files, skipped };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// ../core/src/node/generate.ts
|
|
159
|
+
var GENERATED_HEADER = "// Generated by backmatter — do not edit. Re-run `backmatter generate`.";
|
|
160
|
+
var DEFAULT_OUT_DIR = ".backmatter";
|
|
161
|
+
var CONFIG_FILES = ["backmatter.config.ts", "backmatter.config.mts", "backmatter.config.js", "backmatter.config.mjs"];
|
|
162
|
+
function importSpecifier(fromDir, file) {
|
|
163
|
+
const rel = import_node_path2.default.relative(fromDir, file).split(import_node_path2.default.sep).join("/").replace(/\.(ts|tsx|mts|js|jsx|mjs)$/, "");
|
|
164
|
+
return rel.startsWith(".") ? rel : `./${rel}`;
|
|
165
|
+
}
|
|
166
|
+
function findConfigFile(root) {
|
|
167
|
+
for (const name of CONFIG_FILES) {
|
|
168
|
+
const full = import_node_path2.default.join(root, name);
|
|
169
|
+
if (import_node_fs2.existsSync(full))
|
|
170
|
+
return full;
|
|
171
|
+
}
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
function detectSdk(root) {
|
|
175
|
+
try {
|
|
176
|
+
const pkg = JSON.parse(import_node_fs2.readFileSync(import_node_path2.default.join(root, "package.json"), "utf-8"));
|
|
177
|
+
const deps = { ...pkg.dependencies, ...pkg.devDependencies };
|
|
178
|
+
if (deps["@backmatter/next"])
|
|
179
|
+
return "@backmatter/next";
|
|
180
|
+
} catch {}
|
|
181
|
+
return "@backmatter/core/node";
|
|
182
|
+
}
|
|
183
|
+
function renderModelsBarrel(input) {
|
|
184
|
+
const lines = [GENERATED_HEADER, `import { createBackmatter } from "${input.sdk}";`];
|
|
185
|
+
if (input.configFile) {
|
|
186
|
+
lines.push(`import config from "${importSpecifier(input.outDir, input.configFile)}";`);
|
|
187
|
+
} else {
|
|
188
|
+
lines.push("const config = {};");
|
|
189
|
+
}
|
|
190
|
+
for (const m of input.models) {
|
|
191
|
+
lines.push(`import ${m.importName} from "${importSpecifier(input.outDir, m.file)}";`);
|
|
192
|
+
}
|
|
193
|
+
lines.push("");
|
|
194
|
+
lines.push(`export const models = [${input.models.map((m) => m.importName).join(", ")}] as const;`);
|
|
195
|
+
lines.push("export type Models = typeof models;");
|
|
196
|
+
lines.push("");
|
|
197
|
+
lines.push("/** Typed content client: cms.query.{type}, cms.getDoc, cms.listDocs. */");
|
|
198
|
+
lines.push("export const cms = createBackmatter({ ...config, models });");
|
|
199
|
+
return `${lines.join(`
|
|
200
|
+
`)}
|
|
201
|
+
`;
|
|
202
|
+
}
|
|
203
|
+
function renderSyncBarrel(input) {
|
|
204
|
+
const lines = [GENERATED_HEADER, `import { createBackmatter } from "${input.sdk}";`];
|
|
205
|
+
if (input.configFile) {
|
|
206
|
+
lines.push(`import config from "${importSpecifier(input.outDir, input.configFile)}";`);
|
|
207
|
+
} else {
|
|
208
|
+
lines.push("const config = {};");
|
|
209
|
+
}
|
|
210
|
+
for (const m of input.models) {
|
|
211
|
+
lines.push(`import ${m.importName} from "${importSpecifier(input.outDir, m.file)}";`);
|
|
212
|
+
}
|
|
213
|
+
for (const b of input.blocks) {
|
|
214
|
+
lines.push(`import ${b.name} from "${importSpecifier(input.outDir, b.file)}";`);
|
|
215
|
+
}
|
|
216
|
+
lines.push("");
|
|
217
|
+
lines.push(`export const models = [${input.models.map((m) => m.importName).join(", ")}] as const;`);
|
|
218
|
+
lines.push("export type Models = typeof models;");
|
|
219
|
+
lines.push("");
|
|
220
|
+
lines.push(`export const blocks = [${input.blocks.map((b) => b.name).join(", ")}] as const;`);
|
|
221
|
+
lines.push("");
|
|
222
|
+
lines.push("/** Typed content client: cms.query.{type}, cms.getDoc, cms.listDocs. */");
|
|
223
|
+
lines.push("export const cms = createBackmatter({ ...config, models, blocks });");
|
|
224
|
+
return `${lines.join(`
|
|
225
|
+
`)}
|
|
226
|
+
`;
|
|
227
|
+
}
|
|
228
|
+
function renderMdxBarrel(input) {
|
|
229
|
+
const lines = [GENERATED_HEADER];
|
|
230
|
+
for (const b of input.mdx) {
|
|
231
|
+
lines.push(`import ${b.name} from "${importSpecifier(input.outDir, b.file)}";`);
|
|
232
|
+
}
|
|
233
|
+
lines.push("");
|
|
234
|
+
lines.push("/** MDX component map: <Hero /> in content resolves to mdx/Hero.tsx. */");
|
|
235
|
+
const names = input.mdx.map((b) => b.name).join(", ");
|
|
236
|
+
lines.push(`export const components = ${names ? `{ ${names} }` : "{}"};`);
|
|
237
|
+
return `${lines.join(`
|
|
238
|
+
`)}
|
|
239
|
+
`;
|
|
240
|
+
}
|
|
241
|
+
function renderBlocksBarrel(input) {
|
|
242
|
+
const lines = [GENERATED_HEADER, ""];
|
|
243
|
+
lines.push('/** Composer loaders: JSON `{ type: "Hero" }` → dynamic import of blocks/Hero.tsx. */');
|
|
244
|
+
const entries = [...input.blocks].sort((a, b) => a.name.localeCompare(b.name)).map((b) => `${b.name}: Object.assign(() => import("${importSpecifier(input.outDir, b.file)}"), { $$backmatterLoader: true })`);
|
|
245
|
+
lines.push(`export const blockLoaders = ${entries.length ? `{ ${entries.join(", ")} }` : "{}"};`);
|
|
246
|
+
return `${lines.join(`
|
|
247
|
+
`)}
|
|
248
|
+
`;
|
|
249
|
+
}
|
|
250
|
+
function renderBlockFallbacksBarrel(input) {
|
|
251
|
+
const lines = [GENERATED_HEADER];
|
|
252
|
+
for (const b of input.fallbacks) {
|
|
253
|
+
lines.push(`import ${b.name} from "${importSpecifier(input.outDir, b.file)}";`);
|
|
254
|
+
}
|
|
255
|
+
lines.push("");
|
|
256
|
+
const names = input.fallbacks.map((b) => b.name).join(", ");
|
|
257
|
+
lines.push(`export const blockFallbacks = ${names ? `{ ${names} }` : "{}"};`);
|
|
258
|
+
return `${lines.join(`
|
|
259
|
+
`)}
|
|
260
|
+
`;
|
|
261
|
+
}
|
|
262
|
+
function renderLayoutsBarrel(input) {
|
|
263
|
+
const lines = [GENERATED_HEADER];
|
|
264
|
+
for (const layout of input.layouts) {
|
|
265
|
+
lines.push(`import ${layout.importName} from "${importSpecifier(input.outDir, layout.file)}";`);
|
|
266
|
+
}
|
|
267
|
+
lines.push("");
|
|
268
|
+
lines.push("/** Per-type wrappers: layouts/{type}.tsx wraps that model's catch-all body. */");
|
|
269
|
+
const entries = input.layouts.map((l) => l.importName === l.stem ? l.stem : `${JSON.stringify(l.stem)}: ${l.importName}`);
|
|
270
|
+
lines.push(`export const layouts = ${entries.length ? `{ ${entries.join(", ")} }` : "{}"};`);
|
|
271
|
+
return `${lines.join(`
|
|
272
|
+
`)}
|
|
273
|
+
`;
|
|
274
|
+
}
|
|
275
|
+
function differsOnDisk(file, content) {
|
|
276
|
+
return !import_node_fs2.existsSync(file) || import_node_fs2.readFileSync(file, "utf-8") !== content;
|
|
277
|
+
}
|
|
278
|
+
var REGISTRY_FILES = [
|
|
279
|
+
"index.ts",
|
|
280
|
+
"blocks.ts",
|
|
281
|
+
"block-fallbacks.ts",
|
|
282
|
+
"mdx.ts",
|
|
283
|
+
"layouts.ts",
|
|
284
|
+
"sync.ts"
|
|
285
|
+
];
|
|
286
|
+
function generate(options) {
|
|
287
|
+
const root = import_node_path2.default.resolve(options.root);
|
|
288
|
+
const outDir = import_node_path2.default.resolve(root, options.outDir ?? DEFAULT_OUT_DIR);
|
|
289
|
+
const models = listModelFiles(root, options.modelsDir ?? DEFAULT_DIRS.models);
|
|
290
|
+
const { blocks, skipped: skippedBlocks } = listBlockFiles(root, options.blocksDir ?? DEFAULT_DIRS.blocks);
|
|
291
|
+
const fallbacks = listBlockFallbackFiles(root, options.blocksDir ?? DEFAULT_DIRS.blocks);
|
|
292
|
+
const { blocks: mdx, skipped: skippedMdx } = listMdxFiles(root, options.mdxDir ?? DEFAULT_DIRS.mdx);
|
|
293
|
+
const layouts = listLayoutFiles(root, options.layoutsDir ?? DEFAULT_DIRS.layouts);
|
|
294
|
+
const sdk = options.sdk ?? detectSdk(root);
|
|
295
|
+
const configFile = findConfigFile(root);
|
|
296
|
+
const write = options.write ?? true;
|
|
297
|
+
const rendered = {
|
|
298
|
+
"index.ts": renderModelsBarrel({ outDir, models, configFile, sdk }),
|
|
299
|
+
"blocks.ts": renderBlocksBarrel({ outDir, blocks }),
|
|
300
|
+
"block-fallbacks.ts": renderBlockFallbacksBarrel({ outDir, fallbacks }),
|
|
301
|
+
"mdx.ts": renderMdxBarrel({ outDir, mdx }),
|
|
302
|
+
"layouts.ts": renderLayoutsBarrel({ outDir, layouts }),
|
|
303
|
+
"sync.ts": renderSyncBarrel({ outDir, models, blocks, configFile, sdk })
|
|
304
|
+
};
|
|
305
|
+
const outputs = REGISTRY_FILES.map((name) => {
|
|
306
|
+
const file = import_node_path2.default.join(outDir, name);
|
|
307
|
+
const content = rendered[name];
|
|
308
|
+
const changed = differsOnDisk(file, content);
|
|
309
|
+
if (changed && write) {
|
|
310
|
+
import_node_fs2.mkdirSync(import_node_path2.default.dirname(file), { recursive: true });
|
|
311
|
+
import_node_fs2.writeFileSync(file, content);
|
|
312
|
+
}
|
|
313
|
+
return { file, rel: import_node_path2.default.relative(root, file).split(import_node_path2.default.sep).join("/"), content, changed };
|
|
314
|
+
});
|
|
315
|
+
return {
|
|
316
|
+
outputs,
|
|
317
|
+
files: outputs.map((o) => o.rel),
|
|
318
|
+
changed: outputs.filter((o) => o.changed).map((o) => o.rel),
|
|
319
|
+
models,
|
|
320
|
+
blocks,
|
|
321
|
+
mdx,
|
|
322
|
+
layouts,
|
|
323
|
+
skippedBlocks,
|
|
324
|
+
skippedMdx,
|
|
325
|
+
configFile
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// src/config.ts
|
|
330
|
+
var LOADER = "@backmatter/next/loader";
|
|
331
|
+
var SDK = "@backmatter/next";
|
|
332
|
+
function warnMissingInstrumentation(root) {
|
|
333
|
+
if (detectSdk(root) !== "@backmatter/next")
|
|
334
|
+
return;
|
|
335
|
+
const hasRoot = import_node_fs3.existsSync(import_node_path3.default.join(root, "instrumentation.ts"));
|
|
336
|
+
const hasSrc = import_node_fs3.existsSync(import_node_path3.default.join(root, "src", "instrumentation.ts"));
|
|
337
|
+
if (!hasRoot && !hasSrc) {
|
|
338
|
+
console.warn("backmatter: add instrumentation.ts that imports .backmatter/sync — block schema sync will not run without it");
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
function defaultLog(result, root) {
|
|
342
|
+
for (const file of result.changed)
|
|
343
|
+
console.log(`backmatter: wrote ${file}`);
|
|
344
|
+
for (const skipped of result.skippedBlocks) {
|
|
345
|
+
console.warn(`backmatter: skipped ${skipped} (block file names must be PascalCase)`);
|
|
346
|
+
}
|
|
347
|
+
for (const skipped of result.skippedMdx) {
|
|
348
|
+
console.warn(`backmatter: skipped ${skipped} (MDX file names must be PascalCase)`);
|
|
349
|
+
}
|
|
350
|
+
warnMissingInstrumentation(root);
|
|
351
|
+
}
|
|
352
|
+
function resolveLoaderOptions(options = {}) {
|
|
353
|
+
return {
|
|
354
|
+
root: import_node_path3.default.resolve(options.root ?? process.cwd()),
|
|
355
|
+
outDir: options.outDir ?? DEFAULT_OUT_DIR,
|
|
356
|
+
modelsDir: options.modelsDir ?? DEFAULT_DIRS.models,
|
|
357
|
+
blocksDir: options.blocksDir ?? DEFAULT_DIRS.blocks,
|
|
358
|
+
mdxDir: options.mdxDir ?? DEFAULT_DIRS.mdx,
|
|
359
|
+
layoutsDir: options.layoutsDir ?? DEFAULT_DIRS.layouts,
|
|
360
|
+
sdk: SDK
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
function generateRegistry(options = {}) {
|
|
364
|
+
const loaderOptions = resolveLoaderOptions(options);
|
|
365
|
+
const result = generate(loaderOptions);
|
|
366
|
+
const root = loaderOptions.root;
|
|
367
|
+
(options.onGenerate ?? ((r) => defaultLog(r, root)))(result);
|
|
368
|
+
return result;
|
|
369
|
+
}
|
|
370
|
+
function registryMatchers(loaderOptions) {
|
|
371
|
+
const rel = import_node_path3.default.relative(loaderOptions.root, import_node_path3.default.resolve(loaderOptions.root, loaderOptions.outDir)).split(import_node_path3.default.sep).join("/");
|
|
372
|
+
const escaped = rel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
373
|
+
return {
|
|
374
|
+
glob: `**/${rel}/*.ts`,
|
|
375
|
+
test: new RegExp(`(^|[\\\\/])${escaped.replace(/\//g, "[\\\\/]")}[\\\\/](index|blocks|block-fallbacks|mdx|layouts|sync)\\.ts$`)
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
function withBackmatter(nextConfig = {}, options = {}) {
|
|
379
|
+
const loaderOptions = resolveLoaderOptions(options);
|
|
380
|
+
generateRegistry(options);
|
|
381
|
+
const { glob, test } = registryMatchers(loaderOptions);
|
|
382
|
+
const use = { loader: LOADER, options: loaderOptions };
|
|
383
|
+
const cfg = nextConfig;
|
|
384
|
+
const existingTurbo = cfg.turbopack ?? {};
|
|
385
|
+
const existingRules = existingTurbo.rules ?? {};
|
|
386
|
+
const turbopackRule = { loaders: [use] };
|
|
387
|
+
const turbopack = {
|
|
388
|
+
...existingTurbo,
|
|
389
|
+
rules: { ...existingRules, [glob]: turbopackRule }
|
|
390
|
+
};
|
|
391
|
+
const webpack = (config, context) => {
|
|
392
|
+
config.module ??= {};
|
|
393
|
+
config.module.rules ??= [];
|
|
394
|
+
config.module.rules.push({ test, enforce: "pre", use: [use] });
|
|
395
|
+
return cfg.webpack ? cfg.webpack(config, context) : config;
|
|
396
|
+
};
|
|
397
|
+
const cacheComponents = cfg.cacheComponents ?? true;
|
|
398
|
+
return {
|
|
399
|
+
...nextConfig,
|
|
400
|
+
cacheComponents,
|
|
401
|
+
partialPrefetching: cfg.partialPrefetching ?? cacheComponents,
|
|
402
|
+
turbopack,
|
|
403
|
+
webpack
|
|
404
|
+
};
|
|
405
|
+
}
|