@kasperpeulen/vitest-plugin-nextjs 0.0.0-stage → 0.1.0-alpha.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.
Files changed (250) hide show
  1. package/README.md +249 -2
  2. package/dist/_virtual/_rolldown/runtime.js +14 -0
  3. package/dist/action-ids.d.ts +18 -0
  4. package/dist/action-ids.js +44 -0
  5. package/dist/adapter.d.ts +8 -0
  6. package/dist/adapter.js +12 -0
  7. package/dist/app-requests.d.ts +63 -0
  8. package/dist/app-requests.js +253 -0
  9. package/dist/browser-context.d.ts +24 -0
  10. package/dist/browser-context.js +157 -0
  11. package/dist/build-output.d.ts +68 -0
  12. package/dist/build-output.js +216 -0
  13. package/dist/build.d.ts +26 -0
  14. package/dist/build.js +39 -0
  15. package/dist/built-files.d.ts +9 -0
  16. package/dist/built-files.js +22 -0
  17. package/dist/cache.d.ts +44 -0
  18. package/dist/cache.js +99 -0
  19. package/dist/client-components.d.ts +3 -0
  20. package/dist/client-components.js +4 -0
  21. package/dist/client-file-key.d.ts +5 -0
  22. package/dist/client-file-key.js +5 -0
  23. package/dist/client-files.d.ts +83 -0
  24. package/dist/client-files.js +186 -0
  25. package/dist/client-graph.d.ts +94 -0
  26. package/dist/client-graph.js +266 -0
  27. package/dist/client-modules.d.ts +10 -0
  28. package/dist/client-modules.js +43 -0
  29. package/dist/client.d.ts +42 -0
  30. package/dist/client.js +263 -0
  31. package/dist/commonjs.d.ts +44 -0
  32. package/dist/commonjs.js +111 -0
  33. package/dist/compat/browser/client.d.ts +19 -0
  34. package/dist/compat/browser/client.js +13 -0
  35. package/dist/compat/build.d.ts +145 -0
  36. package/dist/compat/build.js +149 -0
  37. package/dist/compat/checks.d.ts +16 -0
  38. package/dist/compat/checks.js +148 -0
  39. package/dist/compat/patches.d.ts +13 -0
  40. package/dist/compat/patches.js +30 -0
  41. package/dist/compat/paths.d.ts +74 -0
  42. package/dist/compat/paths.js +160 -0
  43. package/dist/compat/rsc/handlers.d.ts +2 -0
  44. package/dist/compat/rsc/handlers.js +2 -0
  45. package/dist/compat/rsc/prerender.d.ts +10 -0
  46. package/dist/compat/rsc/prerender.js +9 -0
  47. package/dist/compat/server/headers.d.ts +3 -0
  48. package/dist/compat/server/headers.js +3 -0
  49. package/dist/compat/ssr/build-output.d.ts +4 -0
  50. package/dist/compat/ssr/build-output.js +3 -0
  51. package/dist/compat/ssr/cache.d.ts +6 -0
  52. package/dist/compat/ssr/cache.js +6 -0
  53. package/dist/compat/ssr/handler.d.ts +8 -0
  54. package/dist/compat/ssr/handler.js +8 -0
  55. package/dist/compat/ssr/node-server.d.ts +5 -0
  56. package/dist/compat/ssr/node-server.js +4 -0
  57. package/dist/compat/ssr/render-errors.d.ts +4 -0
  58. package/dist/compat/ssr/render-errors.js +4 -0
  59. package/dist/compat/ssr/senders.d.ts +6 -0
  60. package/dist/compat/ssr/senders.js +6 -0
  61. package/dist/compat/types.d.ts +8 -0
  62. package/dist/compat/types.js +1 -0
  63. package/dist/compile.d.ts +31 -0
  64. package/dist/compile.js +140 -0
  65. package/dist/disk.d.ts +16 -0
  66. package/dist/disk.js +13 -0
  67. package/dist/document-loads.d.ts +15 -0
  68. package/dist/document-loads.js +53 -0
  69. package/dist/document.d.ts +79 -0
  70. package/dist/document.js +328 -0
  71. package/dist/downloads.d.ts +15 -0
  72. package/dist/downloads.js +71 -0
  73. package/dist/esm-require.d.ts +11 -0
  74. package/dist/esm-require.js +32 -0
  75. package/dist/fast-crypto/index.js +1093 -0
  76. package/dist/fast-crypto.d.ts +20 -0
  77. package/dist/fast-crypto.js +151 -0
  78. package/dist/flight.d.ts +19 -0
  79. package/dist/flight.js +42 -0
  80. package/dist/form-submission.d.ts +15 -0
  81. package/dist/form-submission.js +27 -0
  82. package/dist/from-page.d.ts +5 -0
  83. package/dist/from-page.js +44 -0
  84. package/dist/globals.d.ts +1 -0
  85. package/dist/globals.js +197 -0
  86. package/dist/host-document.d.ts +26 -0
  87. package/dist/host-document.js +34 -0
  88. package/dist/index.d.ts +280 -0
  89. package/dist/index.js +281 -0
  90. package/dist/internal.d.ts +2 -0
  91. package/dist/internal.js +3 -0
  92. package/dist/leftovers.d.ts +11 -0
  93. package/dist/leftovers.js +55 -0
  94. package/dist/mode.d.ts +169 -0
  95. package/dist/mode.js +31 -0
  96. package/dist/node/LICENSES.md +3540 -0
  97. package/dist/node/assert/strict.js +29 -0
  98. package/dist/node/assert.js +3044 -0
  99. package/dist/node/buffer.js +1499 -0
  100. package/dist/node/child_process.js +45 -0
  101. package/dist/node/cluster.js +85 -0
  102. package/dist/node/console.js +96 -0
  103. package/dist/node/constants.js +495 -0
  104. package/dist/node/crypto.js +1388 -0
  105. package/dist/node/dgram.js +78 -0
  106. package/dist/node/diagnostics_channel.js +122 -0
  107. package/dist/node/dns/promises.js +129 -0
  108. package/dist/node/dns.js +137 -0
  109. package/dist/node/domain.js +43 -0
  110. package/dist/node/events.js +1155 -0
  111. package/dist/node/fs/promises.js +40 -0
  112. package/dist/node/fs.js +10375 -0
  113. package/dist/node/http.js +326 -0
  114. package/dist/node/http2.js +305 -0
  115. package/dist/node/https.js +57 -0
  116. package/dist/node/inspector/promises.js +66 -0
  117. package/dist/node/inspector.js +76 -0
  118. package/dist/node/module.js +205 -0
  119. package/dist/node/net.js +392 -0
  120. package/dist/node/os.js +259 -0
  121. package/dist/node/path/posix.js +25 -0
  122. package/dist/node/path/win32.js +25 -0
  123. package/dist/node/path.js +384 -0
  124. package/dist/node/perf_hooks.js +313 -0
  125. package/dist/node/process.js +452 -0
  126. package/dist/node/punycode.js +178 -0
  127. package/dist/node/querystring.js +934 -0
  128. package/dist/node/readline/promises.js +71 -0
  129. package/dist/node/readline.js +56 -0
  130. package/dist/node/repl.js +44 -0
  131. package/dist/node/sqlite.js +22 -0
  132. package/dist/node/stream/consumers.js +47 -0
  133. package/dist/node/stream/promises.js +6 -0
  134. package/dist/node/stream/web.js +52 -0
  135. package/dist/node/stream.js +4323 -0
  136. package/dist/node/string_decoder.js +276 -0
  137. package/dist/node/sys.js +6 -0
  138. package/dist/node/timers/promises.js +71 -0
  139. package/dist/node/timers.js +133 -0
  140. package/dist/node/tls.js +122 -0
  141. package/dist/node/trace_events.js +23 -0
  142. package/dist/node/tty.js +69 -0
  143. package/dist/node/url.js +1426 -0
  144. package/dist/node/util/types.js +1456 -0
  145. package/dist/node/util.js +4807 -0
  146. package/dist/node/v8.js +160 -0
  147. package/dist/node/vm.js +88 -0
  148. package/dist/node/wasi.js +16 -0
  149. package/dist/node/worker_threads.js +123 -0
  150. package/dist/node/zlib.js +1396 -0
  151. package/dist/node-builtins.d.ts +40 -0
  152. package/dist/node-builtins.js +74 -0
  153. package/dist/node-on-page.d.ts +46 -0
  154. package/dist/node-on-page.js +101 -0
  155. package/dist/node-platform.d.ts +36 -0
  156. package/dist/node-platform.js +152 -0
  157. package/dist/node-rendered.d.ts +21 -0
  158. package/dist/node-rendered.js +26 -0
  159. package/dist/node-server.d.ts +46 -0
  160. package/dist/node-server.js +193 -0
  161. package/dist/optimizer-cache-key.d.ts +26 -0
  162. package/dist/optimizer-cache-key.js +73 -0
  163. package/dist/own-package.d.ts +1 -0
  164. package/dist/own-package.js +6 -0
  165. package/dist/page-store.d.ts +33 -0
  166. package/dist/page-store.js +80 -0
  167. package/dist/page-timers.d.ts +22 -0
  168. package/dist/page-timers.js +47 -0
  169. package/dist/page.d.ts +112 -0
  170. package/dist/page.js +549 -0
  171. package/dist/paths.d.ts +13 -0
  172. package/dist/paths.js +47 -0
  173. package/dist/plugin.d.ts +22 -0
  174. package/dist/plugin.js +995 -0
  175. package/dist/prerender-verdict.d.ts +8 -0
  176. package/dist/prerender-verdict.js +67 -0
  177. package/dist/project/context.d.ts +546 -0
  178. package/dist/project/context.js +111 -0
  179. package/dist/project/entries.d.ts +42 -0
  180. package/dist/project/entries.js +189 -0
  181. package/dist/project/env-worker.d.ts +1 -0
  182. package/dist/project/env-worker.js +9 -0
  183. package/dist/project/env.d.ts +36 -0
  184. package/dist/project/env.js +82 -0
  185. package/dist/project/layers.d.ts +31 -0
  186. package/dist/project/layers.js +150 -0
  187. package/dist/project/loaders.d.ts +71 -0
  188. package/dist/project/loaders.js +300 -0
  189. package/dist/project/metadata.d.ts +8 -0
  190. package/dist/project/metadata.js +34 -0
  191. package/dist/project/module-exports.d.ts +29 -0
  192. package/dist/project/module-exports.js +77 -0
  193. package/dist/project/routes.d.ts +19 -0
  194. package/dist/project/routes.js +92 -0
  195. package/dist/project/routing.d.ts +46 -0
  196. package/dist/project/routing.js +280 -0
  197. package/dist/project/transform.d.ts +8 -0
  198. package/dist/project/transform.js +110 -0
  199. package/dist/project/webpack.d.ts +46 -0
  200. package/dist/project/webpack.js +160 -0
  201. package/dist/project.d.ts +264 -0
  202. package/dist/project.js +58 -0
  203. package/dist/registry-global.d.ts +5 -0
  204. package/dist/registry-global.js +5 -0
  205. package/dist/registry.d.ts +241 -0
  206. package/dist/registry.js +30 -0
  207. package/dist/route-chunks.d.ts +13 -0
  208. package/dist/route-chunks.js +32 -0
  209. package/dist/route-lookup.d.ts +34 -0
  210. package/dist/route-lookup.js +207 -0
  211. package/dist/routes-ahead-shared.d.ts +17 -0
  212. package/dist/routes-ahead-shared.js +9 -0
  213. package/dist/routes-ahead.d.ts +10 -0
  214. package/dist/routes-ahead.js +54 -0
  215. package/dist/rsc.d.ts +19 -0
  216. package/dist/rsc.js +171 -0
  217. package/dist/server-action.d.ts +9 -0
  218. package/dist/server-action.js +15 -0
  219. package/dist/server-code.d.ts +176 -0
  220. package/dist/server-code.js +357 -0
  221. package/dist/server-in-front.d.ts +37 -0
  222. package/dist/server-in-front.js +90 -0
  223. package/dist/server-layers.d.ts +22 -0
  224. package/dist/server-layers.js +33 -0
  225. package/dist/setup-browser.d.ts +1 -0
  226. package/dist/setup-browser.js +8 -0
  227. package/dist/setup-node.d.ts +1 -0
  228. package/dist/setup-node.js +6 -0
  229. package/dist/setup.d.ts +8 -0
  230. package/dist/setup.js +62 -0
  231. package/dist/speed-features.d.ts +13 -0
  232. package/dist/speed-features.js +95 -0
  233. package/dist/ssr.d.ts +70 -0
  234. package/dist/ssr.js +480 -0
  235. package/dist/static-build.d.ts +36 -0
  236. package/dist/static-build.js +92 -0
  237. package/dist/storage.d.ts +31 -0
  238. package/dist/storage.js +94 -0
  239. package/dist/styles-shared.d.ts +137 -0
  240. package/dist/styles-shared.js +124 -0
  241. package/dist/styles.d.ts +102 -0
  242. package/dist/styles.js +730 -0
  243. package/dist/stylesheet-versions.d.ts +17 -0
  244. package/dist/stylesheet-versions.js +43 -0
  245. package/dist/tester.html +10 -0
  246. package/dist/use-cache.d.ts +15 -0
  247. package/dist/use-cache.js +93 -0
  248. package/dist/vitest-version.d.ts +6 -0
  249. package/dist/vitest-version.js +12 -0
  250. package/package.json +125 -3
