-
-
Notifications
You must be signed in to change notification settings - Fork 399
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
Implements nesting in template string parser #1103
base: master
Are you sure you want to change the base?
Conversation
Also cc @eps1lon |
@@ -44,7 +44,7 @@ module.exports = config => { | |||
frameworks: ['benchmark'], | |||
// Using a fixed position for a file name, m.b. should use an args parser later. | |||
files: [process.argv[4] || 'packages/jss/benchmark/**/*.js'], | |||
preprocessors: {'packages/jss/benchmark/**/*.js': ['webpack']}, | |||
preprocessors: {'packages/**/benchmark/**/*.js': ['webpack']}, |
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.
was broken
@@ -4,7 +4,7 @@ import template from '../../src/index' | |||
import parse from '../../src/parse' | |||
|
|||
const options = {Renderer: null} | |||
const jss = create(options).use(template()) | |||
const jss = create(options).use(template({cache: 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.
disabling cache here to have a realistic comparison without caching, I didn't document it, because I don't think user should need it, so for now it's a private one, just for tests
package.json
Outdated
@@ -15,7 +15,7 @@ | |||
"format:ci": "yarn format --list-different", | |||
"test": "karma start --single-run", | |||
"test:watch": "karma start", | |||
"test:bench": "cross-env BENCHMARK=true karma start --single-run", | |||
"test:bench": "cross-env NODE_ENV=production BENCHMARK=true karma start --single-run", |
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.
removes dead from the bench~!
suite('Parse', () => { | ||
benchmark('parse()', () => { | ||
parse(css) | ||
}) | ||
|
||
benchmark('stylis()', () => { |
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.
using stylis parser just to see a comparable other value in perspective
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.
Exciting stuff.
if (openCurlyIndex === -1) { | ||
warning(false, `[JSS] Missing opening curly brace in "${decl}".`) | ||
} | ||
const key = decl.substring(0, openCurlyIndex - 1) |
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.
Should we trim the selector here?
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.
yes
} | ||
const key = decl.substring(0, openCurlyIndex - 1) | ||
const nestedStyle = {} | ||
rules[rules.length - 1][key] = nestedStyle |
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 do we add the key here to the last object?
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.
rules array represents an array of nested styles, each level, separate array entry, so we need to do both, reference the nested style in the parent object and add it to the array to have a pointer to the current style object where declarations will be added next in that pass
|
||
// We are closing a nested rule. | ||
if (decl === '}') { | ||
rules.pop() |
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.
Shouldn't we add the rule when we pop it to the style?
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.
we already did, we are just removing it from the array, since we basically finished adding declarations to that rule
@kof should we merge this? |
Implements #837
I think I found a way to implement it without degrading performance, LOC is still pretty small.
The problem it is solving: you can't express a nested rule with a template string. If you used a template string initially to describe the rule, as soon as you want to add nesting, you will have to rewrite it to objects.