tailwindcss-patch 9.0.1 → 9.2.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,193 @@
1
+ import type { NormalizedTailwindCssPatchOptions, TailwindCssPatchOptions } from '../config'
2
+ import type { TailwindV4ResolvedSource, TailwindV4SourceOptions } from './types'
3
+ import { promises as fs } from 'node:fs'
4
+ import process from 'node:process'
5
+ import path from 'pathe'
6
+ import { normalizeOptions } from '../config'
7
+
8
+ function resolveBase(value: string | undefined, fallback: string) {
9
+ return value === undefined
10
+ ? fallback
11
+ : path.isAbsolute(value)
12
+ ? path.resolve(value)
13
+ : path.resolve(fallback, value)
14
+ }
15
+
16
+ function uniquePaths(values: Iterable<string | undefined>) {
17
+ const result: string[] = []
18
+ for (const value of values) {
19
+ if (!value) {
20
+ continue
21
+ }
22
+ const resolved = path.resolve(value)
23
+ if (!result.includes(resolved)) {
24
+ result.push(resolved)
25
+ }
26
+ }
27
+ return result
28
+ }
29
+
30
+ function toCssImportPath(value: string) {
31
+ return value.replaceAll('\\', '/')
32
+ }
33
+
34
+ function quoteCssImport(value: string) {
35
+ return value.replaceAll('\\', '\\\\').replaceAll('"', '\\"')
36
+ }
37
+
38
+ function isPostcssPluginSpecifier(packageName: string) {
39
+ return packageName === '@tailwindcss/postcss'
40
+ || /(?:^|[/\\])@tailwindcss[/\\]postcss(?:[/\\]|$)/.test(packageName)
41
+ || /(?:^|[/\\])postcss(?:[/\\]|$)/i.test(packageName)
42
+ || /postcss\.config\.[cm]?[jt]s$/i.test(packageName)
43
+ }
44
+
45
+ function createDefaultCss(packageName: string | undefined) {
46
+ const cssPackageName = packageName && !isPostcssPluginSpecifier(packageName)
47
+ ? packageName
48
+ : 'tailwindcss'
49
+ return `@import "${quoteCssImport(toCssImportPath(cssPackageName))}";`
50
+ }
51
+
52
+ async function pathExists(filePath: string) {
53
+ try {
54
+ await fs.access(filePath)
55
+ return true
56
+ }
57
+ catch {
58
+ return false
59
+ }
60
+ }
61
+
62
+ async function resolveCssEntries(entries: string[], projectRoot: string, base: string | undefined) {
63
+ const resolvedEntries = entries.map(entry => ({
64
+ original: entry,
65
+ absolute: path.isAbsolute(entry) ? path.resolve(entry) : path.resolve(projectRoot, entry),
66
+ }))
67
+ const resolvedBase = base ?? path.dirname(resolvedEntries[0]?.absolute ?? projectRoot)
68
+ const dependencies = resolvedEntries.map(entry => entry.absolute)
69
+ const cssParts: string[] = []
70
+
71
+ for (const entry of resolvedEntries) {
72
+ if (await pathExists(entry.absolute)) {
73
+ cssParts.push(await fs.readFile(entry.absolute, 'utf8'))
74
+ continue
75
+ }
76
+
77
+ const importPath = path.isAbsolute(entry.original)
78
+ ? entry.absolute
79
+ : path.relative(resolvedBase, entry.absolute)
80
+ cssParts.push(`@import "${quoteCssImport(toCssImportPath(importPath))}";`)
81
+ }
82
+
83
+ return {
84
+ base: resolvedBase,
85
+ css: cssParts.join('\n'),
86
+ dependencies,
87
+ }
88
+ }
89
+
90
+ function normalizeResolvedSource(
91
+ source: {
92
+ projectRoot: string
93
+ cwd: string
94
+ base: string
95
+ baseFallbacks: string[]
96
+ css: string
97
+ dependencies: string[]
98
+ },
99
+ ): TailwindV4ResolvedSource {
100
+ const baseFallbacks = uniquePaths([
101
+ ...source.baseFallbacks,
102
+ source.projectRoot,
103
+ source.cwd,
104
+ ]).filter(base => base !== source.base)
105
+
106
+ return {
107
+ projectRoot: source.projectRoot,
108
+ base: source.base,
109
+ baseFallbacks,
110
+ css: source.css,
111
+ dependencies: Array.from(new Set(source.dependencies.map(dependency => path.resolve(dependency)))),
112
+ }
113
+ }
114
+
115
+ export async function resolveTailwindV4Source(options: TailwindV4SourceOptions = {}): Promise<TailwindV4ResolvedSource> {
116
+ const projectRoot = resolveBase(options.projectRoot, process.cwd())
117
+ const cwd = resolveBase(options.cwd, projectRoot)
118
+ const configuredBase = options.base === undefined ? undefined : resolveBase(options.base, projectRoot)
119
+ const baseFallbacks = uniquePaths(options.baseFallbacks?.map(base => resolveBase(base, projectRoot)) ?? [])
120
+
121
+ if (options.css !== undefined) {
122
+ return normalizeResolvedSource({
123
+ projectRoot,
124
+ cwd,
125
+ base: configuredBase ?? cwd,
126
+ baseFallbacks,
127
+ css: options.css,
128
+ dependencies: [],
129
+ })
130
+ }
131
+
132
+ if (options.cssEntries?.length) {
133
+ const entries = await resolveCssEntries(options.cssEntries, projectRoot, configuredBase)
134
+ return normalizeResolvedSource({
135
+ projectRoot,
136
+ cwd,
137
+ base: entries.base,
138
+ baseFallbacks,
139
+ css: entries.css,
140
+ dependencies: entries.dependencies,
141
+ })
142
+ }
143
+
144
+ return normalizeResolvedSource({
145
+ projectRoot,
146
+ cwd,
147
+ base: configuredBase ?? cwd,
148
+ baseFallbacks,
149
+ css: createDefaultCss(options.packageName),
150
+ dependencies: [],
151
+ })
152
+ }
153
+
154
+ function resolveConfigDir(config: string | undefined, projectRoot: string) {
155
+ if (!config) {
156
+ return undefined
157
+ }
158
+ const configPath = path.isAbsolute(config) ? config : path.resolve(projectRoot, config)
159
+ return path.dirname(configPath)
160
+ }
161
+
162
+ function createSourceOptionsFromNormalizedPatchOptions(
163
+ options: NormalizedTailwindCssPatchOptions,
164
+ ): TailwindV4SourceOptions {
165
+ const v4 = options.tailwind.v4
166
+ const configDir = resolveConfigDir(options.tailwind.config, options.projectRoot)
167
+ const baseFallbacks = uniquePaths([
168
+ v4?.configuredBase,
169
+ options.tailwind.cwd,
170
+ options.projectRoot,
171
+ configDir,
172
+ ])
173
+
174
+ return {
175
+ projectRoot: options.projectRoot,
176
+ ...(options.tailwind.cwd === undefined ? {} : { cwd: options.tailwind.cwd }),
177
+ ...(v4?.configuredBase === undefined ? {} : { base: v4.configuredBase }),
178
+ baseFallbacks,
179
+ ...(v4?.css === undefined ? {} : { css: v4.css }),
180
+ ...(v4?.cssEntries === undefined ? {} : { cssEntries: v4.cssEntries }),
181
+ packageName: options.tailwind.packageName,
182
+ }
183
+ }
184
+
185
+ export function tailwindV4SourceOptionsFromPatchOptions(options: TailwindCssPatchOptions): TailwindV4SourceOptions {
186
+ return createSourceOptionsFromNormalizedPatchOptions(normalizeOptions(options))
187
+ }
188
+
189
+ export async function resolveTailwindV4SourceFromPatchOptions(
190
+ options: TailwindCssPatchOptions,
191
+ ): Promise<TailwindV4ResolvedSource> {
192
+ return resolveTailwindV4Source(tailwindV4SourceOptionsFromPatchOptions(options))
193
+ }
@@ -0,0 +1,64 @@
1
+ export interface TailwindV4SourceOptions {
2
+ projectRoot?: string
3
+ cwd?: string
4
+ base?: string
5
+ baseFallbacks?: string[]
6
+ css?: string
7
+ cssEntries?: string[]
8
+ packageName?: string
9
+ }
10
+
11
+ export interface TailwindV4ResolvedSource {
12
+ projectRoot: string
13
+ base: string
14
+ baseFallbacks: string[]
15
+ css: string
16
+ dependencies: string[]
17
+ }
18
+
19
+ export interface TailwindV4CandidateSource {
20
+ content: string
21
+ extension?: string
22
+ }
23
+
24
+ export interface TailwindV4GenerateOptions {
25
+ candidates?: Iterable<string>
26
+ sources?: TailwindV4CandidateSource[]
27
+ /**
28
+ * 扫描文件系统 source entries 中的候选类名。
29
+ *
30
+ * - `true`:使用 Tailwind v4 编译入口解析出的 `@source` 列表。
31
+ * - `TailwindV4SourcePattern[]`:使用调用方显式传入的 source 列表。
32
+ */
33
+ scanSources?: boolean | TailwindV4SourcePattern[]
34
+ }
35
+
36
+ export interface TailwindV4SourcePattern {
37
+ base: string
38
+ pattern: string
39
+ negated: boolean
40
+ }
41
+
42
+ export interface TailwindV4GenerateResult {
43
+ css: string
44
+ classSet: Set<string>
45
+ rawCandidates: Set<string>
46
+ dependencies: string[]
47
+ sources: TailwindV4SourcePattern[]
48
+ root: null | 'none' | {
49
+ base: string
50
+ pattern: string
51
+ }
52
+ }
53
+
54
+ export interface TailwindV4DesignSystem {
55
+ parseCandidate: (candidate: string) => unknown[]
56
+ candidatesToCss: (candidates: string[]) => Array<string | null | undefined>
57
+ }
58
+
59
+ export interface TailwindV4Engine {
60
+ source: TailwindV4ResolvedSource
61
+ loadDesignSystem: () => Promise<TailwindV4DesignSystem>
62
+ validateCandidates: (candidates: Iterable<string>) => Promise<Set<string>>
63
+ generate: (options?: TailwindV4GenerateOptions) => Promise<TailwindV4GenerateResult>
64
+ }
@@ -1,35 +0,0 @@
1
- require("./chunk-8l464Juk.js");
2
- require("./validate-B8H-8rWO.js");
3
- //#region src/index.bundle.ts
4
- const require$1 = (0, require("node:module").createRequire)(require("url").pathToFileURL(__filename).href);
5
- function loadCliModule() {
6
- return require$1("./commands/cli-runtime.js");
7
- }
8
- function mountTailwindcssPatchCommands(cli, options = {}) {
9
- return loadCliModule().mountTailwindcssPatchCommands(cli, options);
10
- }
11
- function createTailwindcssPatchCli(options = {}) {
12
- return loadCliModule().createTailwindcssPatchCli(options);
13
- }
14
- function defineConfig(config) {
15
- return config;
16
- }
17
- //#endregion
18
- Object.defineProperty(exports, "createTailwindcssPatchCli", {
19
- enumerable: true,
20
- get: function() {
21
- return createTailwindcssPatchCli;
22
- }
23
- });
24
- Object.defineProperty(exports, "defineConfig", {
25
- enumerable: true,
26
- get: function() {
27
- return defineConfig;
28
- }
29
- });
30
- Object.defineProperty(exports, "mountTailwindcssPatchCommands", {
31
- enumerable: true,
32
- get: function() {
33
- return mountTailwindcssPatchCommands;
34
- }
35
- });
@@ -1,18 +0,0 @@
1
- import "./validate-BI8356RT.mjs";
2
- import { createRequire } from "node:module";
3
- //#region src/index.bundle.ts
4
- const require = createRequire(import.meta.url);
5
- function loadCliModule() {
6
- return require("./commands/cli-runtime.js");
7
- }
8
- function mountTailwindcssPatchCommands(cli, options = {}) {
9
- return loadCliModule().mountTailwindcssPatchCommands(cli, options);
10
- }
11
- function createTailwindcssPatchCli(options = {}) {
12
- return loadCliModule().createTailwindcssPatchCli(options);
13
- }
14
- function defineConfig(config) {
15
- return config;
16
- }
17
- //#endregion
18
- export { defineConfig as n, mountTailwindcssPatchCommands as r, createTailwindcssPatchCli as t };