-
Notifications
You must be signed in to change notification settings - Fork 78
feat: add postcss 8 support #121
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
FRSgit
wants to merge
3
commits into
vuejs:master
Choose a base branch
from
FRSOURCE:master
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
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,106 @@ | ||
import { Root } from 'postcss' | ||
import * as postcss from 'postcss' | ||
import { Root, PluginCreator } from 'postcss' | ||
// postcss-selector-parser does have typings but it's problematic to work with. | ||
const selectorParser = require('postcss-selector-parser') | ||
|
||
export default postcss.plugin('add-id', (options: any) => (root: Root) => { | ||
const id: string = options | ||
const keyframes = Object.create(null) | ||
const pluginFn: PluginCreator<any> = options => ({ | ||
postcssPlugin: 'add-id', | ||
Once(root: Root) { | ||
const id: string = options | ||
const keyframes = Object.create(null) | ||
|
||
root.each(function rewriteSelector(node: any) { | ||
if (!node.selector) { | ||
// handle media queries | ||
if (node.type === 'atrule') { | ||
if (node.name === 'media' || node.name === 'supports') { | ||
node.each(rewriteSelector) | ||
} else if (/-?keyframes$/.test(node.name)) { | ||
// register keyframes | ||
keyframes[node.params] = node.params = node.params + '-' + id | ||
root.each(function rewriteSelector(node: any) { | ||
if (!node.selector) { | ||
// handle media queries | ||
if (node.type === 'atrule') { | ||
if (node.name === 'media' || node.name === 'supports') { | ||
node.each(rewriteSelector) | ||
} else if (/-?keyframes$/.test(node.name)) { | ||
// register keyframes | ||
keyframes[node.params] = node.params = node.params + '-' + id | ||
} | ||
} | ||
return | ||
} | ||
return | ||
} | ||
node.selector = selectorParser((selectors: any) => { | ||
selectors.each((selector: any) => { | ||
let node: any = null | ||
node.selector = selectorParser((selectors: any) => { | ||
selectors.each((selector: any) => { | ||
let node: any = null | ||
|
||
// find the last child node to insert attribute selector | ||
selector.each((n: any) => { | ||
// ">>>" combinator | ||
// and /deep/ alias for >>>, since >>> doesn't work in SASS | ||
if ( | ||
n.type === 'combinator' && | ||
(n.value === '>>>' || n.value === '/deep/') | ||
) { | ||
n.value = ' ' | ||
n.spaces.before = n.spaces.after = '' | ||
return false | ||
} | ||
// find the last child node to insert attribute selector | ||
selector.each((n: any) => { | ||
// ">>>" combinator | ||
// and /deep/ alias for >>>, since >>> doesn't work in SASS | ||
if ( | ||
n.type === 'combinator' && | ||
(n.value === '>>>' || n.value === '/deep/') | ||
) { | ||
n.value = ' ' | ||
n.spaces.before = n.spaces.after = '' | ||
return false | ||
} | ||
|
||
// in newer versions of sass, /deep/ support is also dropped, so add a ::v-deep alias | ||
if (n.type === 'pseudo' && n.value === '::v-deep') { | ||
n.value = n.spaces.before = n.spaces.after = '' | ||
return false | ||
} | ||
// in newer versions of sass, /deep/ support is also dropped, so add a ::v-deep alias | ||
if (n.type === 'pseudo' && n.value === '::v-deep') { | ||
n.value = n.spaces.before = n.spaces.after = '' | ||
return false | ||
} | ||
|
||
if (n.type !== 'pseudo' && n.type !== 'combinator') { | ||
node = n | ||
if (n.type !== 'pseudo' && n.type !== 'combinator') { | ||
node = n | ||
} | ||
}) | ||
|
||
if (node) { | ||
node.spaces.after = '' | ||
} else { | ||
// For deep selectors & standalone pseudo selectors, | ||
// the attribute selectors are prepended rather than appended. | ||
// So all leading spaces must be eliminated to avoid problems. | ||
selector.first.spaces.before = '' | ||
} | ||
|
||
selector.insertAfter( | ||
node, | ||
selectorParser.attribute({ | ||
attribute: id | ||
}) | ||
) | ||
}) | ||
}).processSync(node.selector) | ||
}) | ||
|
||
if (node) { | ||
node.spaces.after = '' | ||
} else { | ||
// For deep selectors & standalone pseudo selectors, | ||
// the attribute selectors are prepended rather than appended. | ||
// So all leading spaces must be eliminated to avoid problems. | ||
selector.first.spaces.before = '' | ||
// If keyframes are found in this <style>, find and rewrite animation names | ||
// in declarations. | ||
// Caveat: this only works for keyframes and animation rules in the same | ||
// <style> element. | ||
if (Object.keys(keyframes).length) { | ||
root.walkDecls(decl => { | ||
// individual animation-name declaration | ||
if (/^(-\w+-)?animation-name$/.test(decl.prop)) { | ||
decl.value = decl.value | ||
.split(',') | ||
.map(v => keyframes[v.trim()] || v.trim()) | ||
.join(',') | ||
} | ||
// shorthand | ||
if (/^(-\w+-)?animation$/.test(decl.prop)) { | ||
decl.value = decl.value | ||
.split(',') | ||
.map(v => { | ||
const vals = v.trim().split(/\s+/) | ||
const i = vals.findIndex(val => keyframes[val]) | ||
if (i !== -1) { | ||
vals.splice(i, 1, keyframes[vals[i]]) | ||
return vals.join(' ') | ||
} else { | ||
return v | ||
} | ||
}) | ||
.join(',') | ||
} | ||
|
||
selector.insertAfter( | ||
node, | ||
selectorParser.attribute({ | ||
attribute: id | ||
}) | ||
) | ||
}) | ||
}).processSync(node.selector) | ||
}) | ||
|
||
// If keyframes are found in this <style>, find and rewrite animation names | ||
// in declarations. | ||
// Caveat: this only works for keyframes and animation rules in the same | ||
// <style> element. | ||
if (Object.keys(keyframes).length) { | ||
root.walkDecls(decl => { | ||
// individual animation-name declaration | ||
if (/^(-\w+-)?animation-name$/.test(decl.prop)) { | ||
decl.value = decl.value | ||
.split(',') | ||
.map(v => keyframes[v.trim()] || v.trim()) | ||
.join(',') | ||
} | ||
// shorthand | ||
if (/^(-\w+-)?animation$/.test(decl.prop)) { | ||
decl.value = decl.value | ||
.split(',') | ||
.map(v => { | ||
const vals = v.trim().split(/\s+/) | ||
const i = vals.findIndex(val => keyframes[val]) | ||
if (i !== -1) { | ||
vals.splice(i, 1, keyframes[vals[i]]) | ||
return vals.join(' ') | ||
} else { | ||
return v | ||
} | ||
}) | ||
.join(',') | ||
} | ||
}) | ||
} | ||
} | ||
}) | ||
|
||
pluginFn.postcss = true | ||
export default pluginFn |
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 |
---|---|---|
@@ -1,11 +1,15 @@ | ||
import { Root } from 'postcss' | ||
import * as postcss from 'postcss' | ||
import { PluginCreator, Rule, AtRule } from 'postcss' | ||
|
||
export default postcss.plugin('trim', () => (css: Root) => { | ||
css.walk(({ type, raws }) => { | ||
if (type === 'rule' || type === 'atrule') { | ||
if (raws.before) raws.before = '\n' | ||
if (raws.after) raws.after = '\n' | ||
} | ||
}) | ||
const trim = function({ raws }: Rule | AtRule) { | ||
if (raws.before) raws.before = '\n' | ||
if (raws.after) raws.after = '\n' | ||
} | ||
|
||
const pluginFn: PluginCreator<void> = () => ({ | ||
postcssPlugin: 'trim', | ||
AtRule: trim, | ||
Rule: trim | ||
}) | ||
|
||
pluginFn.postcss = true | ||
export default pluginFn |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why version
8.0.0
? I would use version^8.2.13
or higher.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can change it if you want, but anything with
^
at the beginning will evaluate to the same version (newest one starting with8.
), so there is no differenceThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Being more specific can be helpful to signal where the actual requirement is, but you're not wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hard to tell whether I should change it or not - would be great to have repo maintainers express their opinion on this one (and the whole pr)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the vulnerability was patched in 8.1.3, IMO we should at least require that version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, bumped to the newest version just now