effect-start 0.17.0 → 0.17.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (148) hide show
  1. package/dist/Commander.d.ts +103 -0
  2. package/dist/Commander.js +333 -0
  3. package/dist/ContentNegotiation.d.ts +13 -0
  4. package/dist/ContentNegotiation.js +364 -0
  5. package/dist/Development.d.ts +34 -0
  6. package/dist/Development.js +52 -0
  7. package/dist/Entity.d.ts +47 -0
  8. package/dist/Entity.js +224 -0
  9. package/dist/FileRouter.d.ts +61 -0
  10. package/dist/FileRouter.js +203 -0
  11. package/dist/FileRouterCodegen.d.ts +19 -0
  12. package/dist/FileRouterCodegen.js +176 -0
  13. package/dist/FileRouterPattern.d.ts +9 -0
  14. package/dist/FileRouterPattern.js +35 -0
  15. package/dist/Http.d.ts +37 -0
  16. package/dist/Http.js +92 -0
  17. package/dist/HttpAppExtra.d.ts +7 -0
  18. package/dist/HttpAppExtra.js +320 -0
  19. package/dist/HttpUtils.d.ts +3 -0
  20. package/dist/HttpUtils.js +11 -0
  21. package/dist/PathPattern.d.ts +134 -0
  22. package/dist/PathPattern.js +415 -0
  23. package/dist/Random.d.ts +5 -0
  24. package/dist/Random.js +49 -0
  25. package/dist/Route.d.ts +98 -0
  26. package/dist/Route.js +81 -0
  27. package/dist/RouteBody.d.ts +53 -0
  28. package/dist/RouteBody.js +67 -0
  29. package/dist/RouteHook.d.ts +12 -0
  30. package/dist/RouteHook.js +45 -0
  31. package/dist/RouteHttp.d.ts +21 -0
  32. package/dist/RouteHttp.js +260 -0
  33. package/dist/RouteHttpTracer.d.ts +10 -0
  34. package/dist/RouteHttpTracer.js +62 -0
  35. package/dist/RouteMount.d.ts +119 -0
  36. package/dist/RouteMount.js +77 -0
  37. package/dist/RouteSchema.d.ts +65 -0
  38. package/dist/RouteSchema.js +155 -0
  39. package/dist/RouteSse.d.ts +21 -0
  40. package/dist/RouteSse.js +85 -0
  41. package/dist/RouteTree.d.ts +56 -0
  42. package/dist/RouteTree.js +91 -0
  43. package/dist/RouteTrie.d.ts +20 -0
  44. package/dist/RouteTrie.js +157 -0
  45. package/dist/RouterPattern.d.ts +118 -0
  46. package/dist/RouterPattern.js +269 -0
  47. package/dist/SchemaExtra.d.ts +7 -0
  48. package/dist/SchemaExtra.js +74 -0
  49. package/dist/Start.d.ts +19 -0
  50. package/dist/Start.js +23 -0
  51. package/dist/StartApp.d.ts +19 -0
  52. package/dist/StartApp.js +21 -0
  53. package/dist/StreamExtra.d.ts +28 -0
  54. package/dist/StreamExtra.js +100 -0
  55. package/dist/TuplePathPattern.d.ts +9 -0
  56. package/dist/TuplePathPattern.js +63 -0
  57. package/dist/Values.d.ts +26 -0
  58. package/dist/Values.js +30 -0
  59. package/dist/bun/BunBundle.d.ts +12 -0
  60. package/dist/bun/BunBundle.js +145 -0
  61. package/dist/bun/BunHttpServer.d.ts +44 -0
  62. package/dist/bun/BunHttpServer.js +187 -0
  63. package/dist/bun/BunHttpServer_web.d.ts +60 -0
  64. package/dist/bun/BunHttpServer_web.js +252 -0
  65. package/dist/bun/BunImportTrackerPlugin.d.ts +13 -0
  66. package/dist/bun/BunImportTrackerPlugin.js +71 -0
  67. package/dist/bun/BunRoute.d.ts +49 -0
  68. package/dist/bun/BunRoute.js +131 -0
  69. package/dist/bun/BunRuntime.d.ts +1 -0
  70. package/dist/bun/BunRuntime.js +26 -0
  71. package/dist/bun/BunVirtualFilesPlugin.d.ts +4 -0
  72. package/dist/bun/BunVirtualFilesPlugin.js +40 -0
  73. package/dist/bun/_BunEnhancedResolve.d.ts +45 -0
  74. package/dist/bun/_BunEnhancedResolve.js +104 -0
  75. package/dist/bun/index.d.ts +4 -0
  76. package/dist/bun/index.js +4 -0
  77. package/dist/bundler/Bundle.d.ts +60 -0
  78. package/dist/bundler/Bundle.js +48 -0
  79. package/dist/bundler/BundleFiles.d.ts +13 -0
  80. package/dist/bundler/BundleFiles.js +94 -0
  81. package/dist/bundler/BundleHttp.d.ts +45 -0
  82. package/dist/bundler/BundleHttp.js +176 -0
  83. package/dist/client/Overlay.d.ts +2 -0
  84. package/dist/client/Overlay.js +32 -0
  85. package/dist/client/ScrollState.d.ts +6 -0
  86. package/dist/client/ScrollState.js +98 -0
  87. package/dist/client/index.d.ts +6 -0
  88. package/dist/client/index.js +81 -0
  89. package/dist/experimental/EncryptedCookies.d.ts +51 -0
  90. package/dist/experimental/EncryptedCookies.js +243 -0
  91. package/dist/experimental/SseHttpResponse.d.ts +7 -0
  92. package/dist/experimental/SseHttpResponse.js +28 -0
  93. package/dist/experimental/index.d.ts +2 -0
  94. package/dist/experimental/index.js +2 -0
  95. package/dist/hyper/Hyper.d.ts +32 -0
  96. package/dist/hyper/Hyper.js +34 -0
  97. package/dist/hyper/HyperHtml.d.ts +23 -0
  98. package/dist/hyper/HyperHtml.js +144 -0
  99. package/dist/hyper/HyperNode.d.ts +14 -0
  100. package/dist/hyper/HyperNode.js +11 -0
  101. package/dist/hyper/HyperRoute.d.ts +8 -0
  102. package/dist/hyper/HyperRoute.js +32 -0
  103. package/dist/hyper/HyperRoute.test.d.ts +1 -0
  104. package/dist/hyper/HyperRoute.test.js +72 -0
  105. package/dist/hyper/index.d.ts +4 -0
  106. package/dist/hyper/index.js +4 -0
  107. package/dist/hyper/jsx-runtime.d.ts +7 -0
  108. package/dist/hyper/jsx-runtime.js +8 -0
  109. package/dist/index.d.ts +6 -0
  110. package/dist/index.js +6 -0
  111. package/dist/inference_check.d.ts +1 -0
  112. package/dist/inference_check.js +15 -0
  113. package/dist/middlewares/BasicAuthMiddleware.d.ts +8 -0
  114. package/dist/middlewares/BasicAuthMiddleware.js +22 -0
  115. package/dist/middlewares/index.d.ts +1 -0
  116. package/dist/middlewares/index.js +1 -0
  117. package/dist/node/FileSystem.d.ts +9 -0
  118. package/dist/node/FileSystem.js +440 -0
  119. package/dist/node/Utils.d.ts +1 -0
  120. package/dist/node/Utils.js +19 -0
  121. package/dist/repro_fail.d.ts +1 -0
  122. package/dist/repro_fail.js +14 -0
  123. package/dist/testing/TestHttpClient.d.ts +13 -0
  124. package/dist/testing/TestHttpClient.js +68 -0
  125. package/dist/testing/TestLogger.d.ts +13 -0
  126. package/dist/testing/TestLogger.js +29 -0
  127. package/dist/testing/index.d.ts +3 -0
  128. package/dist/testing/index.js +3 -0
  129. package/dist/testing/utils.d.ts +9 -0
  130. package/dist/testing/utils.js +39 -0
  131. package/dist/x/cloudflare/CloudflareTunnel.d.ts +13 -0
  132. package/dist/x/cloudflare/CloudflareTunnel.js +43 -0
  133. package/dist/x/cloudflare/index.d.ts +1 -0
  134. package/dist/x/cloudflare/index.js +1 -0
  135. package/dist/x/datastar/Datastar.d.ts +6 -0
  136. package/dist/x/datastar/Datastar.js +46 -0
  137. package/dist/x/datastar/index.d.ts +2 -0
  138. package/dist/x/datastar/index.js +2 -0
  139. package/dist/x/tailwind/TailwindPlugin.d.ts +23 -0
  140. package/dist/x/tailwind/TailwindPlugin.js +219 -0
  141. package/dist/x/tailwind/compile.d.ts +19 -0
  142. package/dist/x/tailwind/compile.js +156 -0
  143. package/dist/x/tailwind/plugin.d.ts +2 -0
  144. package/dist/x/tailwind/plugin.js +15 -0
  145. package/package.json +68 -16
  146. package/src/RouteBody.test.ts +18 -0
  147. package/src/RouteBody.ts +126 -2
  148. package/src/x/tailwind/compile.ts +8 -2
