|
| 1 | +import juice from 'juice' |
| 2 | +import postcss from 'postcss' |
| 3 | +import calc from 'postcss-calc' |
| 4 | + |
| 5 | +// 定义正则表达式 |
| 6 | +// Define regular expression |
| 7 | +const VARIABLE_DEFINITION_REGEX = /(--[\w-]+)\s*:\s*([^;]+);/g |
| 8 | +const VARIABLE_USAGE_REGEX = /var\((\s*--[a-zA-Z0-9-_]+\s*)(?:\)|,\s*(.*)\))/g |
| 9 | +const REM_UNIT_REGEX = /([\d.]+)rem/g |
| 10 | +const CLASS_ATTRIBUTE_REGEX = / class="[^"]*"/g |
| 11 | + |
| 12 | +/** |
| 13 | + * 将 CSS 变量转换为静态值 |
| 14 | + * Convert CSS variables to static values |
| 15 | + * @param {string} style |
| 16 | + * @returns {string} |
| 17 | + */ |
| 18 | +const handleCssVariables = (style: string): string => { |
| 19 | + const variableDefinitions = new Map<string, string>() |
| 20 | + let styleWithoutDefinitions = style.replace( |
| 21 | + VARIABLE_DEFINITION_REGEX, |
| 22 | + (_, variable, value) => { |
| 23 | + variableDefinitions.set(variable.trim(), value.trim()) |
| 24 | + return '' |
| 25 | + } |
| 26 | + ) |
| 27 | + |
| 28 | + let maxCycles = 1000 |
| 29 | + while (VARIABLE_USAGE_REGEX.test(styleWithoutDefinitions) && maxCycles > 0) { |
| 30 | + maxCycles-- |
| 31 | + styleWithoutDefinitions = styleWithoutDefinitions.replace( |
| 32 | + VARIABLE_USAGE_REGEX, |
| 33 | + (_, variable, fallback) => { |
| 34 | + const key = variable.trim() |
| 35 | + return variableDefinitions.get(key) || fallback?.trim() || '' |
| 36 | + } |
| 37 | + ) |
| 38 | + } |
| 39 | + |
| 40 | + if (maxCycles <= 0) { |
| 41 | + throw new Error('Max Cycles for replacement exceeded') |
| 42 | + } |
| 43 | + |
| 44 | + return styleWithoutDefinitions || '' |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * 将 CSS 处理并内联到 HTML 中 |
| 49 | + * Process and inline CSS into HTML |
| 50 | + * @param {string} html |
| 51 | + * @param {string} css |
| 52 | + * @param {boolean} remToPx 是否将 rem 转换为 px / Convert CSS units from rem to px? |
| 53 | + * @returns {string} |
| 54 | + */ |
| 55 | +const inlineStyles = ( |
| 56 | + html: string, |
| 57 | + css: string, |
| 58 | + remToPx: boolean = true |
| 59 | +): string => { |
| 60 | + // 将 CSS 单位 rem 转换为 px |
| 61 | + // Convert CSS units from rem to px |
| 62 | + const basePx = 16 |
| 63 | + let cssWithUnitConversion = css |
| 64 | + if (remToPx) { |
| 65 | + cssWithUnitConversion = css.replace( |
| 66 | + REM_UNIT_REGEX, |
| 67 | + (_, value) => `${parseFloat(value) * basePx}px` |
| 68 | + ) |
| 69 | + } |
| 70 | + |
| 71 | + // 将 CSS 变量转换为常量 |
| 72 | + // Convert CSS variables to static |
| 73 | + const cssWithoutVariables = handleCssVariables(cssWithUnitConversion) |
| 74 | + |
| 75 | + // 简化 calc() 表达式 |
| 76 | + // Simplify calc() expressions |
| 77 | + const simplifiedCss = postcss().use(calc({})).process(cssWithoutVariables).css |
| 78 | + |
| 79 | + // 将 CSS 内联到 HTML 中 |
| 80 | + // Inline CSS into HTML |
| 81 | + const htmlWithInlinedCss = juice(html, { |
| 82 | + extraCss: simplifiedCss |
| 83 | + }) |
| 84 | + |
| 85 | + // 移除 HTML 中的 class 属性 |
| 86 | + // remove class attributes from HTML |
| 87 | + const htmlWithoutClassAttributes = htmlWithInlinedCss.replace( |
| 88 | + CLASS_ATTRIBUTE_REGEX, |
| 89 | + '' |
| 90 | + ) |
| 91 | + |
| 92 | + return htmlWithoutClassAttributes |
| 93 | +} |
| 94 | + |
| 95 | +export default inlineStyles |
0 commit comments