Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: follow the community agreement and explicitly use tailwindcss #30

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cool-laws-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@umijs/tnf': patch
---

feat: Follow the community agreement and explicitly use tailwindcss
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Config is loaded from `.tnfrc.ts` by default.
- `externals: Record<string, string>`: An object that maps package names to their corresponding paths.
- `less: { modifyVars?: Record<string, string>; globalVars?: Record<string, string>; math?: 'always' | 'strict' | 'parens-division' | 'parens' | 'strict-legacy' | number; sourceMap?: any; plugins?: (string | [string, Record<string, any>])[];}`: The configuration passed to lessLoader.
- `router: { defaultPreload?: 'intent' | 'render' | 'viewport'; defaultPreloadDelay?: number; devtool?: { options?: { initialIsOpen?: boolean; position?: 'bottom-left' | 'bottom-right' | 'top-left' | 'top-right' }; } | false }`: The router configuration.
- `tailwindcss: boolean`: Turn on/off tailwindcss. Need to be used in conjunction with `src/tailwind.css` and `tailwind.config.js`.

## FAQ

Expand Down
2 changes: 1 addition & 1 deletion examples/tailwindcss/.tnfrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export default {
router: {
defaultPreload: 'intent',
},
tailwindcss: {},
tailwindcss: true,
};
3 changes: 3 additions & 0 deletions examples/tailwindcss/src/tailwind.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
6 changes: 6 additions & 0 deletions examples/tailwindcss/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
content: [
"./src/pages/**/*.{js,ts,jsx,tsx}",
"./src/components/**/*.{js,ts,jsx,tsx}"
]
}
80 changes: 1 addition & 79 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,85 +47,7 @@ const ConfigSchema = z.object({
.optional(),
})
.optional(),
tailwindcss: z
.union([
z.object({
content: z
.union([
z.array(
z.union([
z.string(),
z.object({
raw: z.string(),
extension: z.string().optional(),
}),
]),
),
z.object({
files: z.array(
z.union([
z.string(),
z.object({
raw: z.string(),
extension: z.string().optional(),
}),
]),
),
relative: z.boolean().optional(),
extract: z
.union([
z.function().args(z.string()).returns(z.array(z.string())),
z.record(
z.string(),
z.function().args(z.string()).returns(z.array(z.string())),
),
])
.optional(),
transform: z
.union([
z.function().args(z.string()).returns(z.string()),
z.record(
z.string(),
z.function().args(z.string()).returns(z.string()),
),
])
.optional(),
}),
])
.optional(),
important: z.union([z.boolean(), z.string()]).optional(),
prefix: z.string().optional(),
separator: z.string().optional(),
safelist: z
.array(
z.union([
z.string(),
z.object({
pattern: z.instanceof(RegExp),
variants: z.array(z.string()).optional(),
}),
]),
)
.optional(),
darkMode: z
.union([
z.literal('media'),
z.literal('class'),
z.tuple([z.literal('class'), z.string()]),
z.literal('selector'),
z.tuple([z.literal('selector'), z.string()]),
z.tuple([
z.literal('variant'),
z.union([z.string(), z.array(z.string())]),
]),
])
.optional(),
theme: z.record(z.string(), z.any()).optional(),
plugins: z.array(z.function()).optional(),
}),
z.record(z.string(), z.any()),
])
.optional(),
tailwindcss: z.boolean().optional(),
});

export type Config = z.infer<typeof ConfigSchema>;
Expand Down
46 changes: 15 additions & 31 deletions src/fishkit/tailwindcss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ interface TailwindConfig {
interface GenerateTailwindcssOpts {
cwd: string;
tmpPath: string;
config: Config['tailwindcss'];
mode: 'development' | 'production';
}

Expand Down Expand Up @@ -69,44 +68,29 @@ function getTailwindBinPath(opts: { cwd: string }): string {
export async function generateTailwindcss(
opts: GenerateTailwindcssOpts,
): Promise<string> {
const { cwd, tmpPath, config, mode } = opts;
const { cwd, tmpPath, mode } = opts;
const rootPath = path.join(tmpPath, 'tailwindcss');

// 设置文件路径
const paths = {
input: path.join(rootPath, 'tailwindDirectives.css'),
input: path.join(cwd, 'src/tailwind.css'),
output: path.join(rootPath, 'tailwind.css'),
config: path.join(rootPath, 'tailwind.config.js'),
config: path.join(cwd, 'tailwind.config.js'),
};

// 默认 Tailwind 配置
const defaultConfig: TailwindConfig = {
content: [
'./src/pages/**/*.{js,ts,jsx,tsx}',
'./src/components/**/*.{js,ts,jsx,tsx}',
],
};

const mergedConfig = { ...defaultConfig, ...config };

// 确保目录存在
fs.mkdirpSync(path.dirname(paths.input));

// 写入 Tailwind 指令文件
fs.writeFileSync(
paths.input,
`
@tailwind base;
@tailwind components;
@tailwind utilities;
`.trim(),
);
if (!fs.existsSync(paths.input)) {
console.log(
'Enabling feature tailwindcss requires input file src/tailwind.css',
);
return '';
}

// 写入配置文件
fs.writeFileSync(
paths.config,
`export default ${JSON.stringify(mergedConfig, null, 2)}`,
);
if (!fs.existsSync(paths.config)) {
console.log(
'Enabling feature tailwindcss requires config file tailwind.config.js',
);
return '';
}

// 生成 CSS 文件
await generateFile({
Expand Down
1 change: 0 additions & 1 deletion src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export async function sync(opts: SyncOptions) {
tailwindcssPath = await generateTailwindcss({
cwd,
tmpPath,
config: config?.tailwindcss,
mode,
});
}
Expand Down
Loading