@rentnerkev/pdf 0.0.0-stage → 0.1.1

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 (355) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +135 -2
  3. package/THIRD_PARTY_NOTICES.md +152 -0
  4. package/dist/components/Types/host.d.ts +3 -0
  5. package/dist/components/Types/host.d.ts.map +1 -0
  6. package/dist/components/Types/host.js +2 -0
  7. package/dist/components/Types/host.js.map +1 -0
  8. package/dist/components/Types/props.d.ts +504 -0
  9. package/dist/components/Types/props.d.ts.map +1 -0
  10. package/dist/components/Types/props.js +2 -0
  11. package/dist/components/Types/props.js.map +1 -0
  12. package/dist/components/components.d.ts +87 -0
  13. package/dist/components/components.d.ts.map +1 -0
  14. package/dist/components/components.js +86 -0
  15. package/dist/components/components.js.map +1 -0
  16. package/dist/components/host.d.ts +7 -0
  17. package/dist/components/host.d.ts.map +1 -0
  18. package/dist/components/host.js +15 -0
  19. package/dist/components/host.js.map +1 -0
  20. package/dist/engine/Types/backend.d.ts +120 -0
  21. package/dist/engine/Types/backend.d.ts.map +1 -0
  22. package/dist/engine/Types/backend.js +2 -0
  23. package/dist/engine/Types/backend.js.map +1 -0
  24. package/dist/engine/Types/engine.d.ts +192 -0
  25. package/dist/engine/Types/engine.d.ts.map +1 -0
  26. package/dist/engine/Types/engine.js +2 -0
  27. package/dist/engine/Types/engine.js.map +1 -0
  28. package/dist/engine/Types/errors.d.ts +16 -0
  29. package/dist/engine/Types/errors.d.ts.map +1 -0
  30. package/dist/engine/Types/errors.js +2 -0
  31. package/dist/engine/Types/errors.js.map +1 -0
  32. package/dist/engine/Types/limits.d.ts +55 -0
  33. package/dist/engine/Types/limits.d.ts.map +1 -0
  34. package/dist/engine/Types/limits.js +2 -0
  35. package/dist/engine/Types/limits.js.map +1 -0
  36. package/dist/engine/Types/render-options.d.ts +71 -0
  37. package/dist/engine/Types/render-options.d.ts.map +1 -0
  38. package/dist/engine/Types/render-options.js +2 -0
  39. package/dist/engine/Types/render-options.js.map +1 -0
  40. package/dist/engine/Types/render-result.d.ts +80 -0
  41. package/dist/engine/Types/render-result.d.ts.map +1 -0
  42. package/dist/engine/Types/render-result.js +2 -0
  43. package/dist/engine/Types/render-result.js.map +1 -0
  44. package/dist/engine/abort.d.ts +23 -0
  45. package/dist/engine/abort.d.ts.map +1 -0
  46. package/dist/engine/abort.js +71 -0
  47. package/dist/engine/abort.js.map +1 -0
  48. package/dist/engine/assets.d.ts +22 -0
  49. package/dist/engine/assets.d.ts.map +1 -0
  50. package/dist/engine/assets.js +40 -0
  51. package/dist/engine/assets.js.map +1 -0
  52. package/dist/engine/backend.d.ts +16 -0
  53. package/dist/engine/backend.d.ts.map +1 -0
  54. package/dist/engine/backend.js +43 -0
  55. package/dist/engine/backend.js.map +1 -0
  56. package/dist/engine/backends/native.d.ts +11 -0
  57. package/dist/engine/backends/native.d.ts.map +1 -0
  58. package/dist/engine/backends/native.js +62 -0
  59. package/dist/engine/backends/native.js.map +1 -0
  60. package/dist/engine/backends/wasm.d.ts +16 -0
  61. package/dist/engine/backends/wasm.d.ts.map +1 -0
  62. package/dist/engine/backends/wasm.js +216 -0
  63. package/dist/engine/backends/wasm.js.map +1 -0
  64. package/dist/engine/channel.d.ts +41 -0
  65. package/dist/engine/channel.d.ts.map +1 -0
  66. package/dist/engine/channel.js +140 -0
  67. package/dist/engine/channel.js.map +1 -0
  68. package/dist/engine/create-engine.d.ts +8 -0
  69. package/dist/engine/create-engine.d.ts.map +1 -0
  70. package/dist/engine/create-engine.js +120 -0
  71. package/dist/engine/create-engine.js.map +1 -0
  72. package/dist/engine/default-fonts.d.ts +9 -0
  73. package/dist/engine/default-fonts.d.ts.map +1 -0
  74. package/dist/engine/default-fonts.js +42 -0
  75. package/dist/engine/default-fonts.js.map +1 -0
  76. package/dist/engine/encode.d.ts +9 -0
  77. package/dist/engine/encode.d.ts.map +1 -0
  78. package/dist/engine/encode.js +199 -0
  79. package/dist/engine/encode.js.map +1 -0
  80. package/dist/engine/errors.d.ts +37 -0
  81. package/dist/engine/errors.d.ts.map +1 -0
  82. package/dist/engine/errors.js +71 -0
  83. package/dist/engine/errors.js.map +1 -0
  84. package/dist/engine/fonts.d.ts +9 -0
  85. package/dist/engine/fonts.d.ts.map +1 -0
  86. package/dist/engine/fonts.js +14 -0
  87. package/dist/engine/fonts.js.map +1 -0
  88. package/dist/engine/lifecycle.d.ts +40 -0
  89. package/dist/engine/lifecycle.d.ts.map +1 -0
  90. package/dist/engine/lifecycle.js +104 -0
  91. package/dist/engine/lifecycle.js.map +1 -0
  92. package/dist/engine/markup.d.ts +13 -0
  93. package/dist/engine/markup.d.ts.map +1 -0
  94. package/dist/engine/markup.js +14 -0
  95. package/dist/engine/markup.js.map +1 -0
  96. package/dist/engine/options.d.ts +12 -0
  97. package/dist/engine/options.d.ts.map +1 -0
  98. package/dist/engine/options.js +71 -0
  99. package/dist/engine/options.js.map +1 -0
  100. package/dist/engine/pdf-bytes.d.ts +8 -0
  101. package/dist/engine/pdf-bytes.d.ts.map +1 -0
  102. package/dist/engine/pdf-bytes.js +9 -0
  103. package/dist/engine/pdf-bytes.js.map +1 -0
  104. package/dist/engine/prepare.d.ts +17 -0
  105. package/dist/engine/prepare.d.ts.map +1 -0
  106. package/dist/engine/prepare.js +44 -0
  107. package/dist/engine/prepare.js.map +1 -0
  108. package/dist/engine/render-many.d.ts +14 -0
  109. package/dist/engine/render-many.d.ts.map +1 -0
  110. package/dist/engine/render-many.js +156 -0
  111. package/dist/engine/render-many.js.map +1 -0
  112. package/dist/engine/render-result.d.ts +12 -0
  113. package/dist/engine/render-result.d.ts.map +1 -0
  114. package/dist/engine/render-result.js +62 -0
  115. package/dist/engine/render-result.js.map +1 -0
  116. package/dist/engine/render-stream.d.ts +7 -0
  117. package/dist/engine/render-stream.d.ts.map +1 -0
  118. package/dist/engine/render-stream.js +56 -0
  119. package/dist/engine/render-stream.js.map +1 -0
  120. package/dist/engine/render.d.ts +15 -0
  121. package/dist/engine/render.d.ts.map +1 -0
  122. package/dist/engine/render.js +23 -0
  123. package/dist/engine/render.js.map +1 -0
  124. package/dist/fonts/NotoSans-Bold.ttf +0 -0
  125. package/dist/fonts/NotoSans-BoldItalic.ttf +0 -0
  126. package/dist/fonts/NotoSans-Italic.ttf +0 -0
  127. package/dist/fonts/NotoSans-Regular.ttf +0 -0
  128. package/dist/fonts/OFL.txt +94 -0
  129. package/dist/fonts/sources.json +32 -0
  130. package/dist/index.d.ts +26 -0
  131. package/dist/index.d.ts.map +1 -0
  132. package/dist/index.js +11 -0
  133. package/dist/index.js.map +1 -0
  134. package/dist/ir/Types/input.d.ts +111 -0
  135. package/dist/ir/Types/input.d.ts.map +1 -0
  136. package/dist/ir/Types/input.js +2 -0
  137. package/dist/ir/Types/input.js.map +1 -0
  138. package/dist/ir/factories.d.ts +108 -0
  139. package/dist/ir/factories.d.ts.map +1 -0
  140. package/dist/ir/factories.js +410 -0
  141. package/dist/ir/factories.js.map +1 -0
  142. package/dist/ir/ir.generated.d.ts +811 -0
  143. package/dist/ir/ir.generated.d.ts.map +1 -0
  144. package/dist/ir/ir.generated.js +4 -0
  145. package/dist/ir/ir.generated.js.map +1 -0
  146. package/dist/ir/version.d.ts +9 -0
  147. package/dist/ir/version.d.ts.map +1 -0
  148. package/dist/ir/version.js +13 -0
  149. package/dist/ir/version.js.map +1 -0
  150. package/dist/jsx/Types/context.d.ts +24 -0
  151. package/dist/jsx/Types/context.d.ts.map +1 -0
  152. package/dist/jsx/Types/context.js +2 -0
  153. package/dist/jsx/Types/context.js.map +1 -0
  154. package/dist/jsx/Types/element.d.ts +30 -0
  155. package/dist/jsx/Types/element.d.ts.map +1 -0
  156. package/dist/jsx/Types/element.js +2 -0
  157. package/dist/jsx/Types/element.js.map +1 -0
  158. package/dist/jsx/Types/evaluate.d.ts +61 -0
  159. package/dist/jsx/Types/evaluate.d.ts.map +1 -0
  160. package/dist/jsx/Types/evaluate.js +2 -0
  161. package/dist/jsx/Types/evaluate.js.map +1 -0
  162. package/dist/jsx/Types/jsx.d.ts +16 -0
  163. package/dist/jsx/Types/jsx.d.ts.map +1 -0
  164. package/dist/jsx/Types/jsx.js +2 -0
  165. package/dist/jsx/Types/jsx.js.map +1 -0
  166. package/dist/jsx/context.d.ts +21 -0
  167. package/dist/jsx/context.d.ts.map +1 -0
  168. package/dist/jsx/context.js +49 -0
  169. package/dist/jsx/context.js.map +1 -0
  170. package/dist/jsx/element.d.ts +10 -0
  171. package/dist/jsx/element.d.ts.map +1 -0
  172. package/dist/jsx/element.js +19 -0
  173. package/dist/jsx/element.js.map +1 -0
  174. package/dist/jsx/evaluate.d.ts +16 -0
  175. package/dist/jsx/evaluate.d.ts.map +1 -0
  176. package/dist/jsx/evaluate.js +616 -0
  177. package/dist/jsx/evaluate.js.map +1 -0
  178. package/dist/jsx/source.d.ts +13 -0
  179. package/dist/jsx/source.d.ts.map +1 -0
  180. package/dist/jsx/source.js +49 -0
  181. package/dist/jsx/source.js.map +1 -0
  182. package/dist/jsx-dev-runtime.d.ts +9 -0
  183. package/dist/jsx-dev-runtime.d.ts.map +1 -0
  184. package/dist/jsx-dev-runtime.js +24 -0
  185. package/dist/jsx-dev-runtime.js.map +1 -0
  186. package/dist/jsx-runtime.d.ts +12 -0
  187. package/dist/jsx-runtime.d.ts.map +1 -0
  188. package/dist/jsx-runtime.js +15 -0
  189. package/dist/jsx-runtime.js.map +1 -0
  190. package/dist/native/binding.generated.d.ts +227 -0
  191. package/dist/native/loader.d.ts +22 -0
  192. package/dist/native/loader.d.ts.map +1 -0
  193. package/dist/native/loader.js +98 -0
  194. package/dist/native/loader.js.map +1 -0
  195. package/dist/native/rentnerpdf.linux-arm64-gnu.node +0 -0
  196. package/dist/native/rentnerpdf.linux-arm64-musl.node +0 -0
  197. package/dist/native/rentnerpdf.linux-x64-gnu.node +0 -0
  198. package/dist/native/rentnerpdf.linux-x64-musl.node +0 -0
  199. package/dist/native/rentnerpdf.win32-arm64-msvc.node +0 -0
  200. package/dist/native/rentnerpdf.win32-x64-msvc.node +0 -0
  201. package/dist/node.d.ts +3 -0
  202. package/dist/node.d.ts.map +1 -0
  203. package/dist/node.js +6 -0
  204. package/dist/node.js.map +1 -0
  205. package/dist/react/Types/preview.d.ts +74 -0
  206. package/dist/react/Types/preview.d.ts.map +1 -0
  207. package/dist/react/Types/preview.js +2 -0
  208. package/dist/react/Types/preview.js.map +1 -0
  209. package/dist/react/Types/render.d.ts +24 -0
  210. package/dist/react/Types/render.d.ts.map +1 -0
  211. package/dist/react/Types/render.js +2 -0
  212. package/dist/react/Types/render.js.map +1 -0
  213. package/dist/react/attributes.d.ts +8 -0
  214. package/dist/react/attributes.d.ts.map +1 -0
  215. package/dist/react/attributes.js +44 -0
  216. package/dist/react/attributes.js.map +1 -0
  217. package/dist/react/components.d.ts +60 -0
  218. package/dist/react/components.d.ts.map +1 -0
  219. package/dist/react/components.js +266 -0
  220. package/dist/react/components.js.map +1 -0
  221. package/dist/react/pdf-preview.d.ts +16 -0
  222. package/dist/react/pdf-preview.d.ts.map +1 -0
  223. package/dist/react/pdf-preview.js +26 -0
  224. package/dist/react/pdf-preview.js.map +1 -0
  225. package/dist/react/pdf-render.d.ts +15 -0
  226. package/dist/react/pdf-render.d.ts.map +1 -0
  227. package/dist/react/pdf-render.js +60 -0
  228. package/dist/react/pdf-render.js.map +1 -0
  229. package/dist/react/registry.d.ts +44 -0
  230. package/dist/react/registry.d.ts.map +1 -0
  231. package/dist/react/registry.js +105 -0
  232. package/dist/react/registry.js.map +1 -0
  233. package/dist/react/render.d.ts +36 -0
  234. package/dist/react/render.d.ts.map +1 -0
  235. package/dist/react/render.js +212 -0
  236. package/dist/react/render.js.map +1 -0
  237. package/dist/react/same-document.d.ts +8 -0
  238. package/dist/react/same-document.d.ts.map +1 -0
  239. package/dist/react/same-document.js +59 -0
  240. package/dist/react/same-document.js.map +1 -0
  241. package/dist/react/static.d.ts +13 -0
  242. package/dist/react/static.d.ts.map +1 -0
  243. package/dist/react/static.js +65 -0
  244. package/dist/react/static.js.map +1 -0
  245. package/dist/react/use-pdf.d.ts +25 -0
  246. package/dist/react/use-pdf.d.ts.map +1 -0
  247. package/dist/react/use-pdf.js +59 -0
  248. package/dist/react/use-pdf.js.map +1 -0
  249. package/dist/react.d.ts +9 -0
  250. package/dist/react.d.ts.map +1 -0
  251. package/dist/react.js +10 -0
  252. package/dist/react.js.map +1 -0
  253. package/dist/tailwind/Types/merge-config.d.ts +28 -0
  254. package/dist/tailwind/Types/merge-config.d.ts.map +1 -0
  255. package/dist/tailwind/Types/merge-config.js +2 -0
  256. package/dist/tailwind/Types/merge-config.js.map +1 -0
  257. package/dist/tailwind/merge-config.d.ts +45 -0
  258. package/dist/tailwind/merge-config.d.ts.map +1 -0
  259. package/dist/tailwind/merge-config.js +46 -0
  260. package/dist/tailwind/merge-config.js.map +1 -0
  261. package/dist/tailwind-merge.d.ts +3 -0
  262. package/dist/tailwind-merge.d.ts.map +1 -0
  263. package/dist/tailwind-merge.js +4 -0
  264. package/dist/tailwind-merge.js.map +1 -0
  265. package/dist/tailwind.css +61 -0
  266. package/dist/theme/Types/css-imports.d.ts +38 -0
  267. package/dist/theme/Types/css-imports.d.ts.map +1 -0
  268. package/dist/theme/Types/css-imports.js +2 -0
  269. package/dist/theme/Types/css-imports.js.map +1 -0
  270. package/dist/theme/Types/theme.d.ts +70 -0
  271. package/dist/theme/Types/theme.d.ts.map +1 -0
  272. package/dist/theme/Types/theme.js +2 -0
  273. package/dist/theme/Types/theme.js.map +1 -0
  274. package/dist/theme/Types/warnings.d.ts +9 -0
  275. package/dist/theme/Types/warnings.d.ts.map +1 -0
  276. package/dist/theme/Types/warnings.js +2 -0
  277. package/dist/theme/Types/warnings.js.map +1 -0
  278. package/dist/theme/css-imports.d.ts +11 -0
  279. package/dist/theme/css-imports.d.ts.map +1 -0
  280. package/dist/theme/css-imports.js +138 -0
  281. package/dist/theme/css-imports.js.map +1 -0
  282. package/dist/theme/normalize.d.ts +15 -0
  283. package/dist/theme/normalize.d.ts.map +1 -0
  284. package/dist/theme/normalize.js +51 -0
  285. package/dist/theme/normalize.js.map +1 -0
  286. package/dist/theme/package-imports.d.ts +16 -0
  287. package/dist/theme/package-imports.d.ts.map +1 -0
  288. package/dist/theme/package-imports.js +90 -0
  289. package/dist/theme/package-imports.js.map +1 -0
  290. package/dist/theme/theme-from-css-file.d.ts +35 -0
  291. package/dist/theme/theme-from-css-file.d.ts.map +1 -0
  292. package/dist/theme/theme-from-css-file.js +223 -0
  293. package/dist/theme/theme-from-css-file.js.map +1 -0
  294. package/dist/theme/theme-from-css.d.ts +28 -0
  295. package/dist/theme/theme-from-css.d.ts.map +1 -0
  296. package/dist/theme/theme-from-css.js +62 -0
  297. package/dist/theme/theme-from-css.js.map +1 -0
  298. package/dist/theme/warnings.d.ts +13 -0
  299. package/dist/theme/warnings.d.ts.map +1 -0
  300. package/dist/theme/warnings.js +34 -0
  301. package/dist/theme/warnings.js.map +1 -0
  302. package/dist/vite/Types/plugin.d.ts +47 -0
  303. package/dist/vite/Types/plugin.d.ts.map +1 -0
  304. package/dist/vite/Types/plugin.js +5 -0
  305. package/dist/vite/Types/plugin.js.map +1 -0
  306. package/dist/vite/plugin.d.ts +30 -0
  307. package/dist/vite/plugin.d.ts.map +1 -0
  308. package/dist/vite/plugin.js +80 -0
  309. package/dist/vite/plugin.js.map +1 -0
  310. package/dist/vite.d.ts +3 -0
  311. package/dist/vite.d.ts.map +1 -0
  312. package/dist/vite.js +6 -0
  313. package/dist/vite.js.map +1 -0
  314. package/dist/wasm/binding.generated.d.ts +259 -0
  315. package/dist/wasm/loader.d.ts +17 -0
  316. package/dist/wasm/loader.d.ts.map +1 -0
  317. package/dist/wasm/loader.js +74 -0
  318. package/dist/wasm/loader.js.map +1 -0
  319. package/dist/wasm/pkg/rentnerpdf_wasm.d.ts +259 -0
  320. package/dist/wasm/pkg/rentnerpdf_wasm.js +883 -0
  321. package/dist/wasm/pkg/rentnerpdf_wasm_bg.wasm +0 -0
  322. package/dist/wasm/pkg/rentnerpdf_wasm_bg.wasm.d.ts +40 -0
  323. package/dist/worker/Types/connection.d.ts +25 -0
  324. package/dist/worker/Types/connection.d.ts.map +1 -0
  325. package/dist/worker/Types/connection.js +2 -0
  326. package/dist/worker/Types/connection.js.map +1 -0
  327. package/dist/worker/Types/protocol.d.ts +62 -0
  328. package/dist/worker/Types/protocol.d.ts.map +1 -0
  329. package/dist/worker/Types/protocol.js +2 -0
  330. package/dist/worker/Types/protocol.js.map +1 -0
  331. package/dist/worker/Types/worker-engine.d.ts +91 -0
  332. package/dist/worker/Types/worker-engine.d.ts.map +1 -0
  333. package/dist/worker/Types/worker-engine.js +2 -0
  334. package/dist/worker/Types/worker-engine.js.map +1 -0
  335. package/dist/worker/client.d.ts +20 -0
  336. package/dist/worker/client.d.ts.map +1 -0
  337. package/dist/worker/client.js +131 -0
  338. package/dist/worker/client.js.map +1 -0
  339. package/dist/worker/connection.d.ts +5 -0
  340. package/dist/worker/connection.d.ts.map +1 -0
  341. package/dist/worker/connection.js +119 -0
  342. package/dist/worker/connection.js.map +1 -0
  343. package/dist/worker/protocol.d.ts +24 -0
  344. package/dist/worker/protocol.d.ts.map +1 -0
  345. package/dist/worker/protocol.js +71 -0
  346. package/dist/worker/protocol.js.map +1 -0
  347. package/dist/worker/serve.d.ts +26 -0
  348. package/dist/worker/serve.d.ts.map +1 -0
  349. package/dist/worker/serve.js +119 -0
  350. package/dist/worker/serve.js.map +1 -0
  351. package/dist/worker.d.ts +4 -0
  352. package/dist/worker.d.ts.map +1 -0
  353. package/dist/worker.js +8 -0
  354. package/dist/worker.js.map +1 -0
  355. package/package.json +80 -5
