@typestyles/next 0.2.0 → 0.5.0

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/build.js ADDED
@@ -0,0 +1,129 @@
1
+ // src/build.ts
2
+ import { existsSync } from "fs";
3
+ import { mkdir, writeFile } from "fs/promises";
4
+ import { createRequire } from "module";
5
+ import { dirname, join, resolve } from "path";
6
+ import { pathToFileURL } from "url";
7
+ import {
8
+ buildManifestV2,
9
+ collectAndWriteRouteCss,
10
+ discoverDefaultExtractModules
11
+ } from "@typestyles/build-runner";
12
+ import { collectStylesFromModules } from "typestyles/build";
13
+ import {
14
+ DEFAULT_EXTRACT_MODULE_CANDIDATES,
15
+ discoverDefaultExtractModules as discoverDefaultExtractModules2,
16
+ verifyTypestylesBuild,
17
+ VerifyTypestylesBuildError,
18
+ getRouteCss
19
+ } from "@typestyles/build-runner";
20
+ function resolveExtractModulesForNext(root, modules) {
21
+ if (modules !== void 0) {
22
+ return modules;
23
+ }
24
+ const found = discoverDefaultExtractModules(root);
25
+ if (found.length === 0) {
26
+ throw new Error(
27
+ `[typestyles] No extraction entry found under ${root}. Add a convention file (see DEFAULT_EXTRACT_MODULE_CANDIDATES from @typestyles/build-runner) or pass modules to buildTypestylesForNext.`
28
+ );
29
+ }
30
+ return found;
31
+ }
32
+ async function buildTypestylesForNext(options) {
33
+ const {
34
+ root,
35
+ modules,
36
+ cssOutFile = "app/typestyles.css",
37
+ manifestOutFile,
38
+ manifestCssPath,
39
+ routeCssOutDir = "app/_typestyles/routes",
40
+ appDir = "app"
41
+ } = options;
42
+ const resolvedModules = resolveExtractModulesForNext(root, modules);
43
+ const enableRouteCss = options.routeCss ?? discoverNextAppRoutesEnabled(root, appDir);
44
+ const loaders = resolvedModules.map((mod) => {
45
+ const abs = resolve(root, mod);
46
+ const href = pathToFileURL(abs).href;
47
+ return () => import(href);
48
+ });
49
+ const css = await collectStylesFromModules(loaders);
50
+ const outAbs = resolve(root, cssOutFile);
51
+ await mkdir(dirname(outAbs), { recursive: true });
52
+ await writeFile(outAbs, css, "utf8");
53
+ const manifestRel = manifestOutFile === false ? void 0 : manifestOutFile ?? "app/typestyles.manifest.json";
54
+ if (manifestRel) {
55
+ const cssPath = manifestCssPath ?? cssOutFile;
56
+ let manifest = {
57
+ version: 1,
58
+ css: cssPath
59
+ };
60
+ if (enableRouteCss) {
61
+ const { routes } = await collectAndWriteRouteCss({
62
+ root,
63
+ sharedModules: resolvedModules,
64
+ appDir,
65
+ routeCssOutDir,
66
+ collectCss: collectStylesFromModules
67
+ });
68
+ manifest = buildManifestV2(cssPath, routes);
69
+ }
70
+ const manAbs = resolve(root, manifestRel);
71
+ await mkdir(dirname(manAbs), { recursive: true });
72
+ await writeFile(manAbs, `${JSON.stringify(manifest, null, 2)}
73
+ `, "utf8");
74
+ }
75
+ }
76
+ function discoverNextAppRoutesEnabled(root, appDir) {
77
+ return existsSync(resolve(root, appDir));
78
+ }
79
+ function withTypestyles(nextConfig = {}, options = {}) {
80
+ const projectRoot = options.root ?? process.cwd();
81
+ const hasConventionEntry = discoverDefaultExtractModules(projectRoot).length > 0;
82
+ const prod = process.env.NODE_ENV === "production";
83
+ if (!prod || !hasConventionEntry) {
84
+ return nextConfig;
85
+ }
86
+ return withTypestylesExtract(nextConfig, options);
87
+ }
88
+ function withTypestylesExtract(nextConfig = {}, options = {}) {
89
+ const disableClientRuntime = options.disableClientRuntime !== false;
90
+ const projectRoot = options.root ?? process.cwd();
91
+ return {
92
+ ...nextConfig,
93
+ ...disableClientRuntime ? {
94
+ env: {
95
+ ...typeof nextConfig.env === "object" && nextConfig.env !== null && !Array.isArray(nextConfig.env) ? nextConfig.env : {},
96
+ NEXT_PUBLIC_TYPESTYLES_RUNTIME_DISABLED: "true"
97
+ }
98
+ } : {},
99
+ webpack(config, context) {
100
+ const nextWebpack = typeof nextConfig.webpack === "function" ? nextConfig.webpack(config, context) : config;
101
+ if (disableClientRuntime && !context.isServer) {
102
+ try {
103
+ const require2 = createRequire(join(projectRoot, "package.json"));
104
+ const webpackLib = require2("webpack");
105
+ nextWebpack.plugins.push(
106
+ new webpackLib.DefinePlugin({
107
+ __TYPESTYLES_RUNTIME_DISABLED__: JSON.stringify("true")
108
+ })
109
+ );
110
+ } catch {
111
+ console.warn(
112
+ "[@typestyles/next/build] Could not load webpack; client bundles may still inject <style>."
113
+ );
114
+ }
115
+ }
116
+ return nextWebpack;
117
+ }
118
+ };
119
+ }
120
+ export {
121
+ DEFAULT_EXTRACT_MODULE_CANDIDATES,
122
+ VerifyTypestylesBuildError,
123
+ buildTypestylesForNext,
124
+ discoverDefaultExtractModules2 as discoverDefaultExtractModules,
125
+ getRouteCss,
126
+ verifyTypestylesBuild,
127
+ withTypestyles,
128
+ withTypestylesExtract
129
+ };
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ "use client";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/client.tsx
22
+ var client_exports = {};
23
+ __export(client_exports, {
24
+ TypestylesStylesheet: () => TypestylesStylesheet,
25
+ createTypestylesLayout: () => createTypestylesLayout,
26
+ useServerInsertedHTML: () => import_react.useServerInsertedHTML,
27
+ useTypestyles: () => useTypestyles
28
+ });
29
+ module.exports = __toCommonJS(client_exports);
30
+ var import_react = require("react");
31
+ var import_server = require("typestyles/server");
32
+ var import_jsx_runtime = require("react/jsx-runtime");
33
+ function useTypestyles() {
34
+ return (0, import_react.useSyncExternalStore)(
35
+ import_server.subscribeRegisteredCss,
36
+ () => (0, import_server.getRegisteredCss)(),
37
+ () => (0, import_server.getRegisteredCss)()
38
+ );
39
+ }
40
+ function TypestylesStylesheet({ children }) {
41
+ const [css, setCss] = (0, import_react.useState)("");
42
+ (0, import_react.useServerInsertedHTML)(() => {
43
+ const registeredCss = (0, import_server.getRegisteredCss)();
44
+ if (registeredCss) {
45
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("style", { id: "typestyles", dangerouslySetInnerHTML: { __html: registeredCss } });
46
+ }
47
+ return null;
48
+ });
49
+ (0, import_react.useEffect)(() => {
50
+ setCss((0, import_server.getRegisteredCss)());
51
+ }, []);
52
+ if (children) {
53
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
54
+ children,
55
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("style", { id: "typestyles-client", dangerouslySetInnerHTML: { __html: css } })
56
+ ] });
57
+ }
58
+ return null;
59
+ }
60
+ function createTypestylesLayout(layout) {
61
+ return function TypestylesLayout(props) {
62
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TypestylesStylesheet, { children: layout(props) });
63
+ };
64
+ }
65
+ // Annotate the CommonJS export names for ESM import in node:
66
+ 0 && (module.exports = {
67
+ TypestylesStylesheet,
68
+ createTypestylesLayout,
69
+ useServerInsertedHTML,
70
+ useTypestyles
71
+ });
package/dist/client.js ADDED
@@ -0,0 +1,44 @@
1
+ "use client";
2
+
3
+ // src/client.tsx
4
+ import { useState, useEffect, useServerInsertedHTML, useSyncExternalStore } from "react";
5
+ import { getRegisteredCss, subscribeRegisteredCss } from "typestyles/server";
6
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
7
+ function useTypestyles() {
8
+ return useSyncExternalStore(
9
+ subscribeRegisteredCss,
10
+ () => getRegisteredCss(),
11
+ () => getRegisteredCss()
12
+ );
13
+ }
14
+ function TypestylesStylesheet({ children }) {
15
+ const [css, setCss] = useState("");
16
+ useServerInsertedHTML(() => {
17
+ const registeredCss = getRegisteredCss();
18
+ if (registeredCss) {
19
+ return /* @__PURE__ */ jsx("style", { id: "typestyles", dangerouslySetInnerHTML: { __html: registeredCss } });
20
+ }
21
+ return null;
22
+ });
23
+ useEffect(() => {
24
+ setCss(getRegisteredCss());
25
+ }, []);
26
+ if (children) {
27
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
28
+ children,
29
+ /* @__PURE__ */ jsx("style", { id: "typestyles-client", dangerouslySetInnerHTML: { __html: css } })
30
+ ] });
31
+ }
32
+ return null;
33
+ }
34
+ function createTypestylesLayout(layout) {
35
+ return function TypestylesLayout(props) {
36
+ return /* @__PURE__ */ jsx(TypestylesStylesheet, { children: layout(props) });
37
+ };
38
+ }
39
+ export {
40
+ TypestylesStylesheet,
41
+ createTypestylesLayout,
42
+ useServerInsertedHTML,
43
+ useTypestyles
44
+ };
package/dist/index.cjs CHANGED
@@ -33,29 +33,55 @@ __export(index_exports, {
33
33
  TypestylesStylesheet: () => TypestylesStylesheet,
34
34
  collectStylesFromComponent: () => collectStylesFromComponent,
35
35
  createTypestylesLayout: () => createTypestylesLayout,
36
- getRegisteredCss: () => import_server.getRegisteredCss
36
+ getRegisteredCss: () => import_server.getRegisteredCss,
37
+ getTypestylesMetadata: () => getTypestylesMetadata,
38
+ useServerInsertedHTML: () => import_react.useServerInsertedHTML,
39
+ useTypestyles: () => useTypestyles
37
40
  });
