@typestyles/next 0.0.0-unstable.5b7138167233 → 0.0.0-unstable.6c696fbd20ce
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.md +15 -11
- package/dist/build.cjs +89 -0
- package/dist/build.js +63 -0
- package/package.json +22 -6
- package/src/build.d.ts +27 -0
- package/src/build.test.ts +58 -0
- package/src/build.ts +114 -0
- package/src/client.d.ts +5 -6
- package/src/index.d.ts +6 -4
- package/src/react-augmentation.d.ts +13 -0
- package/src/server.d.ts +7 -0
- package/src/server.ts +12 -11
package/README.md
CHANGED
|
@@ -128,24 +128,28 @@ export default function RootLayout({ children }) {
|
|
|
128
128
|
}
|
|
129
129
|
```
|
|
130
130
|
|
|
131
|
-
### Option 3:
|
|
131
|
+
### Option 3: Collecting from a specific component tree
|
|
132
132
|
|
|
133
|
-
For
|
|
133
|
+
`getTypestylesMetadata` (alias of `collectStylesFromComponent`) renders a React element on the server and returns the CSS registered during that render. Use it when you need CSS scoped to a subtree rather than the whole app. For App Router pages, injecting the result still belongs in `layout.tsx` / `<head>` (Next.js `Metadata` does not provide a supported hook for arbitrary `<style>` payloads).
|
|
134
134
|
|
|
135
135
|
```tsx
|
|
136
|
-
// app/layout.tsx
|
|
137
|
-
import { generateMetadata } from 'next';
|
|
138
136
|
import { getTypestylesMetadata } from '@typestyles/next/server';
|
|
139
137
|
import { Home } from './Home';
|
|
140
138
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
return {
|
|
144
|
-
styles: [{ cssText: css, id: 'typestyles' }],
|
|
145
|
-
};
|
|
146
|
-
}
|
|
139
|
+
const css = await getTypestylesMetadata(<Home />);
|
|
140
|
+
// Pass `css` into your layout <style> or streaming head pipeline.
|
|
147
141
|
```
|
|
148
142
|
|
|
143
|
+
## Build-time CSS + Turbopack
|
|
144
|
+
|
|
145
|
+
To ship a static `typestyles.css` and avoid client-side `<style>` injection (uses `typestyles/build` under the hood):
|
|
146
|
+
|
|
147
|
+
1. Run `buildTypestylesForNext` before `next build` to emit CSS (and optional manifest).
|
|
148
|
+
2. Import that CSS from your root layout (e.g. `import './typestyles.css'`).
|
|
149
|
+
3. Wrap config with `withTypestylesExtract` from `@typestyles/next/build`.
|
|
150
|
+
|
|
151
|
+
`withTypestylesExtract` sets **`NEXT_PUBLIC_TYPESTYLES_RUNTIME_DISABLED`** via `next.config` `env` (works with **webpack and Turbopack**) and adds webpack **`DefinePlugin`** for `__TYPESTYLES_RUNTIME_DISABLED__` on client bundles when webpack runs. Example app: `examples/next-app`.
|
|
152
|
+
|
|
149
153
|
## API Reference
|
|
150
154
|
|
|
151
155
|
### getRegisteredCss
|
|
@@ -171,7 +175,7 @@ const css = await collectStylesFromComponent(<YourComponent />);
|
|
|
171
175
|
|
|
172
176
|
### getTypestylesMetadata
|
|
173
177
|
|
|
174
|
-
|
|
178
|
+
Same as `collectStylesFromComponent` — renders the given element on the server and returns registered CSS. Use when you want a dedicated name for “CSS for this subtree.”
|
|
175
179
|
|
|
176
180
|
```tsx
|
|
177
181
|
import { getTypestylesMetadata } from '@typestyles/next/server';
|
package/dist/build.cjs
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/build.ts
|
|
21
|
+
var build_exports = {};
|
|
22
|
+
__export(build_exports, {
|
|
23
|
+
buildTypestylesForNext: () => buildTypestylesForNext,
|
|
24
|
+
withTypestylesExtract: () => withTypestylesExtract
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(build_exports);
|
|
27
|
+
var import_promises = require("fs/promises");
|
|
28
|
+
var import_node_module = require("module");
|
|
29
|
+
var import_node_path = require("path");
|
|
30
|
+
var import_node_url = require("url");
|
|
31
|
+
var import_build = require("typestyles/build");
|
|
32
|
+
async function buildTypestylesForNext(options) {
|
|
33
|
+
const { root, modules, cssOutFile, manifestOutFile, manifestCssPath } = options;
|
|
34
|
+
const loaders = modules.map((mod) => {
|
|
35
|
+
const abs = (0, import_node_path.resolve)(root, mod);
|
|
36
|
+
const href = (0, import_node_url.pathToFileURL)(abs).href;
|
|
37
|
+
return () => import(href);
|
|
38
|
+
});
|
|
39
|
+
const css = await (0, import_build.collectStylesFromModules)(loaders);
|
|
40
|
+
const outAbs = (0, import_node_path.resolve)(root, cssOutFile);
|
|
41
|
+
await (0, import_promises.mkdir)((0, import_node_path.dirname)(outAbs), { recursive: true });
|
|
42
|
+
await (0, import_promises.writeFile)(outAbs, css, "utf8");
|
|
43
|
+
if (manifestOutFile) {
|
|
44
|
+
const manifest = {
|
|
45
|
+
version: 1,
|
|
46
|
+
css: manifestCssPath ?? cssOutFile
|
|
47
|
+
};
|
|
48
|
+
const manAbs = (0, import_node_path.resolve)(root, manifestOutFile);
|
|
49
|
+
await (0, import_promises.mkdir)((0, import_node_path.dirname)(manAbs), { recursive: true });
|
|
50
|
+
await (0, import_promises.writeFile)(manAbs, `${JSON.stringify(manifest, null, 2)}
|
|
51
|
+
`, "utf8");
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function withTypestylesExtract(nextConfig = {}, options = {}) {
|
|
55
|
+
const disableClientRuntime = options.disableClientRuntime !== false;
|
|
56
|
+
return {
|
|
57
|
+
...nextConfig,
|
|
58
|
+
...disableClientRuntime ? {
|
|
59
|
+
env: {
|
|
60
|
+
...typeof nextConfig.env === "object" && nextConfig.env !== null && !Array.isArray(nextConfig.env) ? nextConfig.env : {},
|
|
61
|
+
NEXT_PUBLIC_TYPESTYLES_RUNTIME_DISABLED: "true"
|
|
62
|
+
}
|
|
63
|
+
} : {},
|
|
64
|
+
webpack(config, context) {
|
|
65
|
+
const nextWebpack = typeof nextConfig.webpack === "function" ? nextConfig.webpack(config, context) : config;
|
|
66
|
+
if (disableClientRuntime && !context.isServer) {
|
|
67
|
+
try {
|
|
68
|
+
const require2 = (0, import_node_module.createRequire)((0, import_node_path.join)(process.cwd(), "package.json"));
|
|
69
|
+
const webpackLib = require2("webpack");
|
|
70
|
+
nextWebpack.plugins.push(
|
|
71
|
+
new webpackLib.DefinePlugin({
|
|
72
|
+
__TYPESTYLES_RUNTIME_DISABLED__: JSON.stringify("true")
|
|
73
|
+
})
|
|
74
|
+
);
|
|
75
|
+
} catch {
|
|
76
|
+
console.warn(
|
|
77
|
+
"[@typestyles/next/build] Could not load webpack; client bundles may still inject <style>."
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return nextWebpack;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
86
|
+
0 && (module.exports = {
|
|
87
|
+
buildTypestylesForNext,
|
|
88
|
+
withTypestylesExtract
|
|
89
|
+
});
|
package/dist/build.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// src/build.ts
|
|
2
|
+
import { mkdir, writeFile } from "fs/promises";
|
|
3
|
+
import { createRequire } from "module";
|
|
4
|
+
import { dirname, join, resolve } from "path";
|
|
5
|
+
import { pathToFileURL } from "url";
|
|
6
|
+
import { collectStylesFromModules } from "typestyles/build";
|
|
7
|
+
async function buildTypestylesForNext(options) {
|
|
8
|
+
const { root, modules, cssOutFile, manifestOutFile, manifestCssPath } = options;
|
|
9
|
+
const loaders = modules.map((mod) => {
|
|
10
|
+
const abs = resolve(root, mod);
|
|
11
|
+
const href = pathToFileURL(abs).href;
|
|
12
|
+
return () => import(href);
|
|
13
|
+
});
|
|
14
|
+
const css = await collectStylesFromModules(loaders);
|
|
15
|
+
const outAbs = resolve(root, cssOutFile);
|
|
16
|
+
await mkdir(dirname(outAbs), { recursive: true });
|
|
17
|
+
await writeFile(outAbs, css, "utf8");
|
|
18
|
+
if (manifestOutFile) {
|
|
19
|
+
const manifest = {
|
|
20
|
+
version: 1,
|
|
21
|
+
css: manifestCssPath ?? cssOutFile
|
|
22
|
+
};
|
|
23
|
+
const manAbs = resolve(root, manifestOutFile);
|
|
24
|
+
await mkdir(dirname(manAbs), { recursive: true });
|
|
25
|
+
await writeFile(manAbs, `${JSON.stringify(manifest, null, 2)}
|
|
26
|
+
`, "utf8");
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function withTypestylesExtract(nextConfig = {}, options = {}) {
|
|
30
|
+
const disableClientRuntime = options.disableClientRuntime !== false;
|
|
31
|
+
return {
|
|
32
|
+
...nextConfig,
|
|
33
|
+
...disableClientRuntime ? {
|
|
34
|
+
env: {
|
|
35
|
+
...typeof nextConfig.env === "object" && nextConfig.env !== null && !Array.isArray(nextConfig.env) ? nextConfig.env : {},
|
|
36
|
+
NEXT_PUBLIC_TYPESTYLES_RUNTIME_DISABLED: "true"
|
|
37
|
+
}
|
|
38
|
+
} : {},
|
|
39
|
+
webpack(config, context) {
|
|
40
|
+
const nextWebpack = typeof nextConfig.webpack === "function" ? nextConfig.webpack(config, context) : config;
|
|
41
|
+
if (disableClientRuntime && !context.isServer) {
|
|
42
|
+
try {
|
|
43
|
+
const require2 = createRequire(join(process.cwd(), "package.json"));
|
|
44
|
+
const webpackLib = require2("webpack");
|
|
45
|
+
nextWebpack.plugins.push(
|
|
46
|
+
new webpackLib.DefinePlugin({
|
|
47
|
+
__TYPESTYLES_RUNTIME_DISABLED__: JSON.stringify("true")
|
|
48
|
+
})
|
|
49
|
+
);
|
|
50
|
+
} catch {
|
|
51
|
+
console.warn(
|
|
52
|
+
"[@typestyles/next/build] Could not load webpack; client bundles may still inject <style>."
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return nextWebpack;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
export {
|
|
61
|
+
buildTypestylesForNext,
|
|
62
|
+
withTypestylesExtract
|
|
63
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typestyles/next",
|
|
3
|
-
"version": "0.0.0-unstable.
|
|
3
|
+
"version": "0.0.0-unstable.6c696fbd20ce",
|
|
4
4
|
"description": "Next.js integration for typestyles SSR",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -22,6 +22,11 @@
|
|
|
22
22
|
"import": "./dist/client.js",
|
|
23
23
|
"require": "./dist/client.cjs",
|
|
24
24
|
"default": "./dist/client.js"
|
|
25
|
+
},
|
|
26
|
+
"./build": {
|
|
27
|
+
"types": "./src/build.d.ts",
|
|
28
|
+
"import": "./dist/build.js",
|
|
29
|
+
"require": "./dist/build.cjs"
|
|
25
30
|
}
|
|
26
31
|
},
|
|
27
32
|
"files": [
|
|
@@ -45,7 +50,8 @@
|
|
|
45
50
|
"next": ">=13.0.0",
|
|
46
51
|
"react": ">=18.0.0",
|
|
47
52
|
"react-dom": ">=18.0.0",
|
|
48
|
-
"typestyles": ">=0.1.0"
|
|
53
|
+
"typestyles": ">=0.1.0",
|
|
54
|
+
"webpack": ">=5.0.0"
|
|
49
55
|
},
|
|
50
56
|
"peerDependenciesMeta": {
|
|
51
57
|
"next": {
|
|
@@ -56,19 +62,29 @@
|
|
|
56
62
|
},
|
|
57
63
|
"react-dom": {
|
|
58
64
|
"optional": false
|
|
65
|
+
},
|
|
66
|
+
"webpack": {
|
|
67
|
+
"optional": true
|
|
59
68
|
}
|
|
60
69
|
},
|
|
61
70
|
"devDependencies": {
|
|
71
|
+
"eslint": "^9.39.2",
|
|
72
|
+
"vitest": "^3.0.0",
|
|
62
73
|
"@types/node": "^25.2.1",
|
|
63
|
-
"@types/react": "^18.
|
|
64
|
-
"@types/react-dom": "^18.
|
|
74
|
+
"@types/react": "^18.3.12",
|
|
75
|
+
"@types/react-dom": "^18.3.1",
|
|
65
76
|
"next": "^14.0.0",
|
|
66
77
|
"react": "^18.0.0",
|
|
67
78
|
"react-dom": "^18.0.0",
|
|
68
79
|
"tsup": "^8.0.0",
|
|
69
|
-
"
|
|
80
|
+
"typestyles": "0.0.0-unstable.6c696fbd20ce",
|
|
81
|
+
"typescript": "^5.4.0",
|
|
82
|
+
"webpack": "^5.97.1"
|
|
70
83
|
},
|
|
71
84
|
"scripts": {
|
|
72
|
-
"build": "tsup"
|
|
85
|
+
"build": "tsup",
|
|
86
|
+
"test": "vitest run",
|
|
87
|
+
"lint": "eslint .",
|
|
88
|
+
"typecheck": "tsc --noEmit"
|
|
73
89
|
}
|
|
74
90
|
}
|
package/src/build.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { NextConfig } from 'next';
|
|
2
|
+
|
|
3
|
+
export interface TypestylesExtractManifestV1 {
|
|
4
|
+
version: 1;
|
|
5
|
+
css: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface BuildTypestylesForNextOptions {
|
|
9
|
+
root: string;
|
|
10
|
+
modules: string[];
|
|
11
|
+
cssOutFile: string;
|
|
12
|
+
manifestOutFile?: string;
|
|
13
|
+
manifestCssPath?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export declare function buildTypestylesForNext(
|
|
17
|
+
options: BuildTypestylesForNextOptions,
|
|
18
|
+
): Promise<void>;
|
|
19
|
+
|
|
20
|
+
export interface WithTypestylesExtractOptions {
|
|
21
|
+
disableClientRuntime?: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export declare function withTypestylesExtract(
|
|
25
|
+
nextConfig?: NextConfig,
|
|
26
|
+
options?: WithTypestylesExtractOptions,
|
|
27
|
+
): NextConfig;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
import type { NextConfig } from 'next';
|
|
3
|
+
import type { Configuration } from 'webpack';
|
|
4
|
+
import { withTypestylesExtract } from './build.js';
|
|
5
|
+
|
|
6
|
+
type WebpackContext = Parameters<NonNullable<NextConfig['webpack']>>[1];
|
|
7
|
+
|
|
8
|
+
describe('withTypestylesExtract', () => {
|
|
9
|
+
it('merges NEXT_PUBLIC_TYPESTYLES_RUNTIME_DISABLED into env for Turbopack + webpack', () => {
|
|
10
|
+
const merged = withTypestylesExtract({ env: { CUSTOM: 'x' } }, { disableClientRuntime: true });
|
|
11
|
+
expect(merged.env).toEqual({
|
|
12
|
+
CUSTOM: 'x',
|
|
13
|
+
NEXT_PUBLIC_TYPESTYLES_RUNTIME_DISABLED: 'true',
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('does not override env when disableClientRuntime is false', () => {
|
|
18
|
+
const merged = withTypestylesExtract({ env: { CUSTOM: 'x' } }, { disableClientRuntime: false });
|
|
19
|
+
expect(merged.env).toEqual({ CUSTOM: 'x' });
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('chains user webpack() and adds DefinePlugin on the client', async () => {
|
|
23
|
+
const webpackMod = await import('webpack');
|
|
24
|
+
const userWebpack = vi.fn((cfg: { plugins: unknown[] }) => {
|
|
25
|
+
cfg.plugins.push('user-marker');
|
|
26
|
+
return cfg;
|
|
27
|
+
});
|
|
28
|
+
const merged = withTypestylesExtract({ webpack: userWebpack });
|
|
29
|
+
const config = { plugins: [] as unknown[] };
|
|
30
|
+
const result = merged.webpack!(
|
|
31
|
+
config as Configuration,
|
|
32
|
+
{
|
|
33
|
+
isServer: false,
|
|
34
|
+
} as WebpackContext,
|
|
35
|
+
);
|
|
36
|
+
expect(userWebpack).toHaveBeenCalled();
|
|
37
|
+
const hasDefine = result.plugins?.some(
|
|
38
|
+
(p: unknown) => p instanceof webpackMod.default.DefinePlugin,
|
|
39
|
+
);
|
|
40
|
+
expect(hasDefine).toBe(true);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('does not add DefinePlugin for server bundles', async () => {
|
|
44
|
+
const webpackMod = await import('webpack');
|
|
45
|
+
const merged = withTypestylesExtract({});
|
|
46
|
+
const config = { plugins: [] as unknown[] };
|
|
47
|
+
const result = merged.webpack!(
|
|
48
|
+
config as Configuration,
|
|
49
|
+
{
|
|
50
|
+
isServer: true,
|
|
51
|
+
} as WebpackContext,
|
|
52
|
+
);
|
|
53
|
+
const hasDefine = result.plugins?.some(
|
|
54
|
+
(p: unknown) => p instanceof webpackMod.default.DefinePlugin,
|
|
55
|
+
);
|
|
56
|
+
expect(hasDefine).toBe(false);
|
|
57
|
+
});
|
|
58
|
+
});
|
package/src/build.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { mkdir, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { createRequire } from 'node:module';
|
|
3
|
+
import { dirname, join, resolve } from 'node:path';
|
|
4
|
+
import { pathToFileURL } from 'node:url';
|
|
5
|
+
import { collectStylesFromModules } from 'typestyles/build';
|
|
6
|
+
import type { NextConfig } from 'next';
|
|
7
|
+
|
|
8
|
+
/** Manifest shape written next to extracted CSS (optional). */
|
|
9
|
+
export interface TypestylesExtractManifestV1 {
|
|
10
|
+
version: 1;
|
|
11
|
+
css: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Shape of `require('webpack')` for `DefinePlugin` only (webpack is an optional peer). */
|
|
15
|
+
type WebpackWithDefinePlugin = {
|
|
16
|
+
DefinePlugin: new (definitions: Record<string, string>) => unknown;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export interface BuildTypestylesForNextOptions {
|
|
20
|
+
root: string;
|
|
21
|
+
modules: string[];
|
|
22
|
+
cssOutFile: string;
|
|
23
|
+
manifestOutFile?: string;
|
|
24
|
+
manifestCssPath?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Extract TypeStyles CSS for a Next.js app (run from a script before `next build`, or in CI).
|
|
29
|
+
* Loads each module path (relative to `root`) so side-effect registrations run, then writes CSS.
|
|
30
|
+
*/
|
|
31
|
+
export async function buildTypestylesForNext(
|
|
32
|
+
options: BuildTypestylesForNextOptions,
|
|
33
|
+
): Promise<void> {
|
|
34
|
+
const { root, modules, cssOutFile, manifestOutFile, manifestCssPath } = options;
|
|
35
|
+
const loaders = modules.map((mod) => {
|
|
36
|
+
const abs = resolve(root, mod);
|
|
37
|
+
const href = pathToFileURL(abs).href;
|
|
38
|
+
return () => import(href);
|
|
39
|
+
});
|
|
40
|
+
const css = await collectStylesFromModules(loaders);
|
|
41
|
+
const outAbs = resolve(root, cssOutFile);
|
|
42
|
+
await mkdir(dirname(outAbs), { recursive: true });
|
|
43
|
+
await writeFile(outAbs, css, 'utf8');
|
|
44
|
+
if (manifestOutFile) {
|
|
45
|
+
const manifest: TypestylesExtractManifestV1 = {
|
|
46
|
+
version: 1,
|
|
47
|
+
css: manifestCssPath ?? cssOutFile,
|
|
48
|
+
};
|
|
49
|
+
const manAbs = resolve(root, manifestOutFile);
|
|
50
|
+
await mkdir(dirname(manAbs), { recursive: true });
|
|
51
|
+
await writeFile(manAbs, `${JSON.stringify(manifest, null, 2)}\n`, 'utf8');
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface WithTypestylesExtractOptions {
|
|
56
|
+
/**
|
|
57
|
+
* When true (default), client bundles skip DOM style injection (`__TYPESTYLES_RUNTIME_DISABLED__`).
|
|
58
|
+
* Pair with a global stylesheet from {@link buildTypestylesForNext}.
|
|
59
|
+
*/
|
|
60
|
+
disableClientRuntime?: boolean;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Merge Next.js config so client bundles use pre-extracted CSS only (no runtime `<style>` injection).
|
|
65
|
+
*
|
|
66
|
+
* - **Webpack**: adds `DefinePlugin` for `__TYPESTYLES_RUNTIME_DISABLED__` on client bundles.
|
|
67
|
+
* - **Turbopack** (`next dev --turbo` / Turbopack production): webpack hooks do not run; the same
|
|
68
|
+
* `next.config` `env` entry `NEXT_PUBLIC_TYPESTYLES_RUNTIME_DISABLED` is merged so the client
|
|
69
|
+
* bundle still disables runtime insertion (see `typestyles` `sheet` implementation).
|
|
70
|
+
*/
|
|
71
|
+
export function withTypestylesExtract(
|
|
72
|
+
nextConfig: NextConfig = {},
|
|
73
|
+
options: WithTypestylesExtractOptions = {},
|
|
74
|
+
): NextConfig {
|
|
75
|
+
const disableClientRuntime = options.disableClientRuntime !== false;
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
...nextConfig,
|
|
79
|
+
...(disableClientRuntime
|
|
80
|
+
? {
|
|
81
|
+
env: {
|
|
82
|
+
...(typeof nextConfig.env === 'object' &&
|
|
83
|
+
nextConfig.env !== null &&
|
|
84
|
+
!Array.isArray(nextConfig.env)
|
|
85
|
+
? (nextConfig.env as Record<string, string>)
|
|
86
|
+
: {}),
|
|
87
|
+
NEXT_PUBLIC_TYPESTYLES_RUNTIME_DISABLED: 'true',
|
|
88
|
+
},
|
|
89
|
+
}
|
|
90
|
+
: {}),
|
|
91
|
+
webpack(config, context) {
|
|
92
|
+
const nextWebpack =
|
|
93
|
+
typeof nextConfig.webpack === 'function' ? nextConfig.webpack(config, context) : config;
|
|
94
|
+
|
|
95
|
+
if (disableClientRuntime && !context.isServer) {
|
|
96
|
+
try {
|
|
97
|
+
const require = createRequire(join(process.cwd(), 'package.json'));
|
|
98
|
+
const webpackLib = require('webpack') as WebpackWithDefinePlugin;
|
|
99
|
+
nextWebpack.plugins.push(
|
|
100
|
+
new webpackLib.DefinePlugin({
|
|
101
|
+
__TYPESTYLES_RUNTIME_DISABLED__: JSON.stringify('true'),
|
|
102
|
+
}),
|
|
103
|
+
);
|
|
104
|
+
} catch {
|
|
105
|
+
console.warn(
|
|
106
|
+
'[@typestyles/next/build] Could not load webpack; client bundles may still inject <style>.',
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return nextWebpack;
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
package/src/client.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type * as React from 'react';
|
|
2
|
-
import type { useSyncExternalStore
|
|
2
|
+
import type { useSyncExternalStore } from 'react';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Subscribe to typestyles CSS changes. Use this in client components
|
|
@@ -8,9 +8,10 @@ import type { useSyncExternalStore, useServerInsertedHTML } from 'react';
|
|
|
8
8
|
export function useTypestyles(): ReturnType<typeof useSyncExternalStore<string>>;
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
11
|
+
* Re-export of React’s `useServerInsertedHTML` (App Router / streaming SSR).
|
|
12
|
+
* Declared here so consumers get a stable signature even when their `@types/react` predates the hook.
|
|
12
13
|
*/
|
|
13
|
-
export
|
|
14
|
+
export function useServerInsertedHTML(callback: () => React.ReactNode | null): void;
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* Props for the TypestylesStylesheet component.
|
|
@@ -22,9 +23,7 @@ export interface TypestylesStylesheetProps {
|
|
|
22
23
|
/**
|
|
23
24
|
* A React component that renders typestyles CSS.
|
|
24
25
|
*/
|
|
25
|
-
export function TypestylesStylesheet(
|
|
26
|
-
props: TypestylesStylesheetProps,
|
|
27
|
-
): React.JSX.Element | null;
|
|
26
|
+
export function TypestylesStylesheet(props: TypestylesStylesheetProps): React.JSX.Element | null;
|
|
28
27
|
|
|
29
28
|
/**
|
|
30
29
|
* Create a Next.js layout export that includes typestyles.
|
package/src/index.d.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
|
+
import type { ReactElement, ReactNode } from 'react';
|
|
2
|
+
|
|
1
3
|
export { getRegisteredCss } from 'typestyles/server';
|
|
2
4
|
|
|
3
5
|
export interface TypestylesStylesheetProps {
|
|
4
|
-
children?:
|
|
6
|
+
children?: ReactNode;
|
|
5
7
|
}
|
|
6
8
|
|
|
7
9
|
export function TypestylesStylesheet(props: TypestylesStylesheetProps): React.JSX.Element;
|
|
8
10
|
|
|
9
|
-
export function collectStylesFromComponent(component:
|
|
11
|
+
export function collectStylesFromComponent(component: ReactElement): Promise<string>;
|
|
10
12
|
|
|
11
|
-
export function createTypestylesLayout<P extends { children?:
|
|
12
|
-
layout: (props: P) =>
|
|
13
|
+
export function createTypestylesLayout<P extends { children?: ReactNode }>(
|
|
14
|
+
layout: (props: P) => ReactNode,
|
|
13
15
|
): (props: P) => React.JSX.Element;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `useServerInsertedHTML` exists at runtime in Next.js App Router / React 18.3+ client builds,
|
|
3
|
+
* but some `@types/react` releases omit it. Augment `react` so local `tsc` and tooling match runtime.
|
|
4
|
+
*
|
|
5
|
+
* @see https://react.dev/reference/react-dom/server/useServerInsertedHTML
|
|
6
|
+
*/
|
|
7
|
+
import type { ReactNode } from 'react';
|
|
8
|
+
|
|
9
|
+
declare module 'react' {
|
|
10
|
+
export function useServerInsertedHTML(callback: () => ReactNode | null): void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export {};
|
package/src/server.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ReactElement } from 'react';
|
|
2
|
+
|
|
3
|
+
export { getRegisteredCss } from 'typestyles/server';
|
|
4
|
+
|
|
5
|
+
export declare function collectStylesFromComponent(component: ReactElement): Promise<string>;
|
|
6
|
+
|
|
7
|
+
export declare function getTypestylesMetadata(component: ReactElement): Promise<string>;
|
package/src/server.ts
CHANGED
|
@@ -28,24 +28,25 @@ export async function collectStylesFromComponent(component: React.ReactElement):
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
31
|
+
* Collect CSS by rendering a React element on the server (alias for {@link collectStylesFromComponent}).
|
|
32
|
+
* Use when you need styles for a specific subtree; inject the returned string in `<head>` / layout as needed.
|
|
33
33
|
*
|
|
34
|
-
*
|
|
34
|
+
* Next.js `metadata` / `generateMetadata` does not support injecting arbitrary `<style>` payloads; prefer
|
|
35
|
+
* {@link getRegisteredCss} in root `layout.tsx` or pipe this string into your own head/streaming pipeline.
|
|
36
|
+
*
|
|
37
|
+
* Note: This only captures CSS registered during that render.
|
|
35
38
|
* For dynamic styles that may be registered client-side, use TypestylesStylesheet.
|
|
36
39
|
*
|
|
37
40
|
* @example
|
|
38
41
|
* ```tsx
|
|
39
|
-
* // app/
|
|
40
|
-
* import { generateMetadata } from 'next/metadata';
|
|
42
|
+
* // app/some-route/head-styles.tsx (Server Component)
|
|
41
43
|
* import { getTypestylesMetadata } from '@typestyles/next/server';
|
|
42
|
-
* import {
|
|
44
|
+
* import { ProductPage } from './ProductPage';
|
|
43
45
|
*
|
|
44
|
-
* export async function
|
|
45
|
-
* const css = await getTypestylesMetadata(<
|
|
46
|
-
* return
|
|
47
|
-
*
|
|
48
|
-
* };
|
|
46
|
+
* export async function ProductPageStyles() {
|
|
47
|
+
* const css = await getTypestylesMetadata(<ProductPage />);
|
|
48
|
+
* if (!css) return null;
|
|
49
|
+
* return <style id="typestyles-product" dangerouslySetInnerHTML={{ __html: css }} />;
|
|
49
50
|
* }
|
|
50
51
|
* ```
|
|
51
52
|
*/
|