<?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 Flarnie Marchan on Medium]]></title>
        <description><![CDATA[Stories by Flarnie Marchan on Medium]]></description>
        <link>https://medium.com/@flarnie?source=rss-3169969abcb3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Uug_VjBBeDvmj3p_-v0sBA.jpeg</url>
            <title>Stories by Flarnie Marchan on Medium</title>
            <link>https://medium.com/@flarnie?source=rss-3169969abcb3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 02:25:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@flarnie/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[Hacking React at Chegg]]></title>
            <link>https://flarnie.medium.com/hacking-react-at-chegg-57b78a4f4540?source=rss-3169969abcb3------2</link>
            <guid isPermaLink="false">https://medium.com/p/57b78a4f4540</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[draftjs]]></category>
            <dc:creator><![CDATA[Flarnie Marchan]]></dc:creator>
            <pubDate>Sat, 05 Jan 2019 01:38:34 GMT</pubDate>
            <atom:updated>2019-01-05T01:38:34.848Z</atom:updated>
            <content:encoded><![CDATA[<p>After working on open source projects at Facebook, it’s time for my next adventure.</p><p><strong>TL;DR:</strong> It was truly awesome to work on the React Core team and to maintain the Draft.js framework. This is the story of how I got there, and why I’m now joining the web engineering team at Chegg.</p><p><strong>I’m excited to be joining Chegg for three reasons;</strong></p><ul><li>Their mission to help students has a positive impact on people’s lives.</li><li>Chegg engineering is going all in on React, Redux, and other modern, Open Source web technologies.</li><li>Chegg has a positive company culture, placing an emphasis on workplace equality, teamwork, and fun.</li></ul><p>At this time of transition, I’d like to share about my experience working on the React and Draft.js frameworks, and how this change will impact my work in open source.</p><h3>How to get into Facebook Open Source</h3><p>My interest in open source started long before I joined Facebook, and so I was looking out for opportunities to get involved in open source when I started working at Facebook in 2015. When the author of Draft.js posted internally looking for volunteers, I showed up.</p><p>In case you haven’t run into <a href="https://draftjs.org/">Draft.js</a> before — it is widely used for rich text editing in Facebook products, and in the open source React community.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*C3NJy3RlL-tD-jXl" /><figcaption>Draft.js is widely used for building rich text editors.</figcaption></figure><p>Things were going well — and then came a plot twist! The Draft.js author left Facebook. The next day I looked at the project and saw that nobody else had picked up the role of “maintainer” internally at Facebook.</p><p>I messaged my manager, saying — “I want to take this on”, and he said “Go for it!”. So I took over maintaining Draft.js using 10–20% of my time, starting in late 2016. With the help of many co-maintainers, we fixed bugs and improved documentation for the framework. The biggest problem we faced was syncing the changes from within Facebook and those from GitHub into both repositories. With the help of the open source support team, we set up scripts to automatically copy changes between Facebook and GitHub. Internal Facebook adoption of Draft.js grew by ~46% during 2017. The Open Source Draft.js community also grew, more than doubling monthly npm downloads between June-Dec 2017. I believe that my involvement in the Draft.js project helped made my interest in Open Source more visible within Facebook, leading to the opportunity to join the React Core team and work on React.js itself.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8825Pj9iWPpCZpShOCd9kw.jpeg" /></figure><p>I’m proud to have helped prepare the <a href="https://reactjs.org/blog/2017/06/13/react-v15.6.0.html">15.6</a> and 16.0 releases of the React framework. During my first few months on the team, I kept lines of communication open with other library maintainers and connected with the open source community at meetups. That was exciting, but I wanted to keep polishing my engineering skills rather than focusing too much on community management. So after the 16.0 release I took on a project to update Facebook’s legacy web infrastructure for compatibility with new features in React.</p><p>You might wonder, what’s it like working on the infrastructure for Facebook.com? I remember a day that Brian Vaughn asked me “How’s it going?” and I told him that it was like spelunking through an ancient, mysterious dungeon or temple, like in Indiana Jones. The custodians of the temple were either long gone or on vacation, or had forgotten the ins and outs of the code they wrote years ago. It feels risky, because pushing a bug could take down all of Facebook.com! So you just get brave and keep going. And you also use feature flags.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*VdtXUgdh-4ygbMg7" /><figcaption><a href="https://www.flickr.com/photos/proviscom/681209344/in/photolist-23cnNG-23bQYA-237xSR-237DdT-237NLK-23bDQs-237ndr-23cjTb-23cdyL-237Ewc-23bPGS-23buiW-8uG6iQ-9xWiXL-9xw6pr-ayWsQg-ayZsy5-ayYNhE-ayWu44-66E6dh-8cn7YU-8s2Fws-b6pgCK-b6piXc-b6puPk-b6pecV-b6ptje-b6pugz-b6pqX4-b6pvsZ-b6pGtH-i8i68-23aqid-ajt3VL-7Xh5JD-i8ic7-p7yGz6-8K9jKP-9QE3fp-ayYS5m-ayZo59-ayWKF2-i8ip5-if97C-euRwJy-81Ssfr-i8g1z-iMUiJ2-iMVtja-kFS48T">cc Avery Studio</a></figcaption></figure><p>I helped with various other projects along the way, such as the rewrite of the Reactjs.org documentation site, but overall I was “heads down” in my focus on the internal Facebook React community. I also put my Draft.js work on hold, in order to focus on React usage within Facebook. By the end of my time on the team, I found myself less involved in the open source community outside of Facebook. So, while I was sad to leave my day-to-day work with the folks on the React Core team, I will be happy to reconnect with the open source JavaScript community outside of Facebook. I will continue contributing to open source projects in my spare time, including React.js.</p><p>React.js still has a dedicated team at Facebook, and I’m excited to watch as <a href="https://reactjs.org/docs/hooks-intro.html">Hooks</a>, <a href="https://reactjs.org/docs/react-api.html#suspense">Suspense</a>, and other improvements are released. I believe React will continue to revolutionize front end development.</p><p>Draft.js, on the other hand, has never had a dedicated team within Facebook. And I just left. So who will maintain Draft.js now? Not to worry! Because — <br>In preparation for leaving, I recruited 5 new Facebook-employed maintainers for the Draft.js framework. They’re already made tons of progress, including <a href="https://github.com/facebook/draft-js/pull/1967">re-licensing the Draft.js framework using MIT and removing the patents clause</a>! If one maintainer was good, then having 5 maintainers will be even better!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*x2dxQZydaJVwn0Lj" /><figcaption>5 maintainers are better than 1!</figcaption></figure><p>And yes, I do still plan to contribute to Draft.js, as an external maintainer. The future of the project looks more hopeful than ever before.</p><h3>How to find the perfect job</h3><p>When considering a new place to work, I relied on a secret that <a href="https://medium.com/@tomocchino">Tom Occhino</a> once told me — just look for these three things:</p><ul><li><strong>🦸‍♀️ Great mission</strong>; a product or mission you are passionate about.</li><li><strong>💻 Great technology</strong>; the languages and frameworks you like most.</li><li><strong>🌈 Great people</strong>; honest, kind folks who make you laugh, in a supportive company culture.</li></ul><p>If you find at least two of those, then you can probably get excited about work and ignore the one that is lacking. If you find all three, that is a true winner. I’m proud to say that Chegg has a great mission, great technology, and great people.</p><h3>A mission to educate</h3><p>The world needs education, now more than ever. Helping students learn will undeniably make the world a better place. Folks I have met at Chegg have a genuine passion for education and for helping students.</p><h3>Awesome web technology</h3><p>Chegg’s audience is mainly college students. Where do college students go to study? <a href="https://www.pearsoned.com/wp-content/uploads/2015-Pearson-Student-Mobile-Device-Survey-College.pdf">Today’s students go online</a>! As someone passionate about the web, I’m excited to be at a company with a strong focus on web technologies. As I mentioned, Chegg engineering is adopting React, Redux, and several other fantastic open source projects.</p><h3>Happy, diverse, successful engineering teams</h3><p>When looking for “Great People” to work with, the main factor is the work environment. Chegg is an excellent case study on how to create a great work environment:</p><ul><li><strong>Happy</strong> — “At Chegg, <a href="http://reviews.greatplacetowork.com/chegg">95% of employees say their work place is great</a>.”</li><li><strong>Diverse</strong> — Women make up nearly half of leadership and of the overall company, and <a href="https://www.marketwatch.com/press-release/chegg-honored-as-2018-100-best-workplaces-for-women-as-well-as-best-medium-workplaces-by-great-place-to-work-and-fortune-magazine-2018-10-30">Chegg has been chosen as one of the top 100 places to work for women</a>. I’ve already seen first hand that people with every type of background are present and welcomed at Chegg.</li><li><strong>Successful</strong> — Engineering at Chegg combines engineering excellence with the lean and focused efficiency of a start-up. We ship fast and take time to polish our work and skills along the way.</li></ul><p>Working in this environment, I will find positive patterns that we can promote in the wider engineering industry, advancing the engineering field overall.</p><h3>Onwards</h3><p>Draft.js and React.js both have internal Facebook maintainers, and I’ll continue helping with each in my spare time. With great thankfulness for the time I spent at Facebook, I’m excited to join the worthy mission and excellent quality of Chegg engineering!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57b78a4f4540" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How Do Overachievers Have a Sane Work-Day?]]></title>
            <link>https://flarnie.medium.com/how-do-overachievers-have-a-sane-work-day-b0078ad4c660?source=rss-3169969abcb3------2</link>
            <guid isPermaLink="false">https://medium.com/p/b0078ad4c660</guid>
            <category><![CDATA[work-life-balance]]></category>
            <category><![CDATA[company-culture]]></category>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Flarnie Marchan]]></dc:creator>
            <pubDate>Fri, 17 Jun 2016 17:54:57 GMT</pubDate>
            <atom:updated>2016-06-17T17:54:57.893Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-nd/4.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_ScsP2dJF_veobTyur5_ZQ.jpeg" /><figcaption>‘Jaws Beach’ cc by Bryce Edwards <a href="https://flic.kr/p/5LnYn2">https://flic.kr/p/5LnYn2</a></figcaption></figure><p>I recently overheard a project manager asking someone:</p><blockquote><em>“If this was so easy,</em><strong><em> why didn’t you do it over the weekend?</em></strong><em>”</em></blockquote><h4>Good Work Requires Rest</h4><p>That question frustrated me because working on the weekend is a trap that can lead to burn-out. In my own experiences and those of other engineers, I have seen that doing good work requires rest.</p><p>For example: Once upon a time, I was tasked with building a “paginated tabs component”. The requirements were complex, and we had a deadline to meet. I worked through most of the weekend in order to get the component ready by Monday morning. <br> <br> Guess what happened when I unveiled the component to the designer and project manager on Monday morning?<br> <br> It wasn’t what they wanted. The requirements had not been clear and in my rush to get something finished, I had filled in the blanks. My implementation was so far from what they expected that I had to throw it all out. Already worn out from working through the weekend, I had to start fresh.<br> <br> What went wrong?</p><ul><li>I worked in isolation, and missed out on communication and feedback.</li><li>I rushed, and made worse decisions because I was tired and stressed.</li></ul><h4>Was it a fluke?</h4><p>The few times I have worked on a weekend, the results have been similar. I made bad decisions, got worn out, and got frustrated with my work.<br> <br> It’s not just me — you too are at risk of burn out and wasted time. How do we know this? Science! There a host of studies and articles out there backing this up:</p><ul><li>“While short bursts of intense work with overtime can be productive, and aren’t always harmful, in the long run, excessive hours are counterproductive.” — <a href="http://www.cnbc.com/2015/01/26/working-more-than-50-hours-makes-you-less-productive.html">this article</a></li><li>“Research that attempts to quantify the relationship between hours worked and productivity found that employee output falls sharply after a 50-hour work-week, and falls off a cliff after 55 hours — so much so that someone who puts in 70 hours produces nothing more with those extra 15 hours” — <a href="http://www.cnbc.com/2015/01/26/working-more-than-50-hours-makes-you-less-productive.html">the same article</a> summarizing <a href="http://ftp.iza.org/dp8129.pdf">this study</a></li><li>“We know from our experience in changing from six to five days and back again that we can get at least as great production in five days as we can in six” — Henry Ford, quoted in <a href="http://www.theatlantic.com/business/archive/2012/08/the-case-for-vacation-why-science-says-breaks-are-good-for-productivity/260747/">this article</a></li><li>“Indeed, it appears that in many, if not most, industries in the United States, shorter hours are actually associated with higher rates of output per hour” — <a href="http://www.ilo.org/wcmsp5/groups/public/@ed_protect/@protrav/@travail/documents/publication/wcms_187307.pdf">The effects of working time on productivity and firm performance: a research synthesis paper</a></li></ul><h4>How To Stop Working Late</h4><p>Between the research findings above and my own experiences, I know the benefits of taking time to rest. And yet, the drive to do great things leads me to consider working long hours. When I feel the temptation to work late, I deal with it by reminding myself to recharge. I focus on the idea that a balanced life is the path to doing great things. Here are some other strategies that have helped me in the pursuit of the fabled “work life balance”:</p><ul><li>Set some goals that relate only to my personal life, and not my work life. Discuss these with my manager and aim for deadlines, just like I would with work related goals.</li><li>Make plans for evenings and weekends. It’s easier to leave the office when I know that I’m going to a meet-up, cooking a nice dinner, or taking a class in a new skill.</li><li>Use the 8 hour limit to ruthlessly prioritize. Accept that some things will not be finished. That is ok as long as the most important things <em>are</em> being finished.</li></ul><p>Having managers who understand and support me in working sane hours is also a huge benefit.</p><h4>Celebrate Good Managers</h4><p>Managers and other leaders can set the tone when it comes to the issue of over-working. I want to thank those managers who have helped me learn this and who continue to advocate for a sane work day. At least once, my current manager has had to tell me “You should go home”, and has been the one to express support when I do need to take time off.</p><h4>Is Your Life Sustainable?</h4><p>Consider trying this exercise that a current manager on my team came up with:</p><blockquote><em>“Look back at your past 4 or so weeks. …. Imagine that playing out for the next five years. Will you be happy with what you’ve accomplished, achieved, experienced, and missed out on? If so, then rock on! But if not, think about how you’d adjust your weekly routine into something you believe you can sustain.”</em></blockquote><h4>Remember to Recharge</h4><p>A friend recently asked me,</p><blockquote>“How do overachievers have a sane work-day?”</blockquote><p>The answer is to remember that rest is the key to keeping high achievement in the long term. This sounds crazy, but you and your team members can <strong>achieve more when you take breaks</strong>.</p><p>So please don’t impose what I call “burn out hours” on yourself or your team. And if you’re on my team, I will push you to stick to a sane work schedule.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b0078ad4c660" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[To Infinity and Beyond with React Waypoint]]></title>
            <link>https://flarnie.medium.com/to-infinity-and-beyond-with-react-waypoint-cb5ba46a9150?source=rss-3169969abcb3------2</link>
            <guid isPermaLink="false">https://medium.com/p/cb5ba46a9150</guid>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[infinite-scroll]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Flarnie Marchan]]></dc:creator>
            <pubDate>Thu, 30 Apr 2015 23:01:11 GMT</pubDate>
            <atom:updated>2020-10-25T20:18:02.282Z</atom:updated>
            <content:encoded><![CDATA[<h4>Infinite Scroll with React Waypoint</h4><p>Using AJAX to load more content as the user scrolls down, also known as “lazy load”, “autopager”, “endless pages”, etc. is the technique used to achieve infinite scrolling. Plenty of plugins and libraries exist to provide lazy loading in JavaScript, but what about a solution that meshes well with React.js? <a href="http://brigade.github.io/react-waypoint/"><strong>React Waypoint</strong></a><strong> comes to the rescue!</strong></p><h4>React Waypoint Infinite Scroll Tutorial</h4><p>Here are the three steps for implementing Lazy Load with React Waypoint in your very own React component:</p><ol><li><strong>Install React Waypoint using your preferred JS package manager.</strong></li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6dc9f43c9a04c9cab77cb3f9b644e230/href">https://medium.com/media/6dc9f43c9a04c9cab77cb3f9b644e230/href</a></iframe><p>2. <strong>Include the React Waypoint component at the end of your list of items.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/210a5f9a348f81403ac9ceb610e84d81/href">https://medium.com/media/210a5f9a348f81403ac9ceb610e84d81/href</a></iframe><p>Note that<strong> we only render React Waypoint when not loading. </strong>The callback will request more items, and we don’t want to keep requesting more until the first request for items has loaded.</p><p>React Waypoint will also call it’s <em>onEnter</em> callback on the initial mount if it detects that it is within the viewport. That means that if the first page of items loaded doesn’t fill the screen, React Waypoint can request the next page.</p><p>3. <strong>Point the <em>onEnter</em> prop at the function that loads more data into your list. </strong>Make sure that the loading triggers a re-render of your list component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a4eebbe742469fa794ca579600097671/href">https://medium.com/media/a4eebbe742469fa794ca579600097671/href</a></iframe><p><strong>Want the load to trigger before the user hits the end?</strong> Just use the <em>threshold</em> prop. In the above example, we set the <em>threshold</em> to 0.5, which means that the waypoint <em>onEnter</em> will trigger when the waypoint is as far away from entering the screen as twice the height of its scrollable parent’s viewport height.</p><p>Also note that if the scrollable area is not the whole window, then the scrollable ancestor element must be a <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/position">positioned element</a>. This allows React Waypoint to determine its own relative position. The easiest way to do this is to add the style <em>position: relative</em> on the scrollable parent:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc0714f53e4ff3cf524fc8b2498b6630/href">https://medium.com/media/cc0714f53e4ff3cf524fc8b2498b6630/href</a></iframe><p>That’s it! Three simple steps, and you’ve added lazy loading to your component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*HqhckM2xmXMpee_Ybv1Imw.jpeg" /></figure><p>You can see the <a href="http://brigade.github.io/react-waypoint/#infinite-scroll">full source code and working example</a> in the React Waypoint documentation.</p><h3>How It Works With React</h3><p>React Waypoint is a React component, meaning you can easily compose it with other React components. In order to handle scroll events, React Waypoint binds event listeners to <em>scroll</em> and <em>resize</em> events, bypassing the usual React approach of passing callbacks as properties to a component to handle events. However, by using <a href="https://facebook.github.io/react/docs/component-specs.html#mounting-componentdidmount"><em>componentDidMount</em></a> and <a href="https://facebook.github.io/react/docs/component-specs.html#unmounting-componentwillunmount"><em>componentWillUnmount</em></a> lifecycle events, we can ensure that those event listeners are only set when the DOM is ready and are cleaned up when React Waypoint is removed from the page.</p><h3>This Is Only The Beginning</h3><p>Infinite scroll is just one of many ways that React Waypoint can be used. It could also be used to simplify building a scroll-spy feature, similar to the <a href="http://getbootstrap.com/javascript/#scrollspy">Twitter Bootstrap “scrollSpy”</a>. Lastly, being able to easily track which page elements are within the viewport opens up interesting possibilities for analytics and data. For example, in a long list of messages, you could track the number of times a user scrolls down to look at a particular message, and track which messages are actually seen in the viewport as opposed to the messages that the user never scrolls down to see. The ability to track real user views rather than just page views or clicks adds more ways to understand user behavior.</p><p>The flexibility of this component is what inspired us at <a href="https://www.brigade.com/">Brigade</a> to release it as open source. I look forward to seeing what other examples show up for how to <a href="https://github.com/brigade/react-waypoint">use React Waypoint</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cb5ba46a9150" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What is the Flux Application Architecture?]]></title>
            <link>https://flarnie.medium.com/what-is-the-flux-application-architecture-b57ebca85b9e?source=rss-3169969abcb3------2</link>
            <guid isPermaLink="false">https://medium.com/p/b57ebca85b9e</guid>
            <category><![CDATA[mvc]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[flux]]></category>
            <dc:creator><![CDATA[Flarnie Marchan]]></dc:creator>
            <pubDate>Wed, 10 Dec 2014 21:46:54 GMT</pubDate>
            <atom:updated>2020-10-25T20:17:15.945Z</atom:updated>
            <content:encoded><![CDATA[<h2>What is the Flux<br>Application Architecture?</h2><p>Flux is an idea for organizing your application that was developed at Facebook, based on one simple principle:</p><blockquote>Data moves in one direction through your application.</blockquote><p>This is called <strong>“unidirectional data flow”</strong>, but it might be easier to remember if you think of your data as a shark: <a href="http://animalquestions.org/fish/sharks/can-sharks-swim-backwards/">Sharks can only swim forward</a>.</p><p>Facebook has published <a href="https://github.com/facebook/flux/tree/master/examples">examples of Flux</a>, and <a href="https://reactjsnews.com/the-state-of-flux/">at least six other libraries have sprung up</a> with interpretations. For the purpose of this article, when we say “Flux”, we are referring to <a href="https://github.com/facebook/flux">the Facebook implementation</a>.</p><h3>A Flux Example</h3><p>To understand Flux, let’s walk through a basic Todo application. You can <a href="https://github.com/facebook/flux/tree/master/examples/flux-todomvc">find the full source code in the Facebook Flux Repository</a>.</p><h4>Loading the ToDo Items</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*nA1DG1kJFvIyBnFyb50Efg.jpeg" /></figure><p>When the application starts, the <em>ToDoApp</em> React component fetches and displays the data that is present in the <em>ToDoStore</em>. The <em>ToDoStore</em> is completely unaware of the <em>ToDoApp</em> component. If you imagine the component acting as a View, and the <em>ToDoStore</em> as a Model, then so far this isn’t so different from MVC.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d7c96c576c8f8edc7fcd60e0e096d08/href">https://medium.com/media/9d7c96c576c8f8edc7fcd60e0e096d08/href</a></iframe><p>In this simplified example, we aren’t worrying about how the <em>ToDoStore</em> loads the initial data.</p><h4>Creating a New ToDo Item</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*5L_Lj-TKzsDoFHio8JMFGw.jpeg" /></figure><p>The <em>ToDoApp</em> Component has a form for creating a new ToDo item. When a user submits that form, it kicks off a flow of data through the Flux system, as illustrated above:</p><ol><li><strong>The component handles the form submission by calling its own callback.</strong></li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f88727241e6a0a9724f4a89edbc58bfb/href">https://medium.com/media/f88727241e6a0a9724f4a89edbc58bfb/href</a></iframe><p><strong>2. The component callback calls a method on the <em>ToDoActionCreator</em>.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be34824c3696d27abbb15d37579b3e03/href">https://medium.com/media/be34824c3696d27abbb15d37579b3e03/href</a></iframe><p><strong>3. The <em>ToDoActionCreator</em> creates an action of the type <em>TODO_CREATE</em>.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c8e5f0745cfdf4d8742d3e7013adaee/href">https://medium.com/media/8c8e5f0745cfdf4d8742d3e7013adaee/href</a></iframe><p><strong>4. The action is sent to the <em>Dispatcher</em>.</strong></p><p><strong>5. The <em>Dispatcher</em> passes the action to all registered callbacks from <em>Stores</em>.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e0ae3b4022315c0a797a203831a20be4/href">https://medium.com/media/e0ae3b4022315c0a797a203831a20be4/href</a></iframe><p><strong>6. The <em>ToDoStore</em> has a registered callback that listens for the <em>TODO_CREATE</em> action, and updates its own data.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/916cf293ab602cd56e247f196a7ad88d/href">https://medium.com/media/916cf293ab602cd56e247f196a7ad88d/href</a></iframe><p><strong>7. The <em>ToDoStore</em> fires a <em>change event</em> after updating its data.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/79c580a4dc37c7a0114319cb76cabc6c/href">https://medium.com/media/79c580a4dc37c7a0114319cb76cabc6c/href</a></iframe><p><strong>8. The ToDoApp component is listening for <em>change events</em> from the <em>ToDoStore</em>, and re-renders the UI based on the latest data from the <em>ToDoStore</em>.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/78671d4ec1ff4939c9bd187a12f9b7c4/href">https://medium.com/media/78671d4ec1ff4939c9bd187a12f9b7c4/href</a></iframe><h3>Flux vs. MVC</h3><p>Flux has been <a href="https://www.youtube.com/watch?v=nYkdrAPrdcw&amp;list=PLb0IAmt7-GS188xDYE-u1ShQmFFGbrk0v#t=1215">presented as an alternative to MVC</a>. <a href="https://github.com/facebook/flux#how-flux-works">The documentation for Flux</a> explains that it “eschews MVC in favor of a unidirectional data flow”. When comparing Flux to MVC, you have to understand three things:</p><ol><li>“MVC” actually means “MV*” in the land of JavaScript.</li><li>Flux is not simpler than MV*.</li><li>Flux keeps things more predictable than MV*.</li></ol><blockquote>“MVC” actually means “MV*” in the land of JavaScript.</blockquote><p>In order to compare Flux to MVC, we need to understand what MVC means in this context.</p><p>There are 15 examples of JavaScript frameworks on <a href="http://todomvc.com/">ToDoMVC</a>, but none are strict implementations of the “Model, View, Controller” design pattern. Take <a href="http://backbonejs.org/">Backbone.js</a> as an example: It has models and views, but there is arguably no controller in Backbone.js. The role of the controller in many JavaScript frameworks is absorbed into the view or model, and there may be other important categories of functionality, such as a router.</p><p>When we use “MVC” or “MV*” to describe a JavaScript architecture, we generally mean that it has a separation of concerns when dealing with business logic and user interfaces. The data storage is compartmentalized into “models”, while the presentation and user interactions are dealt with in “views”.</p><p>This might work as follows: the view gets information from the model and displays it to the user. The user then interacts with it. These interactions trigger the view to update the data stored in the model, which then may trigger a UI update in the view.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/730/1*wqtf5sWAzQs0HTCVOhjnqA.jpeg" /><figcaption>Basic MVC Data Flow</figcaption></figure><blockquote>Flux is not simpler than MV*.</blockquote><p>You may have watched the <a href="https://www.youtube.com/watch?v=nYkdrAPrdcw&amp;list=PLb0IAmt7-GS188xDYE-u1ShQmFFGbrk0v#t=627">Facebook intro to Flux</a> and seen the analysis of why “MVC doesn’t scale”, including a diagram showing the flow of data amongst 7 different models and views:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/837/1*gSSDaoZsDB-dZGKqnVk1gQ.jpeg" /><figcaption>Complex MVC Data Flow</figcaption></figure><p>This makes MVC look super confusing — look at all those arrows! Who can follow what’s going on in that diagram? It seems obvious that Flux is simpler, right?</p><p>But in that video we never get to see the same level of complexity implemented in Flux. Sure, it all “collapses down” to a simple flow of data:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/710/1*ZvLUQRHVsW1eWBN9sJQJDg.jpeg" /><figcaption>Basic Flux Data Flow</figcaption></figure><p>Still, it is worth seeing what a large system implemented in Flux would look like. Here it is, and you’ll notice that there are more arrows and boxes than the MVC version, not fewer:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/1*ICpUjYrHJa9TdIDwEIhooQ.jpeg" /><figcaption>Complex Flux Data Flow</figcaption></figure><p>Flux has just as many moving parts as MV*. That is why this diagram looks just as complex as the MV* diagram — but with one key difference: The arrows point in one direction, forming a consistent cycle through the system.</p><blockquote>Flux keeps things predictable.</blockquote><p>There are many things going on in both the Flux and the MV* diagrams — but the level of predictability in the Flux diagram is much higher.</p><p>The dispatcher in Flux also ensures that the actions flow through the system one at at time. If an action is sent to the dispatcher before it is finished processing an existing action, it will throw an error:</p><blockquote>“Uncaught Error: Invariant Violation: Dispatch.dispatch(…): Cannot dispatch in the middle of a dispatch.”</blockquote><p>This is another way of keeping things predictable. It pushes developers to build applications without complicated interactions between data resources.</p><p>The dispatcher also allows the developer to specify the order that stores execute their callbacks, using the <em>waitFor </em>method to tell one store to wait for another before executing its callback. If you write code where two stores <em>waitFor</em> each other, then <a href="http://facebook.github.io/react/blog/2014/07/30/flux-actions-and-the-dispatcher.html#why-we-need-a-dispatcher">the dispatcher throws an informative error</a>.</p><p>In the Facebook implementation of Flux you can see exactly what causes data to change. Every store includes a list of actions it listens to.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/260280d9320d593253364250df1fb007/href">https://medium.com/media/260280d9320d593253364250df1fb007/href</a></iframe><p>In this example, the <em>ThreadStore</em> listens to the <em>CLICK_THREAD</em> action and the <em>RECEIVE_RAW_MESSAGES</em> action. If the store is not updating as expected, the <em>register</em> callback gives us a place to start debugging. We can log any actions it receives and inspect their payload of data.</p><p>Similarly, every component maintains a list of which stores it listens to.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ab6c4ff36147a73c83691666f45fa96/href">https://medium.com/media/7ab6c4ff36147a73c83691666f45fa96/href</a></iframe><p>Above, we see the <em>ThreadSection</em> component listens for changes in the <em>ThreadStore</em> and the <em>UnreadThreadStore</em>. If we consistently use this method for setting up listeners in the components for changes in the stores, then we can be sure that no other stores will affect the behavior of this component.</p><p>Flux separates the concerns of receiving and sending data, so that when debugging you can easily follow the flow of data to see where things may be going wrong.</p><h3>Difficulties of Flux</h3><p>Every choice in software engineering is a trade-off, and Flux is no exception. We have perceived the following disadvantages:</p><ul><li>It involves writing more boilerplate code</li><li>Migrating existing resources can be a big task</li><li>Unit testing can be difficult without good structure</li></ul><p>Flux does add more files and more lines of code than one might feel are needed to handle the flow of data in an application. This will be more painful when writing fresh code for a new data resource, rather than adding code for a resource that already uses Flux. In the future we may have generators to make the set-up of Flux faster. <a href="https://medium.com/brigade-engineering/sharpen-your-vim-with-snippets-767b693886db">Using Vim snippets can also speed things up.</a></p><p>The easiest way to try Flux is with a new project. With anything new, getting others on board can be a challenge. This post, along with the <a href="http://facebook.github.io/flux/docs/overview.html">documentation</a> and <a href="https://github.com/facebook/flux/tree/master/examples">examples</a> from Facebook, will give you the knowledge needed for educating others. You can be confident that if Facebook and many other companies are using Flux for production code, in projects large and small, then it will scale for your project too.</p><p>When migrating an existing application to Flux, you can try out the Flux architecture with one data resource at a time. However, when considering using Flux to manage a piece of data in your application, think about how many components use that piece of data. If it is used in most of your components, then it could be a big job to move the management of that particular piece of data to Flux. Start with a more isolated piece of data when first trying Flux out.</p><p>With Flux your components start depending on <em>ActionCreators</em> and <em>Stores</em> as well as their usual dependencies on each other. This can make it more difficult to write unit tests. If you restrict the interaction with <em>Stores</em> to the top level “controller” components in your application, then you can unit test the child components without worrying about the <em>Stores</em>. For testing components that do need to send <em>Actions</em> and listen to <em>Stores</em>, we have had some success with mocking the <em>Store</em> methods, or mocking the API responses that the <em>Actions</em> and <em>Stores</em> use to get data.</p><h3>Have you tried Flux?</h3><p>At <a href="http://brigade.com/">Brigade</a>, we have learned from the experience of moving to a Flux approach. Have you tried Flux? Have you run into any difficulties, and how have you solved them? I look forward to seeing more examples and discussion of Flux in the future.</p><p>If you haven’t worked with Flux, then I hope this post gave you some insight into this approach to organizing an application.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b57ebca85b9e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Living in the Future]]></title>
            <link>https://flarnie.medium.com/living-in-the-future-8d577b5d73d9?source=rss-3169969abcb3------2</link>
            <guid isPermaLink="false">https://medium.com/p/8d577b5d73d9</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[transpilers]]></category>
            <dc:creator><![CDATA[Flarnie Marchan]]></dc:creator>
            <pubDate>Wed, 15 Oct 2014 01:15:18 GMT</pubDate>
            <atom:updated>2020-10-25T20:16:51.220Z</atom:updated>
            <content:encoded><![CDATA[<h3>ES6 Now</h3><p>The ECMAScript 6 standard (ES6, a.k.a. “harmony”), introduces <a href="https://github.com/lukehoban/es6features">new features</a> to add speed and elegance to JavaScript development including generators, classes, constants, and much more. Completion of the standard is <a href="https://twitter.com/awbjs/status/474662357516689410">targeted for June 2015</a>, but <a href="https://twitter.com/awbjs/status/474662760400564224">the features are stable</a> and well documented. We know exactly what fun new things will be added, and some ES6 features are already seeing support in browsers.</p><h3>Wouldn’t it be nice</h3><p>Too bad we can’t jump in a time machine and fast forward to a time with full browser support for fat arrow functions, destructuring, and the other shiny new toys of ES6.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*zi0PZmzlR8Z8UNq2Awsdnw.gif" /><figcaption>We need an ES6 time machine.</figcaption></figure><p>Surprise! You don’t actually need a time machine.</p><blockquote>You can use ES6 JavaScript features today.</blockquote><h3>How</h3><p>At <a href="https://www.brigade.com/">Brigade</a>, we rely on <a href="https://github.com/shama/es6-loader">webpack’s ES6 loader</a> to transpile our ES6 syntax into the comparable ES5 version. It uses two projects, the <a href="https://github.com/termi/es6-transpiler">es6-transpiler</a> and the <a href="https://github.com/esnext/es6-module-transpiler">es6-module-transpiler</a>, to support modules as well as a large portion of the ES6 syntax.</p><p>Even when using both of these transpilers, there are a few features that still are not supported: <a href="https://github.com/lukehoban/es6features#generators">generators</a>, <a href="https://github.com/lukehoban/es6features#symbols">symbols</a>, and <a href="https://mathiasbynens.be/notes/es6-unicode-regex">the ‘u’ flag for regular expressions</a>.</p><p>We have found that webpack and the es6 transpilers work for us, but you may want to try something different. There are <a href="https://github.com/addyosmani/es6-tools">dozens of other tools</a> available to provide support for ES6 features, such as:</p><ul><li>Google’s <a href="https://github.com/google/traceur-compiler">Traceur</a> compiler, which supports classes, generators, default parameters, and more.</li><li><a href="http://sweetjs.org/">Sweet.js</a> allows adding your own syntax to JavaScript with macros, and there are some <a href="https://github.com/jlongster/es6-macros">ES6 macros in development</a>.</li><li>An <a href="https://github.com/paulmillr/es6-shim">es6-shim</a> that supports maps, sets, promises, and a bunch of helpful methods.</li></ul><h3>Getting Started with ES6</h3><p>Looking at all the new features, it may seem daunting to start using them all at once. I have enjoyed choosing one at a time to integrate into my coding style.</p><h4>“Fat Arrow” Functions</h4><p>These save potential gotchas with the binding of <em>this</em>, by automatically binding the local <em>this</em> to the value of <em>this</em> in the surrounding scope. They are also more concise.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a97f39ee8d27f908cd0c90a6cca2562f/href">https://medium.com/media/a97f39ee8d27f908cd0c90a6cca2562f/href</a></iframe><h4>Block Scoping</h4><p>Block scoping helps avoid the trouble caused by variable values leaking out of blocks unintentionally. They are especially helpful in <em>for</em> loops, but I would use <em>let</em> in any place where I intend a variable to be local. To be even safer, if I don’t want a variable to accidentally get changed, it can be assigned with <em>const</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a525d7624ac0dcce0ddd692e48a2f93/href">https://medium.com/media/3a525d7624ac0dcce0ddd692e48a2f93/href</a></iframe><h4>Template Literals</h4><p>The template syntax for constructing strings improves readability and is more concise. For long or complex strings, it’s a relief to skip the contatenation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c02ae016a2ec1414610809722a1b5983/href">https://medium.com/media/c02ae016a2ec1414610809722a1b5983/href</a></iframe><h4>Destructuring Assignment</h4><p>Destructuring assignment makes variable assignment concise and flexible.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/960761e268f3ae4b62af963be363bfa8/href">https://medium.com/media/960761e268f3ae4b62af963be363bfa8/href</a></iframe><h4><em>Iterating with for .. of</em> Loops</h4><p>Iterating over the items in an array or object is a little bit nicer when we don’t need a nested conditional that calls <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty"><em>hasOwnProperty</em></a>. The ES6 <em>for .. of</em> syntax also filters even more strictly than the `hasOwnProperty` method, allowing us to just grab the <em>children</em> of an array.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a9b88310bb76c08d1c07390479332cd/href">https://medium.com/media/8a9b88310bb76c08d1c07390479332cd/href</a></iframe><h4>Default Function Arguments</h4><p>When trying to implement default arguments by hand in JavaScript, you have to remember to avoid the gotcha of overriding a parameter of zero or empty string, which will evaluate as “falsey”. Having a native way of adding default arguments saves us from this potential slip-up, and is more concise.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a7f85958f9ea49de52d7c6298a34fecb/href">https://medium.com/media/a7f85958f9ea49de52d7c6298a34fecb/href</a></iframe><p>The above list of ES6 features is not comprehensive, and more could be said about each feature. Check out the links to documentation in each example for more information.</p><h3>Watch Out</h3><p>As with any choice of technology, there are pitfalls to be considered when choosing to move to ES6. Until browsers fully support ES6, your code will be translated into ES5 syntax before the browser runs it, and this could lead to confusion.</p><p>For example; When using block scoping in your code, your variables will be given an alias if they are assigned within a block and then referenced outside of the block. This is how you get the effect of the variables being scoped to the block. The reference to <em>this</em> is also replaced with an aliased version of <em>this</em> in the transpiled version of “fat arrow” functions. This means that when you are debugging in your browser, you might be surprised by the variable names or the name being used for <em>this</em> when you try to inspect them.</p><p>The issue of having different code when debugging in the browser compared to your original source code will happen any time that you choose to use a pre-processer. For example, you might have similar issues when using <a href="http://coffeescript.org/">CoffeeScript</a> (gets compiled to JavaScript), or <a href="http://sass-lang.com/guide">Sass</a> (gets pre-processed to CSS).</p><h3>ES6 Rocks</h3><p>Since we started using ES6 in our codebase, we’ve developed the following JavaScript superpowers:</p><ul><li>The ES6 features improve readability and help us avoid JavaScript “gotchas”</li><li>Our understanding (and code) will be useful when ES6 is supported</li><li>We can help to “Move the Web Forward”</li></ul><p>The movement to increase knowledge and push for progress in web technology has been rolling along for some time now. By adopting and educating about web standards (like ECMAScript 6), we are doing our part to make the web awesome.</p><blockquote>“You can make the web as awesome as you want it to be.”<br><em>— </em><a href="http://movethewebforward.org/"><em>Move the Web Forward</em></a></blockquote><h3>Join the ES6 Club</h3><p>There are some great guides on the full featureset added in ES6 — check out the linked resources at the end for more.</p><p>I hope this post has inspired you to try some ES6 on your own, or even to bring it into your company’s codebase.</p><ul><li><a href="http://colintoh.com/blog/lightweight-es6-features">Lightweight ES6 Features that Pack a Punch</a> by Colin Toh, a web developer and UI designer</li><li><a href="http://espadrine.github.io/New-In-A-Spec/es6/">What’s New in ECMAScript 6th Edition</a> (A one page summary)</li><li><a href="http://people.mozilla.org/~jorendorff/es6-draft.html">ECMAScript Language Specification</a> (Unofficial HTML version) from mozilla.org</li><li><a href="https://github.com/google/traceur-compiler/wiki/LanguageFeatures">ES6 Language Features</a> from the Traceur Compiler Wiki</li><li><a href="http://www.slideshare.net/domenicdenicola/es6-the-awesome-parts">ES6 The <em>Awesome</em> Parts</a> Slides from Domenic Denicola, software engineer at Google Chrome</li><li><a href="https://www.youtube.com/watch?v=PSGEjv3Tqo0">It Gets Better: What’s New in JavaScript</a> (a video of Douglas Crockford)</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8d577b5d73d9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setting Up Webpack 
with Rails]]></title>
            <link>https://flarnie.medium.com/setting-up-webpack-with-rails-c62aea149679?source=rss-3169969abcb3------2</link>
            <guid isPermaLink="false">https://medium.com/p/c62aea149679</guid>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[rails]]></category>
            <dc:creator><![CDATA[Flarnie Marchan]]></dc:creator>
            <pubDate>Fri, 15 Aug 2014 19:11:20 GMT</pubDate>
            <atom:updated>2020-10-25T20:15:03.120Z</atom:updated>
            <content:encoded><![CDATA[<h2>Setting Up Webpack<br>with Rails</h2><h4>Organize your application’s dependencies<br>and speed up your team.</h4><p>In Ruby on Rails, <a href="http://en.wikipedia.org/wiki/Convention_over_configuration">convention over configuration</a> is the paradigm. The convention in Rails for managing JavaScript dependencies is metacomments in JavaScript manifest files. The scripts are then loaded by <a href="https://github.com/sstephenson/sprockets">Sprockets</a>.</p><p>While this works great for smaller applications, larger applications have more dependencies. When the list of required scripts grows long, figuring out which files depend on each other grows increasingly time consuming. Any reorganization beyond adding files to the list is a pain, because you need to search your entire JavaScript codebase to know where a script is being used.</p><h3>Modular JavaScript</h3><p><strong>How do we make it clear which files depend on each other?</strong> One way is to divide our JavaScript into modules that can be loaded with locally specified dependencies. Loading dependencies in each file also keeps the global scope clean. There are many tools to facilitate this type of script loading (e.g. <a href="http://browserify.org/">Browserify</a>, <a href="http://requirejs.org/">RequireJS</a>, and <a href="http://webpack.github.io/">webpack</a>) and there are two styles (<a href="http://wiki.commonjs.org/wiki/Modules">CommonJS</a> and <a href="https://github.com/amdjs/amdjs-api/blob/master/AMD.md">AMD</a>) for loading modules.</p><p>At <a href="http://brigade.com/">Brigade</a> we wanted a JavaScript module bundler that would:</p><ul><li>Integrate simply with Rails</li><li>Allow some JavaScript to be served via Sprockets</li><li>Support CommonJS module definitions</li></ul><p>We found that webpack offered the features we were looking for.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*gFcLgEf7tBjuAqnQ0I8XsA.png" /></figure><h3>How Does Webpack Work?</h3><p>Similar to the way that all scripts are listed in a JavaScript manifest when using Sprockets, all dependencies are listed in an ‘entry point’ script when using webpack. For example, let’s say we want to load an ‘init.js’ script that will render a view on the page. We would require it in the entry point script. Inside the ‘init.js’ file, we require the JavaScript view that will be rendered on the page, as well as a model that will be passed to the view:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9b76cf029bc279655959db6313e08baa/href">https://medium.com/media/9b76cf029bc279655959db6313e08baa/href</a></iframe><p>The JavaScript view and model, in turn, list their own dependencies, which in turn may list more dependencies, and so on; it’s turtles all the way down. Webpack recursively finds all the required dependencies and concatenates them into one “bundle”, outputting the code so that it’s all loaded in the correct order.</p><p>Whatever the ‘module.exports’ is set to in each of the required files will be wrapped in an anonymous function, and these functions are stored in an array in no particular order. Then, instead of requiring a filename, they are referenced by their index in the array, and the contents are loaded as needed.</p><p>Once one of the callbacks is run, it is marked as ‘loaded’ and its ‘exports’ are cached for the next time it is referenced in your code. Here is a simplified example of how the bundled version works:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d35909d025631ae9f2774bdc6fa1f68d/href">https://medium.com/media/d35909d025631ae9f2774bdc6fa1f68d/href</a></iframe><h3>Setting Up Webpack</h3><p>Setting up webpack was simple as running</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc1c47898733a2650ca103ca2b40e648/href">https://medium.com/media/dc1c47898733a2650ca103ca2b40e648/href</a></iframe><p>and writing the ‘webpack.config’ to output javascript in the right place. We’ve embedded an example config file below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9fcd02ea3c369e4b889762ec326ade5/href">https://medium.com/media/f9fcd02ea3c369e4b889762ec326ade5/href</a></iframe><h3>Integrating Webpack With Rails</h3><p>Because we use a hybrid system that utilizes both the Sprockets and webpack, our JavaScript is built and served in many steps:</p><ol><li>Webpack runs and compiles a bundle based on the scripts required in the entry point script.</li><li>The bundle compiled by webpack is required in the JavaScript manifest, along with any scripts that are provided directly by gems.</li><li>Rails uses Sprockets to compile and serve scripts listed in the JavaScript manifest.</li></ol><h3>Developing with Webpack</h3><p>When setting up a development environment, or making changes to our JavaScript, we need to run webpack in order to generate the bundled version of our scripts.</p><p>Fortunately, <a href="http://webpack.github.io/docs/cli.html#watch-mode-watch">webpack has a “watch mode”</a> where it will continuously watch your JavaScript files and re-compile the bundle when a change is detected. In order to make it easier to start this process whenever running the Rails server, we use <a href="https://github.com/ddollar/foreman">foreman</a> to start these and other relevant processes with one command.</p><h3>Using Loaders With Webpack</h3><p>While processing and concatenating your scripts, webpack can also minify or make other types of changes to your code using <a href="http://webpack.github.io/docs/loaders.html">loaders</a>.</p><p>Anyone can write a loader for webpack, because it is modular in nature. There are <a href="http://webpack.github.io/docs/list-of-loaders.html">dozens of webpack loaders</a> that add a range of features, and being able to write your own loaders makes it flexible.</p><h4>Living In The Future</h4><p>One of our favorite loaders is the <a href="https://github.com/shama/es6-loader">es6 loader</a>, which enables us to use all kinds of fun <a href="http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts#may_14_2013_draft">ECMAScript 6th edition</a> (ES6) syntax in our code. ES6 is the latest proposed standard for ECMAScript, which is implemented as JavaScript in most browsers and JScript in Internet Explorer. The features proposed in the ES6 draft are not currently supported by all browsers, but our developers like to play with the latest toys. The “fat arrow function” is one example of a new JavaScript feature we have started using:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47b14e4121f862dcd6f89fa315238dc7/href">https://medium.com/media/47b14e4121f862dcd6f89fa315238dc7/href</a></iframe><p>Because it is automatically bound to the enclosing scope, it helps us avoid errors caused by forgetting to bind callbacks to ‘this’. It’s also a concise shorthand for anonymous functions.</p><h3>Gotchas</h3><p>We considered completely replacing Sprockets with a tool like webpack. In our case, there was the added challenge of using some scripts that are provided directly by gems. For example, we use a gem called <a href="https://github.com/railsware/js-routes">js-routes</a> that gives our JavaScript access to Rails URL helpers. Adding a metacomment is the way to make the URL helpers available in our JavaScript:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/08f7d6df6c1cdc6e06846cde842bfe88/href">https://medium.com/media/08f7d6df6c1cdc6e06846cde842bfe88/href</a></iframe><p>We use several gems that load JavaScript in this way, so we wanted to keep loading some with Sprockets and use a module bundler for the rest.</p><h3>Choose Wisely</h3><p>Making technical decisions is all about balancing sets of tradeoffs. It’s worth doing a little research and using the tool best suited to your project’s needs.</p><p>We’ve used Sprockets to load JavaScript in the past, and we’ve seen how moving to webpack improved the organization of our application and allows us to work faster. I hope that by sharing our perspective I helped you improve your JavaScript build system too.</p><h3>Webpack with Rails Demo Repository</h3><p><em>(This section added on Thursday, Sept. 4th.)</em></p><p>A reader requested a full example of how to set up Ruby on Rails with webpack, so I have created <a href="https://github.com/flarnie/webpack_rails_demo">a sample repository</a>. Hopefully it will help demonstrate one way to set this up!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c62aea149679" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>