@@ -0,0 +1,94 @@
1
+ //#region src/storage.ts
2
+ /**
3
+ * The items of the page's `localStorage` and `sessionStorage`. One for each
4
+ * browser context of the app.
5
+ */
6
+ var PageStorage = class {
7
+ localStorage = /* @__PURE__ */ new Map();
8
+ sessionStorage = /* @__PURE__ */ new Map();
9
+ };
10
+ /** The items of a `Storage` of the browser's, as a map. */
11
+ function itemsOfStorage(storage) {
12
+ return {
13
+ get: (key) => storage.getItem(key) ?? void 0,
14
+ set: (key, value) => storage.setItem(key, value),
15
+ delete(key) {
16
+ const had = storage.getItem(key) !== null;
17
+ storage.removeItem(key);
18
+ return had;
19
+ },
20
+ has: (key) => storage.getItem(key) !== null,
21
+ clear: () => storage.clear(),
22
+ keys: () => Array.from({ length: storage.length }, (_, index) => storage.key(index)),
23
+ get size() {
24
+ return storage.length;
25
+ }
26
+ };
27
+ }
28
+ const sources = /* @__PURE__ */ new WeakMap();
29
+ const itemsOf = (storage) => sources.get(storage)();
30
+ const storagePrototype = Object.create(Storage.prototype, {
31
+ length: {
32
+ get() {
33
+ return itemsOf(this).size;
34
+ },
35
+ enumerable: true,
36
+ configurable: true
37
+ },
38
+ ...Object.getOwnPropertyDescriptors({
39
+ key(index) {
40
+ return [...itemsOf(this).keys()][index >>> 0] ?? null;
41
+ },
42
+ getItem(key) {
43
+ return itemsOf(this).get(`${key}`) ?? null;
44
+ },
45
+ setItem(key, value) {
46
+ itemsOf(this).set(`${key}`, `${value}`);
47
+ },
48
+ removeItem(key) {
49
+ itemsOf(this).delete(`${key}`);
50
+ },
51
+ clear() {
52
+ itemsOf(this).clear();
53
+ }
54
+ })
55
+ });
56
+ /**
57
+ * A `Storage` of the items that `items()` returns each time it is used. Its
58
+ * items are its properties too, as in a browser: `storage.theme`,
59
+ * `Object.keys(storage)` and `JSON.stringify(storage)` have them, and setting
60
+ * or deleting a property sets or removes an item. A name of `Storage`, like
61
+ * `key` or `length`, hides the item of that name.
62
+ */
63
+ function storageOf(items) {
64
+ const target = Object.create(storagePrototype);
65
+ const isItem = (name) => typeof name === "string" && !(name in target) && items().has(name);
66
+ const storage = new Proxy(target, {
67
+ get: (target, name, receiver) => isItem(name) ? items().get(name) : Reflect.get(target, name, receiver),
68
+ set(target, name, value, receiver) {
69
+ if (typeof name !== "string" || name in target || receiver !== storage) return Reflect.set(target, name, value, receiver);
70
+ items().set(name, `${value}`);
71
+ return true;
72
+ },
73
+ has: (target, name) => isItem(name) || Reflect.has(target, name),
74
+ deleteProperty: (target, name) => isItem(name) ? items().delete(name) : Reflect.deleteProperty(target, name),
75
+ ownKeys: (target) => [...[...items().keys()].filter(isItem), ...Reflect.ownKeys(target)],
76
+ getOwnPropertyDescriptor: (target, name) => isItem(name) ? {
77
+ value: items().get(name),
78
+ writable: true,
79
+ enumerable: true,
80
+ configurable: true
81
+ } : Reflect.getOwnPropertyDescriptor(target, name),
82
+ defineProperty(target, name, descriptor) {
83
+ if (typeof name !== "string" || name in target) return Reflect.defineProperty(target, name, descriptor);
84
+ if (!("value" in descriptor)) return false;
85
+ items().set(name, `${descriptor.value}`);
86
+ return true;
87
+ },
88
+ preventExtensions: () => false
89
+ });
90
+ sources.set(storage, items);
91
+ return storage;
92
+ }
93
+ //#endregion
94
+ export { PageStorage, itemsOfStorage, storageOf };
@@ -0,0 +1,137 @@
1
+ //#region src/styles-shared.d.ts
2
+ /**
3
+ * The file of a static build with the files a page loads besides the layers:
4
+ * the stylesheets and the modules of every page route, and the stylesheets
5
+ * of every file of the host, in its directory.
6
+ */
7
+ declare const builtFilesPath = "vitest-plugin-rsc/next-files.json";
8
+ /**
9
+ * Where the dev server answers the page with stylesheets, over the websocket
10
+ * of the page (`askDevServer()` in ../utils.ts), for a `StylesheetsQuestion`.
11
+ */
12
+ declare const stylesheetsEvent = "vitest-plugin-rsc:next-stylesheets";
13
+ /**
14
+ * What the dev server tells the page after a change of stylesheets that it
15
+ * links, without class names: the URL of the version of now of each, which
16
+ * the page moves every link of the stylesheet to (stylesheet-versions.ts).
17
+ */
18
+ declare const stylesheetsChangedEvent = "vitest-plugin-rsc:next-stylesheets-changed";
19
+ /** Where Next's build puts the CSS of an app, under `/_next/`. */
20
+ declare const cssDirectory = "static/css";
21
+ /**
22
+ * The version of its CSS that the path of a stylesheet of the dev server has
23
+ * after the directory, `static/css/<version>/app/globals.css`: see styles.ts.
24
+ */
25
+ declare const cssVersionPattern = "[0-9a-f]{16}";
26
+ /**
27
+ * The stylesheet that a URL of a link is of, whatever its version and however
28
+ * its path is encoded: the path of the URL without its version, decoded.
29
+ * Nothing for a URL that is no stylesheet of the dev server.
30
+ */
31
+ declare function stylesheetOfUrl(url: string): string | undefined;
32
+ /**
33
+ * What the page asks at `stylesheetsEvent`. For a route: by the name of its
34
+ * modules, with `Stylesheets`, and `files`, the files of the host that render
35
+ * the node of the route, in their order, absolute or from the root of the
36
+ * project: see `hostFiles` of `renderServer()`. For the files of the host
37
+ * that the host uses, `linkStylesheetsOf()`: by their paths, as
38
+ * `hostFiles`, with the URLs of their stylesheets.
39
+ */
40
+ type StylesheetsQuestion = {
41
+ entry: string;
42
+ inline: boolean;
43
+ files: string[];
44
+ } | {
45
+ hostFiles: string[];
46
+ };
47
+ /**
48
+ * What marks the `<link>` of a stylesheet of a file of the host, which
49
+ * `linkStylesheetsOf()` linked (client-graph.ts): the host's, not a page's,
50
+ * so a page that is left does not take it (document.ts).
51
+ */
52
+ declare const graphStylesheetAttribute = "data-vitest-plugin-rsc-graph";
53
+ /**
54
+ * What the loader tree of the route of a node names its page, where a page of
55
+ * the app has its file. Next looks up the stylesheets of the node by it.
56
+ */
57
+ declare const componentPagePath = "vitest-plugin-rsc/component";
58
+ /**
59
+ * The stylesheets of a route. For each file of a segment that has any, by the
60
+ * name the loader tree has for it without its extension, which is how Next
61
+ * looks them up: their paths under `/_next/`, and their CSS where the app has
62
+ * Next inline it (`experimental.inlineCss`). The name is the path of the file
63
+ * from the root of the project, `app/layout`: see `RouteSegment` in project.ts.
64
+ * The stylesheet of a `next/font` call has its font files too, under
65
+ * `/_next/`, which Next preloads with the segment.
66
+ */
67
+ type Stylesheets = Record<string, {
68
+ path: string;
69
+ content?: string;
70
+ fonts?: string[];
71
+ }[]>;
72
+ /** What `builtFilesPath` has: the stylesheets and the modules of a build, as files of it. */
73
+ type BuiltFiles = {
74
+ /** Where the browser asks for the files of the app, `/_next/`, from which Next links a stylesheet. */assetPath: string;
75
+ /**
76
+ * For each page route, by the name of its modules: the files of its
77
+ * stylesheets, per segment as `Stylesheets` has them, from the directory of
78
+ * the build. The route of a node has an empty list for the node, at
79
+ * `componentPagePath`: those are the stylesheets of the files of the host
80
+ * that render it, which the host names when it renders, see `hostFiles`.
81
+ */
82
+ routes: Record<string, Record<string, string[]>>;
83
+ /**
84
+ * For each file of the host that the rsc layer built, by its path from the
85
+ * root of the project, `host/button.demo.tsx`: the files of the
86
+ * stylesheets it imports, as the dev server walks them for `hostFiles` of
87
+ * `renderServer()`. In a build the files of the host are what `host.files`
88
+ * of `vitestPluginNext()` matches. An empty list for one without any.
89
+ */
90
+ hostFiles: Record<string, string[]>; /** The files of the rsc layer that `modules` names, from the directory of the build. */
91
+ chunks: string[];
92
+ /**
93
+ * For each page route, by the name of its modules: the files of the rsc
94
+ * layer that its entry loads, by their index in `chunks`, the entry's own
95
+ * first. Those of the segments and boundaries of its loader tree, and
96
+ * every one those import, but not one that a segment imports with
97
+ * `import()`, which loads only when the segment asks for it, nor one the
98
+ * page has before it loads a route: see route-chunks.ts.
99
+ */
100
+ modules: Record<string, number[]>;
101
+ /**
102
+ * For the stylesheet of each `next/font` call, by its file: the font files
103
+ * of the call, from the directory of the build. See `Stylesheets`.
104
+ */
105
+ fonts: Record<string, string[]>;
106
+ };
107
+ /**
108
+ * The key of a file of the host in `BuiltFiles.hostFiles`, of a path that
109
+ * the host names it by: from the root of the project, which `./` and `..`
110
+ * may be part of, or absolute. A build has no path of the machine, so an
111
+ * absolute path is the longest key that it ends with.
112
+ */
113
+ declare function builtFileKey(file: string, keys: string[]): string | undefined;
114
+ /**
115
+ * The stylesheets of a route of a static build that is served from
116
+ * `directory`, a URL: what the dev server answers at `stylesheetsEvent`. For
117
+ * the route of a node, the node has the stylesheets of `files`, the files of
118
+ * the host that render it (`hostFiles` of `renderServer()`), in their order,
119
+ * each once. A file that the build has none for, `missing` is told of.
120
+ *
121
+ * Next links a stylesheet at its path under the asset path, from the root of
122
+ * the site. A build can be served from any directory, so that path goes up to
123
+ * the root first when it has to: `/_next/../docs/_next/static/css/page.css`.
124
+ *
125
+ * Next inlines no CSS of a static build: what a stylesheet of it names, like a
126
+ * font, is named by the way from that stylesheet.
127
+ */
128
+ declare function builtStylesheetsOf(built: BuiltFiles, entry: string, directory: string, files?: string[], missing?: (file: string) => void): Stylesheets;
129
+ /**
130
+ * The stylesheets of a file of the host in a static build that is served
131
+ * from `directory`, a URL: those that `hostFiles` has for it by its key,
132
+ * `file`, as URLs. What the dev server answers for the file at
133
+ * `stylesheetsEvent`.
134
+ */
135
+ declare function builtHostFileStylesheetsOf(built: BuiltFiles, file: string, directory: string): string[];
136
+ //#endregion
137
+ export { BuiltFiles, Stylesheets, StylesheetsQuestion, builtFileKey, builtFilesPath, builtHostFileStylesheetsOf, builtStylesheetsOf, componentPagePath, cssDirectory, cssVersionPattern, graphStylesheetAttribute, stylesheetOfUrl, stylesheetsChangedEvent, stylesheetsEvent };
@@ -0,0 +1,124 @@
1
+ //#region src/styles-shared.ts
2
+ /**
3
+ * The file of a static build with the files a page loads besides the layers:
4
+ * the stylesheets and the modules of every page route, and the stylesheets
5
+ * of every file of the host, in its directory.
6
+ */
7
+ const builtFilesPath = "vitest-plugin-rsc/next-files.json";
8
+ /**
9
+ * Where the dev server answers the page with stylesheets, over the websocket
10
+ * of the page (`askDevServer()` in ../utils.ts), for a `StylesheetsQuestion`.
11
+ */
12
+ const stylesheetsEvent = "vitest-plugin-rsc:next-stylesheets";
13
+ /**
14
+ * What the dev server tells the page after a change of stylesheets that it
15
+ * links, without class names: the URL of the version of now of each, which
16
+ * the page moves every link of the stylesheet to (stylesheet-versions.ts).
17
+ */
18
+ const stylesheetsChangedEvent = "vitest-plugin-rsc:next-stylesheets-changed";
19
+ /** Where Next's build puts the CSS of an app, under `/_next/`. */
20
+ const cssDirectory = "static/css";
21
+ /**
22
+ * The version of its CSS that the path of a stylesheet of the dev server has
23
+ * after the directory, `static/css/<version>/app/globals.css`: see styles.ts.
24
+ */
25
+ const cssVersionPattern = "[0-9a-f]{16}";
26
+ const versionSegmentRE = new RegExp(`/${cssDirectory}/${cssVersionPattern}/`);
27
+ /**
28
+ * The stylesheet that a URL of a link is of, whatever its version and however
29
+ * its path is encoded: the path of the URL without its version, decoded.
30
+ * Nothing for a URL that is no stylesheet of the dev server.
31
+ */
32
+ function stylesheetOfUrl(url) {
33
+ const { pathname } = new URL(url, "http://n");
34
+ if (!versionSegmentRE.test(pathname)) return void 0;
35
+ try {
36
+ return pathname.replace(versionSegmentRE, `/${cssDirectory}/`).split("/").map(decodeURIComponent).join("/");
37
+ } catch {
38
+ return;
39
+ }
40
+ }
41
+ /**
42
+ * What marks the `<link>` of a stylesheet of a file of the host, which
43
+ * `linkStylesheetsOf()` linked (client-graph.ts): the host's, not a page's,
44
+ * so a page that is left does not take it (document.ts).
45
+ */
46
+ const graphStylesheetAttribute = "data-vitest-plugin-rsc-graph";
47
+ /**
48
+ * What the loader tree of the route of a node names its page, where a page of
49
+ * the app has its file. Next looks up the stylesheets of the node by it.
50
+ */
51
+ const componentPagePath = "vitest-plugin-rsc/component";
52
+ /**
53
+ * The key of a file of the host in `BuiltFiles.hostFiles`, of a path that
54
+ * the host names it by: from the root of the project, which `./` and `..`
55
+ * may be part of, or absolute. A build has no path of the machine, so an
56
+ * absolute path is the longest key that it ends with.
57
+ */
58
+ function builtFileKey(file, keys) {
59
+ file = file.replaceAll("\\", "/");
60
+ if (file.startsWith("/") || /^[a-z]:\//i.test(file)) {
61
+ let found;
62
+ for (const key of keys) if (file.endsWith(`/${key}`) && key.length > (found?.length ?? -1)) found = key;
63
+ return found;
64
+ }
65
+ const segments = [];
66
+ for (const segment of file.split("/")) {
67
+ if (segment === "" || segment === ".") continue;
68
+ if (segment === ".." && segments.length > 0 && segments.at(-1) !== "..") segments.pop();
69
+ else segments.push(segment);
70
+ }
71
+ const key = segments.join("/");
72
+ return keys.includes(key) ? key : void 0;
73
+ }
74
+ /**
75
+ * The stylesheets of a route of a static build that is served from
76
+ * `directory`, a URL: what the dev server answers at `stylesheetsEvent`. For
77
+ * the route of a node, the node has the stylesheets of `files`, the files of
78
+ * the host that render it (`hostFiles` of `renderServer()`), in their order,
79
+ * each once. A file that the build has none for, `missing` is told of.
80
+ *
81
+ * Next links a stylesheet at its path under the asset path, from the root of
82
+ * the site. A build can be served from any directory, so that path goes up to
83
+ * the root first when it has to: `/_next/../docs/_next/static/css/page.css`.
84
+ *
85
+ * Next inlines no CSS of a static build: what a stylesheet of it names, like a
86
+ * font, is named by the way from that stylesheet.
87
+ */
88
+ function builtStylesheetsOf(built, entry, directory, files = [], missing = () => {}) {
89
+ const from = built.assetPath.split("/").filter(Boolean);
90
+ const pathOf = (file) => {
91
+ const to = `${decodeURIComponent(new URL(directory).pathname)}${file}`.split("/").filter(Boolean);
92
+ let common = 0;
93
+ while (common < from.length && common < to.length - 1 && from[common] === to[common]) common++;
94
+ return [...from.slice(common).map(() => ".."), ...to.slice(common)].join("/");
95
+ };
96
+ const segments = { ...built.routes[entry] };
97
+ if (segments["vitest-plugin-rsc/component"] && files.length > 0) {
98
+ const keys = Object.keys(built.hostFiles);
99
+ const ofFiles = files.flatMap((file) => {
100
+ const key = builtFileKey(file, keys);
101
+ if (key === void 0) missing(file);
102
+ return key === void 0 ? [] : built.hostFiles[key];
103
+ });
104
+ segments[componentPagePath] = [.../* @__PURE__ */ new Set([...ofFiles, ...segments[componentPagePath]])];
105
+ }
106
+ return Object.fromEntries(Object.entries(segments).map(([segment, stylesheets]) => [segment, stylesheets.map((file) => {
107
+ const fonts = built.fonts[file];
108
+ return fonts ? {
109
+ path: pathOf(file),
110
+ fonts: fonts.map(pathOf)
111
+ } : { path: pathOf(file) };
112
+ })]));
113
+ }
114
+ /**
115
+ * The stylesheets of a file of the host in a static build that is served
116
+ * from `directory`, a URL: those that `hostFiles` has for it by its key,
117
+ * `file`, as URLs. What the dev server answers for the file at
118
+ * `stylesheetsEvent`.
119
+ */
120
+ function builtHostFileStylesheetsOf(built, file, directory) {
121
+ return (built.hostFiles[file] ?? []).map((stylesheet) => new URL(stylesheet, directory).href);
122
+ }
123
+ //#endregion
124
+ export { builtFileKey, builtFilesPath, builtHostFileStylesheetsOf, builtStylesheetsOf, componentPagePath, cssDirectory, cssVersionPattern, graphStylesheetAttribute, stylesheetOfUrl, stylesheetsChangedEvent, stylesheetsEvent };
@@ -0,0 +1,102 @@
1
+ import { ComponentRoute, NextLayer, NextProject, NextRoute } from "./project.js";
2
+ import { EnvironmentModuleNode, Plugin, ViteDevServer } from "vite";
3
+
4
+ //#region src/styles.d.ts
5
+ /** The id of a stylesheet that Next links, of the id of its file, in a build or not. */
6
+ declare const linked: (id: string, build?: boolean) => string;
7
+ /**
8
+ * Whether a module is that of a stylesheet that Next links, whose links the
9
+ * page moves to the version of now after a change
10
+ * (`stylesheetsChangedEvent`). One without class names, which its module
11
+ * exports as a CSS module does, and so exports nothing. Not with
12
+ * `experimental.inlineCss`, with which Next puts the CSS of a page load in a
13
+ * `<style>`, which is no link: the page loads again for it.
14
+ */
15
+ declare const movesItsLinks: (node: EnvironmentModuleNode) => boolean;
16
+ /**
17
+ * Whether a stylesheet changes in place after a change, and what imports it
18
+ * sees nothing of it: one that accepts its own update, which Vite swaps as
19
+ * its `<style>`, and the module of a stylesheet that Next links without
20
+ * class names, whose links the page moves to the version of now
21
+ * (`stylesheetsChangedEvent`).
22
+ */
23
+ declare const changesInPlace: (node: EnvironmentModuleNode) => boolean;
24
+ declare function isCode(id: string, pageExtensions: string[]): boolean;
25
+ /**
26
+ * The order of the cascade layers of a stylesheet, kept where Vite inlines
27
+ * its `@import`s. A layer comes in the order in which a stylesheet names it
28
+ * first, and an `@import` with `layer()` names it before an `@layer`
29
+ * statement that follows the import. `postcss-import`, with which Vite
30
+ * inlines an import, puts such a statement before the CSS of the import, so
31
+ * that its layers come first. With a statement of every layer of the imports
32
+ * at the top, which can precede an `@import`, the order is the stylesheet's
33
+ * own. Not that of a stylesheet it imports, which `postcss-import` reads
34
+ * itself, nor of a layer without a name, which a statement cannot name.
35
+ */
36
+ declare function declareImportedLayers(css: string): string;
37
+ /**
38
+ * The exports of Vite's module for a stylesheet, without the code that puts
39
+ * the CSS in the document.
40
+ *
41
+ * Vite's module for a client (`vite:css-post`) puts the CSS in a `<style>`,
42
+ * and has the class names of a CSS module as its exports, as Vite documents
43
+ * them. Its module for a server is those exports, with the constants they
44
+ * are made of: one for a class name that is no name of a variable, like
45
+ * `switch`. So the exports are kept, with the statements they use, and the
46
+ * rest is not. That holds as long as what they use is not the code of the
47
+ * `<style>`: an import, or `import.meta`.
48
+ */
49
+ declare function exportsOfStylesheet(code: string, id: string): string;
50
+ type PageRoute = NextRoute | ComponentRoute;
51
+ type StylesOptions = {
52
+ getProject(): NextProject; /** The Vite environment of each layer. */
53
+ environments: Record<NextLayer, string>;
54
+ /**
55
+ * Whether a file is server code in the rsc layer, which has the test files
56
+ * too: the app's, and that of the packages it imports.
57
+ */
58
+ isServerCode(file: string): boolean;
59
+ /**
60
+ * Whether a file is the host's: a test file or a setup file of Vitest, or a
61
+ * file of another host.
62
+ */
63
+ isHostFile(file: string): boolean; /** Whether a file is of a package of the host, like Vitest's runtime. */
64
+ isHostPackageFile(file: string): boolean; /** The page routes, by the name of their modules. */
65
+ pageRoutes(): Map<string, PageRoute>; /** The id of the entry of a page route in the rsc layer, by the name of its modules. */
66
+ entryIdOf(route: string): string; /** The ids of the modules that list the routes: what leads from a test file to every route. */
67
+ lists: string[];
68
+ };
69
+ type Styles = {
70
+ plugins: Plugin[];
71
+ /**
72
+ * Has the dev server compile a page route, by the name of its modules, as
73
+ * the first page load of it does: its modules in both layers, the CSS it
74
+ * links and the modules of that CSS. See routes-ahead.ts.
75
+ */
76
+ compileRoute(vite: ViteDevServer, entry: string): Promise<void>;
77
+ /**
78
+ * What a static build has of the stylesheets, and the file that lists them
79
+ * with the modules of every route, once it is built, by its path in the build.
80
+ */
81
+ builtFiles(): {
82
+ pathname: string;
83
+ body: Uint8Array;
84
+ }[];
85
+ };
86
+ declare function createStyles(options: StylesOptions): Styles;
87
+ /**
88
+ * The key of a file of the host in `hostFiles` of `BuiltFiles`, by the
89
+ * id of its module in a build: its path from the root of the project, the
90
+ * same on every machine. Nothing for a file on another drive of Windows,
91
+ * which has no way from the root: its path is the machine's.
92
+ */
93
+ declare function builtHostFileKey(id: string, root: string): string | undefined;
94
+ /**
95
+ * The key of a module of a build, as `builtHostFileKey()` has it, when the
96
+ * module is a file of the host: matched by its path from the root, as
97
+ * `host.files` are from the root as the config has it. Nothing for a module
98
+ * that is no file, or one with a query.
99
+ */
100
+ declare function builtHostFileOf(id: string, root: string, isHostFile: (file: string) => boolean): string | undefined;
101
+ //#endregion
102
+ export { Styles, StylesOptions, builtHostFileKey, builtHostFileOf, changesInPlace, createStyles, declareImportedLayers, exportsOfStylesheet, isCode, linked, movesItsLinks };