-
Notifications
You must be signed in to change notification settings - Fork 54
Add plugins #75
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
Open
treygriffith
wants to merge
8
commits into
segmentio:master
Choose a base branch
from
treygriffith:plugins
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add plugins #75
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
df324ac
add plugins for processing selectors and rules
treygriffith 763bf94
bump minor version to reflect new functionality
treygriffith da00208
Revert "bump minor version to reflect new functionality"
treygriffith 9226188
rename plugins.reset() to plugins.clear()
treygriffith cc63ccd
add JSDoc to new plugin functions
treygriffith 318e26c
remove extraneous object creation
treygriffith efcdd8f
handle plugin that return obviously invalid rules
treygriffith a48526d
Merge branch 'master' into plugins
treygriffith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { Rule, isRule } from './prefixer' | ||
| import hasOwnProperty from './utils/has-own-property' | ||
|
|
||
| export interface RuleSet { | ||
| selector: string, | ||
| rules: Rule[] | ||
| } | ||
|
|
||
| type Plugin = (set: RuleSet) => RuleSet | ||
|
|
||
| const plugins: Plugin[] = [] | ||
|
|
||
| function isRuleSet (set: unknown): set is RuleSet { | ||
| if (typeof set !== 'object' || set === null) { | ||
| return false | ||
| } | ||
| if (!hasOwnProperty(set, 'selector') || | ||
| typeof set.selector !== 'string') { | ||
| return false | ||
| } | ||
| if (!hasOwnProperty(set, 'rules')) { | ||
| return false | ||
| } | ||
|
|
||
| const rules = set.rules | ||
|
|
||
| return Array.isArray(rules) && rules.every(isRule) | ||
| } | ||
|
|
||
| /** | ||
| * Adds a plugin that ui-box will apply before emitting styles into a stylesheet. | ||
| * Plugins are applied in the *opposite* order of insertion (most recent `use` first). | ||
| */ | ||
| export function use(plugin: Plugin): void { | ||
| plugins.unshift(plugin) | ||
| } | ||
|
|
||
| /** | ||
| * Applies the added plugins to a given set of CSS rules. | ||
| * Should be used only internally by ui-box, not by plugin authors or consumers. | ||
| */ | ||
| export function apply(set: RuleSet) { | ||
| let newSet = set | ||
|
|
||
| for (const plugin of plugins) { | ||
| const updatedSet = plugin({...newSet}) | ||
|
|
||
| // Skip broken plugins | ||
| if (isRuleSet(updatedSet)) { | ||
| newSet = updatedSet | ||
| } else if (process.env.NODE_ENV !== 'production') { | ||
| throw new Error(`📦 ui-box: Plugin "${plugin.name}" returned an invalid RuleSet.`) | ||
| } | ||
| } | ||
|
|
||
| return newSet | ||
| } | ||
|
|
||
| /** | ||
| * Removes all previously added plugins. | ||
| */ | ||
| export function clear(): void { | ||
| plugins.length = 0 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| /** | ||
| * Test existence of a property on an object in a Typescript-friendly way | ||
| */ | ||
| export default function hasOwnProperty<UnknownObject extends {}, Prop extends PropertyKey> | ||
| (obj: UnknownObject, prop: Prop): obj is UnknownObject & Record<Prop, unknown> { | ||
| return Object.prototype.hasOwnProperty.call(obj, prop) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import test from 'ava' | ||
| import * as plugins from '../src/plugins' | ||
|
|
||
| const originalNodeEnv = process.env.NODE_ENV | ||
| test.afterEach.always(() => { | ||
| process.env.NODE_ENV = originalNodeEnv | ||
| plugins.clear() | ||
| }) | ||
|
|
||
| test('applies a selector prefix to the styles', t => { | ||
| const ruleset = { | ||
| selector: '.ub-min-w_10px', | ||
| rules: [ | ||
| { property: 'min-width', value: '10px' }, | ||
| { property: 'user-select', value: 'none' } | ||
| ] | ||
| } | ||
| plugins.use(({ selector, rules }) => { | ||
| return { | ||
| selector: `#my-div ${selector}`, | ||
| rules | ||
| } | ||
| }) | ||
| const result = plugins.apply(ruleset) | ||
| t.deepEqual(result, { | ||
| selector: '#my-div .ub-min-w_10px', | ||
| rules: [ | ||
| { property: 'min-width', value: '10px' }, | ||
| { property: 'user-select', value: 'none' } | ||
| ] | ||
| }) | ||
| }) | ||
|
|
||
| test('removes plugins', t => { | ||
| const ruleset = { | ||
| selector: '.ub-min-w_10px', | ||
| rules: [ | ||
| { property: 'min-width', value: '10px' }, | ||
| { property: 'user-select', value: 'none' } | ||
| ] | ||
| } | ||
| plugins.use(({ selector, rules }) => { | ||
| return { | ||
| selector: `#my-div ${selector}`, | ||
| rules | ||
| } | ||
| }) | ||
| plugins.clear() | ||
| const result = plugins.apply(ruleset) | ||
| t.deepEqual(result, ruleset) | ||
| }) | ||
|
|
||
| test('errors on plugins in dev that return invalid rules', t => { | ||
| const ruleset = { | ||
| selector: '.ub-min-w_10px', | ||
| rules: [ | ||
| { property: 'min-width', value: '10px' }, | ||
| { property: 'user-select', value: 'none' } | ||
| ] | ||
| } | ||
| plugins.use(({ selector, rules }) => { | ||
| return { | ||
| selector: `#my-div ${selector}`, | ||
| rules: { bad: 'rule' } | ||
| } | ||
| }) | ||
| t.throws(() => plugins.apply(ruleset), /invalid RuleSet/) | ||
| }) | ||
|
|
||
| test('skips plugins in production that return invalid rules', t => { | ||
| process.env.NODE_ENV = 'production' | ||
| const ruleset = { | ||
| selector: '.ub-min-w_10px', | ||
| rules: [ | ||
| { property: 'min-width', value: '10px' }, | ||
| { property: 'user-select', value: 'none' } | ||
| ] | ||
| } | ||
| plugins.use(({ selector, rules }) => { | ||
| return { | ||
| selector: `#bad-div ${selector}`, | ||
| rules: { bad: 'rule' } | ||
| } | ||
| }) | ||
| plugins.use(({ selector, rules }) => { | ||
| return { | ||
| selector: `#my-div ${selector}`, | ||
| rules | ||
| } | ||
| }) | ||
| const result = plugins.apply(ruleset) | ||
| t.deepEqual(result, { | ||
| selector: '#my-div .ub-min-w_10px', | ||
| rules: [ | ||
| { property: 'min-width', value: '10px' }, | ||
| { property: 'user-select', value: 'none' } | ||
| ] | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.