@@ -0,0 +1,223 @@
1
+ import { open, realpath } from 'node:fs/promises';
2
+ import { dirname, isAbsolute, relative, resolve, sep } from 'node:path';
3
+ import { appliesToPrint, cssImports, importKind } from './css-imports.js';
4
+ import { resolvePackageImport } from './package-imports.js';
5
+ import { themeScopes, themeStylesheet } from './theme-from-css.js';
6
+ import { themeWarningList } from './warnings.js';
7
+ /** Files one theme reads, the entry included (07 – Security). */
8
+ export const MAX_THEME_FILES = 64;
9
+ /** Nesting of `@import`s below the entry file. */
10
+ export const MAX_IMPORT_DEPTH = 16;
11
+ /** Bytes of one file; the engine reads at most 4 MiB of a stylesheet anyway. */
12
+ export const MAX_THEME_FILE_BYTES = 4 * 1024 * 1024;
13
+ /** Bytes of all files of one theme. */
14
+ export const MAX_THEME_BYTES = 16 * 1024 * 1024;
15
+ /** `@import` rules of one file that are followed; the rest are not resolved. */
16
+ export const MAX_IMPORTS_PER_FILE = 256;
17
+ /** A file path for warnings: relative to the working directory when inside it. */
18
+ const display = (path) => {
19
+ const local = relative(process.cwd(), path);
20
+ const shown = local !== '' && !local.startsWith('..') && !isAbsolute(local)
21
+ ? local
22
+ : path;
23
+ return shown.split(sep).join('/');
24
+ };
25
+ const isInside = (directory, path) => {
26
+ const local = relative(directory, path);
27
+ return (local === '' ||
28
+ (local !== '..' && !local.startsWith(`..${sep}`) && !isAbsolute(local)));
29
+ };
30
+ /**
31
+ * Reads a regular file through one handle, so the size check and the read
32
+ * see the same file: at most `limit` bytes, plus one to notice a file that
33
+ * grew after its size was taken.
34
+ */
35
+ const readLimited = async (file, limit) => {
36
+ const handle = await open(file, 'r');
37
+ try {
38
+ const stats = await handle.stat();
39
+ if (!stats.isFile())
40
+ return { refused: 'it is not a regular file' };
41
+ const tooLarge = (size) => ({
42
+ refused: `the file has ${size}; themes read at most ${MAX_THEME_FILE_BYTES} bytes per file and ${MAX_THEME_BYTES} in all, so it is skipped`,
43
+ });
44
+ if (stats.size > limit)
45
+ return tooLarge(`${stats.size} bytes`);
46
+ const buffer = Buffer.allocUnsafe(limit + 1);
47
+ let read = 0;
48
+ while (read < buffer.length) {
49
+ // oxlint-disable-next-line no-await-in-loop -- each read continues where the previous one stopped
50
+ const { bytesRead } = await handle.read(buffer, read, buffer.length - read, read);
51
+ if (bytesRead === 0)
52
+ break;
53
+ read += bytesRead;
54
+ }
55
+ if (read > limit)
56
+ return tooLarge(`more than ${limit} bytes`);
57
+ return { css: buffer.toString('utf8', 0, read), bytes: read };
58
+ }
59
+ finally {
60
+ await handle.close();
61
+ }
62
+ };
63
+ /** An error's code (`ENOENT`) without the paths its message contains. */
64
+ const errorCode = (error) => {
65
+ const code = typeof error === 'object' && error !== null
66
+ ? Reflect.get(error, 'code')
67
+ : undefined;
68
+ return typeof code === 'string' ? code : 'unknown error';
69
+ };
70
+ /** Resolves an import to a real file and the root its imports must stay in. */
71
+ const resolveImport = async (cssImport, importer, root) => {
72
+ const { specifier } = cssImport;
73
+ const refused = (reason) => `\`@import '${specifier}'\` is not followed: ${reason}`;
74
+ switch (importKind(cssImport)) {
75
+ case 'relative': {
76
+ const target = specifier.split(/[?#]/, 1)[0] ?? specifier;
77
+ if (!target.toLowerCase().endsWith('.css')) {
78
+ return refused('only .css files are read');
79
+ }
80
+ const leaves = refused(`it leaves ${display(root)} (set the \`root\` option to allow it)`);
81
+ // Checked before the file system is asked, so a refused path
82
+ // does not reveal whether it exists; symlinks are checked below.
83
+ const path = resolve(dirname(importer), target);
84
+ if (!isInside(root, path))
85
+ return leaves;
86
+ let file;
87
+ try {
88
+ file = await realpath(path);
89
+ }
90
+ catch (error) {
91
+ return refused(`the file cannot be read (${errorCode(error)})`);
92
+ }
93
+ return isInside(root, file) ? { file, root } : leaves;
94
+ }
95
+ case 'package': {
96
+ const resolved = await resolvePackageImport(specifier, importer);
97
+ return typeof resolved === 'string' ? refused(resolved) : resolved;
98
+ }
99
+ case 'refused':
100
+ return refused('absolute paths and file: URLs are never read; use a relative path');
101
+ case 'built-in':
102
+ case 'skipped':
103
+ return undefined;
104
+ }
105
+ };
106
+ /** Reads one file and, before it, the files it imports (CSS order). */
107
+ async function readTheme(reader, file, root, depth) {
108
+ reader.visited.add(file);
109
+ reader.stack.push(file);
110
+ try {
111
+ await readFile(reader, file, root, depth);
112
+ }
113
+ finally {
114
+ reader.stack.pop();
115
+ }
116
+ }
117
+ async function readFile(reader, file, root, depth) {
118
+ const path = display(file);
119
+ const limit = Math.min(MAX_THEME_FILE_BYTES, MAX_THEME_BYTES - reader.bytes);
120
+ let read;
121
+ try {
122
+ read = await readLimited(file, limit);
123
+ }
124
+ catch (error) {
125
+ // Only a missing entry file throws; imported files are warnings.
126
+ if (depth === 0)
127
+ throw error;
128
+ read = { refused: `the file cannot be read (${errorCode(error)})` };
129
+ }
130
+ if ('refused' in read) {
131
+ reader.warnings.add(read.refused, path);
132
+ return;
133
+ }
134
+ const { css } = read;
135
+ reader.bytes += read.bytes;
136
+ let followed = 0;
137
+ for (const cssImport of cssImports(css)) {
138
+ if (!appliesToPrint(cssImport.media))
139
+ continue;
140
+ if (importKind(cssImport) === 'built-in')
141
+ continue;
142
+ if (importKind(cssImport) === 'skipped')
143
+ continue;
144
+ followed += 1;
145
+ if (followed > MAX_IMPORTS_PER_FILE) {
146
+ reader.warnings.add(`the file has more than ${MAX_IMPORTS_PER_FILE} @import rules; the rest are not followed`, path);
147
+ break;
148
+ }
149
+ const notFollowed = (reason) => reader.warnings.add(`\`@import '${cssImport.specifier}'\` is not followed: ${reason}`, path);
150
+ // The limits are checked before an import is resolved, so a file
151
+ // with thousands of imports cannot make the file system work.
152
+ if (depth + 1 > MAX_IMPORT_DEPTH) {
153
+ notFollowed(`imports nest deeper than ${MAX_IMPORT_DEPTH} levels`);
154
+ continue;
155
+ }
156
+ if (reader.visited.size >= MAX_THEME_FILES) {
157
+ notFollowed(`a theme reads at most ${MAX_THEME_FILES} files`);
158
+ continue;
159
+ }
160
+ // oxlint-disable-next-line no-await-in-loop -- imports are read one after another in CSS order
161
+ const resolved = await resolveImport(cssImport, file, root);
162
+ if (resolved === undefined)
163
+ continue;
164
+ if (typeof resolved === 'string') {
165
+ reader.warnings.add(resolved, path);
166
+ continue;
167
+ }
168
+ if (reader.stack.includes(resolved.file)) {
169
+ notFollowed('it imports a file that is already being read (a cycle)');
170
+ continue;
171
+ }
172
+ if (reader.visited.has(resolved.file))
173
+ continue;
174
+ // oxlint-disable-next-line no-await-in-loop -- an import's files come before the next import's
175
+ await readTheme(reader, resolved.file, resolved.root, depth + 1);
176
+ }
177
+ reader.stylesheets.push(themeStylesheet(css, reader.scopes, path));
178
+ }
179
+ /**
180
+ * Reads design tokens from a CSS file and the files it imports, such as the
181
+ * app's Tailwind CSS entry (D-22). Relative `@import`s (`./theme.css`) are
182
+ * followed inside `root` (default: the entry's directory), and imports of
183
+ * `@rentnerkev/*` packages (`@rentnerkev/table/theme.css`) are resolved
184
+ * like Node resolves modules from the importing file and confined to that
185
+ * package. Tailwind itself and `@rentnerkev/pdf/tailwind.css` are built
186
+ * into the engine; other packages and URLs are never read. Only regular
187
+ * `.css` files are read, each file once, with limits on file count,
188
+ * imports, depth and size.
189
+ *
190
+ * ```ts
191
+ * import { themeFromCssFile } from '@rentnerkev/pdf/node'
192
+ *
193
+ * const engine = await createEngine({
194
+ * theme: await themeFromCssFile('src/styles.css'),
195
+ * })
196
+ * ```
197
+ *
198
+ * The entry file must exist; problems with imported files and the CSS are
199
+ * warnings (`theme.warnings`, `engine.themeWarnings`).
200
+ */
201
+ export async function themeFromCssFile(path, options = {}) {
202
+ if (typeof path !== 'string' || path === '') {
203
+ throw new TypeError('themeFromCssFile() expects a file path');
204
+ }
205
+ const entry = await realpath(resolve(path));
206
+ const root = options.root === undefined
207
+ ? dirname(entry)
208
+ : await realpath(resolve(options.root));
209
+ const reader = {
210
+ scopes: themeScopes(options.scopes),
211
+ stylesheets: [],
212
+ warnings: themeWarningList(),
213
+ visited: new Set(),
214
+ stack: [],
215
+ bytes: 0,
216
+ };
217
+ await readTheme(reader, entry, root, 0);
218
+ return Object.freeze({
219
+ stylesheets: Object.freeze(reader.stylesheets),
220
+ warnings: reader.warnings.list(),
221
+ });
222
+ }
223
+ //# sourceMappingURL=theme-from-css-file.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"theme-from-css-file.js","sourceRoot":"","sources":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AAW,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC;QAC9C,QAAQ,EAAE,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE;KACnC,CAAC,CAAA;AACN,CAAC","sourcesContent":["import { open, realpath } from 'node:fs/promises'\nimport { dirname, isAbsolute, relative, resolve, sep } from 'node:path'\nimport { appliesToPrint, cssImports, importKind } from './css-imports.ts'\nimport { resolvePackageImport } from './package-imports.ts'\nimport { themeScopes, themeStylesheet } from './theme-from-css.ts'\nimport type { CssImport, ResolvedImport } from './Types/css-imports.ts'\nimport type {\n Theme,\n ThemeFromCssFileOptions,\n ThemeStylesheet,\n} from './Types/theme.ts'\nimport type { ThemeWarningList } from './Types/warnings.ts'\nimport { themeWarningList } from './warnings.ts'\n\n/** Files one theme reads, the entry included (07 – Security). */\nexport const MAX_THEME_FILES = 64\n/** Nesting of `@import`s below the entry file. */\nexport const MAX_IMPORT_DEPTH = 16\n/** Bytes of one file; the engine reads at most 4 MiB of a stylesheet anyway. */\nexport const MAX_THEME_FILE_BYTES = 4 * 1024 * 1024\n/** Bytes of all files of one theme. */\nexport const MAX_THEME_BYTES = 16 * 1024 * 1024\n/** `@import` rules of one file that are followed; the rest are not resolved. */\nexport const MAX_IMPORTS_PER_FILE = 256\n\ninterface Reader {\n readonly scopes: readonly string[]\n readonly stylesheets: ThemeStylesheet[]\n readonly warnings: ThemeWarningList\n /** Real paths already read: diamonds are read once, cycles are cut. */\n readonly visited: Set<string>\n /** Real paths of the files being read, for cycle detection. */\n readonly stack: string[]\n bytes: number\n}\n\n/** A file path for warnings: relative to the working directory when inside it. */\nconst display = (path: string): string => {\n const local = relative(process.cwd(), path)\n const shown =\n local !== '' && !local.startsWith('..') && !isAbsolute(local)\n ? local\n : path\n return shown.split(sep).join('/')\n}\n\nconst isInside = (directory: string, path: string): boolean => {\n const local = relative(directory, path)\n return (\n local === '' ||\n (local !== '..' && !local.startsWith(`..${sep}`) && !isAbsolute(local))\n )\n}\n\n/** A file's text and byte count, or why it is not read. */\ntype LimitedRead =\n | { readonly css: string; readonly bytes: number }\n | { readonly refused: string }\n\n/**\n * Reads a regular file through one handle, so the size check and the read\n * see the same file: at most `limit` bytes, plus one to notice a file that\n * grew after its size was taken.\n */\nconst readLimited = async (\n file: string,\n limit: number,\n): Promise<LimitedRead> => {\n const handle = await open(file, 'r')\n try {\n const stats = await handle.stat()\n if (!stats.isFile()) return { refused: 'it is not a regular file' }\n const tooLarge = (size: string) => ({\n refused: `the file has ${size}; themes read at most ${MAX_THEME_FILE_BYTES} bytes per file and ${MAX_THEME_BYTES} in all, so it is skipped`,\n })\n if (stats.size > limit) return tooLarge(`${stats.size} bytes`)\n const buffer = Buffer.allocUnsafe(limit + 1)\n let read = 0\n while (read < buffer.length) {\n // oxlint-disable-next-line no-await-in-loop -- each read continues where the previous one stopped\n const { bytesRead } = await handle.read(\n buffer,\n read,\n buffer.length - read,\n read,\n )\n if (bytesRead === 0) break\n read += bytesRead\n }\n if (read > limit) return tooLarge(`more than ${limit} bytes`)\n return { css: buffer.toString('utf8', 0, read), bytes: read }\n } finally {\n await handle.close()\n }\n}\n\n/** An error's code (`ENOENT`) without the paths its message contains. */\nconst errorCode = (error: unknown): string => {\n const code: unknown =\n typeof error === 'object' && error !== null\n ? Reflect.get(error, 'code')\n : undefined\n return typeof code === 'string' ? code : 'unknown error'\n}\n\n/** Resolves an import to a real file and the root its imports must stay in. */\nconst resolveImport = async (\n cssImport: CssImport,\n importer: string,\n root: string,\n): Promise<ResolvedImport | string | undefined> => {\n const { specifier } = cssImport\n const refused = (reason: string) =>\n `\\`@import '${specifier}'\\` is not followed: ${reason}`\n switch (importKind(cssImport)) {\n case 'relative': {\n const target = specifier.split(/[?#]/, 1)[0] ?? specifier\n if (!target.toLowerCase().endsWith('.css')) {\n return refused('only .css files are read')\n }\n const leaves = refused(\n `it leaves ${display(root)} (set the \\`root\\` option to allow it)`,\n )\n // Checked before the file system is asked, so a refused path\n // does not reveal whether it exists; symlinks are checked below.\n const path = resolve(dirname(importer), target)\n if (!isInside(root, path)) return leaves\n let file: string\n try {\n file = await realpath(path)\n } catch (error) {\n return refused(`the file cannot be read (${errorCode(error)})`)\n }\n return isInside(root, file) ? { file, root } : leaves\n }\n case 'package': {\n const resolved = await resolvePackageImport(specifier, importer)\n return typeof resolved === 'string' ? refused(resolved) : resolved\n }\n case 'refused':\n return refused(\n 'absolute paths and file: URLs are never read; use a relative path',\n )\n case 'built-in':\n case 'skipped':\n return undefined\n }\n}\n\n/** Reads one file and, before it, the files it imports (CSS order). */\nasync function readTheme(\n reader: Reader,\n file: string,\n root: string,\n depth: number,\n): Promise<void> {\n reader.visited.add(file)\n reader.stack.push(file)\n try {\n await readFile(reader, file, root, depth)\n } finally {\n reader.stack.pop()\n }\n}\n\nasync function readFile(\n reader: Reader,\n file: string,\n root: string,\n depth: number,\n): Promise<void> {\n const path = display(file)\n const limit = Math.min(MAX_THEME_FILE_BYTES, MAX_THEME_BYTES - reader.bytes)\n let read: LimitedRead\n try {\n read = await readLimited(file, limit)\n } catch (error) {\n // Only a missing entry file throws; imported files are warnings.\n if (depth === 0) throw error\n read = { refused: `the file cannot be read (${errorCode(error)})` }\n }\n if ('refused' in read) {\n reader.warnings.add(read.refused, path)\n return\n }\n const { css } = read\n reader.bytes += read.bytes\n let followed = 0\n for (const cssImport of cssImports(css)) {\n if (!appliesToPrint(cssImport.media)) continue\n if (importKind(cssImport) === 'built-in') continue\n if (importKind(cssImport) === 'skipped') continue\n followed += 1\n if (followed > MAX_IMPORTS_PER_FILE) {\n reader.warnings.add(\n `the file has more than ${MAX_IMPORTS_PER_FILE} @import rules; the rest are not followed`,\n path,\n )\n break\n }\n const notFollowed = (reason: string) =>\n reader.warnings.add(\n `\\`@import '${cssImport.specifier}'\\` is not followed: ${reason}`,\n path,\n )\n // The limits are checked before an import is resolved, so a file\n // with thousands of imports cannot make the file system work.\n if (depth + 1 > MAX_IMPORT_DEPTH) {\n notFollowed(`imports nest deeper than ${MAX_IMPORT_DEPTH} levels`)\n continue\n }\n if (reader.visited.size >= MAX_THEME_FILES) {\n notFollowed(`a theme reads at most ${MAX_THEME_FILES} files`)\n continue\n }\n // oxlint-disable-next-line no-await-in-loop -- imports are read one after another in CSS order\n const resolved = await resolveImport(cssImport, file, root)\n if (resolved === undefined) continue\n if (typeof resolved === 'string') {\n reader.warnings.add(resolved, path)\n continue\n }\n if (reader.stack.includes(resolved.file)) {\n notFollowed(\n 'it imports a file that is already being read (a cycle)',\n )\n continue\n }\n if (reader.visited.has(resolved.file)) continue\n // oxlint-disable-next-line no-await-in-loop -- an import's files come before the next import's\n await readTheme(reader, resolved.file, resolved.root, depth + 1)\n }\n reader.stylesheets.push(themeStylesheet(css, reader.scopes, path))\n}\n\n/**\n * Reads design tokens from a CSS file and the files it imports, such as the\n * app's Tailwind CSS entry (D-22). Relative `@import`s (`./theme.css`) are\n * followed inside `root` (default: the entry's directory), and imports of\n * `@rentnerkev/*` packages (`@rentnerkev/table/theme.css`) are resolved\n * like Node resolves modules from the importing file and confined to that\n * package. Tailwind itself and `@rentnerkev/pdf/tailwind.css` are built\n * into the engine; other packages and URLs are never read. Only regular\n * `.css` files are read, each file once, with limits on file count,\n * imports, depth and size.\n *\n * ```ts\n * import { themeFromCssFile } from '@rentnerkev/pdf/node'\n *\n * const engine = await createEngine({\n * theme: await themeFromCssFile('src/styles.css'),\n * })\n * ```\n *\n * The entry file must exist; problems with imported files and the CSS are\n * warnings (`theme.warnings`, `engine.themeWarnings`).\n */\nexport async function themeFromCssFile(\n path: string,\n options: ThemeFromCssFileOptions = {},\n): Promise<Theme> {\n if (typeof path !== 'string' || path === '') {\n throw new TypeError('themeFromCssFile() expects a file path')\n }\n const entry = await realpath(resolve(path))\n const root =\n options.root === undefined\n ? dirname(entry)\n : await realpath(resolve(options.root))\n const reader: Reader = {\n scopes: themeScopes(options.scopes),\n stylesheets: [],\n warnings: themeWarningList(),\n visited: new Set(),\n stack: [],\n bytes: 0,\n }\n await readTheme(reader, entry, root, 0)\n return Object.freeze({\n stylesheets: Object.freeze(reader.stylesheets),\n warnings: reader.warnings.list(),\n })\n}\n"]}
@@ -0,0 +1,28 @@
1
+ import type { Theme, ThemeFromCssOptions, ThemeStylesheet } from './Types/theme.ts';
2
+ /**
3
+ * The scope selectors `themeFromCss()` and `themeFromCssFile()` read when
4
+ * none are given: custom properties on `.rentner-pdf` override `:root` ones.
5
+ */
6
+ export declare const DEFAULT_THEME_SCOPES: readonly string[];
7
+ /** Validates `scopes` (a list of selectors) and copies it. */
8
+ export declare const themeScopes: (scopes: readonly string[] | undefined) => readonly string[];
9
+ /** A frozen stylesheet entry. */
10
+ export declare const themeStylesheet: (css: string, scopes: readonly string[], source: string | undefined) => ThemeStylesheet;
11
+ /**
12
+ * Reads design tokens from app CSS, such as a Tailwind CSS entry with
13
+ * `@theme` blocks (D-22). The engine reads `@theme` variables, custom
14
+ * properties on `:root` and on the scope selectors (default
15
+ * `.rentner-pdf`) and `@media print` blocks; it never runs Tailwind or
16
+ * fetches anything. `@import`s are not followed: use `themeFromCssFile()`
17
+ * from `@rentnerkev/pdf/node` for files that import others.
18
+ *
19
+ * ```ts
20
+ * const engine = await createEngine({
21
+ * theme: themeFromCss(`@theme { --color-brand: #0f766e; }`),
22
+ * })
23
+ * ```
24
+ *
25
+ * Problems in the CSS never throw: `engine.themeWarnings` lists them.
26
+ */
27
+ export declare function themeFromCss(css: string, options?: ThemeFromCssOptions): Theme;
28
+ //# sourceMappingURL=theme-from-css.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"theme-from-css.d.ts","sourceRoot":"","sources":["../../web/src/theme/theme-from-css.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACR,KAAK,EACL,mBAAmB,EACnB,eAAe,EAClB,MAAM,kBAAkB,CAAA;AAGzB;;;GAGG;AACH,eAAO,MAAM,oBAAoB,EAAE,SAAS,MAAM,EAEhD,CAAA;AAEF,8DAA8D;AAC9D,eAAO,MAAM,WAAW,WACZ,SAAS,MAAM,EAAE,GAAG,SAAS,KACtC,SAAS,MAAM,EASjB,CAAA;AAED,iCAAiC;AACjC,eAAO,MAAM,eAAe,QACnB,MAAM,UACH,SAAS,MAAM,EAAE,UACjB,MAAM,GAAG,SAAS,KAC3B,eAKG,CAAA;AAEN;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,YAAY,CACxB,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,mBAAwB,GAClC,KAAK,CAqBP"}
@@ -0,0 +1,62 @@
1
+ import { cssImports, importKind } from './css-imports.js';
2
+ import { themeWarningList } from './warnings.js';
3
+ /**
4
+ * The scope selectors `themeFromCss()` and `themeFromCssFile()` read when
5
+ * none are given: custom properties on `.rentner-pdf` override `:root` ones.
6
+ */
7
+ export const DEFAULT_THEME_SCOPES = Object.freeze([
8
+ '.rentner-pdf',
9
+ ]);
10
+ /** Validates `scopes` (a list of selectors) and copies it. */
11
+ export const themeScopes = (scopes) => {
12
+ if (scopes === undefined)
13
+ return DEFAULT_THEME_SCOPES;
14
+ if (!Array.isArray(scopes) ||
15
+ !scopes.every((scope) => typeof scope === 'string')) {
16
+ throw new TypeError('scopes must be an array of selector strings');
17
+ }
18
+ return Object.freeze([...scopes]);
19
+ };
20
+ /** A frozen stylesheet entry. */
21
+ export const themeStylesheet = (css, scopes, source) => Object.freeze({
22
+ css,
23
+ scopes,
24
+ ...(source === undefined ? {} : { source }),
25
+ });
26
+ /**
27
+ * Reads design tokens from app CSS, such as a Tailwind CSS entry with
28
+ * `@theme` blocks (D-22). The engine reads `@theme` variables, custom
29
+ * properties on `:root` and on the scope selectors (default
30
+ * `.rentner-pdf`) and `@media print` blocks; it never runs Tailwind or
31
+ * fetches anything. `@import`s are not followed: use `themeFromCssFile()`
32
+ * from `@rentnerkev/pdf/node` for files that import others.
33
+ *
34
+ * ```ts
35
+ * const engine = await createEngine({
36
+ * theme: themeFromCss(`@theme { --color-brand: #0f766e; }`),
37
+ * })
38
+ * ```
39
+ *
40
+ * Problems in the CSS never throw: `engine.themeWarnings` lists them.
41
+ */
42
+ export function themeFromCss(css, options = {}) {
43
+ if (typeof css !== 'string') {
44
+ throw new TypeError('themeFromCss() expects the CSS text as a string');
45
+ }
46
+ const scopes = themeScopes(options.scopes);
47
+ const path = options.source;
48
+ const warnings = themeWarningList();
49
+ for (const cssImport of cssImports(css)) {
50
+ const kind = importKind(cssImport);
51
+ if (kind !== 'relative' && kind !== 'package')
52
+ continue;
53
+ warnings.add(`\`@import '${cssImport.specifier}'\` is not followed by themeFromCss(); use themeFromCssFile() from @rentnerkev/pdf/node or pass that file's CSS as well`, path);
54
+ }
55
+ return Object.freeze({
56
+ stylesheets: Object.freeze([
57
+ themeStylesheet(css, scopes, options.source),
58
+ ]),
59
+ warnings: warnings.list(),
60
+ });
61
+ }
62
+ //# sourceMappingURL=theme-from-css.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"theme-from-css.js","sourceRoot":"","sources":["../../web/src/theme/theme-from-css.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAMzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAA;AAEhD;;;GAGG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAsB,MAAM,CAAC,MAAM,CAAC;IACjE,cAAc;CACjB,CAAC,CAAA;AAEF,8DAA8D;AAC9D,MAAM,CAAC,MAAM,WAAW,GAAG,CACvB,MAAqC,EACpB,EAAE;IACnB,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,oBAAoB,CAAA;IACrD,IACI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC;QACtB,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC,EACrD,CAAC;QACC,MAAM,IAAI,SAAS,CAAC,6CAA6C,CAAC,CAAA;IACtE,CAAC;IACD,OAAO,MAAM,CAAC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Imports, importKind } from './css-imports.ts'\nimport type {\n Theme,\n ThemeFromCssOptions,\n ThemeStylesheet,\n} from './Types/theme.ts'\nimport { themeWarningList } from './warnings.ts'\n\n/**\n * The scope selectors `themeFromCss()` and `themeFromCssFile()` read when\n * none are given: custom properties on `.rentner-pdf` override `:root` ones.\n */\nexport const DEFAULT_THEME_SCOPES: readonly string[] = Object.freeze([\n '.rentner-pdf',\n])\n\n/** Validates `scopes` (a list of selectors) and copies it. */\nexport const themeScopes = (\n scopes: readonly string[] | undefined,\n): readonly string[] => {\n if (scopes === undefined) return DEFAULT_THEME_SCOPES\n if (\n !Array.isArray(scopes) ||\n !scopes.every((scope) => typeof scope === 'string')\n ) {\n throw new TypeError('scopes must be an array of selector strings')\n }\n return Object.freeze([...scopes])\n}\n\n/** A frozen stylesheet entry. */\nexport const themeStylesheet = (\n css: string,\n scopes: readonly string[],\n source: string | undefined,\n): ThemeStylesheet =>\n Object.freeze({\n css,\n scopes,\n ...(source === undefined ? {} : { source }),\n })\n\n/**\n * Reads design tokens from app CSS, such as a Tailwind CSS entry with\n * `@theme` blocks (D-22). The engine reads `@theme` variables, custom\n * properties on `:root` and on the scope selectors (default\n * `.rentner-pdf`) and `@media print` blocks; it never runs Tailwind or\n * fetches anything. `@import`s are not followed: use `themeFromCssFile()`\n * from `@rentnerkev/pdf/node` for files that import others.\n *\n * ```ts\n * const engine = await createEngine({\n * theme: themeFromCss(`@theme { --color-brand: #0f766e; }`),\n * })\n * ```\n *\n * Problems in the CSS never throw: `engine.themeWarnings` lists them.\n */\nexport function themeFromCss(\n css: string,\n options: ThemeFromCssOptions = {},\n): Theme {\n if (typeof css !== 'string') {\n throw new TypeError('themeFromCss() expects the CSS text as a string')\n }\n const scopes = themeScopes(options.scopes)\n const path = options.source\n const warnings = themeWarningList()\n for (const cssImport of cssImports(css)) {\n const kind = importKind(cssImport)\n if (kind !== 'relative' && kind !== 'package') continue\n warnings.add(\n `\\`@import '${cssImport.specifier}'\\` is not followed by themeFromCss(); use themeFromCssFile() from @rentnerkev/pdf/node or pass that file's CSS as well`,\n path,\n )\n }\n return Object.freeze({\n stylesheets: Object.freeze([\n themeStylesheet(css, scopes, options.source),\n ]),\n warnings: warnings.list(),\n })\n}\n"]}
@@ -0,0 +1,13 @@
1
+ import type { RenderWarning } from '../engine/Types/render-result.ts';
2
+ import type { ThemeWarningList } from './Types/warnings.ts';
3
+ /**
4
+ * Warnings one theme collects at most; the rest are summed up in a last
5
+ * warning, so hostile CSS with many imports cannot grow the list (or make
6
+ * spreading it throw) without bound.
7
+ */
8
+ export declare const MAX_THEME_WARNINGS = 100;
9
+ /** An `INVALID_THEME` warning, optionally at a stylesheet. */
10
+ export declare const themeWarning: (message: string, path: string | undefined) => RenderWarning;
11
+ /** A list of at most [`MAX_THEME_WARNINGS`] warnings and a summary. */
12
+ export declare function themeWarningList(): ThemeWarningList;
13
+ //# sourceMappingURL=warnings.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"warnings.d.ts","sourceRoot":"","sources":["../../web/src/theme/warnings.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kCAAkC,CAAA;AACrE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAA;AAE3D;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,MAAM,CAAA;AAErC,8DAA8D;AAC9D,eAAO,MAAM,YAAY,YACZ,MAAM,QACT,MAAM,GAAG,SAAS,KACzB,aAKG,CAAA;AAEN,uEAAuE;AACvE,wBAAgB,gBAAgB,IAAI,gBAAgB,CAwBnD"}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Warnings one theme collects at most; the rest are summed up in a last
3
+ * warning, so hostile CSS with many imports cannot grow the list (or make
4
+ * spreading it throw) without bound.
5
+ */
6
+ export const MAX_THEME_WARNINGS = 100;
7
+ /** An `INVALID_THEME` warning, optionally at a stylesheet. */
8
+ export const themeWarning = (message, path) => Object.freeze({
9
+ code: 'INVALID_THEME',
10
+ message,
11
+ ...(path === undefined ? {} : { path }),
12
+ });
13
+ /** A list of at most [`MAX_THEME_WARNINGS`] warnings and a summary. */
14
+ export function themeWarningList() {
15
+ const warnings = [];
16
+ let dropped = 0;
17
+ return {
18
+ add: (message, path) => {
19
+ if (warnings.length < MAX_THEME_WARNINGS) {
20
+ warnings.push(themeWarning(message, path));
21
+ }
22
+ else {
23
+ dropped += 1;
24
+ }
25
+ },
26
+ list: () => Object.freeze(dropped === 0
27
+ ? [...warnings]
28
+ : [
29
+ ...warnings,
30
+ themeWarning(`${dropped} more theme warnings are not listed`, undefined),
31
+ ]),
32
+ };
33
+ }
34
+ //# sourceMappingURL=warnings.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"warnings.js","sourceRoot":"","sources":["../../web/src/theme/warnings.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,GAAG,CAAA;AAErC,8DAA8D;AAC9D,MAAM,CAAC,MAAM,YAAY,GAAG,CACxB,OAAe,EACf,IAAwB,EACX,EAAE,CACf,MAAM,CAAC,MAAM,CAAC;IACV,IAAI,EAAE,eAAwB;IAC9B,OAAO;IACP,GAAG,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC;CAC1C,CAAC,CAAA;AAEN,uEAAuE;AACvE,MAAM,UAAU,gBAAgB;IAC5B,MAAM,QAAQ,GAAoB,EAAE,CAAA;IACpC,IAAI,OAAO,GAAG,CAAC,CAAA;IACf,OAAO;QACH,GAAG,EAAE,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE;YACnB,IAAI,QAAQ,CAAC,MAAM,GAAG,kBAAkB,EAAE,CAAC;gBACvC,QAAQ,CAAC,IAAI,CAAC,YAAY,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAA;YAC9C,CAAC;iBAAM,CAAC;gBACJ,OAAO,IAAI,CAAC,CAAA;YAChB,CAAC;QACL,CAAC;QACD,IAAI,EAAE,GAAG,EAAE,CACP,MAAM,CAAC,MAAM,CACT,OAAO,KAAK,CAAC;YACT,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC;YACf,CAAC,CAAC;gBACI,GAAG,QAAQ;gBACX,YAAY,CACR,GAAG,OAAO,qCAAqC,EAC/C,SAAS,CACZ;aACJ,CACV;KACR,CAAA;AACL,CAAC","sourcesContent":["import type { RenderWarning } from '../engine/Types/render-result.ts'\nimport type { ThemeWarningList } from './Types/warnings.ts'\n\n/**\n * Warnings one theme collects at most; the rest are summed up in a last\n * warning, so hostile CSS with many imports cannot grow the list (or make\n * spreading it throw) without bound.\n */\nexport const MAX_THEME_WARNINGS = 100\n\n/** An `INVALID_THEME` warning, optionally at a stylesheet. */\nexport const themeWarning = (\n message: string,\n path: string | undefined,\n): RenderWarning =>\n Object.freeze({\n code: 'INVALID_THEME' as const,\n message,\n ...(path === undefined ? {} : { path }),\n })\n\n/** A list of at most [`MAX_THEME_WARNINGS`] warnings and a summary. */\nexport function themeWarningList(): ThemeWarningList {\n const warnings: RenderWarning[] = []\n let dropped = 0\n return {\n add: (message, path) => {\n if (warnings.length < MAX_THEME_WARNINGS) {\n warnings.push(themeWarning(message, path))\n } else {\n dropped += 1\n }\n },\n list: () =>\n Object.freeze(\n dropped === 0\n ? [...warnings]\n : [\n ...warnings,\n themeWarning(\n `${dropped} more theme warnings are not listed`,\n undefined,\n ),\n ],\n ),\n }\n}\n"]}
@@ -0,0 +1,47 @@
1
+ /** The part of Nitro's options the module changes. */
2
+ export interface NitroOptionsLike {
3
+ rootDir?: string;
4
+ traceDeps?: (string | RegExp)[];
5
+ }
6
+ /** The part of a Nitro instance the module receives. */
7
+ export interface NitroLike {
8
+ readonly options: NitroOptionsLike;
9
+ }
10
+ /** A Nitro module; Nitro 3 installs the `nitro` property of Vite plugins. */
11
+ export interface RentnerPdfNitroModule {
12
+ readonly name: string;
13
+ setup(nitro: NitroLike): void;
14
+ }
15
+ /** The part of Vite's resolved config the plugin reads. */
16
+ export interface ResolvedConfigLike {
17
+ readonly command: 'build' | 'serve';
18
+ }
19
+ /** The part of Vite's plugin context `resolveId` uses. */
20
+ export interface ResolveContextLike {
21
+ readonly environment?: {
22
+ readonly config: {
23
+ readonly consumer?: 'client' | 'server';
24
+ };
25
+ };
26
+ error(message: string): never;
27
+ }
28
+ /** Options of `rentnerPdf()`. */
29
+ export interface RentnerPdfViteOptions {
30
+ /**
31
+ * `'error'` (default) fails client builds that import `@rentnerkev/pdf`
32
+ * or `@rentnerkev/pdf/react`; `'allow'` lets them in, for rendering on
33
+ * WebAssembly in the browser. `@rentnerkev/pdf/node` always fails.
34
+ */
35
+ readonly client?: 'error' | 'allow';
36
+ }
37
+ /** The Vite plugin `rentnerPdf()` returns. */
38
+ export interface RentnerPdfVitePlugin {
39
+ readonly name: 'rentnerpdf';
40
+ readonly enforce: 'pre';
41
+ readonly nitro: RentnerPdfNitroModule;
42
+ configResolved(config: ResolvedConfigLike): void;
43
+ resolveId(this: ResolveContextLike, source: string, importer: string | undefined, options?: {
44
+ readonly ssr?: boolean;
45
+ }): null;
46
+ }
47
+ //# sourceMappingURL=plugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../../web/src/vite/Types/plugin.ts"],"names":[],"mappings":"AAIA,sDAAsD;AACtD,MAAM,WAAW,gBAAgB;IAC7B,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,SAAS,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAA;CAClC;AAED,wDAAwD;AACxD,MAAM,WAAW,SAAS;IACtB,QAAQ,CAAC,OAAO,EAAE,gBAAgB,CAAA;CACrC;AAED,6EAA6E;AAC7E,MAAM,WAAW,qBAAqB;IAClC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,KAAK,CAAC,KAAK,EAAE,SAAS,GAAG,IAAI,CAAA;CAChC;AAED,2DAA2D;AAC3D,MAAM,WAAW,kBAAkB;IAC/B,QAAQ,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAA;CACtC;AAED,0DAA0D;AAC1D,MAAM,WAAW,kBAAkB;IAC/B,QAAQ,CAAC,WAAW,CAAC,EAAE;QACnB,QAAQ,CAAC,MAAM,EAAE;YAAE,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,GAAG,QAAQ,CAAA;SAAE,CAAA;KAC/D,CAAA;IACD,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,KAAK,CAAA;CAChC;AAED,iCAAiC;AACjC,MAAM,WAAW,qBAAqB;IAClC;;;;OAIG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,GAAG,OAAO,CAAA;CACtC;AAED,8CAA8C;AAC9C,MAAM,WAAW,oBAAoB;IACjC,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAA;IAC3B,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAA;IACvB,QAAQ,CAAC,KAAK,EAAE,qBAAqB,CAAA;IACrC,cAAc,CAAC,MAAM,EAAE,kBAAkB,GAAG,IAAI,CAAA;IAChD,SAAS,CACL,IAAI,EAAE,kBAAkB,EACxB,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,GAAG,SAAS,EAC5B,OAAO,CAAC,EAAE;QAAE,QAAQ,CAAC,GAAG,CAAC,EAAE,OAAO,CAAA;KAAE,GACrC,IAAI,CAAA;CACV"}
@@ -0,0 +1,5 @@
1
+ // Structural types for the Vite plugin: the package depends on neither Vite
2
+ // nor Nitro, so it declares only the parts it uses. Vite and Nitro accept
3
+ // these objects as their own plugin and module types.
4
+ export {};
5
+ //# sourceMappingURL=plugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../../web/src/vite/Types/plugin.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,0EAA0E;AAC1E,sDAAsD","sourcesContent":["// Structural types for the Vite plugin: the package depends on neither Vite\n// nor Nitro, so it declares only the parts it uses. Vite and Nitro accept\n// these objects as their own plugin and module types.\n\n/** The part of Nitro's options the module changes. */\nexport interface NitroOptionsLike {\n rootDir?: string\n traceDeps?: (string | RegExp)[]\n}\n\n/** The part of a Nitro instance the module receives. */\nexport interface NitroLike {\n readonly options: NitroOptionsLike\n}\n\n/** A Nitro module; Nitro 3 installs the `nitro` property of Vite plugins. */\nexport interface RentnerPdfNitroModule {\n readonly name: string\n setup(nitro: NitroLike): void\n}\n\n/** The part of Vite's resolved config the plugin reads. */\nexport interface ResolvedConfigLike {\n readonly command: 'build' | 'serve'\n}\n\n/** The part of Vite's plugin context `resolveId` uses. */\nexport interface ResolveContextLike {\n readonly environment?: {\n readonly config: { readonly consumer?: 'client' | 'server' }\n }\n error(message: string): never\n}\n\n/** Options of `rentnerPdf()`. */\nexport interface RentnerPdfViteOptions {\n /**\n * `'error'` (default) fails client builds that import `@rentnerkev/pdf`\n * or `@rentnerkev/pdf/react`; `'allow'` lets them in, for rendering on\n * WebAssembly in the browser. `@rentnerkev/pdf/node` always fails.\n */\n readonly client?: 'error' | 'allow'\n}\n\n/** The Vite plugin `rentnerPdf()` returns. */\nexport interface RentnerPdfVitePlugin {\n readonly name: 'rentnerpdf'\n readonly enforce: 'pre'\n readonly nitro: RentnerPdfNitroModule\n configResolved(config: ResolvedConfigLike): void\n resolveId(\n this: ResolveContextLike,\n source: string,\n importer: string | undefined,\n options?: { readonly ssr?: boolean },\n ): null\n}\n"]}
@@ -0,0 +1,30 @@
1
+ import type { NitroLike, RentnerPdfViteOptions, RentnerPdfVitePlugin } from './Types/plugin.ts';
2
+ /**
3
+ * Packages Nitro must keep out of its server bundle and copy to
4
+ * `.output/server/node_modules`: the engine reads its fonts and native
5
+ * addon next to its own files, and the React path must share the app's
6
+ * React (`react-dom/static` from one copy, hooks from another fail).
7
+ */
8
+ export declare const NITRO_TRACE_DEPS: readonly string[];
9
+ /** Adds the server packages to Nitro's `traceDeps`, once each. */
10
+ export declare function addNitroTraceDeps({ options }: NitroLike): void;
11
+ /** The build error for an engine import in a client bundle. */
12
+ export declare const clientImportMessage: (source: string, importer: string | undefined) => string;
13
+ /**
14
+ * The optional Vite plugin (D-23). In Nitro builds (TanStack Start, Nitro
15
+ * with Vite) it keeps `@rentnerkev/pdf`, `react` and `react-dom` external
16
+ * and traced into the server output; in client builds it turns an engine
17
+ * import into a build error (`client: 'allow'` permits WebAssembly
18
+ * rendering in the browser; `@rentnerkev/pdf/node` always fails).
19
+ *
20
+ * ```ts
21
+ * import { rentnerPdf } from '@rentnerkev/pdf/vite'
22
+ *
23
+ * export default defineConfig({ plugins: [rentnerPdf(), tanstackStart(), nitro()] })
24
+ * ```
25
+ *
26
+ * @experimental Exempt from SemVer in 1.x until promoted: it follows
27
+ * Nitro's module API, which still changes quickly.
28
+ */
29
+ export declare function rentnerPdf(options?: RentnerPdfViteOptions): RentnerPdfVitePlugin;
30
+ //# sourceMappingURL=plugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../web/src/vite/plugin.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACR,SAAS,EACT,qBAAqB,EACrB,oBAAoB,EAEvB,MAAM,mBAAmB,CAAA;AAQ1B;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,mBAI3B,CAAA;AAaF,kEAAkE;AAClE,wBAAgB,iBAAiB,CAAC,EAAE,OAAO,EAAE,EAAE,SAAS,GAAG,IAAI,CAS9D;AAKD,+DAA+D;AAC/D,eAAO,MAAM,mBAAmB,WACpB,MAAM,YACJ,MAAM,GAAG,SAAS,KAC7B,MAC8Q,CAAA;AAEjR;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,UAAU,CACtB,OAAO,GAAE,qBAA0B,GACpC,oBAAoB,CA8BtB"}
@@ -0,0 +1,80 @@
1
+ import { createRequire } from 'node:module';
2
+ import { join } from 'node:path';
3
+ /** Entry points that load the engine (native on servers, WebAssembly in browsers). */
4
+ const engineEntries = new Set(['@rentnerkev/pdf', '@rentnerkev/pdf/react']);
5
+ /** Entry points that read files; never part of a client bundle. */
6
+ const serverEntries = new Set(['@rentnerkev/pdf/node']);
7
+ /**
8
+ * Packages Nitro must keep out of its server bundle and copy to
9
+ * `.output/server/node_modules`: the engine reads its fonts and native
10
+ * addon next to its own files, and the React path must share the app's
11
+ * React (`react-dom/static` from one copy, hooks from another fail).
12
+ */
13
+ export const NITRO_TRACE_DEPS = Object.freeze([
14
+ '@rentnerkev/pdf',
15
+ 'react',
16
+ 'react-dom',
17
+ ]);
18
+ const installed = (rootDir, name) => {
19
+ try {
20
+ createRequire(join(rootDir, 'package.json')).resolve(`${name}/package.json`);
21
+ return true;
22
+ }
23
+ catch {
24
+ return false;
25
+ }
26
+ };
27
+ /** Adds the server packages to Nitro's `traceDeps`, once each. */
28
+ export function addNitroTraceDeps({ options }) {
29
+ const rootDir = options.rootDir ?? process.cwd();
30
+ const traceDeps = options.traceDeps ?? [];
31
+ const additions = NITRO_TRACE_DEPS.filter((name) => !traceDeps.includes(name) &&
32
+ (name === '@rentnerkev/pdf' || installed(rootDir, name)));
33
+ options.traceDeps = [...traceDeps, ...additions];
34
+ }
35
+ const allowHint = "; to render on WebAssembly in the browser on purpose, use rentnerPdf({ client: 'allow' }).";
36
+ /** The build error for an engine import in a client bundle. */
37
+ export const clientImportMessage = (source, importer) => `${importer ?? 'A module'} imports ${source} into a client bundle. RentnerPDF renders on the server: import it from server functions, server routes or other server-only modules. Type-only imports (\`import type\`) are fine${serverEntries.has(source) ? '.' : allowHint}`;
38
+ /**
39
+ * The optional Vite plugin (D-23). In Nitro builds (TanStack Start, Nitro
40
+ * with Vite) it keeps `@rentnerkev/pdf`, `react` and `react-dom` external
41
+ * and traced into the server output; in client builds it turns an engine
42
+ * import into a build error (`client: 'allow'` permits WebAssembly
43
+ * rendering in the browser; `@rentnerkev/pdf/node` always fails).
44
+ *
45
+ * ```ts
46
+ * import { rentnerPdf } from '@rentnerkev/pdf/vite'
47
+ *
48
+ * export default defineConfig({ plugins: [rentnerPdf(), tanstackStart(), nitro()] })
49
+ * ```
50
+ *
51
+ * @experimental Exempt from SemVer in 1.x until promoted: it follows
52
+ * Nitro's module API, which still changes quickly.
53
+ */
54
+ export function rentnerPdf(options = {}) {
55
+ const guarded = options.client === 'allow'
56
+ ? serverEntries
57
+ : new Set([...engineEntries, ...serverEntries]);
58
+ let build = false;
59
+ return {
60
+ name: 'rentnerpdf',
61
+ enforce: 'pre',
62
+ nitro: { name: 'rentnerpdf', setup: addNitroTraceDeps },
63
+ configResolved(config) {
64
+ build = config.command === 'build';
65
+ },
66
+ resolveId(source, importer, resolveOptions) {
67
+ // Only builds: the dev server's dependency scan also visits
68
+ // server-only files that the framework compiler strips later.
69
+ if (!build || !guarded.has(source))
70
+ return null;
71
+ const client = this.environment === undefined
72
+ ? resolveOptions?.ssr !== true
73
+ : this.environment.config.consumer === 'client';
74
+ if (client)
75
+ this.error(clientImportMessage(source, importer));
76
+ return null;
77
+ },
78
+ };
79
+ }
80
+ //# sourceMappingURL=plugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../web/src/vite/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAQhC,sFAAsF;AACtF,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC,CAAC,iBAAiB,EAAE,uBAAuB,CAAC,CAAC,CAAA;AAE3E,mEAAmE;AACnE,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC,CAAC,sBAAsB,CAAC,CAAC,CAAA;AAEvD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,CAAC;IAC1C,iBAAiB;IACjB,OAAO;IACP,WAAW;CACd,CAAC,CAAA;AAEF,MAAM,S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IAAI,GAAG,CAAC,CAAC,GAAG,aAAa,EAAE,GAAG,aAAa,CAAC,CAAC,CAAA;IACvD,IAAI,KAAK,GAAG,KAAK,CAAA;IACjB,OAAO;QACH,IAAI,EAAE,YAAY;QAClB,OAAO,EAAE,KAAK;QACd,KAAK,EAAE,EAAE,IAAI,EAAE,YAAY,EAAE,KAAK,EAAE,iBAAiB,EAAE;QACvD,cAAc,CAAC,MAAM;YACjB,KAAK,GAAG,MAAM,CAAC,OAAO,KAAK,OAAO,CAAA;QACtC,CAAC;QACD,SAAS,CAEL,MAAc,EACd,QAA4B,EAC5B,cAA2C;YAE3C,4DAA4D;YAC5D,8DAA8D;YAC9D,IAAI,CAAC,KAAK,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC;gBAAE,OAAO,IAAI,CAAA;YAC/C,MAAM,MAAM,GACR,IAAI,CAAC,WAAW,KAAK,SAAS;gBAC1B,CAAC,CAAC,cAAc,EAAE,GAAG,KAAK,IAAI;gBAC9B,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,QAAQ,KAAK,QAAQ,CAAA;YACvD,IAAI,MAAM;gBAAE,IAAI,CAAC,KAAK,CAAC,mBAAmB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAA;YAC7D,OAAO,IAAI,CAAA;QACf,CAAC;KACJ,CAAA;AACL,CAAC","sourcesContent":["import { createRequire } from 'node:module'\nimport { join } from 'node:path'\nimport type {\n NitroLike,\n RentnerPdfViteOptions,\n RentnerPdfVitePlugin,\n ResolveContextLike,\n} from './Types/plugin.ts'\n\n/** Entry points that load the engine (native on servers, WebAssembly in browsers). */\nconst engineEntries = new Set(['@rentnerkev/pdf', '@rentnerkev/pdf/react'])\n\n/** Entry points that read files; never part of a client bundle. */\nconst serverEntries = new Set(['@rentnerkev/pdf/node'])\n\n/**\n * Packages Nitro must keep out of its server bundle and copy to\n * `.output/server/node_modules`: the engine reads its fonts and native\n * addon next to its own files, and the React path must share the app's\n * React (`react-dom/static` from one copy, hooks from another fail).\n */\nexport const NITRO_TRACE_DEPS = Object.freeze([\n '@rentnerkev/pdf',\n 'react',\n 'react-dom',\n])\n\nconst installed = (rootDir: string, name: string): boolean => {\n try {\n createRequire(join(rootDir, 'package.json')).resolve(\n `${name}/package.json`,\n )\n return true\n } catch {\n return false\n }\n}\n\n/** Adds the server packages to Nitro's `traceDeps`, once each. */\nexport function addNitroTraceDeps({ options }: NitroLike): void {\n const rootDir = options.rootDir ?? process.cwd()\n const traceDeps = options.traceDeps ?? []\n const additions = NITRO_TRACE_DEPS.filter(\n (name) =>\n !traceDeps.includes(name) &&\n (name === '@rentnerkev/pdf' || installed(rootDir, name)),\n )\n options.traceDeps = [...traceDeps, ...additions]\n}\n\nconst allowHint =\n \"; to render on WebAssembly in the browser on purpose, use rentnerPdf({ client: 'allow' }).\"\n\n/** The build error for an engine import in a client bundle. */\nexport const clientImportMessage = (\n source: string,\n importer: string | undefined,\n): string =>\n `${importer ?? 'A module'} imports ${source} into a client bundle. RentnerPDF renders on the server: import it from server functions, server routes or other server-only modules. Type-only imports (\\`import type\\`) are fine${serverEntries.has(source) ? '.' : allowHint}`\n\n/**\n * The optional Vite plugin (D-23). In Nitro builds (TanStack Start, Nitro\n * with Vite) it keeps `@rentnerkev/pdf`, `react` and `react-dom` external\n * and traced into the server output; in client builds it turns an engine\n * import into a build error (`client: 'allow'` permits WebAssembly\n * rendering in the browser; `@rentnerkev/pdf/node` always fails).\n *\n * ```ts\n * import { rentnerPdf } from '@rentnerkev/pdf/vite'\n *\n * export default defineConfig({ plugins: [rentnerPdf(), tanstackStart(), nitro()] })\n * ```\n *\n * @experimental Exempt from SemVer in 1.x until promoted: it follows\n * Nitro's module API, which still changes quickly.\n */\nexport function rentnerPdf(\n options: RentnerPdfViteOptions = {},\n): RentnerPdfVitePlugin {\n const guarded =\n options.client === 'allow'\n ? serverEntries\n : new Set([...engineEntries, ...serverEntries])\n let build = false\n return {\n name: 'rentnerpdf',\n enforce: 'pre',\n nitro: { name: 'rentnerpdf', setup: addNitroTraceDeps },\n configResolved(config) {\n build = config.command === 'build'\n },\n resolveId(\n this: ResolveContextLike,\n source: string,\n importer: string | undefined,\n resolveOptions?: { readonly ssr?: boolean },\n ) {\n // Only builds: the dev server's dependency scan also visits\n // server-only files that the framework compiler strips later.\n if (!build || !guarded.has(source)) return null\n const client =\n this.environment === undefined\n ? resolveOptions?.ssr !== true\n : this.environment.config.consumer === 'client'\n if (client) this.error(clientImportMessage(source, importer))\n return null\n },\n }\n}\n"]}
package/dist/vite.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { rentnerPdf } from './vite/plugin.ts';
2
+ export type { RentnerPdfViteOptions, RentnerPdfVitePlugin, } from './vite/Types/plugin.ts';
3
+ //# sourceMappingURL=vite.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite.d.ts","sourceRoot":"","sources":["../web/src/vite.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAC7C,YAAY,EACR,qBAAqB,EACrB,oBAAoB,GACvB,MAAM,wBAAwB,CAAA"}