@farming-labs/next 0.0.58 → 0.0.60

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.
@@ -1,7 +1,7 @@
1
1
  "use client";
2
2
 
3
3
  import { jsx } from "react/jsx-runtime";
4
- import docsConfig from "@/docs.config";
4
+ import docsConfig from "@farming-labs/next-internal-docs-config";
5
5
  import { DocsClientHooks } from "@farming-labs/theme/client-hooks";
6
6
 
7
7
  //#region src/client-callbacks.tsx
package/dist/config.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
2
- import { join, relative } from "node:path";
2
+ import { join } from "node:path";
3
3
  import createMDX from "@next/mdx";
4
4
 
5
5
  //#region src/config.ts
@@ -31,7 +31,6 @@ function getNextAppDir(root) {
31
31
  return "app";
32
32
  }
33
33
  const GENERATED_BANNER = "// Auto-generated by @farming-labs/next — do not edit manually.\n";
34
- const GENERATED_CSS_BANNER = "/* Auto-generated by @farming-labs/next — do not edit manually. */\n";
35
34
  const MDX_COMPONENTS_TEMPLATE = `\
36
35
  ${GENERATED_BANNER}
37
36
  import { getMDXComponents } from "@farming-labs/theme/mdx";
@@ -55,7 +54,6 @@ const DOCS_LAYOUT_TEMPLATE = `\
55
54
  ${GENERATED_BANNER}
56
55
  import docsConfig from "@/docs.config";
57
56
  import { createNextDocsLayout, createNextDocsMetadata } from "@farming-labs/next/layout";
58
- import "./docs-theme.css";
59
57
 
60
58
  export const metadata = createNextDocsMetadata(docsConfig);
61
59
 
