Skip to content

Commit dde88b6

Browse files
authored
enhancement: spread props coming from plugin options (#38)
1 parent 0d6c7aa commit dde88b6

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

index.js

+23-25
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,26 @@ const glob = require('tiny-glob/sync');
1818

1919
const processed = new WeakSet();
2020

21-
const getAdaptivePropSelector = (userProps) => {
21+
const getAdaptivePropSelector = (selector) => {
2222
return (prop) => {
23-
if (!userProps || !userProps.adaptive_prop_selector) {
23+
if (!selector) {
2424
return `${prop}-@media:dark`
2525
}
2626

27-
return `${prop}${userProps.adaptive_prop_selector}`
27+
return `${prop}${selector}`
2828
}
2929
}
3030

3131
/** @type { import('postcss').PluginCreator<any> }*/
32-
module.exports = (UserProps) => {
32+
module.exports = (options) => {
33+
const { files, adaptive_prop_selector, custom_selector, layer, ...props } = options
34+
3335
const FilePropsCache = new Map();
3436

3537
return {
3638
postcssPlugin: 'postcss-jit-props',
3739
prepare() {
38-
const UserPropsCopy = JSON.parse(JSON.stringify(UserProps));
40+
const UserProps = { ...props }
3941

4042
const STATE = {
4143
mapped: null, // track prepended props
@@ -48,23 +50,23 @@ module.exports = (UserProps) => {
4850
target_media_dark: null, // dark media query props
4951
}
5052

51-
const adaptivePropSelector = getAdaptivePropSelector(UserProps)
53+
const adaptivePropSelector = getAdaptivePropSelector(adaptive_prop_selector)
5254

5355
return {
5456
Once(node, { parse, result, Rule, AtRule }) {
55-
let target_selector = ':root'
57+
let target_selector = custom_selector || ':root'
5658

57-
if (!Object.keys(UserPropsCopy).length) {
59+
if (!files && !Object.keys(props).length) {
5860
return console.warn('postcss-jit-props: Variable source(s) not passed.')
5961
}
6062

61-
if (UserPropsCopy?.files?.length) {
63+
if (files?.length) {
6264

63-
const files = UserPropsCopy?.files
65+
const globs = files
6466
.map((file) => glob(file))
6567
.reduce((flattenedFileList, files) => flattenedFileList.concat(files), [])
6668

67-
files.map(file => {
69+
globs.map(file => {
6870
result.messages.push({
6971
type: 'dependency',
7072
plugin: 'postcss-jit-props',
@@ -82,7 +84,7 @@ module.exports = (UserProps) => {
8284
if (FilePropsCache.has(fileCacheKey)) {
8385
const fileProps = FilePropsCache.get(fileCacheKey)
8486
for (const [key, value] of fileProps) {
85-
UserPropsCopy[key] = value
87+
UserProps[key] = value
8688
}
8789

8890
return
@@ -95,39 +97,35 @@ module.exports = (UserProps) => {
9597

9698
dependencyResult.walkDecls(decl => {
9799
if (!decl.variable) return
98-
UserPropsCopy[decl.prop] = decl.value
100+
UserProps[decl.prop] = decl.value
99101
fileProps.set(decl.prop, decl.value)
100102
})
101103

102104
dependencyResult.walkAtRules(atrule => {
103105
if (atrule.name === 'custom-media') {
104106
let media = atrule.params.slice(0, atrule.params.indexOf(' '))
105-
UserPropsCopy[media] = `@custom-media ${atrule.params};`
107+
UserProps[media] = `@custom-media ${atrule.params};`
106108
fileProps.set(media, `@custom-media ${atrule.params};`)
107109
}
108110
else if (atrule.name === 'keyframes') {
109111
let keyframeName = `--${atrule.params}-@`
110112
let keyframes = atrule.source.input.css.slice(atrule.source.start.offset, atrule.source.end.offset + 1)
111-
UserPropsCopy[keyframeName] = keyframes
113+
UserProps[keyframeName] = keyframes
112114
fileProps.set(keyframeName, keyframes)
113115
}
114116
})
115117
})
116118
}
117119

118-
if (UserPropsCopy?.custom_selector) {
119-
target_selector = UserPropsCopy.custom_selector
120-
}
121-
122120
STATE.mapped = new Set()
123121
STATE.mapped_dark = new Set()
124122

125123
STATE.target_rule = new Rule({ selector: target_selector, source: node.first.source })
126124
STATE.target_rule_dark = new Rule({ selector: target_selector, source: node.first.source })
127125
STATE.target_media_dark = new AtRule({ name: 'media', params: '(prefers-color-scheme: dark)', source: node.first.source })
128126

129-
if (UserPropsCopy?.layer) {
130-
STATE.target_layer = new AtRule({ name: 'layer', params: UserPropsCopy.layer, source: node.first.source })
127+
if (layer) {
128+
STATE.target_layer = new AtRule({ name: 'layer', params: layer, source: node.first.source })
131129
node.root().prepend(STATE.target_layer)
132130
STATE.target_ss = STATE.target_layer
133131
}
@@ -151,7 +149,7 @@ module.exports = (UserProps) => {
151149
STATE.target_ss.prepend(STATE.target_rule)
152150

153151
// lookup prop value from pool
154-
let value = UserPropsCopy[prop] || null
152+
let value = UserProps[prop] || null
155153

156154
// warn if media prop not resolved
157155
if (!value) {
@@ -188,7 +186,7 @@ module.exports = (UserProps) => {
188186
if (STATE.mapped.has(prop)) continue
189187

190188
// lookup prop from options object
191-
let value = UserPropsCopy[prop] || null
189+
let value = UserProps[prop] || null
192190

193191
// warn if props won't resolve from plugin
194192
if (!value) {
@@ -201,7 +199,7 @@ module.exports = (UserProps) => {
201199
STATE.mapped.add(prop)
202200

203201
// lookup keyframes for the prop and append if found
204-
let keyframes = UserPropsCopy[`${prop}-@`]
202+
let keyframes = UserProps[`${prop}-@`]
205203
if (keyframes) {
206204
const keyframesNode = parse(keyframes).first
207205
keyframesNode.source = node.source
@@ -210,7 +208,7 @@ module.exports = (UserProps) => {
210208
}
211209

212210
// lookup dark adaptive prop and append if found
213-
let adaptive = UserPropsCopy[adaptivePropSelector(prop)]
211+
let adaptive = UserProps[adaptivePropSelector(prop)]
214212
if (adaptive && !STATE.mapped_dark.has(prop)) {
215213
// create @media ... { :root {} } context just in time
216214
if (STATE.mapped_dark.size === 0) {

0 commit comments

Comments
 (0)