@allejo/decap-extras 0.0.1 → 0.0.2

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.
@@ -0,0 +1,26 @@
1
+ name: Publish Package
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - 'v*'
7
+
8
+ permissions:
9
+ id-token: write # Required for OIDC
10
+ contents: read
11
+
12
+ jobs:
13
+ publish:
14
+ runs-on: ubuntu-latest
15
+ steps:
16
+ - uses: actions/checkout@v6
17
+
18
+ - uses: actions/setup-node@v6
19
+ with:
20
+ node-version: '24'
21
+ registry-url: 'https://registry.npmjs.org'
22
+ package-manager-cache: false
23
+ - run: npm ci
24
+ - run: npm run build --if-present
25
+ - run: npm test
26
+ - run: npm publish
package/README.md CHANGED
@@ -250,6 +250,49 @@ function getPageProps<F extends PageName>(
250
250
 
251
251
  ---
252
252
 
253
+ ## Server utilities
254
+
255
+ A server-side utility for getting CSS from Next.js build manifests. This is useful for registering CSS stylesheets into Decap's CMS preview system.
256
+
257
+ ```ts
258
+ import { getCssFilesFromManifest } from '@allejo/decap-extras/server';
259
+ ```
260
+
261
+ Use `getCssFilesFromManifest` in `getStaticProps` to collect the CSS paths needed by your page and pass them as props:
262
+
263
+ ```ts
264
+ export async function getStaticProps() {
265
+ const cssFiles = getCssFilesFromManifest(process.env.NODE_ENV, [
266
+ '/',
267
+ '/_app',
268
+ ]);
269
+
270
+ return {
271
+ props: {
272
+ cssFiles: cssFiles['flattenedCssFiles'],
273
+ },
274
+ };
275
+ }
276
+ ```
277
+
278
+ Then register each path as a preview stylesheet in your Decap CMS admin component:
279
+
280
+ ```ts
281
+ export default function Admin({ cssFiles }: Props) {
282
+ // ...
283
+
284
+ cssFiles.forEach((css) => {
285
+ cms.registerPreviewStyle(css);
286
+ });
287
+ }
288
+ ```
289
+
290
+ > **Note:** This utility reads `.next/build-manifest.json` (or `.next/dev/build-manifest.json` in development) and is intended for Next.js projects only.
291
+
292
+ ---
293
+
294
+ ## API reference
295
+
253
296
  ### Widget functions
254
297
 
255
298
  | Function | Decap widget | TypeScript type |
@@ -283,6 +326,12 @@ function getPageProps<F extends PageName>(
283
326
  | `OptionalWidget<T>` | Brands a `CmsField` as optional (added by `optional()`). |
284
327
  | `WidgetOpts<T>` | The options type for a given widget field type, with `widget` and `fields` stripped. |
285
328
 
329
+ ### Server utilities
330
+
331
+ | Function | Description |
332
+ | ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
333
+ | `getCssFilesFromManifest(env, pages)` | Reads the Next.js build manifest and returns CSS paths for the given page routes, grouped by page (`allCssFiles`) and as a deduplicated flat list (`flattenedCssFiles`). |
334
+
286
335
  ## License
287
336
 
288
337
  MIT
@@ -0,0 +1,27 @@
1
+ //#region src/server.d.ts
2
+ /**
3
+ * Reads the Next.js build manifest and extracts CSS file paths for the given
4
+ * pages, resolving the manifest location based on the current environment.
5
+ *
6
+ * In development, reads from `.next/dev/build-manifest.json`; in all other
7
+ * environments, reads from `.next/build-manifest.json`.
8
+ *
9
+ * CSS paths are rewritten from the `static/` prefix used internally by Next.js
10
+ * to the `/_next/static/` public URL path.
11
+ *
12
+ * @param env - The current environment (e.g. `"development"` or `"production"`).
13
+ * @param pages - List of Next.js page routes to extract CSS files for
14
+ * (e.g. `["/", "/_app"]`).
15
+ *
16
+ * @returns An object with two properties:
17
+ * - `allCssFiles` — a map of page route to its CSS file paths.
18
+ * - `flattenedCssFiles` — a deduplicated flat list of all CSS file paths
19
+ * across every requested page.
20
+ */
21
+ declare function getCssFilesFromManifest(env: string, pages: string[]): {
22
+ flattenedCssFiles: string[];
23
+ allCssFiles: Record<string, string[]>;
24
+ };
25
+ //#endregion
26
+ export { getCssFilesFromManifest };
27
+ //# sourceMappingURL=server.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.d.mts","names":[],"sources":["../src/server.ts"],"mappings":";;AAsBA;;;;;;;;;;;;;;;;;;iBAAgB,uBAAA,CAAwB,GAAA,UAAa,KAAA;;eAAd,MAAA;AAAA"}
@@ -0,0 +1,45 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+
4
+ //#region src/server.ts
5
+ /**
6
+ * Reads the Next.js build manifest and extracts CSS file paths for the given
7
+ * pages, resolving the manifest location based on the current environment.
8
+ *
9
+ * In development, reads from `.next/dev/build-manifest.json`; in all other
10
+ * environments, reads from `.next/build-manifest.json`.
11
+ *
12
+ * CSS paths are rewritten from the `static/` prefix used internally by Next.js
13
+ * to the `/_next/static/` public URL path.
14
+ *
15
+ * @param env - The current environment (e.g. `"development"` or `"production"`).
16
+ * @param pages - List of Next.js page routes to extract CSS files for
17
+ * (e.g. `["/", "/_app"]`).
18
+ *
19
+ * @returns An object with two properties:
20
+ * - `allCssFiles` — a map of page route to its CSS file paths.
21
+ * - `flattenedCssFiles` — a deduplicated flat list of all CSS file paths
22
+ * across every requested page.
23
+ */
24
+ function getCssFilesFromManifest(env, pages) {
25
+ const manifestPath = env === "development" ? path.join(process.cwd(), ".next", "dev", "build-manifest.json") : path.join(process.cwd(), ".next", "build-manifest.json");
26
+ const buildManifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
27
+ const allCssFiles = {};
28
+ const flattenedCssFilesSet = /* @__PURE__ */ new Set();
29
+ pages.forEach((page) => {
30
+ buildManifest?.pages[page].filter((file) => file.endsWith(".css")).forEach((file) => {
31
+ if (!allCssFiles[page]) allCssFiles[page] = [];
32
+ const fullPath = file.replace("static/", "/_next/static/");
33
+ allCssFiles[page].push(fullPath);
34
+ flattenedCssFilesSet.add(fullPath);
35
+ });
36
+ });
37
+ return {
38
+ flattenedCssFiles: [...flattenedCssFilesSet],
39
+ allCssFiles
40
+ };
41
+ }
42
+
43
+ //#endregion
44
+ export { getCssFilesFromManifest };
45
+ //# sourceMappingURL=server.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.mjs","names":[],"sources":["../src/server.ts"],"sourcesContent":["import fs from 'node:fs';\nimport path from 'node:path';\n\n/**\n * Reads the Next.js build manifest and extracts CSS file paths for the given\n * pages, resolving the manifest location based on the current environment.\n *\n * In development, reads from `.next/dev/build-manifest.json`; in all other\n * environments, reads from `.next/build-manifest.json`.\n *\n * CSS paths are rewritten from the `static/` prefix used internally by Next.js\n * to the `/_next/static/` public URL path.\n *\n * @param env - The current environment (e.g. `\"development\"` or `\"production\"`).\n * @param pages - List of Next.js page routes to extract CSS files for\n * (e.g. `[\"/\", \"/_app\"]`).\n *\n * @returns An object with two properties:\n * - `allCssFiles` — a map of page route to its CSS file paths.\n * - `flattenedCssFiles` — a deduplicated flat list of all CSS file paths\n * across every requested page.\n */\nexport function getCssFilesFromManifest(env: string, pages: string[]) {\n\tconst manifestPath =\n\t\tenv === 'development'\n\t\t\t? path.join(process.cwd(), '.next', 'dev', 'build-manifest.json')\n\t\t\t: path.join(process.cwd(), '.next', 'build-manifest.json');\n\tconst buildManifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));\n\n\tconst allCssFiles: Record<string, string[]> = {};\n\tconst flattenedCssFilesSet = new Set<string>();\n\n\tpages.forEach((page) => {\n\t\tbuildManifest?.pages[page]\n\t\t\t.filter((file: string) => file.endsWith('.css'))\n\t\t\t.forEach((file: string) => {\n\t\t\t\tif (!allCssFiles[page]) {\n\t\t\t\t\tallCssFiles[page] = [];\n\t\t\t\t}\n\n\t\t\t\tconst fullPath = file.replace('static/', '/_next/static/');\n\n\t\t\t\tallCssFiles[page].push(fullPath);\n\t\t\t\tflattenedCssFilesSet.add(fullPath);\n\t\t\t});\n\t});\n\n\treturn {\n\t\tflattenedCssFiles: [...flattenedCssFilesSet],\n\t\tallCssFiles,\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,wBAAwB,KAAa,OAAiB;CACrE,MAAM,eACL,QAAQ,gBACL,KAAK,KAAK,QAAQ,IAAI,GAAG,SAAS,OAAO,qBAAqB,IAC9D,KAAK,KAAK,QAAQ,IAAI,GAAG,SAAS,qBAAqB;CAC3D,MAAM,gBAAgB,KAAK,MAAM,GAAG,aAAa,cAAc,MAAM,CAAC;CAEtE,MAAM,cAAwC,CAAC;CAC/C,MAAM,uCAAuB,IAAI,IAAY;CAE7C,MAAM,SAAS,SAAS;EACvB,eAAe,MAAM,MACnB,QAAQ,SAAiB,KAAK,SAAS,MAAM,CAAC,EAC9C,SAAS,SAAiB;GAC1B,IAAI,CAAC,YAAY,OAChB,YAAY,QAAQ,CAAC;GAGtB,MAAM,WAAW,KAAK,QAAQ,WAAW,gBAAgB;GAEzD,YAAY,MAAM,KAAK,QAAQ;GAC/B,qBAAqB,IAAI,QAAQ;EAClC,CAAC;CACH,CAAC;CAED,OAAO;EACN,mBAAmB,CAAC,GAAG,oBAAoB;EAC3C;CACD;AACD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@allejo/decap-extras",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "A TypeScript utility library for Decap CMS with utility types to derive types from Decap CMS configuration files.",
5
5
  "keywords": [
6
6
  "decapcms"
@@ -12,8 +12,16 @@
12
12
  "license": "MIT",
13
13
  "author": "Vladimir \"allejo\" Jimenez",
14
14
  "type": "module",
15
- "main": "./dist/index.mjs",
16
- "types": "./dist/index.d.mts",
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/index.d.mts",
18
+ "import": "./dist/index.mjs"
19
+ },
20
+ "./server": {
21
+ "types": "./dist/server.d.mts",
22
+ "import": "./dist/server.mjs"
23
+ }
24
+ },
17
25
  "scripts": {
18
26
  "build": "tsdown",
19
27
  "format": "prettier --write .",