@@ -77,13 +75,6 @@ export const { GET, POST } = createDocsAPI({
77
75
 
78
76
  export const revalidate = false;
79
77
  `;
80
- function docsThemeCssTemplate(cssImport) {
81
- return `\
82
- ${GENERATED_CSS_BANNER}
83
- @reference "tailwindcss";
84
- @import "${cssImport}";
85
- `;
86
- }
87
78
  const API_REFERENCE_ROUTE_TEMPLATE = `\
88
79
  ${GENERATED_BANNER}
89
80
  import docsConfig from "@/docs.config";
@@ -99,6 +90,7 @@ const FILE_EXTS = [
99
90
  "jsx",
100
91
  "js"
101
92
  ];
93
+ const INTERNAL_DOCS_CONFIG_ALIAS = "@farming-labs/next-internal-docs-config";
102
94
  function hasFile(root, baseName) {
103
95
  return FILE_EXTS.some((ext) => existsSync(join(root, `${baseName}.${ext}`)));
104
96
  }
@@ -121,6 +113,13 @@ function readDocsEntry(root) {
121
113
  }
122
114
  return "docs";
123
115
  }
116
+ function readDocsConfigPath(root) {
117
+ for (const ext of FILE_EXTS) {
118
+ const relativePath = `docs.config.${ext}`;
119
+ if (existsSync(join(root, relativePath))) return relativePath;
120
+ }
121
+ return "docs.config.ts";
122
+ }
124
123
  /** Read the OG endpoint from docs.config.ts[x] (returns undefined if not set). */
125
124
  function readOgEndpoint(root) {
126
125
  for (const ext of FILE_EXTS) {
@@ -188,67 +187,11 @@ function extractObjectLiteral(content, key) {
188
187
  if (depth === 0) return content.slice(braceStart + 1, index);
189
188
  }
190
189
  }
191
- function parseImportSource(content, localName) {
192
- for (const match of content.matchAll(/import\s+([^;]+?)\s+from\s+["']([^"']+)["'];?/g)) {
193
- const clause = match[1]?.trim();
194
- const source = match[2];
195
- if (!clause || !source) continue;
196
- if (clause.startsWith("* as ")) {
197
- if (clause.slice(5).trim() === localName) return source;
198
- continue;
199
- }
200
- const namedMatch = clause.match(/\{([\s\S]+)\}/);
201
- if (namedMatch) {
202
- const entries = namedMatch[1].split(",");
203
- for (const entry of entries) {
204
- const part = entry.trim();
205
- if (!part) continue;
206
- const aliasMatch = part.match(/^([A-Za-z_$][\w$]*)\s+as\s+([A-Za-z_$][\w$]*)$/);
207
- if ((aliasMatch ? aliasMatch[2] : part) === localName) return source;
208
- }
209
- }
210
- const defaultClause = clause.split(",")[0]?.trim();
211
- if (defaultClause && !defaultClause.startsWith("{") && !defaultClause.startsWith("* as ") && defaultClause === localName) return source;
212
- }
213
- }
214
- function resolveThemeCssImportPath(source, root, layoutDir) {
215
- const builtIn = {
216
- "@farming-labs/theme": "@farming-labs/theme/default/css",
217
- "@farming-labs/theme/default": "@farming-labs/theme/default/css",
218
- "@farming-labs/theme/colorful": "@farming-labs/theme/colorful/css",
219
- "@farming-labs/theme/darksharp": "@farming-labs/theme/darksharp/css",
220
- "@farming-labs/theme/pixel-border": "@farming-labs/theme/pixel-border/css",
221
- "@farming-labs/theme/shiny": "@farming-labs/theme/shiny/css",
222
- "@farming-labs/theme/darkbold": "@farming-labs/theme/darkbold/css",
223
- "@farming-labs/theme/greentree": "@farming-labs/theme/greentree/css"
224
- }[source];
225
- if (builtIn) return builtIn;
226
- let resolvedModulePath;
227
- if (source.startsWith("@/")) resolvedModulePath = join(root, source.slice(2));
228
- else if (source.startsWith("./") || source.startsWith("../")) resolvedModulePath = join(root, source);
229
- if (!resolvedModulePath) return void 0;
230
- const cssPath = resolvedModulePath.replace(/\.(tsx?|jsx?|mts|cts)$/i, ".css").replace(/$/, "");
231
- const rel = relative(layoutDir, /\.(css)$/i.test(cssPath) ? cssPath : `${cssPath}.css`).replaceAll("\\", "/");
232
- return rel.startsWith(".") ? rel : `./${rel}`;
233
- }
234
- function readThemeCssImport(root, layoutDir) {
235
- for (const ext of FILE_EXTS) {
236
- const configPath = join(root, `docs.config.${ext}`);
237
- if (!existsSync(configPath)) continue;
238
- try {
239
- const content = readFileSync(configPath, "utf-8");
240
- const themeIdentifier = content.match(/theme\s*:\s*([A-Za-z_$][\w$]*)\s*\(/)?.[1];
241
- if (!themeIdentifier) continue;
242
- const source = parseImportSource(content, themeIdentifier);
243
- if (!source) continue;
244
- const cssImport = resolveThemeCssImportPath(source, root, layoutDir);
245
- if (cssImport) return cssImport;
246
- } catch {}
247
- }
248
- return "@farming-labs/theme/default/css";
249
- }
250
190
  function withDocs(nextConfig = {}) {
251
191
  const root = process.cwd();
192
+ const docsConfigPath = readDocsConfigPath(root);
193
+ const docsConfigAbsolutePath = join(root, docsConfigPath);
194
+ const docsConfigRelativeAlias = docsConfigPath.startsWith("./") || docsConfigPath.startsWith("../") ? docsConfigPath : `./${docsConfigPath}`;
252
195
  if (!hasFile(root, "mdx-components")) writeFileSync(join(root, "mdx-components.tsx"), MDX_COMPONENTS_TEMPLATE);
253
196
  const entry = readDocsEntry(root);
254
197
  const appDir = getNextAppDir(root);
@@ -256,7 +199,6 @@ function withDocs(nextConfig = {}) {
256
199
  if (!existsSync(layoutDir)) mkdirSync(layoutDir, { recursive: true });
257
200
  const docsLayoutPath = join(layoutDir, "layout.tsx");
258
201
  if (!hasFile(layoutDir, "layout") || isManagedGeneratedFile(docsLayoutPath)) writeFileSync(join(layoutDir, "layout.tsx"), DOCS_LAYOUT_TEMPLATE);
259
- writeFileSync(join(layoutDir, "docs-theme.css"), docsThemeCssTemplate(readThemeCssImport(root, layoutDir)));
260
202
  const isStaticExport = nextConfig.output === "export";
261
203
  const docsApiRouteDir = join(root, appDir, "api", "docs");
262
204
  if (!isStaticExport && !hasFile(docsApiRouteDir, "route")) {
@@ -297,6 +239,23 @@ function withDocs(nextConfig = {}) {
297
239
  if (userExts) {
298
240
  for (const ext of ["md", "mdx"]) if (!userExts.includes(ext)) userExts.push(ext);
299
241
  } else nextConfig.pageExtensions = defaultExts;
242
+ const existingTurbopack = nextConfig.turbopack ?? {};
243
+ const existingResolveAlias = existingTurbopack.resolveAlias ?? {};
244
+ nextConfig.turbopack = {
245
+ ...existingTurbopack,
246
+ resolveAlias: {
247
+ ...existingResolveAlias,
248
+ [INTERNAL_DOCS_CONFIG_ALIAS]: docsConfigRelativeAlias
249
+ }
250
+ };
251
+ const userWebpack = nextConfig.webpack;
252
+ nextConfig.webpack = (config, options) => {
253
+ const resolvedConfig = userWebpack ? userWebpack(config, options) : config;
254
+ resolvedConfig.resolve ??= {};
255
+ resolvedConfig.resolve.alias ??= {};
256
+ resolvedConfig.resolve.alias[INTERNAL_DOCS_CONFIG_ALIAS] = docsConfigAbsolutePath;
257
+ return resolvedConfig;
258
+ };
300
259
  return withMDX(nextConfig);
301
260
  }
302
261
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@farming-labs/next",
3
- "version": "0.0.58",
3
+ "version": "0.0.60",
4
4
  "description": "Next.js adapter for @farming-labs/docs — MDX config wrapper",
5
5
  "keywords": [
6
6
  "docs",
@@ -79,8 +79,8 @@
79
79
  "tsdown": "^0.20.3",
80
80
  "typescript": "^5.9.3",
81
81
  "vitest": "^3.2.4",
82
- "@farming-labs/docs": "0.0.58",
83
- "@farming-labs/theme": "0.0.58"
82
+ "@farming-labs/docs": "0.0.60",
83
+ "@farming-labs/theme": "0.0.60"
84
84
  },
85
85
  "peerDependencies": {
86
86
  "@farming-labs/docs": ">=0.0.1",