diff --git a/components/advanced/babel-plugin.js b/components/advanced/babel-plugin.js new file mode 100644 index 00000000..c9b82c45 --- /dev/null +++ b/components/advanced/babel-plugin.js @@ -0,0 +1,175 @@ +import React from 'react'; +import SectionLayout from '../SectionLayout'; +import CodeBlock from '../CodeBlock'; +import Code from '../Code'; +import Note from '../Note'; +import Link from '../Link' + +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 transpilation = ` +{ + "plugins": [ + ["styled-components", { + "transpileTemplateLiterals": false + }] + ] +} +`.trim(); + +const BabelPlugin = () => + +

+ This plugin adds support for server-side rendering, for minification of + styles and gives you a nicer debugging experience. +

+ +

Install the babel-plugin first:

+ +

Then add it to your babel configuration like so:

+ +
+ + This option is turned off by default +

+ By adding a unique identifier to every styled component this plugin + 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 styled-components React will complain. +

+

+ You can enable it with the ssr option: +

+ +
+ +

+ This options adds the components' name and displayName to the class name + attached to the DOM node. In your browser's DevTools you'll see:{' '} + + <button class="sc-Button-asdf123 asdf123" /> + {' '} + instead of just <button class="asdf123" />. +

+

+ This also adds support for showing your components' real name in the + React DevTools. Consider writing a styled component that renders a{' '} + button element, called MyButton. It will + normally show up as <styled.button> for all of your + components, but with this plugin they show{' '} + <MyButton />. +

+

+ This makes it easier to find your components and to figure out where + they live in your app. +

+

+ If you don't need this feature, you can disable it with the{' '} + displayName option: +

+ +
+ + + This is experimental and we don't yet know of all limitations and bugs! + Consider this non-production ready for now. ⚠️ + +

+ This plugin preprocesses your styles with stylis and uses the{' '} + no-parser.js entrypoint on styled-components. +

+

+ This effectively removes stylis from your runtime bundle and should + slightly improve runtime performance and shrink your bundle size. +

+

+ It automatically disables the minify option, since stylis + already does some minification on your CSS. +

+

+ You can enable preprocessing with the preprocess option: +

+ +
+ + + This option is turned on by default. If you experience mangled CSS + results, turn it off and open an issue please. + +

+ This plugin minifies your styles in the tagged template literals, giving + you big bundle size savings. +

+ + 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. + +

+ You can disable minification with the minify option: +

+ +
+ +

+ We also transpile styled-components tagged template + literals down to a smaller representation than what Babel normally does, + because styled-components template literals don't need to + be 100% spec compliant. see{' '} + Tagged Template Literals for + more information about that) You can use the {' '} + transpileTemplateLiterals option to turn this feature off. +

+ +
+
; + +export default BabelPlugin; diff --git a/pages/docs.json b/pages/docs.json index 8a9bafbc..374d315f 100644 --- a/pages/docs.json +++ b/pages/docs.json @@ -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" - }, { + }, + { "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?" } ] } ] diff --git a/pages/docs/advanced.js b/pages/docs/advanced.js index 3ec6280f..d28f91bd 100644 --- a/pages/docs/advanced.js +++ b/pages/docs/advanced.js @@ -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 = () => @@ -19,12 +20,9 @@ const Advanced = () => ( + - - -) + + ; -export default Advanced +export default Advanced; diff --git a/test/components/NavBar/__snapshots__/Menu.spec.js.snap b/test/components/NavBar/__snapshots__/Menu.spec.js.snap index aa226dd4..3d46b732 100644 --- a/test/components/NavBar/__snapshots__/Menu.spec.js.snap +++ b/test/components/NavBar/__snapshots__/Menu.spec.js.snap @@ -273,6 +273,16 @@ exports[`Menu renders correctly 1`] = ` Server Side Rendering +
+ + Babel Plugin + +
+ +
+ + + Babel Plugin + + +
+
+ +
+ + + Babel Plugin + + +
+