Skip to content

Commit 74baa65

Browse files
committed
feat: extract query-string.parse function
1 parent 9e0c22f commit 74baa65

File tree

5 files changed

+447
-35
lines changed

5 files changed

+447
-35
lines changed

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@
4747
"postcss": ">=8.0.0"
4848
},
4949
"dependencies": {
50-
"query-string": "7.1.3"
50+
"decode-uri-component": "^0.4.1",
51+
"split-on-first": "^3.0.0"
5152
},
5253
"devDependencies": {
5354
"@minko-fe/eslint-config-ts": "^1.2.32",
5455
"@minko-fe/prettier-config": "^1.2.32",
5556
"@minko-fe/tsconfig": "^1.2.32",
57+
"@types/decode-uri-component": "^0.2.0",
5658
"bumpp": "^9.0.0",
5759
"conventional-changelog-cli": "^2.2.2",
5860
"eslint": "^8.36.0",

pnpm-lock.yaml

+16-28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from './utils'
1818
import { getUnitRegexp } from './utils/pixel-unit-regex'
1919
import { disableNextComment } from './utils/constant'
20+
import type { ParseOptions } from './utils/parse-query'
2021

2122
export interface ConvertUnit {
2223
sourceUnit: string | RegExp
@@ -36,6 +37,7 @@ export type PxtoremOptions = Partial<{
3637
exclude: string | RegExp | ((filePath: string) => boolean) | null
3738
disable: boolean
3839
convertUnitOnEnd: ConvertUnit | ConvertUnit[] | false | null
40+
parseOptions: ParseOptions
3941
}>
4042

4143
export const defaultOptions: Required<PxtoremOptions> = {
@@ -51,6 +53,7 @@ export const defaultOptions: Required<PxtoremOptions> = {
5153
exclude: null,
5254
disable: false,
5355
convertUnitOnEnd: null,
56+
parseOptions: {},
5457
}
5558

5659
const postcssPlugin = 'postcss-pxtorem'
@@ -74,7 +77,7 @@ function pxtorem(options?: PxtoremOptions) {
7477
if (isOptionComment(firstNode)) {
7578
opts = {
7679
...opts,
77-
...getOptionsFromComment(firstNode, Warning),
80+
...getOptionsFromComment(firstNode, Warning, opts.parseOptions),
7881
}
7982
}
8083

@@ -160,7 +163,7 @@ function pxtorem(options?: PxtoremOptions) {
160163

161164
opts = {
162165
...opts,
163-
...getOptionsFromComment(node, Warning),
166+
...getOptionsFromComment(node, Warning, opts.parseOptions),
164167
}
165168

166169
const exclude = opts.exclude

src/utils/index.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type { AtRule, ChildNode, Comment, Container, Declaration, Rule, Warning as postcssWarning } from 'postcss'
2-
import queryString from 'query-string'
32
import type { ConvertUnit, PxtoremOptions } from '..'
43
import { defaultOptions } from '..'
54
import { maybeRegExp } from './constant'
65
import { filterPropList } from './filter-prop-list'
6+
import type { ParseOptions } from './parse-query'
7+
import { parse } from './parse-query'
78

89
function reRegExp() {
910
return /^\/((?:\\\/|[^\/])+)\/([imgy]*)$/
@@ -38,7 +39,11 @@ function parseRegExp(maybeRegExpArg: unknown) {
3839

3940
export const isPxtoremReg = /(?<=^pxtorem\?).+/g
4041

41-
export function getOptionsFromComment(comment: Comment, Warning: typeof postcssWarning): PxtoremOptions | undefined {
42+
export function getOptionsFromComment(
43+
comment: Comment,
44+
Warning: typeof postcssWarning,
45+
parseOptions: ParseOptions,
46+
): PxtoremOptions | undefined {
4247
try {
4348
let query = isPxtoremReg.exec(comment.text)?.[0]
4449
const ret: Record<string, any> = {}
@@ -47,11 +52,12 @@ export function getOptionsFromComment(comment: Comment, Warning: typeof postcssW
4752
query = query.replaceAll(/\s+/g, '')
4853

4954
const defaultKeys = Object.keys(defaultOptions)
50-
const parsed = queryString.parse(query, {
55+
const parsed = parse(query, {
5156
parseBooleans: true,
5257
parseNumbers: true,
5358
arrayFormat: 'bracket-separator',
5459
arrayFormatSeparator: '|',
60+
...parseOptions,
5561
})
5662
const RE_REGEXP = reRegExp()
5763
for (const k of Object.keys(parsed)) {
@@ -209,7 +215,7 @@ enum EnumDataType {
209215
}
210216

211217
function is(val: unknown, type: string) {
212-
return toString.call(val) === `[object ${type}]`
218+
return Object.prototype.toString.call(val) === `[object ${type}]`
213219
}
214220
export function isNumber(data: unknown): data is number {
215221
return is(data, EnumDataType.number)

0 commit comments

Comments
 (0)