Skip to content

Commit e4c046f

Browse files
committed
fix: comment might be undefined
1 parent 7ad4259 commit e4c046f

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface ConvertUnit {
2222
}
2323

2424
export type PxtoremOptions = Partial<{
25-
rootValue: number | ((input: Input) => number)
25+
rootValue: number | ((input: Input | undefined) => number)
2626
unitToConvert: string
2727
unitPrecision: number
2828
selectorBlackList: (string | RegExp)[]
@@ -71,10 +71,10 @@ function pxtorem(options?: PxtoremOptions) {
7171
originOpts: ORIGINAL_OPTIONS,
7272
}
7373

74-
setupCurrentOptions(h as any, firstNode)
74+
setupCurrentOptions(h as any, { node, comment: firstNode })
7575
},
7676
Comment(node, h) {
77-
setupCurrentOptions(h as any, node)
77+
setupCurrentOptions(h as any, { node, comment: node })
7878
},
7979
CommentExit(comment) {
8080
if (comment.text.match(isPxtoremReg)?.length) {

src/utils/index.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AtRule, ChildNode, Comment, Container, Declaration, Rule } from 'postcss'
1+
import type { AtRule, ChildNode, Comment, Container, Declaration, Root, Rule } from 'postcss'
22
import type { ConvertUnit, PxtoremOptions } from '..'
33
import { defaultOptions } from '..'
44
import { MAYBE_REGEXP } from './constant'
@@ -202,15 +202,24 @@ export type H = {
202202
}
203203
}
204204

205-
export function setupCurrentOptions(h: H, node: Comment | ChildNode) {
205+
export function setupCurrentOptions(
206+
h: H,
207+
{
208+
node,
209+
comment,
210+
}: {
211+
node?: ChildNode | Root
212+
comment?: ChildNode | Comment
213+
},
214+
) {
206215
const opts = h[currentOptions].originOpts
207216

208217
const filePath = node?.source?.input.file
209218

210-
if ((node as Comment)?.text) {
219+
if (isOptionComment(comment)) {
211220
h[currentOptions].originOpts = {
212221
...opts,
213-
...getOptionsFromComment(node as Comment, opts.parseOptions),
222+
...getOptionsFromComment(comment, opts.parseOptions),
214223
}
215224
}
216225

@@ -223,7 +232,7 @@ export function setupCurrentOptions(h: H, node: Comment | ChildNode) {
223232
return
224233
}
225234

226-
h[currentOptions].rootValue = isFunction(opts.rootValue) ? opts.rootValue(node.source!.input) : opts.rootValue
235+
h[currentOptions].rootValue = isFunction(opts.rootValue) ? opts.rootValue(node?.source!.input) : opts.rootValue
227236

228237
h[currentOptions].pxReplace = createPxReplace(h[currentOptions].rootValue, opts.unitPrecision, opts.minPixelValue)
229238
}

0 commit comments

Comments
 (0)