@@ -0,0 +1,13 @@
1
+ import { Effect, Layer, LogLevel } from "effect";
2
+ /**
3
+ * Starts Cloudflare tunnel using cloudflared cli.
4
+ */
5
+ export declare const start: (opts: {
6
+ command?: string;
7
+ tunnelName: string;
8
+ tunnelUrl?: string;
9
+ cleanLogs?: false;
10
+ logLevel?: LogLevel.LogLevel;
11
+ logPrefix?: string;
12
+ }) => Effect.Effect<void, import("@effect/platform/Error").PlatformError, import("effect/Scope").Scope | import("@effect/platform/CommandExecutor").CommandExecutor>;
13
+ export declare const layer: () => Layer.Layer<never, import("effect/ConfigError").ConfigError, import("@effect/platform/CommandExecutor").CommandExecutor>;
@@ -0,0 +1,43 @@
1
+ import { Command, } from "@effect/platform";
2
+ import { Config, Effect, identity, Layer, LogLevel, Option, pipe, Stream, String, } from "effect";
3
+ /**
4
+ * Starts Cloudflare tunnel using cloudflared cli.
5
+ */
6
+ export const start = (opts) => Effect.gen(function* () {
7
+ const logPrefix = String.isString(opts.logPrefix)
8
+ ? opts.logPrefix
9
+ : "CloudflareTunnel: ";
10
+ const args = [
11
+ "tunnel",
12
+ "run",
13
+ opts.tunnelUrl
14
+ ? [
15
+ "--url",
16
+ opts.tunnelUrl,
17
+ ]
18
+ : [],
19
+ opts.tunnelName,
20
+ ]
21
+ .flatMap(v => v);
22
+ const process = yield* pipe(Command.make(opts.command ?? "cloudflared", ...args), Command.start);
23
+ yield* Effect.logInfo(`Cloudflare tunnel started name=${opts.tunnelName} pid=${process.pid} tunnelUrl=${opts.tunnelUrl ?? "<empty>"}`);
24
+ yield* pipe(Stream.merge(process.stdout, process.stderr), Stream.decodeText("utf-8"), Stream.splitLines, opts.cleanLogs ?? true
25
+ ? Stream.map(v => v.replace(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z\s\w+\s/, ""))
26
+ : identity, logPrefix
27
+ ? Stream.map(v => logPrefix + v)
28
+ : identity, Stream.runForEach(v => Effect.logWithLevel(opts.logLevel ?? LogLevel.Debug, v)));
29
+ });
30
+ export const layer = () => Layer.scopedDiscard(Effect.gen(function* () {
31
+ const tunnelName = yield* pipe(Config.string("CLOUDFLARE_TUNNEL_NAME"), Config.option, Effect.andThen(Option.getOrUndefined));
32
+ const tunnelUrl = yield* pipe(Config.string("CLOUDFLARE_TUNNEL_URL"), Config.option, Effect.andThen(Option.getOrUndefined));
33
+ if (!tunnelName) {
34
+ yield* Effect.logWarning("CLOUDFLARE_TUNNEL_NAME not provided. Skipping.");
35
+ return;
36
+ }
37
+ yield* Effect
38
+ .forkScoped(pipe(start({
39
+ tunnelName,
40
+ tunnelUrl,
41
+ })))
42
+ .pipe(Effect.catchAll(err => Effect.logError("Cloudflare tunnel failed", err)));
43
+ }));
@@ -0,0 +1 @@
1
+ export * as CloudflareTunnel from "./CloudflareTunnel.ts";
@@ -0,0 +1 @@
1
+ export * as CloudflareTunnel from "./CloudflareTunnel.js";
@@ -0,0 +1,6 @@
1
+ import * as HyperNode from "../../hyper/HyperNode.ts";
2
+ export declare const HyperHooks: {
3
+ readonly onNode: typeof onNode;
4
+ };
5
+ declare function onNode(node: HyperNode.HyperNode): void;
6
+ export {};
@@ -0,0 +1,46 @@
1
+ export const HyperHooks = {
2
+ onNode,
3
+ };
4
+ function onNode(node) {
5
+ const { "data-signals": dataSignals, "data-class": dataClass, "data-attr": dataAttr, "data-style": dataStyle, "data-show": dataShow, "data-ignore": dataIgnore, "data-ignore-morph": dataIgnoreMorph, } = node.props;
6
+ if (typeof dataSignals === "object" && dataSignals !== null) {
7
+ node.props["data-signals"] = JSON.stringify(dataSignals);
8
+ }
9
+ if (typeof dataClass === "function") {
10
+ node.props["data-class"] = `(${dataClass.toString()})()`;
11
+ }
12
+ else if (typeof dataClass === "object" && dataClass !== null) {
13
+ node.props["data-class"] = JSON.stringify(dataClass);
14
+ }
15
+ if (typeof dataAttr === "object" && dataAttr !== null) {
16
+ node.props["data-attr"] = JSON.stringify(dataAttr);
17
+ }
18
+ if (typeof dataStyle === "function") {
19
+ node.props["data-style"] = `(${dataStyle.toString()})()`;
20
+ }
21
+ else if (typeof dataStyle === "object" && dataStyle !== null) {
22
+ node.props["data-style"] = JSON.stringify(dataStyle);
23
+ }
24
+ if (typeof dataShow === "boolean") {
25
+ node.props["data-show"] = dataShow.toString();
26
+ }
27
+ if (dataIgnore !== true && dataIgnore !== undefined) {
28
+ delete node.props["data-ignore"];
29
+ }
30
+ if (dataIgnoreMorph !== true && dataIgnoreMorph !== undefined) {
31
+ delete node.props["data-ignore-morph"];
32
+ }
33
+ // Handle dynamic attributes with suffixes
34
+ for (const [key, value] of Object.entries(node.props)) {
35
+ if (key.startsWith("data-signals-")
36
+ && typeof value === "object"
37
+ && value !== null) {
38
+ node.props[key] = JSON.stringify(value);
39
+ }
40
+ if (key.startsWith("data-on-")
41
+ && typeof value === "function") {
42
+ // @ts-ignore
43
+ node.props[key] = `(${value.toString()})()`;
44
+ }
45
+ }
46
+ }
@@ -0,0 +1,2 @@
1
+ export * as Datastar from "./Datastar.ts";
2
+ export { HyperHooks, } from "./Datastar.ts";
@@ -0,0 +1,2 @@
1
+ export * as Datastar from "./Datastar.js";
2
+ export { HyperHooks, } from "./Datastar.js";
@@ -0,0 +1,23 @@
1
+ import type { BunPlugin } from "bun";
2
+ export declare const make: (opts?: {
3
+ /**
4
+ * Pattern to match component and HTML files for class name extraction.
5
+ */
6
+ filesPattern?: RegExp;
7
+ /**
8
+ * Pattern to match CSS files that import Tailwind.
9
+ */
10
+ cssPattern?: RegExp;
11
+ /**
12
+ * Scan a path for candidates.
13
+ * By default, only class names found in files that are part of the import graph
14
+ * that imports tailwind are considered.
15
+ *
16
+ * This option scans the provided path and ensures that class names found under this path
17
+ * are includedd, even if they are not part of the import graph.
18
+ * Useful when we want to scan clientside code which is not imported directly on serverside.
19
+ */
20
+ scanPath?: string;
21
+ target?: "browser" | "bun" | "node";
22
+ }) => BunPlugin;
23
+ export declare function extractClassNames(source: string): Set<string>;
@@ -0,0 +1,219 @@
1
+ import * as NPath from "node:path";
2
+ import * as Tailwind from "./compile.js";
3
+ export const make = (opts) => {
4
+ const { filesPattern = /\.(jsx?|tsx?|html|svelte|vue|astro)$/, cssPattern = /\.css$/, target = "browser", } = opts ?? {};
5
+ return {
6
+ name: "Tailwind.css plugin",
7
+ target,
8
+ async setup(builder) {
9
+ const scannedCandidates = new Set();
10
+ // (file) -> (class names)
11
+ const classNameCandidates = new Map();
12
+ // (importer path) -> (imported paths)
13
+ const importAncestors = new Map();
14
+ // (imported path) -> (importer paths)
15
+ const importDescendants = new Map();
16
+ const prepopulateCandidates = opts?.scanPath
17
+ ? async () => {
18
+ const candidates = await scanFiles(opts.scanPath);
19
+ scannedCandidates.clear();
20
+ candidates.forEach(candidate => scannedCandidates.add(candidate));
21
+ }
22
+ : null;
23
+ // Track import relationships when dynamically scanning
24
+ // from tailwind entrypoints.
25
+ // As of Bun 1.3 this pathway break for Bun Full-Stack server.
26
+ // Better to pass scanPath explicitly.
27
+ // @see https://github.com/oven-sh/bun/issues/20877
28
+ if (!prepopulateCandidates) {
29
+ builder.onResolve({
30
+ filter: /.*/,
31
+ }, (args) => {
32
+ const fullPath = Bun.resolveSync(args.path, args.resolveDir);
33
+ const importer = args.importer;
34
+ if (fullPath.includes("/node_modules/")) {
35
+ return undefined;
36
+ }
37
+ /**
38
+ * Register every visited module.
39
+ */
40
+ {
41
+ if (!importAncestors.has(fullPath)) {
42
+ importAncestors.set(fullPath, new Set());
43
+ }
44
+ if (!importDescendants.has(fullPath)) {
45
+ importDescendants.set(fullPath, new Set());
46
+ }
47
+ if (!importAncestors.has(importer)) {
48
+ importAncestors.set(args.importer, new Set());
49
+ }
50
+ if (!importDescendants.has(importer)) {
51
+ importDescendants.set(importer, new Set());
52
+ }
53
+ }
54
+ importAncestors.get(fullPath).add(importer);
55
+ importDescendants.get(importer).add(fullPath);
56
+ return undefined;
57
+ });
58
+ }
59
+ /**
60
+ * Scan for class name candidates in component files.
61
+ */
62
+ builder.onLoad({
63
+ filter: filesPattern,
64
+ }, async (args) => {
65
+ const contents = await Bun.file(args.path).text();
66
+ const classNames = extractClassNames(contents);
67
+ if (classNames.size > 0) {
68
+ classNameCandidates.set(args.path, classNames);
69
+ }
70
+ return undefined;
71
+ });
72
+ /**
73
+ * Compile tailwind entrypoints.
74
+ */
75
+ builder.onLoad({
76
+ filter: cssPattern,
77
+ }, async (args) => {
78
+ const source = await Bun.file(args.path).text();
79
+ if (!hasCssImport(source, "tailwindcss")) {
80
+ return undefined;
81
+ }
82
+ const compiler = await Tailwind.compile(source, {
83
+ base: NPath.dirname(args.path),
84
+ onDependency: (path) => { },
85
+ });
86
+ await prepopulateCandidates?.();
87
+ // wait for other files to be loaded so we can collect class name candidates
88
+ await args.defer();
89
+ const candidates = new Set(scannedCandidates);
90
+ // when we scan a path, we don't need to track candidate tree
91
+ if (!prepopulateCandidates) {
92
+ const pendingModules = [
93
+ // get class name candidates from all modules that import this one
94
+ ...(importAncestors.get(args.path) ?? []),
95
+ ];
96
+ const visitedModules = new Set();
97
+ while (pendingModules.length > 0) {
98
+ const currentPath = pendingModules.shift();
99
+ if (visitedModules.has(currentPath)) {
100
+ continue;
101
+ }
102
+ const moduleImports = importDescendants.get(currentPath);
103
+ moduleImports?.forEach(moduleImport => {
104
+ const moduleCandidates = classNameCandidates.get(moduleImport);
105
+ moduleCandidates?.forEach(candidate => candidates.add(candidate));
106
+ pendingModules.push(moduleImport);
107
+ });
108
+ visitedModules.add(currentPath);
109
+ }
110
+ }
111
+ const contents = compiler.build([
112
+ ...candidates,
113
+ ]);
114
+ return {
115
+ contents,
116
+ loader: "css",
117
+ };
118
+ });
119
+ },
120
+ };
121
+ };
122
+ const CSS_IMPORT_REGEX = /@import\s+(?:url\()?["']?([^"')]+)["']?\)?\s*[^;]*;/;
123
+ const HTML_COMMENT_REGEX = /<!--[\s\S]*?-->/g;
124
+ const TEMPLATE_EXPRESSION_REGEX = /\$\{[^}]*\}/g;
125
+ const TAILWIND_CLASS_REGEX = /^[a-zA-Z0-9_:-]+(\[[^\]]*\])?$/;
126
+ const CLASS_NAME_PATTERNS = [
127
+ // HTML class attributes with double quotes: <div class="bg-blue-500 text-white">
128
+ "<[^>]*?\\sclass\\s*=\\s*\"([^\"]+)\"",
129
+ // HTML class attributes with single quotes: <div class='bg-blue-500 text-white'>
130
+ "<[^>]*?\\sclass\\s*=\\s*'([^']+)'",
131
+ // JSX className attributes with double quotes: <div className="bg-blue-500 text-white">
132
+ "<[^>]*?\\sclassName\\s*=\\s*\"([^\"]+)\"",
133
+ // JSX className attributes with single quotes: <div className='bg-blue-500 text-white'>
134
+ "<[^>]*?\\sclassName\\s*=\\s*'([^']+)'",
135
+ // JSX className with braces and double quotes: <div className={"bg-blue-500 text-white"}>
136
+ "<[^>]*?\\sclassName\\s*=\\s*\\{\\s*\"([^\"]+)\"\\s*\\}",
137
+ // JSX className with braces and single quotes: <div className={'bg-blue-500 text-white'}>
138
+ "<[^>]*?\\sclassName\\s*=\\s*\\{\\s*'([^']+)'\\s*\\}",
139
+ // JSX className with template literals: <div className={`bg-blue-500 ${variable}`}>
140
+ "<[^>]*?\\sclassName\\s*=\\s*\\{\\s*`([^`]*)`\\s*\\}",
141
+ // HTML class with template literals: <div class={`bg-blue-500 ${variable}`}>
142
+ "<[^>]*?\\sclass\\s*=\\s*\\{\\s*`([^`]*)`\\s*\\}",
143
+ // HTML class at start of tag with double quotes: <div class="bg-blue-500">
144
+ "<\\w+\\s+class\\s*=\\s*\"([^\"]+)\"",
145
+ // HTML class at start of tag with single quotes: <div class='bg-blue-500'>
146
+ "<\\w+\\s+class\\s*=\\s*'([^']+)'",
147
+ // JSX className at start of tag with double quotes: <div className="bg-blue-500">
148
+ "<\\w+\\s+className\\s*=\\s*\"([^\"]+)\"",
149
+ // JSX className at start of tag with single quotes: <div className='bg-blue-500'>
150
+ "<\\w+\\s+className\\s*=\\s*'([^']+)'",
151
+ // JSX className at start with braces and double quotes: <div className={"bg-blue-500"}>
152
+ "<\\w+\\s+className\\s*=\\s*\\{\\s*\"([^\"]+)\"\\s*\\}",
153
+ // JSX className at start with braces and single quotes: <div className={'bg-blue-500'}>
154
+ "<\\w+\\s+className\\s*=\\s*\\{\\s*'([^']+)'\\s*\\}",
155
+ // JSX className at start with template literals: <div className={`bg-blue-500 ${variable}`}>
156
+ "<\\w+\\s+className\\s*=\\s*\\{\\s*`([^`]*)`\\s*\\}",
157
+ // HTML class at start with template literals: <div class={`bg-blue-500 ${variable}`}>
158
+ "<\\w+\\s+class\\s*=\\s*\\{\\s*`([^`]*)`\\s*\\}",
159
+ ];
160
+ const CLASS_NAME_REGEX = new RegExp(CLASS_NAME_PATTERNS.map(pattern => `(?:${pattern})`).join("|"), "g");
161
+ function hasCssImport(css, specifier) {
162
+ const [, importPath] = css.match(CSS_IMPORT_REGEX) ?? [];
163
+ if (!importPath)
164
+ return false;
165
+ return specifier === undefined
166
+ || importPath.includes(specifier);
167
+ }
168
+ export function extractClassNames(source) {
169
+ const candidates = new Set();
170
+ const sourceWithoutComments = source.replace(HTML_COMMENT_REGEX, "");
171
+ for (const match of sourceWithoutComments.matchAll(CLASS_NAME_REGEX)) {
172
+ // Find the first non-undefined capture group (skip match[0] which is full match)
173
+ let classString = "";
174
+ for (let i = 1; i < match.length; i++) {
175
+ if (match[i] !== undefined) {
176
+ classString = match[i];
177
+ break;
178
+ }
179
+ }
180
+ if (!classString) {
181
+ continue;
182
+ }
183
+ if (classString.includes("${")) {
184
+ const staticParts = classString.split(TEMPLATE_EXPRESSION_REGEX);
185
+ for (const part of staticParts) {
186
+ const names = part.trim().split(/\s+/).filter(name => {
187
+ if (name.length === 0)
188
+ return false;
189
+ if (name.endsWith("-") || name.startsWith("-"))
190
+ return false;
191
+ return TAILWIND_CLASS_REGEX.test(name);
192
+ });
193
+ names.forEach(name => candidates.add(name));
194
+ }
195
+ }
196
+ else {
197
+ // Simple case: regular class string without expressions
198
+ const names = classString.split(/\s+/).filter(name => name.length > 0);
199
+ names.forEach(name => candidates.add(name));
200
+ }
201
+ }
202
+ return candidates;
203
+ }
204
+ async function scanFiles(dir) {
205
+ const candidates = new Set();
206
+ const glob = new Bun.Glob("**/*.{js,jsx,ts,tsx,html,vue,svelte,astro}");
207
+ for await (const filePath of glob.scan({
208
+ cwd: dir,
209
+ absolute: true,
210
+ })) {
211
+ if (filePath.includes("/node_modules/")) {
212
+ continue;
213
+ }
214
+ const contents = await Bun.file(filePath).text();
215
+ const classNames = extractClassNames(contents);
216
+ classNames.forEach((className) => candidates.add(className));
217
+ }
218
+ return candidates;
219
+ }
@@ -0,0 +1,19 @@
1
+ import { compile as _compile, compileAst as _compileAst, Features, Polyfills } from "tailwindcss";
2
+ type AstNode = Parameters<typeof _compileAst>[0][number];
3
+ export { Features, Polyfills, };
4
+ export type Resolver = (id: string, base: string) => Promise<string | false | undefined>;
5
+ export interface CompileOptions {
6
+ base: string;
7
+ from?: string;
8
+ onDependency: (path: string) => void;
9
+ polyfills?: Polyfills;
10
+ customCssResolver?: Resolver;
11
+ customJsResolver?: Resolver;
12
+ }
13
+ export declare function compileAst(ast: AstNode[], options: CompileOptions): ReturnType<typeof _compileAst>;
14
+ export declare function compile(css: string, options: CompileOptions): ReturnType<typeof _compile>;
15
+ export declare function loadModule(id: string, base: string, _onDependency: (path: string) => void, customJsResolver?: Resolver): Promise<{
16
+ path: string;
17
+ base: string;
18
+ module: any;
19
+ }>;
@@ -0,0 +1,156 @@
1
+ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
2
+ if (typeof path === "string" && /^\.\.?\//.test(path)) {
3
+ return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
4
+ return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
5
+ });
6
+ }
7
+ return path;
8
+ };
9
+ import fsPromises from "node:fs/promises";
10
+ import path from "node:path";
11
+ import { pathToFileURL } from "node:url";
12
+ import { compile as _compile, compileAst as _compileAst, } from "tailwindcss";
13
+ import * as BunEnhancedResolve from "../../bun/_BunEnhancedResolve";
14
+ function createCompileOptions({ base, from, polyfills, onDependency, customCssResolver, customJsResolver, }) {
15
+ return {
16
+ base,
17
+ polyfills,
18
+ from,
19
+ async loadModule(id, base) {
20
+ return loadModule(id, base, onDependency, customJsResolver);
21
+ },
22
+ async loadStylesheet(id, sheetBase) {
23
+ let sheet = await loadStylesheet(id, sheetBase, onDependency, customCssResolver);
24
+ return sheet;
25
+ },
26
+ };
27
+ }
28
+ async function ensureSourceDetectionRootExists(compiler) {
29
+ // Verify if the `source(…)` path exists (until the glob pattern starts)
30
+ if (compiler.root && compiler.root !== "none") {
31
+ let globSymbols = /[*{]/;
32
+ let basePath = [];
33
+ for (let segment of compiler.root.pattern.split("/")) {
34
+ if (globSymbols.test(segment)) {
35
+ break;
36
+ }
37
+ basePath.push(segment);
38
+ }
39
+ let exists = await fsPromises
40
+ .stat(path.resolve(compiler.root.base, basePath.join("/")))
41
+ .then((stat) => stat.isDirectory())
42
+ .catch(() => false);
43
+ if (!exists) {
44
+ throw new Error(`The \`source(${compiler.root.pattern})\` does not exist or is not a directory.`);
45
+ }
46
+ }
47
+ }
48
+ export async function compileAst(ast, options) {
49
+ let compiler = await _compileAst(ast, createCompileOptions(options));
50
+ await ensureSourceDetectionRootExists(compiler);
51
+ return compiler;
52
+ }
53
+ export async function compile(css, options) {
54
+ let compiler = await _compile(css, createCompileOptions(options));
55
+ await ensureSourceDetectionRootExists(compiler);
56
+ return compiler;
57
+ }
58
+ export async function loadModule(id, base, _onDependency, customJsResolver) {
59
+ if (id[0] !== ".") {
60
+ let resolvedPath = await resolveJsId(id, base, customJsResolver);
61
+ if (!resolvedPath) {
62
+ throw new Error(`Could not resolve '${id}' from '${base}'`);
63
+ }
64
+ let module = await importModule(pathToFileURL(resolvedPath).href);
65
+ return {
66
+ path: resolvedPath,
67
+ base: path.dirname(resolvedPath),
68
+ module: module.default ?? module,
69
+ };
70
+ }
71
+ let resolvedPath = await resolveJsId(id, base, customJsResolver);
72
+ if (!resolvedPath) {
73
+ throw new Error(`Could not resolve '${id}' from '${base}'`);
74
+ }
75
+ let [module] = await Promise.all([
76
+ importModule(pathToFileURL(resolvedPath).href + "?id=" + Date.now()),
77
+ ]);
78
+ return {
79
+ path: resolvedPath,
80
+ base: path.dirname(resolvedPath),
81
+ module: module.default ?? module,
82
+ };
83
+ }
84
+ async function loadStylesheet(id, base, onDependency, cssResolver) {
85
+ let resolvedPath = await resolveCssId(id, base, cssResolver);
86
+ if (!resolvedPath)
87
+ throw new Error(`Could not resolve '${id}' from '${base}'`);
88
+ onDependency(resolvedPath);
89
+ let file = await fsPromises.readFile(resolvedPath, "utf-8");
90
+ return {
91
+ path: resolvedPath,
92
+ base: path.dirname(resolvedPath),
93
+ content: file,
94
+ };
95
+ }
96
+ async function importModule(path) {
97
+ if (typeof globalThis.__tw_load === "function") {
98
+ let module = await globalThis.__tw_load(path);
99
+ if (module) {
100
+ return module;
101
+ }
102
+ }
103
+ return await import(__rewriteRelativeImportExtension(path));
104
+ }
105
+ const cssResolver = BunEnhancedResolve.ResolverFactory.createResolver({
106
+ extensions: [".css"],
107
+ mainFields: ["style"],
108
+ conditionNames: ["style"],
109
+ });
110
+ async function resolveCssId(id, base, customCssResolver) {
111
+ if (typeof globalThis.__tw_resolve === "function") {
112
+ let resolved = globalThis.__tw_resolve(id, base);
113
+ if (resolved) {
114
+ return Promise.resolve(resolved);
115
+ }
116
+ }
117
+ if (customCssResolver) {
118
+ let customResolution = await customCssResolver(id, base);
119
+ if (customResolution) {
120
+ return customResolution;
121
+ }
122
+ }
123
+ return runResolver(cssResolver, id, base);
124
+ }
125
+ const esmResolver = BunEnhancedResolve.ResolverFactory.createResolver({
126
+ extensions: [".js", ".json", ".node", ".ts"],
127
+ conditionNames: ["node", "import"],
128
+ mainFields: ["module", "main"],
129
+ });
130
+ const cjsResolver = BunEnhancedResolve.ResolverFactory.createResolver({
131
+ extensions: [".js", ".json", ".node", ".ts"],
132
+ conditionNames: ["node", "require"],
133
+ mainFields: ["main"],
134
+ });
135
+ async function resolveJsId(id, base, customJsResolver) {
136
+ if (typeof globalThis.__tw_resolve === "function") {
137
+ let resolved = globalThis.__tw_resolve(id, base);
138
+ if (resolved) {
139
+ return Promise.resolve(resolved);
140
+ }
141
+ }
142
+ if (customJsResolver) {
143
+ let customResolution = await customJsResolver(id, base);
144
+ if (customResolution) {
145
+ return customResolution;
146
+ }
147
+ }
148
+ return runResolver(esmResolver, id, base).catch(() => runResolver(cjsResolver, id, base));
149
+ }
150
+ function runResolver(resolver, id, base) {
151
+ return new Promise((resolve, reject) => resolver.resolve({}, base, id, {}, (err, result) => {
152
+ if (err)
153
+ return reject(err);
154
+ resolve(result);
155
+ }));
156
+ }
@@ -0,0 +1,2 @@
1
+ declare const _default: Bun.BunPlugin;
2
+ export default _default;
@@ -0,0 +1,15 @@
1
+ import * as NPath from "node:path";
2
+ import * as NodeUtils from "../../node/Utils.js";
3
+ import * as TailwindPlugin from "./TailwindPlugin.js";
4
+ // Append `?dir=` to module identifier to pass custom directory to scan
5
+ const dirParam = URL.parse(import.meta.url)?.searchParams.get("dir");
6
+ const packageJson = await NodeUtils.findClosestPackageJson(process.cwd());
7
+ const scanPath = dirParam
8
+ ? NPath.resolve(process.cwd(), dirParam)
9
+ : packageJson
10
+ ? NPath.dirname(packageJson)
11
+ : process.cwd();
12
+ // Export as default to be used in bunfig.toml
13
+ export default TailwindPlugin.make({
14
+ scanPath,
15
+ });