38
41
  module.exports = __toCommonJS(index_exports);
39
- var import_react = require("react");
42
+
43
+ // src/server.ts
40
44
  var import_server = require("typestyles/server");
45
+ var import_build_runner = require("@typestyles/build-runner");
46
+ async function collectStylesFromComponent(component) {
47
+ const { renderToString } = await import("react-dom/server");
48
+ const { css } = (0, import_server.collectStyles)(() => renderToString(component));
49
+ return css;
50
+ }
51
+ async function getTypestylesMetadata(component) {
52
+ return collectStylesFromComponent(component);
53
+ }
54
+
55
+ // src/client.tsx
56
+ var import_react = require("react");
57
+ var import_server2 = require("typestyles/server");
41
58
  var import_jsx_runtime = require("react/jsx-runtime");
59
+ function useTypestyles() {
60
+ return (0, import_react.useSyncExternalStore)(
61
+ import_server2.subscribeRegisteredCss,
62
+ () => (0, import_server2.getRegisteredCss)(),
63
+ () => (0, import_server2.getRegisteredCss)()
64
+ );
65
+ }
42
66
  function TypestylesStylesheet({ children }) {
43
67
  const [css, setCss] = (0, import_react.useState)("");
68
+ (0, import_react.useServerInsertedHTML)(() => {
69
+ const registeredCss = (0, import_server2.getRegisteredCss)();
70
+ if (registeredCss) {
71
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("style", { id: "typestyles", dangerouslySetInnerHTML: { __html: registeredCss } });
72
+ }
73
+ return null;
74
+ });
44
75
  (0, import_react.useEffect)(() => {
45
- setCss((0, import_server.getRegisteredCss)());
76
+ setCss((0, import_server2.getRegisteredCss)());
46
77
  }, []);
47
78
  if (children) {
48
79
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
49
80
  children,
50
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)("style", { id: "typestyles", dangerouslySetInnerHTML: { __html: css } })
81
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("style", { id: "typestyles-client", dangerouslySetInnerHTML: { __html: css } })
51
82
  ] });
