Skip to content

Commit 8a98d0b

Browse files
committed
feat(px-to-local-var): 更新至0.0.4版本并重构代码
1 parent 044944e commit 8a98d0b

3 files changed

Lines changed: 17 additions & 13 deletions

File tree

packages/postcss/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cherrywind/postcss",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"private": false,
55
"keywords": [
66
"postcss",

packages/postcss/src/px-to-local-var/gen-fixtures.cts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'path';
33
import postcss, { AcceptedPlugin } from 'postcss';
44
import less from 'postcss-less';
55
import scss from 'postcss-scss';
6-
import { PxToRemOptions, postcssPxToRem } from '.';
6+
import { PxToLocalVarOptions, postcssPxToLocalVar } from '.';
77

88
const cssDir = path.join(__dirname, 'css');
99
const cacheDir = path.join(__dirname, 'cache');
@@ -14,7 +14,11 @@ const parsers: Record<string, any> = {
1414
'.scss': scss,
1515
};
1616

17-
async function processFile(file: string, opts: PxToRemOptions, plugin: (options: PxToRemOptions) => AcceptedPlugin) {
17+
async function processFile(
18+
file: string,
19+
opts: PxToLocalVarOptions,
20+
plugin: (options: PxToLocalVarOptions) => AcceptedPlugin,
21+
) {
1822
const ext = path.extname(file);
1923
const content = fs.readFileSync(file, 'utf8');
2024
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
@@ -53,7 +57,7 @@ async function main() {
5357
{
5458
replace: false,
5559
},
56-
postcssPxToRem,
60+
postcssPxToLocalVar,
5761
);
5862
}
5963
}

packages/postcss/src/px-to-local-var/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import { DISABLE_COMMENT_REG } from './lib/constant';
44
import { createPropListMatcher } from './lib/filter-prop-list';
55
import pixelUnitRegex from './lib/pixel-unit-regex';
66

7-
export interface PxToRemOptions {
7+
export interface PxToLocalVarOptions {
88
/**
9-
* rem 的根元素字体大小,计算 rem 值的基准 (rem = px / rootValue)。
9+
* scope rem的根元素字体大小,计算 scope rem 值的基准 (vw = px / rootValue)。
1010
* @default 16
1111
*/
1212
rootValue?: number;
1313
/**
14-
* 转换后 rem 值的小数点位数。
14+
* 转换后 scope rem 值的小数点位数。
1515
* @default 5
1616
*/
1717
unitPrecision?: number;
@@ -52,7 +52,7 @@ export interface PxToRemOptions {
5252
varName?: string;
5353
}
5454

55-
const defaults: Required<PxToRemOptions> = {
55+
const defaults: Required<PxToLocalVarOptions> = {
5656
rootValue: 16,
5757
unitPrecision: 5,
5858
selectorBlackList: [],
@@ -64,7 +64,7 @@ const defaults: Required<PxToRemOptions> = {
6464
varName: '--local-scope-rem',
6565
};
6666

67-
function shouldExclude(exclude: PxToRemOptions['exclude'], file?: string) {
67+
function shouldExclude(exclude: PxToLocalVarOptions['exclude'], file?: string) {
6868
if (!exclude || !file) return false;
6969
if (typeof exclude === 'function') return exclude(file);
7070
if (exclude instanceof RegExp) return exclude.test(file);
@@ -75,7 +75,7 @@ function toFixed(number: number, precision: number) {
7575
return parseFloat(number.toFixed(precision)).toString();
7676
}
7777

78-
function createPxReplace(opts: Required<PxToRemOptions>) {
78+
function createPxReplace(opts: Required<PxToLocalVarOptions>) {
7979
return function (m: string, $1: string) {
8080
if (!$1) return m;
8181
const pixels = parseFloat($1);
@@ -108,13 +108,13 @@ function hasDisableNextLineComment(decl: Declaration): boolean {
108108
return false;
109109
}
110110

111-
export const postcssPxToRem = (options: PxToRemOptions = {}): Plugin => {
111+
export const postcssPxToLocalVar = (options: PxToLocalVarOptions = {}): Plugin => {
112112
const opts = { ...defaults, ...options };
113113
const propListMatch = createPropListMatcher(opts.propList);
114114
const isBlackSelector = createSelectorBlackList(opts.selectorBlackList);
115115

116116
return {
117-
postcssPlugin: 'postcss-px-to-rem',
117+
postcssPlugin: 'postcss-px-to-local-var',
118118
Once(root: Root) {
119119
const file = root.source?.input.file;
120120
if (shouldExclude(opts.exclude, file)) return;
@@ -145,4 +145,4 @@ export const postcssPxToRem = (options: PxToRemOptions = {}): Plugin => {
145145
},
146146
};
147147
};
148-
postcssPxToRem.postcss = true;
148+
postcssPxToLocalVar.postcss = true;

0 commit comments

Comments
 (0)