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

Babel plugin #62

Merged
merged 9 commits into from
Jul 25, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
165 changes: 165 additions & 0 deletions components/advanced/babel-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import React from 'react';
import SectionLayout from '../SectionLayout';
import CodeBlock from '../CodeBlock';
import Code from '../Code';
import Note from '../Note';

const installNPM = `
npm install --save-dev babel-plugin-styled-components
`.trim();

const usage = `
{
"plugins": ["styled-components"]
}
`.trim();

const ssr = `
{
"plugins": [
["styled-components", {
"ssr": true
}]
]
}
`.trim();

const displayName = `
{
"plugins": [
["styled-components", {
"displayName": false
}]
]
}
`.trim();

const preprocess = `
{
"plugins": [
["styled-components", {
"preprocess": true
}]
]
}
`.trim();

const minify = `
{
"plugins": [
["styled-components", {
"minify": false
}]
]
}
`.trim();

const BabelPlugin = () =>
<SectionLayout title="Babel Plugin" labels={['v2']}>
<p>
This plugin adds support for server-side rendering, for minification of
styles and gives you a nicer debugging experience when using{' '}
<Code>styled-components</Code>.
Copy link
Member

@kitten kitten Jul 23, 2017

Choose a reason for hiding this comment

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

You can leave out "when using styled-components" as that's quite obvious on the site. But we should add a paragraph that it's only for the web, not for React Native

Copy link
Member Author

Choose a reason for hiding this comment

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

Done 🎉

</p>
<p>Usage</p>
Copy link
Member

Choose a reason for hiding this comment

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

This subsection can use the SectionLayout component with the sub prop, like the ones below.

Also, this is missing a small explanation, like:

Install the babel-plugin first:

Copy link
Member Author

Choose a reason for hiding this comment

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

Done 🌮

<CodeBlock code={installNPM} language="node" />
<p>
Then in your babel configuration (probably <Code>.babelrc</Code>):
Copy link
Member

Choose a reason for hiding this comment

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

This sounds a bit odd. Maybe something like:

Then add it to your babel configuration ([...]) like so:

would make it nicer

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed 👍

</p>
<CodeBlock code={usage} language="node" />
<SectionLayout sub title="Server-side rendering">
<Note>This option is turned off by default</Note>
<p>
By adding a unique identifier to every styled component this plugin
Copy link
Member

Choose a reason for hiding this comment

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

this plugin this option

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm sorry ?

Copy link
Member

Choose a reason for hiding this comment

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

Just indicating that we need to replace the mention of "this plugin" with "this option" since each section only talks about a single option :)

avoids checksum mismatches due to different class generation on the
client and on the server. If you do not use this plugin and try to
server-side render <Code>styled-components</Code> React will complain.
Copy link
Member

Choose a reason for hiding this comment

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

<Code>styled-components</Code> needs to be changed to just styled components. We're not talking about the lib 😄

If you do not use this plugin and try to server-side render styled-components React will complain.

This is a bit outdated and also sounds weird, so let's update it:

If you do not use this plugin and try to server-side render, you will likely encounter React warnings, if the import order is different on the client. For this reason we generally recommend you to turn on this option in any SSR setup.

We also need to add a reference to this babel-plugin option to our SSR section.

Copy link
Member Author

Choose a reason for hiding this comment

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

We also need to add a reference to this babel-plugin option to our SSR section. -> What can I do here ?

Copy link
Member

Choose a reason for hiding this comment

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

I can do this, if it's unclear. In the SSR section on the website, it'd just be useful to say "You will probably need to use our babel plugin's ssr option to not run into checksum mismatches with server-side rendering."

</p>
<p>
If you want server-side rendering support you can enable it with the{' '}
Copy link
Member

Choose a reason for hiding this comment

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

We can cut this short due to the prior intro:

If you want server-side rendering support You can enable it with the ssr option:

Copy link
Member Author

Choose a reason for hiding this comment

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

Done 🎉

<Code>ssr</Code> option:
</p>
<CodeBlock code={ssr} language="node" />
</SectionLayout>
<SectionLayout sub title="Better debugging">
<p>
This babel plugin adds the components' name to the class name attached
Copy link
Member

Choose a reason for hiding this comment

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

This babel plugin This option

Also maybe we should mention that it sets a displayName

Copy link
Member Author

Choose a reason for hiding this comment

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

This options adds the components' name and displayName to the class name attached to the DOM node. In your browsers DevTools you'll see

Copy link
Member

@kitten kitten Jul 24, 2017

Choose a reason for hiding this comment

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

Well, that sounds a bit wrong ^^"

Maybe more like:

This options sets a displayName on your styled component which is also used to generate a human-readable class name.

