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

Implements nesting in template string parser #1103

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
10 changes: 6 additions & 4 deletions docs/jss-plugin-template.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
## Enables string templates

Allows you to use string templates to declare CSS rules. It implements a **very naive** but **very fast (~42000 ops/sec)** runtime CSS parser, with certain limitations:
Allows you to use string templates to declare CSS rules. It implements a **simplified** but **very fast** runtime CSS parser, with certain limitations:

- Supports only rule body (no selectors)
- Requires semicolon and a new line after the value (except the last line)
- No nested rules support
- Requires new lines at the end of each declaration.
- Requires a closing curly brace of a nested rule to be on a separate line.

```js
const styles = {
Expand All @@ -14,6 +13,9 @@ const styles = {
color: red;
margin: 20px 40px;
padding: 10px;
&:hover span {
color: green;
}
`,
'@media print': {
button: `color: black`
Expand Down
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']},
Copy link
Member Author

Choose a reason for hiding this comment

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

was broken

reporters: ['benchmark'],
// Some tests are slow.
browserNoActivityTimeout: 20000
Expand Down
24 changes: 12 additions & 12 deletions packages/jss-plugin-template/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"dist/jss-plugin-template.js": {
"bundled": 1777,
"minified": 730,
"gzipped": 453
"bundled": 3019,
"minified": 1136,
"gzipped": 604
},
"dist/jss-plugin-template.min.js": {
"bundled": 1418,
"minified": 564,
"gzipped": 355
"bundled": 2485,
"minified": 859,
"gzipped": 487
},
"dist/jss-plugin-template.cjs.js": {
"bundled": 1341,
"minified": 686,
"gzipped": 423
"bundled": 2611,
"minified": 1165,
"gzipped": 575
},
"dist/jss-plugin-template.esm.js": {
"bundled": 1123,
"minified": 518,
"gzipped": 341,
"bundled": 2388,
"minified": 991,
"gzipped": 496,
"treeshaked": {
"rollup": {
"code": 21,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}))
Copy link
Member Author

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


const css = `
color: rgb(77, 77, 77);
Expand Down
14 changes: 14 additions & 0 deletions packages/jss-plugin-template/benchmark/tests/parse.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import stylis from 'stylis'
import parse from '../../src/parse'

const css = `
Expand Down Expand Up @@ -37,8 +38,21 @@ const css = `
font-variant: normal normal;
border-spacing: 0px;
`

stylis.set({
global: false,
keyframe: false,
prefix: false,
compress: false,
semicolon: true
})

suite('Parse', () => {
benchmark('parse()', () => {
parse(css)
})

benchmark('stylis()', () => {
Copy link
Member Author

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

stylis('#id', css)
})
})
4 changes: 4 additions & 0 deletions packages/jss-plugin-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,9 @@
"@babel/runtime": "^7.3.1",
"jss": "10.0.0-alpha.16",
"tiny-warning": "^1.0.2"
},
"devDependencies": {
"jss-plugin-nested": "^10.0.0-alpha.16",
"stylis": "^3.5.4"
}
}
28 changes: 21 additions & 7 deletions packages/jss-plugin-template/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@
import {type Plugin} from 'jss'
import parse from './parse'

const onProcessRule = rule => {
if (typeof rule.style === 'string') {
// $FlowFixMe: We can safely assume that rule has the style property
rule.style = parse(rule.style)
export const cache = {}

type Options = {|cache: boolean|}

export default function templatePlugin(options: Options = {cache: true}): Plugin {
const onProcessStyle = style => {
if (typeof style !== 'string') {
return style
}

if (style in cache) {
return cache[style]
HenriBeck marked this conversation as resolved.
Show resolved Hide resolved
}

if (options.cache) {
cache[style] = parse(style)
return cache[style]
}

return parse(style)
}
}

export default function templatePlugin(): Plugin {
return {onProcessRule}
return {onProcessStyle}
}
Loading