Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make rule-value-function and global work together #1427

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions packages/jss-plugin-rule-value-function/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
{
"jss-plugin-rule-value-function.js": {
"bundled": 2958,
"minified": 1046,
"gzipped": 579
"bundled": 3816,
"minified": 1409,
"gzipped": 750
},
"jss-plugin-rule-value-function.min.js": {
"bundled": 2379,
"minified": 795,
"gzipped": 455
"bundled": 3237,
"minified": 1158,
"gzipped": 631
},
"jss-plugin-rule-value-function.cjs.js": {
"bundled": 2526,
"minified": 1058,
"gzipped": 551
"bundled": 3100,
"minified": 1341,
"gzipped": 674
},
"jss-plugin-rule-value-function.esm.js": {
"bundled": 2240,
"minified": 835,
"gzipped": 452,
"bundled": 2731,
"minified": 1062,
"gzipped": 569,
"treeshaked": {
"rollup": {
"code": 33,
"import_statements": 33
"code": 76,
"import_statements": 76
},
"webpack": {
"code": 1071
"code": 1147
}
}
}
Expand Down
15 changes: 14 additions & 1 deletion packages/jss-plugin-rule-value-function/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import warning from 'tiny-warning'
import {
createRule,
RuleList,
type Rule,
type JssStyle,
type RuleOptions,
Expand All @@ -22,7 +23,19 @@ export default function functionPlugin() {
onCreateRule(name?: string, decl: JssStyle, options: RuleOptions): Rule | null {
if (typeof decl !== 'function') return null
const rule: StyleRuleWithRuleFunction = (createRule(name, {}, options): any)
rule[fnRuleNs] = decl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was how we hide the implementation detail of the interface that only this plugin knows how to handle

if (rule.type == 'global')
rule.updateFun = data => {
// compute styles
let styles = decl(data)
// Build a new RuleList that replaces the former one
rule.rules = new RuleList({
...rule.options,
parent: rule
})
for (const selector in styles) rule.rules.add(selector, styles[selector])
rule.rules.process()
}
else rule[fnRuleNs] = decl
return rule
},

Expand Down
28 changes: 14 additions & 14 deletions packages/jss/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
{
"jss.js": {
"bundled": 63033,
"minified": 23299,
"gzipped": 7090
"bundled": 63305,
"minified": 23353,
"gzipped": 7113
},
"jss.min.js": {
"bundled": 61636,
"minified": 22531,
"gzipped": 6743
"bundled": 61908,
"minified": 22585,
"gzipped": 6766
},
"jss.cjs.js": {
"bundled": 58745,
"minified": 26132,
"gzipped": 7181
"bundled": 59011,
"minified": 26186,
"gzipped": 7209
},
"jss.esm.js": {
"bundled": 57127,
"minified": 24833,
"gzipped": 7000,
"bundled": 57393,
"minified": 24887,
"gzipped": 7028,
"treeshaked": {
"rollup": {
"code": 20291,
"code": 20345,
"import_statements": 345
},
"webpack": {
"code": 21773
"code": 21827
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions packages/jss/src/RuleList.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,22 @@ export default class RuleList {
sheet
} = this.options

// updateFun is a workaround to use together
// jss-plugin-rule-value-function and jss-plugin-global
const hasUpdateFun = typeof rule.updateFun == 'function'

// It is a rules container like for e.g. ConditionalRule.
if (rule.rules instanceof RuleList) {
// Ignore if there is an updateFun
if (!hasUpdateFun && rule.rules instanceof RuleList) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

breaks encapsulation between plugins and the core, core has no knowledge about style functions, so far we managed to run the plugin in a way that doesn't require this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the idea is that the plugin reacts to onUpdate hook and calls the function

rule.rules.update(data, options)
return
}

const styleRule: StyleRule = (rule: any)
const {style} = styleRule

plugins.onUpdate(data, rule, sheet, options)
if (hasUpdateFun) rule.updateFun(data)
else plugins.onUpdate(data, rule, sheet, options)

// We rely on a new `style` ref in case it was mutated during onUpdate hook.
if (options.process && style && style !== styleRule.style) {
Expand Down