(We can't say "DOM node" since it might also be passed to a component)

to the DOM node. In your browsers DevTools you'll see:{' '}
Copy link
Member

Choose a reason for hiding this comment

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

browsers browser's

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed 😄

<Code>
&lt;button class=&quot;sc-Button-asdf123 asdf123&quot; /&gt;
</Code>{' '}
instead of just <Code>&lt;button class=&quot;asdf123&quot; /&gt;</Code>.
</p>
<p>
This also adds support for showing your components' real name in the
React DevTools. They will normally show{' '}
<Code>&lt;styled.button&gt;</Code> for all of your components, but with
Copy link
Member

Choose a reason for hiding this comment

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

The examples here are nice, but they're missing reference code.

It should probably intro with:

Consider writing a styled component that renders a button element, called MyButton. It will normally show up as [...], but with [...]

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed 👍

this plugin they show <Code>&lt;MyButton /&gt;</Code>.
</p>
<p>
This makes it easier to find your components and to figure out where
they live in your app.
</p>
<p>
If you don't need this feature, you can disable it with the{' '}
<Code>displayName</Code> option:
</p>
<CodeBlock code={displayName} language="node" />
</SectionLayout>
<SectionLayout sub title="Preprocessing">
<Note>
This is experimental and we don't yet know of all limitations and bugs!
Consider this non-production ready for now. ⚠️
</Note>
<p>
This plugin preprocesses your styles with stylis and uses the{' '}
<Code>no-parser.js</Code> entrypoint on styled-components.
</p>
<p>
This effectively removes stylis from your runtime bundle and should
slightly improve runtime performance and shrink your bundle size.
</p>
<p>
It automatically disables the <Code>minify</Code> option, since stylis
already does some minification on your CSS.
</p>
<p>
You can enable preprocessing with the <Code>preprocess</Code> option:
</p>
<CodeBlock code={preprocess} language="node" />
</SectionLayout>
<SectionLayout sub title="Preprocessing">
Copy link
Member

Choose a reason for hiding this comment

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

Incorrect title 😉

Copy link
Member Author

Choose a reason for hiding this comment

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

Damn , fixed :p

<Note>
This option is turned on by default! If you experience mangled CSS
Copy link
Member

Choose a reason for hiding this comment

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

We can lose the exclamation mark here

Copy link
Member Author

Choose a reason for hiding this comment

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

Done 😄

results, turn it off and open an issue please.
</Note>
<p>
This plugin minifies your styles in the tagged template literals, giving
you big bundle size savings. (note that you will not see the effect of
Copy link
Member

Choose a reason for hiding this comment

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

We can lose the parantheses here

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed 💃

minification in generated <Code>&lt;style&gt;</Code> tags, it solely
affects the style strings inside your JS bundle)
</p>
<Note>
This operation may potentially break your styles in some rare cases, so
we recommend to keep this option enabled in development if it's enabled
in the production build.
</Note>
<p>
You can disable minification with the <Code>minify</Code> option:
</p>
<CodeBlock code={minify} language="node" />
<p>
We also transpile <Code>styled-components</Code> tagged template
Copy link
Member

Choose a reason for hiding this comment

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

This should go into a new section, since it's a separate option actually.

Copy link
Member Author

Choose a reason for hiding this comment

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

What should go in this section ? Just this paragraph ?

Copy link
Member

Choose a reason for hiding this comment

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

Yea, this entire part about the template string transpilation

Copy link
Member Author

Choose a reason for hiding this comment

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

screen shot 2017-07-24 at 12 44 40

Done 👍

literals down to a smaller representation than what Babel normally does,
because <Code>styled-components</Code> template literals don't need to
be 100% spec compliant. see{' '}
<a href="https://github.com/styled-components/babel-plugin-styled-components/blob/master/minification.md">
Copy link
Member

Choose a reason for hiding this comment

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

The link here can be swapped out for a link to our "Tagged Template Literals" section.

Also there's a trailing closed parenthesis below.

Copy link
Member Author

Choose a reason for hiding this comment

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

Link swapped :)

minification.md
</a>{' '}
for more information about that) You can use the
<Code>transpileTemplateLiterals</Code> option to turn this feature off.
</p>
</SectionLayout>
</SectionLayout>;

