tailwind-styled-v4 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,214 @@
1
+ /**
2
+ * tailwind-styled-v4 — AST Transform Core
3
+ *
4
+ * Pure-string AST-like transformer yang menganalisis source code dan
5
+ * mengubah semua tw.tag`...` / tw.tag({...}) calls menjadi static
6
+ * className strings (zero-runtime output).
7
+ *
8
+ * Dipakai oleh:
9
+ * - swcPlugin.ts → SWC/Next.js transform
10
+ * - vitePlugin.ts → Vite/Rollup transform
11
+ * - nextPlugin.ts → Next.js webpack loader
12
+ * - postcssPlugin.ts → PostCSS safelist generation
13
+ *
14
+ * Zero-runtime berarti output akhirnya adalah:
15
+ * BEFORE: const Box = tw.div`p-4 bg-zinc-900`
16
+ * AFTER: const Box = styled.div``.attrs(()=>({className:"p-4 bg-zinc-900"}))``)
17
+ *
18
+ * Untuk kasus yang pure static (tidak ada dynamic expr),
19
+ * output bisa di-inline langsung sebagai string literal:
20
+ * AFTER: const Box = styled.div.attrs(()=>({className:"p-4 bg-zinc-900"}))``)
21
+ */
22
+ interface TransformOptions {
23
+ /**
24
+ * "runtime" → keep tw.tag, resolve at runtime (default dev mode)
25
+ * "zero-runtime"→ transform to styled.tag with static className
26
+ * "extract-only"→ do not transform, only extract class names
27
+ */
28
+ mode?: "runtime" | "zero-runtime" | "extract-only";
29
+ /** Add data-tw attribute for debugging in dev tools */
30
+ addDataAttr?: boolean;
31
+ /** Generate unique stable hash per component for CSS layer naming */
32
+ generateHash?: boolean;
33
+ /** Tailwind prefix (if configured in tailwind.config.ts) */
34
+ prefix?: string;
35
+ /** Filename being transformed — used for source maps */
36
+ filename?: string;
37
+ }
38
+ interface TransformResult {
39
+ code: string;
40
+ classes: string[];
41
+ map?: string;
42
+ changed: boolean;
43
+ }
44
+ declare function transformSource(source: string, opts?: TransformOptions): TransformResult;
45
+ declare function extractAllClasses(source: string): string[];
46
+ declare function transformBatch(files: Array<{
47
+ path: string;
48
+ source: string;
49
+ }>, opts?: TransformOptions): {
50
+ results: Array<TransformResult & {
51
+ path: string;
52
+ }>;
53
+ allClasses: string[];
54
+ };
55
+
56
+ /**
57
+ * tailwind-styled-v4 — transformTw
58
+ *
59
+ * Low-level transform used by all build plugins.
60
+ * Wraps astTransform with file-type filtering and source-map passthrough.
61
+ */
62
+
63
+ /**
64
+ * Transform a single file's source code.
65
+ * Returns unchanged code for non-JS/TS files.
66
+ */
67
+ declare function transformTw(source: string, opts?: TransformOptions & {
68
+ filepath?: string;
69
+ }): TransformResult;
70
+ /**
71
+ * Extract all Tailwind classes from source without transforming.
72
+ * Used for safelist generation.
73
+ */
74
+ declare function extractTwClasses(source: string): string[];
75
+ /**
76
+ * Check whether a file should be processed by the transform.
77
+ */
78
+ declare function shouldProcess(filepath: string): boolean;
79
+
80
+ /**
81
+ * tailwind-styled-v4 — SWC Plugin / Next.js Transform
82
+ *
83
+ * Integrates with Next.js compiler (SWC-based) via next.config.ts.
84
+ * Supports both Webpack and Turbopack (Next.js 15.3+).
85
+ *
86
+ * Usage in next.config.ts:
87
+ * ─────────────────────────────────────────────────────────────
88
+ * import { withTailwindStyled } from "tailwind-styled-v4/compiler"
89
+ *
90
+ * const nextConfig = withTailwindStyled({
91
+ * mode: "zero-runtime",
92
+ * addDataAttr: true,
93
+ * generateHash: true,
94
+ * })({
95
+ * // ...your Next.js config
96
+ * })
97
+ *
98
+ * export default nextConfig
99
+ * ─────────────────────────────────────────────────────────────
100
+ *
101
+ * What it does at build time:
102
+ * BEFORE: const Box = tw.div`p-4 bg-zinc-900 rounded-xl`
103
+ * AFTER: const Box = styled.div.attrs(()=>({className:"p-4 bg-zinc-900 rounded-xl"}))`
104
+ *
105
+ * Turbopack support (Next.js 15.3+):
106
+ * ─────────────────────────────────────────────────────────────
107
+ * Turbopack does not support webpack loaders. Instead it uses
108
+ * experimental.turbopack.rules. withTailwindStyled automatically
109
+ * detects and configures both bundlers.
110
+ */
111
+
112
+ interface SwcPluginOptions extends TransformOptions {
113
+ /**
114
+ * Directories to scan for class extraction (safelist generation).
115
+ * Defaults to ["src", "app", "pages", "components"]
116
+ */
117
+ scanDirs?: string[];
118
+ /**
119
+ * Output path for generated safelist JSON.
120
+ * Defaults to "tailwind.safelist.json"
121
+ */
122
+ safelistOutput?: string;
123
+ /**
124
+ * Whether to generate the safelist file on each build.
125
+ * Defaults to true
126
+ */
127
+ generateSafelist?: boolean;
128
+ }
129
+ declare function turbopackTransform(source: string, resourcePath: string, opts?: TransformOptions): {
130
+ code: string;
131
+ };
132
+ declare function withTailwindStyled(pluginOpts?: SwcPluginOptions): (nextConfig?: Record<string, any>) => Record<string, any>;
133
+ declare function swcTransformFile(source: string, filepath: string, opts?: TransformOptions): string;
134
+ interface SafelistOptions {
135
+ scanDirs: string[];
136
+ outputPath: string;
137
+ cwd: string;
138
+ }
139
+ declare function runSafelistGeneration(opts: SafelistOptions): void;
140
+ declare function swcPlugin(source: string, opts?: TransformOptions): string;
141
+
142
+ /**
143
+ * tailwind-styled-v4 — Vite Plugin
144
+ *
145
+ * Usage in vite.config.ts:
146
+ * ─────────────────────────────────────────────────────────────
147
+ * import { tailwindStyledPlugin } from "tailwind-styled-v4/compiler"
148
+ *
149
+ * export default defineConfig({
150
+ * plugins: [
151
+ * react(),
152
+ * tailwindStyledPlugin({
153
+ * mode: "zero-runtime",
154
+ * addDataAttr: true,
155
+ * }),
156
+ * ],
157
+ * })
158
+ * ─────────────────────────────────────────────────────────────
159
+ *
160
+ * Compatible with: Vite 4+, Remix, SvelteKit (JS files only)
161
+ */
162
+
163
+ interface VitePluginOptions extends TransformOptions {
164
+ /** Include patterns. Default: all .tsx/.ts/.jsx/.js */
165
+ include?: RegExp;
166
+ /** Exclude patterns. Default: node_modules */
167
+ exclude?: RegExp;
168
+ /** Directories to scan for safelist generation */
169
+ scanDirs?: string[];
170
+ /** Output path for safelist JSON */
171
+ safelistOutput?: string;
172
+ /** Generate safelist on build end */
173
+ generateSafelist?: boolean;
174
+ }
175
+ declare function tailwindStyledPlugin(opts?: VitePluginOptions): any;
176
+
177
+ /**
178
+ * tailwind-styled-v4 — Safelist Generator
179
+ *
180
+ * Scans your entire src/ directory and extracts every Tailwind class
181
+ * used via tw.tag`...`, className="...", cn(), etc.
182
+ *
183
+ * The output JSON can be referenced in your tailwind.config.ts:
184
+ *
185
+ * ─────────────────────────────────────────────────────────────
186
+ * // tailwind.config.ts
187
+ * import safelist from "./tailwind.safelist.json"
188
+ *
189
+ * export default {
190
+ * safelist: safelist.safelist,
191
+ * // ...
192
+ * }
193
+ * ─────────────────────────────────────────────────────────────
194
+ *
195
+ * Run manually: npx ts-node src/build/generateSafelist.ts
196
+ * Or via script: npm run build:safelist
197
+ */
198
+ interface SafelistConfig {
199
+ /** Root directory of your project */
200
+ cwd: string;
201
+ /** Directories to scan (relative to cwd) */
202
+ scanDirs: string[];
203
+ /** File extensions to scan */
204
+ extensions: RegExp;
205
+ /** Directories to skip */
206
+ excludeDirs: string[];
207
+ /** Output file path (relative to cwd) */
208
+ outputPath: string;
209
+ /** Include Tailwind pattern annotations in output */
210
+ verbose: boolean;
211
+ }
212
+ declare function generateSafelist(userConfig?: Partial<SafelistConfig>): void;
213
+
214
+ export { type SwcPluginOptions, type TransformOptions, type TransformResult, type VitePluginOptions, extractAllClasses, extractTwClasses, generateSafelist, runSafelistGeneration, shouldProcess, swcPlugin, swcTransformFile, tailwindStyledPlugin, transformBatch, transformSource, transformTw, turbopackTransform, withTailwindStyled };