52
83
  }
53
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("style", { id: "typestyles", dangerouslySetInnerHTML: { __html: css } });
54
- }
55
- async function collectStylesFromComponent(component) {
56
- const { renderToString } = await import("react-dom/server");
57
- const { css } = (0, import_server.collectStyles)(() => renderToString(component));
58
- return css;
84
+ return null;
59
85
  }
60
86
  function createTypestylesLayout(layout) {
61
87
  return function TypestylesLayout(props) {
@@ -67,5 +93,8 @@ function createTypestylesLayout(layout) {
67
93
  TypestylesStylesheet,
68
94
  collectStylesFromComponent,
69
95
  createTypestylesLayout,
70
- getRegisteredCss
96
+ getRegisteredCss,
97
+ getTypestylesMetadata,
98
+ useServerInsertedHTML,
99
+ useTypestyles
71
100
  });
package/dist/index.js CHANGED
@@ -1,24 +1,45 @@
1
- // src/index.tsx
2
- import { useEffect, useState } from "react";
1
+ // src/server.ts
3
2
  import { collectStyles, getRegisteredCss } from "typestyles/server";
3
+ import { getRouteCss } from "@typestyles/build-runner";
4
+ async function collectStylesFromComponent(component) {
5
+ const { renderToString } = await import("react-dom/server");
6
+ const { css } = collectStyles(() => renderToString(component));
7
+ return css;
8
+ }
9
+ async function getTypestylesMetadata(component) {
10
+ return collectStylesFromComponent(component);
11
+ }
12
+
13
+ // src/client.tsx
14
+ import { useState, useEffect, useServerInsertedHTML, useSyncExternalStore } from "react";
15
+ import { getRegisteredCss as getRegisteredCss2, subscribeRegisteredCss } from "typestyles/server";
4
16
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
17
+ function useTypestyles() {
18
+ return useSyncExternalStore(
19
+ subscribeRegisteredCss,
20
+ () => getRegisteredCss2(),
21
+ () => getRegisteredCss2()
22
+ );
23
+ }
5
24
  function TypestylesStylesheet({ children }) {
6
25
  const [css, setCss] = useState("");
26
+ useServerInsertedHTML(() => {
27
+ const registeredCss = getRegisteredCss2();
28
+ if (registeredCss) {
29
+ return /* @__PURE__ */ jsx("style", { id: "typestyles", dangerouslySetInnerHTML: { __html: registeredCss } });
30
+ }
31
+ return null;
32
+ });
7
33
  useEffect(() => {
8
- setCss(getRegisteredCss());
34
+ setCss(getRegisteredCss2());
9
35
  }, []);
10
36
  if (children) {
11
37
  return /* @__PURE__ */ jsxs(Fragment, { children: [
12
38
  children,
13
- /* @__PURE__ */ jsx("style", { id: "typestyles", dangerouslySetInnerHTML: { __html: css } })
39
+ /* @__PURE__ */ jsx("style", { id: "typestyles-client", dangerouslySetInnerHTML: { __html: css } })
14
40
  ] });
15
41
  }
16
- return /* @__PURE__ */ jsx("style", { id: "typestyles", dangerouslySetInnerHTML: { __html: css } });
17
- }
18
- async function collectStylesFromComponent(component) {
19
- const { renderToString } = await import("react-dom/server");
20
- const { css } = collectStyles(() => renderToString(component));
21
- return css;
42
+ return null;
22
43
  }
23
44
  function createTypestylesLayout(layout) {
24
45
  return function TypestylesLayout(props) {
@@ -29,5 +50,8 @@ export {
29
50
  TypestylesStylesheet,
30
51
  collectStylesFromComponent,
31
52
  createTypestylesLayout,
32
- getRegisteredCss
53
+ getRegisteredCss,
54
+ getTypestylesMetadata,
55
+ useServerInsertedHTML,
56
+ useTypestyles
33
57
  };
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/server.ts
31
+ var server_exports = {};
32
+ __export(server_exports, {
33
+ collectStylesFromComponent: () => collectStylesFromComponent,
34
+ getRegisteredCss: () => import_server.getRegisteredCss,
35
+ getRouteCss: () => import_build_runner.getRouteCss,
36
+ getTypestylesMetadata: () => getTypestylesMetadata
37
+ });
38
+ module.exports = __toCommonJS(server_exports);
39
+ var import_server = require("typestyles/server");
40
+ var import_build_runner = require("@typestyles/build-runner");
41
+ async function collectStylesFromComponent(component) {
42
+ const { renderToString } = await import("react-dom/server");
43
+ const { css } = (0, import_server.collectStyles)(() => renderToString(component));
44
+ return css;
45
+ }
46
+ async function getTypestylesMetadata(component) {
47
+ return collectStylesFromComponent(component);
48
+ }
49
+ // Annotate the CommonJS export names for ESM import in node:
50
+ 0 && (module.exports = {
51
+ collectStylesFromComponent,
52
+ getRegisteredCss,
53
+ getRouteCss,
54
+ getTypestylesMetadata
55
+ });
package/dist/server.js ADDED
@@ -0,0 +1,17 @@
1
+ // src/server.ts
2
+ import { collectStyles, getRegisteredCss } from "typestyles/server";
3
+ import { getRouteCss } from "@typestyles/build-runner";
4
+ async function collectStylesFromComponent(component) {
5
+ const { renderToString } = await import("react-dom/server");
6
+ const { css } = collectStyles(() => renderToString(component));
7
+ return css;
8
+ }
9
+ async function getTypestylesMetadata(component) {
10
+ return collectStylesFromComponent(component);
11
+ }
12
+ export {
13
+ collectStylesFromComponent,
14
+ getRegisteredCss,
15
+ getRouteCss,
16
+ getTypestylesMetadata
17
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typestyles/next",
3
- "version": "0.2.0",
3
+ "version": "0.5.0",
4
4
  "description": "Next.js integration for typestyles SSR",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -11,6 +11,22 @@
11
11
  "types": "./src/index.d.ts",
12
12
  "import": "./dist/index.js",
13
13
  "require": "./dist/index.cjs"
14
+ },
15
+ "./server": {
16
+ "types": "./src/server.d.ts",
17
+ "import": "./dist/server.js",
18
+ "require": "./dist/server.cjs"
19
+ },
20
+ "./client": {
21
+ "types": "./src/client.d.ts",
22
+ "import": "./dist/client.js",
23
+ "require": "./dist/client.cjs",
24
+ "default": "./dist/client.js"
25
+ },
26
+ "./build": {
27
+ "types": "./src/build.d.ts",
28
+ "import": "./dist/build.js",
29
+ "require": "./dist/build.cjs"
14
30
  }
15
31
  },
16
32
  "files": [
@@ -24,17 +40,18 @@
24
40
  "ssr",
25
41
  "react"
26
42
  ],
27
- "license": "MIT",
43
+ "license": "Apache-2.0",
28
44
  "repository": {
29
45
  "type": "git",
30
- "url": "https://github.com/dbanksdesign/typestyles",
46
+ "url": "https://github.com/type-styles/typestyles",
31
47
  "directory": "packages/next"
32
48
  },
33
49
  "peerDependencies": {
34
50
  "next": ">=13.0.0",
35
51
  "react": ">=18.0.0",
36
52
  "react-dom": ">=18.0.0",
37
- "typestyles": ">=0.1.0"
53
+ "typestyles": ">=0.1.0",
54
+ "webpack": ">=5.0.0"
38
55
  },
39
56
  "peerDependenciesMeta": {
40
57
  "next": {
@@ -45,19 +62,32 @@
45
62
  },
46
63
  "react-dom": {
47
64
  "optional": false
65
+ },
66
+ "webpack": {
67
+ "optional": true
48
68
  }
49
69
  },
70
+ "dependencies": {
71
+ "@typestyles/build-runner": "0.5.0"
72
+ },
50
73
  "devDependencies": {
74
+ "eslint": "^9.39.2",
75
+ "vitest": "^3.0.0",
51
76
  "@types/node": "^25.2.1",
52
- "@types/react": "^18.0.0",
53
- "@types/react-dom": "^18.0.0",
77
+ "@types/react": "^18.3.12",
78
+ "@types/react-dom": "^18.3.1",
54
79
  "next": "^14.0.0",
55
80
  "react": "^18.0.0",
56
81
  "react-dom": "^18.0.0",
57
82
  "tsup": "^8.0.0",
58
- "typescript": "^5.4.0"
83
+ "typescript": "^5.4.0",
84
+ "webpack": "^5.97.1",
85
+ "typestyles": "0.7.0"
59
86
  },
60
87
  "scripts": {
61
- "build": "tsup"
88
+ "build": "tsup",
89
+ "test": "vitest run",
90
+ "lint": "eslint .",
91
+ "typecheck": "tsc --noEmit"
62
92
  }
63
93
  }
@@ -0,0 +1,48 @@
1
+ import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { tmpdir } from 'node:os';
4
+ import { describe, expect, it } from 'vitest';
5
+ import { buildTypestylesForNext } from './build';
6
+
7
+ describe('buildTypestylesForNext route CSS', () => {
8
+ it('emits manifest v2 with per-route CSS files', async () => {
9
+ const root = mkdtempSync(join(tmpdir(), 'typestyles-next-build-routes-'));
10
+ mkdirSync(join(root, 'styles'), { recursive: true });
11
+ mkdirSync(join(root, 'app/about'), { recursive: true });
12
+
13
+ writeFileSync(
14
+ join(root, 'styles/typestyles-entry.ts'),
15
+ `import { styles } from 'typestyles';\nstyles.class('shared', { margin: '0' });\n`,
16
+ );
17
+ writeFileSync(
18
+ join(root, 'app/layout.tsx'),
19
+ `export default function Layout() { return null; }\n`,
20
+ );
21
+ writeFileSync(
22
+ join(root, 'app/page.tsx'),
23
+ `import { styles } from 'typestyles';\nstyles.class('home', { color: 'red' });\nexport default function Home() { return null; }\n`,
24
+ );
25
+ writeFileSync(
26
+ join(root, 'app/about/page.tsx'),
27
+ `import { styles } from 'typestyles';\nstyles.class('about', { color: 'blue' });\nexport default function About() { return null; }\n`,
28
+ );
29
+
30
+ try {
31
+ await buildTypestylesForNext({ root });
32
+
33
+ const manifest = JSON.parse(readFileSync(join(root, 'app/typestyles.manifest.json'), 'utf8'));
34
+ expect(manifest.version).toBe(2);
35
+ expect(manifest.routes['/']).toBeDefined();
36
+ expect(manifest.routes['/about']).toBeDefined();
37
+ expect(existsSync(join(root, manifest.routes['/'].css))).toBe(true);
38
+ expect(existsSync(join(root, manifest.routes['/about'].css))).toBe(true);
39
+
40
+ const homeCss = readFileSync(join(root, manifest.routes['/'].css), 'utf8');
41
+ const aboutCss = readFileSync(join(root, manifest.routes['/about'].css), 'utf8');
42
+ expect(homeCss).toContain('.home {');
43
+ expect(aboutCss).toContain('.about {');
44
+ } finally {
45
+ rmSync(root, { recursive: true, force: true });
46
+ }
47
+ });
48
+ });
package/src/build.d.ts ADDED
@@ -0,0 +1,70 @@
1
+ import type { NextConfig } from 'next';
2
+
3
+ export interface TypestylesExtractManifestV1 {
4
+ version: 1;
5
+ css: string;
6
+ }
7
+
8
+ export interface TypestylesRouteCssEntry {
9
+ css: string;
10
+ }
11
+
12
+ export interface TypestylesExtractManifestV2 {
13
+ version: 2;
14
+ css: string;
15
+ routes: Record<string, TypestylesRouteCssEntry>;
16
+ }
17
+
18
+ export type TypestylesExtractManifest = TypestylesExtractManifestV1 | TypestylesExtractManifestV2;
19
+
20
+ export declare const DEFAULT_EXTRACT_MODULE_CANDIDATES: readonly string[];
21
+
22
+ export declare function discoverDefaultExtractModules(root: string): string[];
23
+
24
+ export interface BuildTypestylesForNextOptions {
25
+ root: string;
26
+ modules?: string[];
27
+ cssOutFile?: string;
28
+ manifestOutFile?: string | false;
29
+ manifestCssPath?: string;
30
+ routeCss?: boolean;
31
+ routeCssOutDir?: string;
32
+ appDir?: string;
33
+ }
34
+
35
+ export declare function buildTypestylesForNext(
36
+ options: BuildTypestylesForNextOptions,
37
+ ): Promise<void>;
38
+
39
+ export interface WithTypestylesExtractOptions {
40
+ disableClientRuntime?: boolean;
41
+ root?: string;
42
+ }
43
+
44
+ export interface WithTypestylesOptions extends WithTypestylesExtractOptions {
45
+ root?: string;
46
+ }
47
+
48
+ export declare function withTypestyles(
49
+ nextConfig?: NextConfig,
50
+ options?: WithTypestylesOptions,
51
+ ): NextConfig;
52
+
53
+ export declare function withTypestylesExtract(
54
+ nextConfig?: NextConfig,
55
+ options?: WithTypestylesExtractOptions,
56
+ ): NextConfig;
57
+
58
+ export interface GetRouteCssOptions {
59
+ root: string;
60
+ manifestFile?: string;
61
+ }
62
+
63
+ export declare function getRouteCss(routePath: string, options: GetRouteCssOptions): string;
64
+
65
+ export {
66
+ verifyTypestylesBuild,
67
+ VerifyTypestylesBuildError,
68
+ type VerifyTypestylesBuildOptions,
69
+ type VerifyTypestylesBuildResult,
70
+ } from '@typestyles/build-runner';