From f54811c675f9409587a202cd6d5eb55eb1c1209b Mon Sep 17 00:00:00 2001 From: John Otander Date: Thu, 11 Jan 2018 07:58:00 -0700 Subject: [PATCH] Use styled-component template syntax In order to support SSR, styled-components requires string templates to be used since that's what the babel plugin searches for. https://github.com/styled-components/babel-plugin-styled-components/blob/master/src/index.js#L16 --- c8r/lab#17 --- lib/templates/styledComponents.js | 16 ++++++++------ test/snapshots/modules.js.md | 34 ++++++++++++++++++------------ test/snapshots/modules.js.snap | Bin 1060 -> 1174 bytes test/snapshots/templates.js.md | 29 ++++++++++++------------- test/snapshots/templates.js.snap | Bin 1006 -> 1031 bytes 5 files changed, 43 insertions(+), 36 deletions(-) diff --git a/lib/templates/styledComponents.js b/lib/templates/styledComponents.js index 8d92deb..b8de8f5 100644 --- a/lib/templates/styledComponents.js +++ b/lib/templates/styledComponents.js @@ -4,6 +4,7 @@ module.exports = ({ styleString, propsString, systemFunctions, + systemExpressions = '', extensionImport }) => `import styled from 'styled-components' import { @@ -15,13 +16,14 @@ import { } from 'styled-system' ${extensionImport} -const ${name} = styled(${type})([], props => (${styleString}), - space, - fontSize, - width, - color, - ${systemFunctions} -) +const ${name} = styled(${type})\` + \${styleString} + \${space} + \${fontSize} + \${width} + \${color} + ${systemExpressions} +\` ${name}.defaultProps = ${propsString} diff --git a/test/snapshots/modules.js.md b/test/snapshots/modules.js.md index 6b016f6..8c9992e 100644 --- a/test/snapshots/modules.js.md +++ b/test/snapshots/modules.js.md @@ -196,6 +196,8 @@ Generated by [AVA](https://ava.li). value: true␊ });␊ ␊ + var _templateObject = _taggedTemplateLiteral(['\\n ', '\\n ', '\\n ', '\\n ', '\\n ', '\\n \\n'], ['\\n ', '\\n ', '\\n ', '\\n ', '\\n ', '\\n \\n']);␊ + ␊ var _styledComponents = require('styled-components');␊ ␊ var _styledComponents2 = _interopRequireDefault(_styledComponents);␊ @@ -204,9 +206,9 @@ Generated by [AVA](https://ava.li). ␊ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }␊ ␊ - var Box = (0, _styledComponents2.default)('div')([], function (props) {␊ - return {};␊ - }, _styledSystem.space, _styledSystem.fontSize, _styledSystem.width, _styledSystem.color);␊ + function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }␊ + ␊ + var Box = (0, _styledComponents2.default)('div')(_templateObject, styleString, _styledSystem.space, _styledSystem.fontSize, _styledSystem.width, _styledSystem.color);␊ ␊ Box.defaultProps = {};␊ ␊ @@ -223,6 +225,8 @@ Generated by [AVA](https://ava.li). value: true␊ });␊ ␊ + var _templateObject = _taggedTemplateLiteral(['\\n ', '\\n ', '\\n ', '\\n ', '\\n ', '\\n \\n'], ['\\n ', '\\n ', '\\n ', '\\n ', '\\n ', '\\n \\n']);␊ + ␊ var _styledComponents = require('styled-components');␊ ␊ var _styledComponents2 = _interopRequireDefault(_styledComponents);␊ @@ -231,9 +235,9 @@ Generated by [AVA](https://ava.li). ␊ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }␊ ␊ - var Text = (0, _styledComponents2.default)('p')([], function (props) {␊ - return {};␊ - }, _styledSystem.space, _styledSystem.fontSize, _styledSystem.width, _styledSystem.color);␊ + function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }␊ + ␊ + var Text = (0, _styledComponents2.default)('p')(_templateObject, styleString, _styledSystem.space, _styledSystem.fontSize, _styledSystem.width, _styledSystem.color);␊ ␊ Text.defaultProps = {␊ m: 0␊ @@ -287,6 +291,8 @@ Generated by [AVA](https://ava.li). value: true␊ });␊ ␊ + var _templateObject = _taggedTemplateLiteral(['\\n ', '\\n ', '\\n ', '\\n ', '\\n ', '\\n \\n'], ['\\n ', '\\n ', '\\n ', '\\n ', '\\n ', '\\n \\n']);␊ + ␊ var _styledComponents = require('styled-components');␊ ␊ var _styledComponents2 = _interopRequireDefault(_styledComponents);␊ @@ -299,9 +305,9 @@ Generated by [AVA](https://ava.li). ␊ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }␊ ␊ - var Beep = (0, _styledComponents2.default)(_Box2.default)([], function (props) {␊ - return {};␊ - }, _styledSystem.space, _styledSystem.fontSize, _styledSystem.width, _styledSystem.color);␊ + function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }␊ + ␊ + var Beep = (0, _styledComponents2.default)(_Box2.default)(_templateObject, styleString, _styledSystem.space, _styledSystem.fontSize, _styledSystem.width, _styledSystem.color);␊ ␊ Beep.defaultProps = {␊ p: 3,␊ @@ -321,6 +327,8 @@ Generated by [AVA](https://ava.li). value: true␊ });␊ ␊ + var _templateObject = _taggedTemplateLiteral(['\\n ', '\\n ', '\\n ', '\\n ', '\\n ', '\\n ', '\\n', '\\n', '\\n', '\\n'], ['\\n ', '\\n ', '\\n ', '\\n ', '\\n ', '\\n ', '\\n', '\\n', '\\n', '\\n']);␊ + ␊ var _styledComponents = require('styled-components');␊ ␊ var _styledComponents2 = _interopRequireDefault(_styledComponents);␊ @@ -333,11 +341,9 @@ Generated by [AVA](https://ava.li). ␊ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }␊ ␊ - var Flex = (0, _styledComponents2.default)(_Box2.default)([], function (props) {␊ - return {␊ - display: 'flex'␊ - };␊ - }, _styledSystem.space, _styledSystem.fontSize, _styledSystem.width, _styledSystem.color, _styledSystem.alignItems, _styledSystem.justifyContent, _styledSystem.flexDirection, _styledSystem.flexWrap);␊ + function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }␊ + ␊ + var Flex = (0, _styledComponents2.default)(_Box2.default)(_templateObject, styleString, _styledSystem.space, _styledSystem.fontSize, _styledSystem.width, _styledSystem.color, _styledSystem.alignItems, _styledSystem.justifyContent, _styledSystem.flexDirection, _styledSystem.flexWrap);␊ ␊ Flex.defaultProps = {};␊ ␊ diff --git a/test/snapshots/modules.js.snap b/test/snapshots/modules.js.snap index 3fdb07db3553c25518bdbfeb3e5a52522b889c2a..88e3842753b335ef12d5db48c6f295831d00a8a1 100644 GIT binary patch literal 1174 zcmV;H1Zn$0RzV_9$$!}6%l*iJ zL`^|~L`4TZ5)vTMK$jMY3L2_(v{VS*%-FuO*Y-KmI=LubY1a0e_rCc&^WK{o&yFNX zE$Q2r9{=^=@^@Dc-+4d0^TkhJuS)Fq?}{kDpAqGUM@9Mk2~mFA5ap|c{__{p`+jTUXBf+~U$NpAqHUCTnGlqsnV?`$fDCvHXGP8}izd^6FdT9hgvAPHxxN z*IWO6BFSeZxh2Vup<3%Hxh=^Z)UcdsU_$iR`gBDJd>}scj0shS-L3%WPhn=*@V4i; z;L&{*_FTuKevfR9M&Q56AvSyDuuI6UZU!(Q)C-^+YKX9_dt_wlWAZZb;G@9sKvnt| zP@rfh<10*00D~P^;OS#CLhqq$4s<|ffjyyyW3wk!2Tv zdgShH5?VgpH(|QvSgvD(P5tBkC+eP^B$Y~`+4Jm$3VbN5n*rJ0_pxts+Vy#bau3|? zcmw&P!rZ~0<8XWWtz#`kzA^Iv4nWQG6a)Mt<3nc77(_0THs{bG2{nyVVqGL=H1%RH zr^n-{)Kq0^>?)d?aj_oZCfmI6Ub?sYzN=3lE1Wqt-8K#u!XP>hdUbBdjDPoA!8HoACktnKy{gCiM43F5*6JUyH$UJ&lE13xbd^8%Ga#ClVXJ)7ukVn zio0@q(J6^7x6{xL`yt<_In7e1Ug0WvUVW~!1h2l`%v+~#-db89KdcjAv^76z0!&5v z{afK8*}SRH9gr7T(izVOM4^tQQ%5OCBMhQ^YGfLDN#vVlB$3xU4^k2l*PQ>mPA$bq zO~QG&5>E7O?er&{tWY10h1YuCXONi0A(&wBme*a literal 1060 zcmV+<1l#*TRzV4SwQ98U7XgA?PiEefmAxhe)qfge!lzN>$_W(Bq<}koj(3) zWA6RJ=B<0p?Ux^34@vCr*Pv)yBcgdnMZ0!PG%_aI&bTCf8~OVE^U#wYqtCa`&CDP9 znBmr+r$l>knnfArUgcpqdl7!i(7u58ae4TJJanV7imOzX+mB;oW0{?&k~}KO8A(0{ zu*^w0E6D?ZFiFibaO*YmY*6uBj9lvIRjQP8IpLsK!*$)lHyvVQhc;BaZWD*P1vIg= zgx%}h#Y6#Z<`7!b3=fwObv&GF=E1|7=Ab1*tDsBB!H+!M!KzX`3kD^>zkh|{gkx-Z zCU&%n(ehpZ+cKslRQIeZ)rrO4)I7%`2t_@|LQA?uF?hW#BG2Nw<{4CFeDW+Pdn?0$ z<{qwe1)Dg?mYcfbE08HL=8j`IUm@#3#$f1*FTcGDzp;Gh4IC0MvvkFm15@D3{6_I@ z`-&MzDytB|_YQApa+3WNcT*+0%#m^QJnMcus%E&9$>+nq}6u=6l3Zn47HkdwuROR*Ud*X13`0j#Uw zBl5I5RfzaJUu$_)sp)G5bZi~Zavz%)dg5OwlZLNyK#yYX+qQ22# zf2`N&O4wK>265Po09l_CTYR=|EM%@F0KX5+fGj5oHw%C$*}mz8Z0Gv2OQ5`G5j9S)O5t zr{wWsQrctrxIB!5d5G3Dr(Bo6XaD-MfBh+nv0fa*$5T46EE}3h9O4D4u(na@#3fZc eZO~!8D%x|yx%)vKSDXLNwdQY4t*fr}9RL6fn;y*o diff --git a/test/snapshots/templates.js.md b/test/snapshots/templates.js.md index d561e87..17d4958 100644 --- a/test/snapshots/templates.js.md +++ b/test/snapshots/templates.js.md @@ -315,13 +315,14 @@ Generated by [AVA](https://ava.li). } from 'styled-system'␊ import Div from './Div'␊ ␊ - const Box = styled(Div)([], props => ({}),␊ - space,␊ - fontSize,␊ - width,␊ - color,␊ + const Box = styled(Div)`␊ + ${styleString}␊ + ${space}␊ + ${fontSize}␊ + ${width}␊ + ${color}␊ ␊ - )␊ + `␊ ␊ Box.defaultProps = {}␊ ␊ @@ -341,16 +342,14 @@ Generated by [AVA](https://ava.li). } from 'styled-system'␊ ␊ ␊ - const Hello = styled(h2)([], props => ((props, theme) => ({␊ - fontFamily: 'inherit',␊ - color: props.theme('colors.blue')␊ - })),␊ - space,␊ - fontSize,␊ - width,␊ - color,␊ + const Hello = styled(h2)`␊ + ${styleString}␊ + ${space}␊ + ${fontSize}␊ + ${width}␊ + ${color}␊ ␊ - )␊ + `␊ ␊ Hello.defaultProps = {␊ p: 2␊ diff --git a/test/snapshots/templates.js.snap b/test/snapshots/templates.js.snap index 46d83e7f452ff0707c51c4f46ddea09812e2138d..cd6cab90d7ad91e56b5b1cbeba52f02d49a5d54b 100644 GIT binary patch literal 1031 zcmV+i1o-l7&pIj{d&Jy@t%M%0~qbKb_@?zKDG>2Xm#sPqqf z>CYh*DE)tkUhLmX2K5jG(H{$w#2_e&B78{5YIgQ^J=03v|0j=X5+y8H7|_mI6TGo4G~R z-N*7zU-`8DIhy|!QuSym0G(M^GINx(E|7KW;hVEd+aXok(*Wo%ztP*|x;YTB&)xny zuOkmqb>TDs%Fw&im)Q~5JNxWMC+c1uhg9{Z15kN?U`PHy+O{W=<$Y_@m$X8ve#ro! zK}9w1=LDLwXY}v?{Px-hNYx4kfN8_KE;SDo_H;DdZR^~ax&>18OeO$rJ=a{Ft$qIP zbC1sDwOlTOR2`fFz`enW{tcg6r#sudI~#jGB1lyN2OK?cr}XKW1qUlG9*JaJ?X986 z009Pu9t7v@z0lTOTGPx_zvx>5sVe0FaC+bBz0Fls`FnOd3T_{|J`+;)G5Mx|bV?Vd zf!ve~DEI@if>{+eipaxy71_^ndSekUlV=i=u*xyf*Fp@-ssT5TY$Wi?60Q`QXuL=8 zV4oK7vM4KBz=TJSrZ%b=`8kGRcv(_0yB>+6%$7zoTYSY<7wJ_HqU}no2vS{G7qonO zXk^q|G^ZZYtXjyp7>15)_aLvkUc{RfIiQj>TIGQfwwMVs3<}c8MBmUW8HFV``B8U?6qTtIcA+b=6}eUkiQ3FrkoS&JUp zSnNZ7WTjFlnsJ@mFNlpLEGI}lqzITZvzD;5mz{R9a8y(6wPH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