<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Justin Spears on Medium]]></title>
        <description><![CDATA[Stories by Justin Spears on Medium]]></description>
        <link>https://medium.com/@speajus?source=rss-204a38ec3bd9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*yGtKq4tJoKUdqbGX5YcRTg.jpeg</url>
            <title>Stories by Justin Spears on Medium</title>
            <link>https://medium.com/@speajus?source=rss-204a38ec3bd9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 10:22:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@speajus/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Styling Components with CSS Modules]]></title>
            <link>https://medium.com/@speajus/styling-components-with-css-modules-cde78638eeb5?source=rss-204a38ec3bd9------2</link>
            <guid isPermaLink="false">https://medium.com/p/cde78638eeb5</guid>
            <category><![CDATA[css-modules]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Justin Spears]]></dc:creator>
            <pubDate>Tue, 07 Aug 2018 23:09:47 GMT</pubDate>
            <atom:updated>2018-08-07T23:09:47.695Z</atom:updated>
            <content:encoded><![CDATA[<p>CSS has been the Achilles heel of modern component-based web development since its invent, and has continued to be a difficult situation. Fundamentally components have different lifecycles than the applications they are used, and often used in multiple applications needing to be styled in response to their demands of the application. So the questions of where to expose configuration and providing some level encapsulation, have been core issue to resolve. While React and friends have done a reasonable job of making components reusable, CSS has lagged behind. At the highest levels combining styling with a component makes sense, but this is not something for which CSS was designed. As a result multiple solutions have come about like BEM (<a href="http://getbem.com/introduction/">Block Element Modifier)</a> and inline styling tools.</p><p>While BEM ostensibly provides a way to namespace CSS, it is entirely manual and requires a large amount of mental overhead, while at the same time offers few protections from inadvertent mistakes (super easy to do bad things).The proponents of BEM claim this is a good thing, we’ll have to disagree.</p><p>There are also inline (or inline like) styling tools like <a href="http://cssinjs.org/">JSS</a>, <a href="https://github.com/threepointone/glamor">Glamour</a>, <a href="https://www.styled-components.com/docs/basics">Styled Components</a> and a host of other solutions, however they tend towards the baby bathwater situation, that is inlining css or css creation, preventing any overrides, and breaking any css conventions and tooling to style components. These solutions also tend to provide little in the way of migrating existing code or knowledge. All of these characteristics, together, make things harder than they need to be as all overrides have to be carefully planned and communicated during the creation of the component.</p><p>Finally there are <a href="https://github.com/css-modules/css-modules">CSS Modules</a>. CSS Modules automatically namespace CSS class names and expose an object with the keys that correspond to the compiled class names in the CSS file. CSS Modules contain real CSS; as such they are significantly easier to migrate to, than the aforementioned alternatives, and they maintain most of the tooling such as preprocessors, linters, auto-prefixers, inspectors and infrastructure that may already exist in a project.</p><p>To use CSS Modules, you write CSS and the compiler exposes the class’s as key value pairs on an object.</p><p>For example if the CSS looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c3b19bc856ea16434b1540d9309d344/href">https://medium.com/media/5c3b19bc856ea16434b1540d9309d344/href</a></iframe><p>It would expose a module like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94ba875d244dc06b80e56cc0ccf6eae2/href">https://medium.com/media/94ba875d244dc06b80e56cc0ccf6eae2/href</a></iframe><p>The crazy looking CSS keys provide name-spacing so elements have accidental styling. Thus a CSS Module can then be used with a component like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd0f1e27a4334368712a80d2110981f3/href">https://medium.com/media/fd0f1e27a4334368712a80d2110981f3/href</a></iframe><p>This works well, but because CSS Modules namespace the CSS (again a good thing), the namespaces themselves are very hard to override; though sometimes you really do want to; think theme’ing,. Overrides are particularly important for components where each usage may require significant divergence from the original look.</p><p>To address this problem with CSS Modules, I wrote a tiny util called <a href="https://github.com/jspears/emeth">Emeth</a>, (less than 100 lines of code, including whitespace and comments, and no dependencies). Emeth essentially keeps a map between component names (displayName) and the corresponding css. Decoupling the component from original style module.</p><p>As a result you can create a component with multiple class’s without props or hard to override inlines.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c10064c736f658ed425a6d3dc92bd81c/href">https://medium.com/media/c10064c736f658ed425a6d3dc92bd81c/href</a></iframe><p>Then you can export your “theme” like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f999f9ff0cd5dbeefce6fb1a339cc40b/href">https://medium.com/media/f999f9ff0cd5dbeefce6fb1a339cc40b/href</a></iframe><p>This slight bit of indirection allows consumers to easily and more safely override your CSS. For example, if you had an app that wanted to change the font weight of the header, you could add there own CSS module named SuperComponent.cssm and Emeth would return the new CSS class for header and the existing class for container. Thus providing overrides, while at the same time preventing accidental styling that can happen with poorly name-spaced CSS.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9771a486b1339c7166f51da7736cdab/href">https://medium.com/media/c9771a486b1339c7166f51da7736cdab/href</a></iframe><p>In a sense the components CSS Module becomes the styling “contract”. A way to define what can be styled, and how it is styled by default. In addition, it does not require a bunch of class properties to be passed down the call chain, if this is attempted via propTypes, or HOCS (<a href="https://reactjs.org/docs/higher-order-components.html">Higher Order Components</a>) to do the injection.</p><p>To make things a little easier, if you are using Webpack, you can setup the import of the CSS Modules to happen automatically. A context file, like the one below will map the components CSS Module to the component, by component name.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3827523da37e8055e32c060b32aaf465/href">https://medium.com/media/3827523da37e8055e32c060b32aaf465/href</a></iframe><p>So now a developer using the component would just need to create a CSS file in the same directory as this script and it becomes available to the respective component, able to override both the components created within the application and external components used by the application. In addition if a class is used in a component and not found in the components styling a warning appears in the console; super helpful for misspellings and other common errors.</p><p>Using CSS Modules gives you the best of CSS: debug-ability, pre-processors, 20 years of experience, tooling, namespaces. Without the infernal pain of inlining styles. With a little help it can also give you support for overrides, restyling, and a bit of documentation in the form a styling contract.</p><p>For a super simple example see this <a href="https://github.com/jspears/emeth-example">repo</a> and let me know what you think.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cde78638eeb5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why I wrote MrBuilder.]]></title>
            <link>https://medium.com/@speajus/why-i-wrote-mrbuilder-a1cd8c110c3d?source=rss-204a38ec3bd9------2</link>
            <guid isPermaLink="false">https://medium.com/p/a1cd8c110c3d</guid>
            <category><![CDATA[npm]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[yarn-package-manager]]></category>
            <dc:creator><![CDATA[Justin Spears]]></dc:creator>
            <pubDate>Thu, 02 Aug 2018 16:06:40 GMT</pubDate>
            <atom:updated>2020-04-20T20:36:16.310Z</atom:updated>
            <content:encoded><![CDATA[<p>I didn’t want to write it. I really didn’t. I tried not to. I tried everything. I did. Honest. I did not want to write <a href="http://mr-builder.github.io/">MrBuilder</a>. I know the world is full of javascript build tools; most of them are pretty solid, but none of them had what I needed. None of them could be managed in sensible way. All of them had opinions, and I love opinions, as long as they are mine. Alas what I wanted; no, what I needed did not exist. So I did; I wrote it.</p><p><strong>What did I need?</strong></p><p>I needed a build system that could compile components, hundreds of components, individually packaged components. Sure <a href="https://webpack.js.org/">Webpack</a> could do it, but you can’t build hundreds of independent packages, with hundreds of webpack.config.js’s and maintain your sanity. Starter kits can get you started but, your stuck in the box of your starter kit, and stuck maintaining all of their scaffolding. Besides most starter kits are built to build applications, not packages. So I needed a build system where I could manage hundreds of modules, in a consistent way, with a way of extending their configurations, per module when necessary.</p><p><strong>But why did I need it?</strong></p><p>I wrote <a href="https://subschema.github.io/">subschema</a> a few years ago while working at PayPal. It was meant to be a dependency injection framework for React, but in the end looked more like a forms library. Subschema was meant to be modular. Use your components, use my components, use someone else’s components and you could tie them all together with a little JSON. Amazingly it worked. But, dependencies, are dependencies. The thing that makes the now so easy, and the future so hard. I needed to know what components used what packages, so I created a monorepo. I did my duty and separated components into packages and managed dependencies, and things where fine. Until, you know they were not. Eventually subschema became 80 modules, all being built by a crappy build tool, a precursor to MrBuilder that wrapped <a href="https://webpack.js.org/">Webpack</a>, and worked with <a href="https://lernajs.io/">Lerna</a> (the fantastic monorepo manager). This seemed reasonable. Until I noticed I spent 70% of my time wrestling the build. And then, there where more modules not just for subschema, but for work. Dozens of them. All needing to built and managed independently. But work used different tooling, <a href="http://stylus-lang.com/">Stylus</a> instead of Less, different Babel plugins, similar but different. So I forked my crappy build tool, and spent time wrestling its quirks. It sucked. I needed something better. I needed to get rid of config files, I needed the config files that do exist to be easily parsable, I needed a way to build not just components but apps, examples, tests, documentation. I needed a way to optimize all of this.</p><p><strong>What did I write?</strong></p><p>So I wrote MrBuilder, a monorepo capable, multi project single config build tool. A tool that can be configured for most scenarios, that out of the box can build a react project, with css and all of its glory. When I write a component, I need to see it, to interact with it; so MrBuilder can do that too. With a moment of inspiration, I stole Babel’s configuration format, and then proceeded to create a tool to help build, test, lint, maintain, and document components, and applications.</p><p><strong>How does it work?</strong></p><p>Pretty much all of MrBuilder is based on <em>plugins</em>, in a very similar format to the .babelrc. The “OptionsManager” loads the <em>plugins</em> and runs them against your package. These plugins can be configured to build css, typescript, uglify, sass, stylus, webworkers, fonts, yaml, transpile, optimize, lint, document all sorts of things. Mostly they load and configure plugins into webpack, but this allows them to do what is needed. The plugins can be grouped together to create <em>presets</em>, which can then be used inside projects. This works pretty well, the <em>presets</em> can run the same project as an application, as a test, or compile it to a module. It can compile optimized applications, or just precompiled modules. The tool can be wrapped to create your own build tool, so you don’t have to configure each of your projects, you can just use this tool instead.</p><p><strong>How do I use it?</strong></p><p>In the simplest case the lonely single module you can just</p><pre>$ yarn add @mrbuilder/cli</pre><p>Your source code is in ./src as it should be, then you can just run MrBuilder.</p><p>Here is a little component in ./src/index.js.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/937c33374790c906f6211e1524b8497a/href">https://medium.com/media/937c33374790c906f6211e1524b8497a/href</a></iframe><p>But you probably want to modify your./package.json to run it with yarn.</p><pre>{<br>  &quot;name&quot;:&quot;your-package&quot;,<br>  ....<br>  &quot;scripts&quot;:{<br>     &quot;start&quot;:&quot;mrbuilder&quot;,<br>     &quot;test&quot;:&quot;mrbuilder&quot;,<br>     &quot;karma&quot;:&quot;mrbuilder&quot;,<br>     &quot;prepublish&quot;:&quot;mrbuilder&quot;<br>  } </pre><pre>}</pre><p>Then run it.</p><pre>$ yarn prepare</pre><p>If you want to run your component as an application add ./public/index.js that returns a component like this. Its configurable but the default takes care of mounting the html and rendering the component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc2a4dcb6492af0447bcb498ac19c97c/href">https://medium.com/media/bc2a4dcb6492af0447bcb498ac19c97c/href</a></iframe><p>Now you can run the example with yarn run start . <a href="https://github.com/gaearon/react-transform-hmr">HMR</a>, <a href="http://lesscss.org/">Less</a> and <a href="http://babeljs.io/">Babel</a> all just working nicely in the background. Oh, and <a href="https://github.com/css-modules/css-modules">CSS Modules</a> if your into that sort of thing.</p><p>If you wanna write some tests, and you should just add them to ./test and end them in `-test.js`</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d44765bbcfbcb388976f66f2a4cf22c/href">https://medium.com/media/8d44765bbcfbcb388976f66f2a4cf22c/href</a></iframe><p>Now you can run your tests by yarn run test or in test development mode yarn run karma . And yes you have <a href="https://github.com/airbnb/enzyme">Enzyme</a> and <a href="https://mochajs.org/">Mocha</a> already good to go.</p><p><strong>But what about plugins?</strong></p><p>Ah yes, by default things should “just work”. But things by default never “just work”, I almost always need to cover some corner case. Plugins are how we do that with MrBuilder.</p><p>You can put plugins under a mrbuilderkey in your package.jsonor better create a .mrbuilderrc to host your plugins.</p><p>A .mrbuilderrc looks like the file below and it uses <a href="https://json5.org/">JSON5</a> parser so you can have comments, and unqouted keys. One day it may support yaml, but… not today.</p><pre><br>{<br>  plugins:[<br>  //&quot;plugin-name&quot; or [&quot;plugin-name&quot;, {//plugin configuration}] or [&quot;plugin-name&quot;, false// to disable a plugin] <br>  ],<br>  prsets:[<br>  ],<br>  env:{<br>    app:{<br>       plugins:[<br>      ],<br>       presets:[],</pre><pre>    }</pre><pre>  }</pre><pre>}</pre><p>MrBuilder has ~35 plugins, a small fraction of the plugins in the webpack universe. That’s ok, because its pretty easy to write and use your own. (PR’s are always welcome, and usually merged)</p><p>To create a plugin create file let’s say ./super-duper.js it should export a function that takes config, webpack</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66c191aedb546a78655f22b5ca66a162/href">https://medium.com/media/66c191aedb546a78655f22b5ca66a162/href</a></iframe><p>Then you just add ./super-duper to your plugins in .mrbuilderrc . If you want you can make your plugin a package and reference it without the ./</p><p><strong>Still a configuration per module?</strong></p><p>Well yes and no, this is the simplest case. If you have a monorepo and/or a bunch of modules you want to compile with the same settings, then you can create your own build tool. Well a meta build tool.</p><p>First create a new package for your build tool, I’ll call your-build-tool .</p><pre>$ yarn init</pre><p>Then add mrbuilder as a dependency (not dev dependency, your-build-tool will be a devDependency to your modules).</p><pre>$ yarn add @mrbuilder/cli</pre><p>Add a .mrbuilderrc configured to your desires.</p><p>Create a script to use your build tool.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4531565c1e2ba06def6e22ef80cfb77c/href">https://medium.com/media/4531565c1e2ba06def6e22ef80cfb77c/href</a></iframe><p>Add your-build-tool to the bin section of your tools package.json</p><pre>{<br>  &quot;name&quot;:&quot;your-build-tool&quot;,<br>  ...<br>  &quot;bin&quot;:{<br>    &quot;your-build-tool&quot;:&quot;./your-build-tool.js&quot;<br>  }</pre><pre>}</pre><p>Voila: you don’t have to manage separate .mrbuilderrc for each package.</p><p>Now you have a your own build tool, publish it if you need to. Add your-build-tool to your component as devDependency. Now use your-build-tool instead of MrBuilder in your components package.json .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d7cebaa3fc5ce369351724d6d433de7/href">https://medium.com/media/3d7cebaa3fc5ce369351724d6d433de7/href</a></iframe><p><strong>I wrote it, let me know if it works for you</strong></p><p>So I wrote it, I didn’t want to, I really didn’t want to. But since I did, it would be great to let me know what you think, what’s borked or what can be improved. Sometimes you have to write what you need even if you don’t want to, I am guessing there are a few of you out there that want something that looks like this, even if you don’t know it.</p><p>Also if you’ve written something better, please let me know, I will totally use it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a1cd8c110c3d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>