-
Notifications
You must be signed in to change notification settings - Fork 61
Add opts.preserveInjectedVariables
#74
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
Add opts.preserveInjectedVariables
#74
Conversation
Was this inspired by the conversation here https://gitter.im/postcss/postcss?at=5b1eae1582b1b355c94e9efa ? cc @killtheliterate |
It was not inspired by that conversation, but probably covers the issue. If I understand the discussion, it looks like there's a use case for "global vars I want everywhere" without duplicating the rules or importing them. This new option would provide a solution in this form:
|
@MadLittleMods Does this option make sense to merge into the mainline project, or should we plan on using a fork to meet our need of stripping js-injected As a maintainer, you might have a better idea how common this use case would be. |
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.
.
index.js
Outdated
variables: {}, | ||
// Remove variables injected via JS with the `variables` option above | ||
// before serializing to CSS | ||
cleanInjectedVariables: false |
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 think we should call this preserveInjectedVariables: true
index.js
Outdated
@@ -106,6 +113,12 @@ module.exports = postcss.plugin('postcss-css-variables', function(options) { | |||
}); | |||
variableRootRule.append(varDecl); | |||
|
|||
// mark JS-injected variables for removal if `cleanInjectedVariables` |
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.
Just above lines 106-114, we insert a root note with the variables. We can probably just add an if-statement if(!opts.preserveInjectedVariables)
instead of having to remove the decls we just added.
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'm waiting until the end to remove injected variable declarations so that they're available in the AST to calculate fallbacks for var()
declarations as expected.
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.
@akdetrick I think the map
should be sufficient though? We don't look in the AST for variables after finding them all in the beginning
test/test.js
Outdated
); | ||
test( | ||
'should preserve var() values and clean injected declarations with `options.variables` AND `options.preserve` AND `options.cleanInjectedVariables`', | ||
'js-defined-preserve-cleanInjected', |
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.
js-defined-preserve-injected-false
@akdetrick Thanks for the contribution. I added some review comments on a alternative approach. It seems like this use-case could be covered by |
README.md
Outdated
@@ -368,6 +368,12 @@ Can be a simple key-value pair or an object with a `value` property and an optio | |||
|
|||
The object keys are automatically prefixed with `--` (according to CSS custom property syntax) if you do not provide it. | |||
|
|||
### `cleanInjectedVariables` (default: `false`) |
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.
preserveInjectedVariables
(default: true
)
README.md
Outdated
@@ -368,6 +368,12 @@ Can be a simple key-value pair or an object with a `value` property and an optio | |||
|
|||
The object keys are automatically prefixed with `--` (according to CSS custom property syntax) if you do not provide it. | |||
|
|||
### `cleanInjectedVariables` (default: `false`) | |||
|
|||
Removes custom property declarations inserted via the `variables` option from final output. |
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.
Whether to preserve the ....
@akdetrick This comment is still outstanding, #74 (comment) |
@MadLittleMods Thanks for your attention on this. Regarding #74 - I agree it's preferable to avoid adding js-injected vars to the AST in the first place, but it may involve a non-trivial refactor of the tools in Consider the // Add the entry to the map
prevVariableMap[variableName] = (prevVariableMap[variableName] || []).concat({
decl: varDecl,
prop: variableName,
calculatedInPlaceValue: variableValue,
isImportant: isImportant,
variablesUsed: [],
parent: variableRootRule, // Expects an AST node
isUnderAtRule: false
}); As the // Add a root node to the AST
var variableRootRule = postcss.rule({ selector: ':root' });
css.root().prepend(variableRootRule);
// Add the variable decl to the root node
var varDecl = postcss.decl({
prop: variableName,
value: variableValue
});
// only append the declaration for the var if `preserveInjectedVariables` is `true`
if (opts.preserveInjectedVariables) {
variableRootRule.append(varDecl);
} This means that
tl;dr |
cleanInjectedVariables
optionopts.preserveInjectedVariables
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.
Small comment touch-ups
index.js
Outdated
@@ -82,6 +85,10 @@ module.exports = postcss.plugin('postcss-css-variables', function(options) { | |||
// We use this because we don't want to modify the AST when we still need to reference these later on | |||
var nodesToRemoveAtEnd = []; | |||
|
|||
// List of declarations injected from `opts.variables` to remove at the end | |||
// if user passes `opts.preserveInjectedVariables` |
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.
// if user passes
opts.preserveInjectedVariables = false
index.js
Outdated
@@ -106,6 +113,12 @@ module.exports = postcss.plugin('postcss-css-variables', function(options) { | |||
}); | |||
variableRootRule.append(varDecl); | |||
|
|||
// mark JS-injected variables for removal if `preserveInjectedVariables` | |||
// option is passed. |
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.
// Colect JS-injected variables for removal if
opts.preserveInjectedVariables = false
@akdetrick Thanks for trying the alternative. We can go with it for now 🙇 |
`opts.preserveInjectedVariables = false` means remove JS injected variables
@akdetrick Thanks for the contribution ❤️ Shipped in |
🙌 thank you! |
This Pull Request updates dependency [postcss-css-variables](https://github.com/MadLittleMods/postcss-css-variables) from `v0.8.1` to `v0.9.0` <details> <summary>Release Notes</summary> ### [`v0.9.0`](https://github.com/MadLittleMods/postcss-css-variables/blob/master/CHANGELOG.md#v090---2018-6-26) [Compare Source](MadLittleMods/postcss-css-variables@v0.8.1...v0.9.0) ### v0.9.0 - 2018-6-26 - Adds `opts.preserveInjectedVariables`, which when set to `false`, removes the `:root { ... }` custom property declarations added via `opts.variables` - Thank you to [@​akdetrick] for the [contribution](`https://github.com/MadLittleMods/postcss-css-variables/pull/74`) --- </details> --- This PR has been generated by [Renovate Bot](https://renovatebot.com).
What is this new option?
This PR adds an option,
preserveInjectedVariables
, which when set tofalse
, removes custom propertydeclarations added via
opts.variables
.Why is this option useful?
The current behavior of
opts.variables
is to write a:root
rule block containing custom property declarations passed fromopts.variables
whenpreserve
is set totrue
.When working with a single stylesheet, this behavior is exactly what you would want when using
the
preserve
option. Custom property declarations are added to output CSS so that preservedvar()
declarations can be resolved by a modern browser.When working with CSS Modules, injected custom properties are repeated in every style module.
This
cleanInjectedVariables
option gives users control over where global custom property definitions live. Users can passopts.variables
sopostcss-css-variables
can resolve fallbacks for older browsers, and provide custom property declarations in a global stylesheet so that modern browsers can resolvevar()
at runtime without the need to repeat custom property declarations in every module/stylesheet transformed by this plugin.Example
Usage in plugin options
someModule.module.css
transformed withpreserveInjectedVariables: true
someModule.module.css
transformed withcleanInjectedVariables: false