export default BabelPlugin;
75 changes: 50 additions & 25 deletions pages/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,94 @@
"sections": [
{
"title": "Motivation"
}, {
},
{
"title": "Installation"
}, {
},
{
"title": "Getting Started"
}, {
},
{
"title": "Passed props"
}, {
},
{
"title": "Adapting based on props"
}, {
},
{
"title": "Styling any components"
}, {
},
{
"title": "Extending styles"
}, {
},
Copy link
Member Author

Choose a reason for hiding this comment

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

Prettier auto did this , should I revert ?

{
"title": "Attaching additional props"
}, {
},
{
"title": "Animations"
}, {
},
{
"title": "React Native"
}
]
}, {
},
{
"title": "Advanced",
"pathname": "advanced",
"sections": [
{
"title": "Theming"
}, {
},
{
"title": "Refs"
}, {
},
{
"title": "Security"
}, {
},
{
"title": "Existing CSS"
}, {
},
{
"title": "Media Templates"
}, {
},
{
"title": "Tagged Template Literals"
}, {
},
{
"title": "Server Side Rendering"
},
{
"title": "Babel Plugin"
}
]
}, {
},
{
"title": "API Reference",
"pathname": "api",
"sections": [
{
"title": "Primary"
}, {
},
{
"title": "Helpers"
}, {
},
{
"title": "Supported CSS"
}, {
},
{
"title": "Flow"
}, {
},
{
"title": "TypeScript"
}
]
}, {
},
{
"title": "FAQs",
"pathname": "faqs",
"sections": [
{"title": "Can I nest rules?"},
{"title": "Can I refer to other components?"},
{"title": "Can I use css frameworks?"}
{ "title": "Can I nest rules?" },
{ "title": "Can I refer to other components?" },
{ "title": "Can I use css frameworks?" }
]
}
]
Expand Down
34 changes: 16 additions & 18 deletions pages/docs/advanced.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import React from 'react'
import DocsLayout from '../../components/DocsLayout'
import NextPage from '../../components/NextPage'
import React from 'react';
import DocsLayout from '../../components/DocsLayout';
import NextPage from '../../components/NextPage';

import Theming from '../../components/advanced/theming'
import Refs from '../../components/advanced/refs'
import Security from '../../components/advanced/security'
import ExistingCSS from '../../components/advanced/existing-css'
import MediaTemplates from '../../components/advanced/media-templates'
import TaggedTemplateLiterals from '../../components/advanced/tagged-template-literals'
import ServerSideRendering from '../../components/advanced/server-side-rendering'
import Theming from '../../components/advanced/theming';
import Refs from '../../components/advanced/refs';
import Security from '../../components/advanced/security';
import ExistingCSS from '../../components/advanced/existing-css';
import MediaTemplates from '../../components/advanced/media-templates';
import TaggedTemplateLiterals from '../../components/advanced/tagged-template-literals';
import ServerSideRendering from '../../components/advanced/server-side-rendering';
import BabelPlugin from '../../components/advanced/babel-plugin';

const Advanced = () => (
const Advanced = () =>
<DocsLayout title="Advanced">
<Theming />
<Refs />
Expand All @@ -19,12 +20,9 @@ const Advanced = () => (
<MediaTemplates />
<TaggedTemplateLiterals />
<ServerSideRendering />
<BabelPlugin />

<NextPage
href="/docs/api"
title="API Reference"
/>
</DocsLayout>
)
<NextPage href="/docs/api" title="API Reference" />
Copy link
Member Author

Choose a reason for hiding this comment

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

Prettier again

Copy link
Member

Choose a reason for hiding this comment

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

No worries! I might add prettier soon anyway

Copy link
Member Author

Choose a reason for hiding this comment

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

Awesome ! I can do that if you want 👍

</DocsLayout>;

export default Advanced
export default Advanced;
10 changes: 10 additions & 0 deletions test/components/NavBar/__snapshots__/Menu.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,16 @@ exports[`Menu renders correctly 1`] = `
Server Side Rendering
</a>
</h5>
<h5
className="c5"
>
<a
className="c4"
href="/docs/advanced#babel-plugin"
>
Babel Plugin
</a>
</h5>
</div>
<div
className="c2"
Expand Down
16 changes: 16 additions & 0 deletions test/components/NavBar/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,22 @@ exports[`NavBar renders correctly 1`] = `
</styled.a>
</h5>
</styled.h5>
<styled.h5>
<h5
className="c11"
>
<styled.a
href="/docs/advanced#babel-plugin"
>
<a
className="c5"
href="/docs/advanced#babel-plugin"
>
Babel Plugin
</a>
</styled.a>
</h5>
</styled.h5>
</div>
</styled.div>
<styled.div
Expand Down
16 changes: 16 additions & 0 deletions test/components/__snapshots__/DocsLayout.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,22 @@ exports[`DocsLayout renders correctly 1`] = `
</styled.a>
</h5>
</styled.h5>
<styled.h5>
<h5
className="c12"
>
<styled.a
href="/docs/advanced#babel-plugin"
>
<a
className="c6"
href="/docs/advanced#babel-plugin"
>
Babel Plugin
</a>
</styled.a>
</h5>
</styled.h5>
</div>
</styled.div>
<styled.div
Expand Down