This commit is contained in:
icssoa 2025-05-21 18:52:17 +08:00
parent 861ff875ce
commit b9ee6dcacf
7 changed files with 62 additions and 96 deletions

View File

@ -1,24 +1,2 @@
import type { Config } from "../types";
export declare function cool(options: Config.Options): (import("vite").Plugin<any> | Promise<import("vite").Plugin<any>> | {
name: string;
enforce: "pre";
config(): {
css: {
postcss: {
plugins: {
postcssPlugin: string;
prepare(): {
Rule(rule: any): void;
Declaration(decl: any): void;
};
}[];
};
};
};
transform(code: string, id: string): {
code: string;
map: {
mappings: string;
};
} | null;
}[])[];
export declare function cool(options: Config.Options): (import("vite").Plugin<any> | Promise<import("vite").Plugin<any>> | import("vite").Plugin<any>[])[];

View File

@ -44,6 +44,12 @@
svg: {
skipNames: ["base"],
},
tailwind: {
enable: true,
remUnit: 16,
remPrecision: 6,
rpxRatio: 2,
},
};
// 根目录
@ -1052,6 +1058,9 @@ if (typeof window !== 'undefined') {
"]": "-",
"(": "-",
")": "-",
"{": "-",
"}": "-",
$: "-v-",
"#": "-h-",
"!": "-i-",
"/": "-s-",
@ -1231,12 +1240,12 @@ if (typeof window !== 'undefined') {
* @param options 配置项
* @returns PostCSS 插件对象
*/
function postcssRemToRpx(options) {
function postcssRemToRpx() {
return {
postcssPlugin: "vite-cool-uniappx-remToRpx",
prepare() {
const handledSelectors = new Set();
const { remUnit = 16, remPrecision = 6, rpxRatio = 2 } = options;
const { remUnit = 16, remPrecision = 6, rpxRatio = 2 } = config.tailwind;
const factor = remUnit * rpxRatio;
return {
Rule(rule) {
@ -1307,16 +1316,8 @@ if (typeof window !== 'undefined') {
/**
* Vite 插件自动转换 .uvue 文件中的 Tailwind 类名为安全字符
* 并自动注入 rem rpx PostCSS 插件
* @param options 配置项
* @returns Vite 插件对象
*/
function tailwindTransformPlugin(options = {}) {
const merged = {
remUnit: 16,
remPrecision: 6,
rpxRatio: 2,
...options,
};
function tailwindTransformPlugin() {
return {
name: "vite-cool-uniappx-tailwind",
enforce: "pre",
@ -1324,7 +1325,7 @@ if (typeof window !== 'undefined') {
return {
css: {
postcss: {
plugins: [postcssRemToRpx(merged)],
plugins: [postcssRemToRpx()],
},
},
};
@ -1368,9 +1369,11 @@ if (typeof window !== 'undefined') {
* @param options 配置项
* @returns Vite 插件数组
*/
function uniappX(options) {
function uniappX() {
if (config.type == "uniapp-x") {
return [tailwindTransformPlugin(options?.tailwind)];
if (config.tailwind.enable) {
return [tailwindTransformPlugin()];
}
}
return [];
}
@ -1404,6 +1407,10 @@ if (typeof window !== 'undefined') {
lodash.merge(config.eps.mapping, mapping);
}
}
// tailwind
if (options.tailwind) {
lodash.assign(config.tailwind, options.tailwind);
}
return [base(), virtual(), uniappX(), demo(options.demo)];
}

View File

@ -1,38 +1,7 @@
interface PostcssRemToRpxOptions {
remUnit?: number;
remPrecision?: number;
rpxRatio?: number;
}
interface TailwindTransformOptions extends PostcssRemToRpxOptions {
}
import type { Plugin } from "vite";
/**
* uniappX Tailwind
* @param options
* @returns Vite
*/
export declare function uniappX(options?: {
tailwind?: TailwindTransformOptions;
}): {
name: string;
enforce: "pre";
config(): {
css: {
postcss: {
plugins: {
postcssPlugin: string;
prepare(): {
Rule(rule: any): void;
Declaration(decl: any): void;
};
}[];
};
};
};
transform(code: string, id: string): {
code: string;
map: {
mappings: string;
};
} | null;
}[];
export {};
export declare function uniappX(): Plugin<any>[];

View File

@ -41,4 +41,10 @@ export const config: Config.Data = {
svg: {
skipNames: ["base"],
},
tailwind: {
enable: true,
remUnit: 16,
remPrecision: 6,
rpxRatio: 2,
},
};

View File

@ -45,5 +45,10 @@ export function cool(options: Config.Options) {
}
}
// tailwind
if (options.tailwind) {
assign(config.tailwind, options.tailwind);
}
return [base(), virtual(), uniappX(), demo(options.demo)];
}

View File

@ -1,6 +1,8 @@
// @ts-ignore
import valueParser from "postcss-value-parser";
import { config } from "../config";
import type { Plugin } from "vite";
import { Config } from "../../types";
/**
* Tailwind CSS
@ -11,6 +13,9 @@ const TAILWIND_SAFE_CHAR_MAP: Record<string, string> = {
"]": "-",
"(": "-",
")": "-",
"{": "-",
"}": "-",
$: "-v-",
"#": "-h-",
"!": "-i-",
"/": "-s-",
@ -202,23 +207,17 @@ function rgbToRgba(value: string): string {
return value;
}
interface PostcssRemToRpxOptions {
remUnit?: number;
remPrecision?: number;
rpxRatio?: number;
}
/**
* PostCSS rem rpx Tailwind
* @param options
* @returns PostCSS
*/
function postcssRemToRpx(options: PostcssRemToRpxOptions) {
function postcssRemToRpx() {
return {
postcssPlugin: "vite-cool-uniappx-remToRpx",
prepare() {
const handledSelectors = new Set<string>();
const { remUnit = 16, remPrecision = 6, rpxRatio = 2 } = options;
const { remUnit = 16, remPrecision = 6, rpxRatio = 2 } = config.tailwind;
const factor = remUnit * rpxRatio;
return {
@ -289,37 +288,26 @@ function postcssRemToRpx(options: PostcssRemToRpxOptions) {
}
postcssRemToRpx.postcss = true;
interface TailwindTransformOptions extends PostcssRemToRpxOptions {}
/**
* Vite .uvue Tailwind
* rem rpx PostCSS
* @param options
* @returns Vite
*/
function tailwindTransformPlugin(options: TailwindTransformOptions = {}) {
const merged: Required<TailwindTransformOptions> = {
remUnit: 16,
remPrecision: 6,
rpxRatio: 2,
...options,
};
function tailwindTransformPlugin() {
return {
name: "vite-cool-uniappx-tailwind",
enforce: "pre" as const,
enforce: "pre",
config() {
return {
css: {
postcss: {
plugins: [postcssRemToRpx(merged)],
plugins: [postcssRemToRpx()],
},
},
};
},
transform(code: string, id: string) {
transform(code, id) {
if (!id.includes(".uvue")) return null;
let resultCode = code;
@ -353,7 +341,7 @@ function tailwindTransformPlugin(options: TailwindTransformOptions = {}) {
}
return null;
},
};
} as Plugin;
}
/**
@ -361,9 +349,11 @@ function tailwindTransformPlugin(options: TailwindTransformOptions = {}) {
* @param options
* @returns Vite
*/
export function uniappX(options?: { tailwind?: TailwindTransformOptions }) {
export function uniappX() {
if (config.type == "uniapp-x") {
return [tailwindTransformPlugin(options?.tailwind)];
if (config.tailwind.enable) {
return [tailwindTransformPlugin()];
}
}
return [];

View File

@ -106,6 +106,17 @@ export declare namespace Config {
// 跳过拼接模块名
skipNames?: string[];
};
// tailwind
tailwind?: {
// 是否开启tailwind
enable?: boolean;
// 根元素字体大小
remUnit?: number;
// 小数位数
remPrecision?: number;
// 转换比例
rpxRatio?: number;
};
}
interface Data {
type: Type;