<?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 Spencer Elliott on Medium]]></title>
        <description><![CDATA[Stories by Spencer Elliott on Medium]]></description>
        <link>https://medium.com/@spe_?source=rss-ff3d2977e897------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Spencer Elliott on Medium</title>
            <link>https://medium.com/@spe_?source=rss-ff3d2977e897------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 09:22:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@spe_/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[Debugging C/C++ Programs Remotely Using Visual Studio Code and gdbserver]]></title>
            <link>https://medium.com/@spe_/debugging-c-c-programs-remotely-using-visual-studio-code-and-gdbserver-559d3434fb78?source=rss-ff3d2977e897------2</link>
            <guid isPermaLink="false">https://medium.com/p/559d3434fb78</guid>
            <category><![CDATA[gdb]]></category>
            <category><![CDATA[visual-studio-code]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[vscode]]></category>
            <dc:creator><![CDATA[Spencer Elliott]]></dc:creator>
            <pubDate>Wed, 23 Nov 2016 21:24:49 GMT</pubDate>
            <atom:updated>2016-11-23T21:33:50.151Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’re like me and prefer using a GUI to a command line for setting breakpoints, stepping through code, and inspecting values as your program runs, here is how you can set up VSCode and gdbserver to edit and debug your code locally while running it on a remote server.</p><p><em>Background: I’m working on an </em><a href="http://www.teach.cs.toronto.edu/~csc469h/fall/assignments/a3/a3.shtml"><em>assignment for CSC469 at the University of Toronto</em></a><em>, and it will only compile and run on the university’s teaching lab machines. My goal was to be able to edit and debug locally in a familiar editor while compiling and running on the remote teaching lab machines.</em></p><p>Note: I’m using macOS Sierra locally, with the remote machine running Ubuntu 14.04, but this guide <em>should</em> work with any Unix system. (Sorry, Windows users).</p><p>Note: Commands to be run on the remote machine are prefixed with remote$ and local commands are prefixed with local$ .</p><h4>1. Install gdbserver on the remote machine</h4><p>Installation varies by system. On Debian/Ubuntu, you can do:</p><pre>remote$ apt-get install gdbserver</pre><p>Since students are not allowed to install software via apt on the U of T machines, I used <a href="http://linuxbrew.sh/">Linuxbrew</a> to install it to my user folder:</p><pre>remote$ brew install gdbserver</pre><h4>2. Install gdb on your local machine</h4><p>On macOS Sierra, I used <a href="http://brew.sh/">Homebrew</a> to install gdb:</p><pre>local$ brew install gdb --with-all-targets</pre><p><em>Note: The </em><em>--with-all-targets option is important; without it, you won’t be able to debug on a remote machine with a different OS or architecture than your local machine.</em></p><h4>3. Test gdb</h4><p>At this point, you should be able to run gdbserver on the remote machine and connect to it from your local gdb CLI. I’ll use the -L option of <a href="http://man.openbsd.org/ssh">ssh</a> to forward local port 9091 connections to the remote port 9091:</p><pre>local$ ssh -L9091:localhost:9091 user@remote<br>remote$ cd ./myproject/ &amp;&amp; make<br>remote$ gdbserver :9091 ./myprogram</pre><p><em>(Port 9091 is arbitrary; use any port number you like)</em></p><p>Leave that command running in a terminal window; it will wait until gdb connects before running ./myprogram .</p><p>In another terminal window on your local machine, run gdb:</p><pre>local$ gdb<br>GNU gdb (GDB) 7.12<br>Copyright (C) 2016 Free Software Foundation, Inc.<br>...<br>For help, type &quot;help&quot;.<br>Type &quot;apropos word&quot; to search for commands related to &quot;word&quot;.<br>(gdb)</pre><p>Then connect to the gdbserver instance:</p><pre>(gdb) target remote localhost:9091<br>Remote debugging using localhost:9091<br>...<br>(gdb)</pre><p>To verify things are working, you can run various gdb commands like info sources , or set a breakpoint with break . Use continue to run ./myprogram .</p><h4>4. codesign gdb</h4><p>VSCode prevents you from running gdb unless it’s signed; <a href="https://sourceware.org/gdb/wiki/BuildingOnDarwin#Method_for_Mac_OS_X_10.5_.28Leopard.29_and_later">follow these steps to ensure it’s signed</a>.</p><h4>5. Synchronize local and remote file systems</h4><p>You may have noticed that, for basic functionality, the gdb CLI on your local machine doesn’t need to be provided any information about the program or its source code except for the host and port on which gdbserver is running. But there are two big reasons why you’ll want to keep your local and remote project directories in sync:</p><ul><li>Viewing source code in the gdb CLI (i.e. list).</li><li>The <a href="https://github.com/Microsoft/vscode-cpptools/blob/master/launch.md#configure-vs-codes-debugging-behavior">VSCode C/C++ extension</a> requires you to provide the path to the compiled executable to launch gdb. (The &quot;program&quot; field in launch.json).</li></ul><p>I opted to use <a href="https://github.com/libfuse/sshfs">sshfs</a> since it required the least server-side setup, but you could use NFS, rsync, or other alternatives.</p><p>Using sshfs, mount the remote project folder locally:</p><pre>local$ mkdir ./myproject<br>local$ sshfs user@remote:myproject ./myproject</pre><p><em>Note: on macOS, you can later unmount the directory with </em><em>umount ./myproject. On Linux, use </em><em>fusermount -u ./myproject.</em></p><h4>6. Configure Visual Studio Code</h4><p>Open your newly-mounted project ./myproject/ in VSCode, then open .vscode/launch.json or create it if it doesn’t exist:</p><pre>{<br>  &quot;version&quot;: &quot;0.2.0&quot;,<br>  &quot;configurations&quot;: [<br>    {<br>      &quot;name&quot;: &quot;C++ Launch&quot;,<br>      &quot;type&quot;: &quot;cppdbg&quot;,<br>      &quot;request&quot;: &quot;launch&quot;,<br>      &quot;program&quot;: &quot;${workspaceRoot}/myprogram&quot;,<br>      &quot;miDebuggerServerAddress&quot;: &quot;localhost:9091&quot;,<br>      &quot;args&quot;: [],<br>      &quot;stopAtEntry&quot;: false,<br>      &quot;cwd&quot;: &quot;${workspaceRoot}&quot;,<br>      &quot;environment&quot;: [],<br>      &quot;externalConsole&quot;: true,<br>      &quot;linux&quot;: {<br>        &quot;MIMode&quot;: &quot;gdb&quot;<br>      },<br>      &quot;osx&quot;: {<br>        &quot;MIMode&quot;: &quot;gdb&quot;<br>      },<br>      &quot;windows&quot;: {<br>        &quot;MIMode&quot;: &quot;gdb&quot;<br>      }<br>    }<br>  ]<br>}</pre><p>This configuration will make it so clicking “C++ Launch” will run gdb similar to:</p><pre>local$ gdb ./myprogram<br>...<br>(gdb) target remote localhost:9091</pre><h4>7. Write a script to compile your program and launch gdbserver</h4><p>Ideally, you’d want to be able to run a single command or click a single button to compile &amp; debug your program.</p><p>This might be possible to do via <a href="https://code.visualstudio.com/Docs/editor/tasks">VSCode tasks</a> and the <a href="https://tstringer.github.io/javascript/vscode/nodejs/gulpjs/2015/10/14/vscode-prelaunchtask.html">preLaunchTask option in launch.json</a>, but I was not able to put together a simple solution using those.</p><p>Instead I wrote a quick-and-dirty shell script prepare_remote_debug.sh :</p><pre># Kill gdbserver if it&#39;s running<br>ssh <a href="/service/mailto:ellio128@teach.cs.toronto.edu">user@r</a>emote killall gdbserver &amp;&gt; /dev/null</pre><pre># Compile myprogram and launch gdbserver, listening on port 9091<br>ssh \<br>  -L9091:localhost:9091 \<br>  user@remote \<br>  &quot;zsh -l -c &#39;cd myproject &amp;&amp; make &amp;&amp; gdbserver :9091 ./myprogram&#39;&quot;</pre><h4>8. Start debugging</h4><p>Here is your new workflow:</p><ol><li>Edit some code.</li><li>Run ./prepare_remote_debug.sh in a terminal window. Your program’s output will appear here.</li><li>Set some breakpoints.</li><li>Run “C++ Launch”.</li><li>Step through your code in VSCode’s debugger.</li><li>Repeat.</li></ol><p>That’s it! I hope this helps someone, and let me know if it can be simplified even further.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=559d3434fb78" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Incrementally Switch to webpack]]></title>
            <link>https://medium.com/eventmobi/how-to-incrementally-switch-to-webpack-203a1b431f7a?source=rss-ff3d2977e897------2</link>
            <guid isPermaLink="false">https://medium.com/p/203a1b431f7a</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[software]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Spencer Elliott]]></dc:creator>
            <pubDate>Tue, 06 Sep 2016 12:46:02 GMT</pubDate>
            <atom:updated>2016-09-06T13:31:30.075Z</atom:updated>
            <content:encoded><![CDATA[<p>This is the second of a two-part series on why and how we switched our JavaScript bundling system from an ad hoc system of Grunt tasks and PHP, to a declarative webpack configuration. Click here to go to <a href="https://medium.com/eventmobi/why-we-switched-to-webpack-69b7396f3ec5"><em>Why We Switched to webpack</em></a> to find out why we switched to webpack.</p><p>This article is for you if your codebase’s JavaScript build system has problems similar to ours in <a href="https://medium.com/eventmobi/why-we-switched-to-webpack-69b7396f3ec5"><em>Why We Switched to webpack</em></a>, and you want to migrate to webpack in an <em>incremental</em> fashion by dividing it into <a href="https://medium.com/@kurtisnusbaum/large-diffs-are-hurting-your-ability-to-ship-e0b2b41e8acf#.r4gp5amj6">smaller steps</a>. Some parts of this article are specific to AngularJS 1.x, but the rest can be applied to any framework.</p><p>To make the migration as painless as possible, break it down into these steps:</p><ol><li>Introduce webpack <em>alongside</em> your existing build system <em>without</em> changing your application code.</li><li>Use webpack during development for a period of time, and resolve issues as they come up. Deprecate usage of the old build system, but continue to use it in production.</li><li>Remove the deprecated build system, leaving only webpack.</li><li>Update the codebase with improvements which are now possible with webpack.</li></ol><p>By breaking the migration down into these steps, you avoid spending the initial time that would have been spent testing everything and covering all edge cases to ensure the new build system works in production; simply continue to use your existing build system for production deploys.</p><p>During development, you immediately benefit from certain webpack features, like fast rebuild times, and sourcemap-based debugging, and if there’s a problem with the new build step, you can always fall back to your old one.</p><h3>1. Introduce webpack <em>alongside</em> your existing build system</h3><p>Start by replicating your old build system’s core features necessary to build your application:</p><ul><li>Creating a JavaScript bundle</li><li>Creating a CSS bundle</li><li>Rendering JS / CSS asset paths to your HTML template</li></ul><p>Note that you can exclude more advanced requirements from this list, such as minification, bundling for karma tests, or bundling translated language strings. These can be handled during step #2.</p><h4>Creating a JavaScript bundle</h4><p>Your existing build system probably involves a concatenation step to combine many scripts into one, such as with <a href="https://github.com/gruntjs/grunt-contrib-concat">grunt-contrib-concat</a>:</p><pre>grunt.initConfig({<br>  concat: {<br>    js: {<br>      files: [{<br>        src: [<br>          &#39;vendor/lodash.js&#39;,<br>          &#39;vendor/jquery.js&#39;,<br>          &#39;vendor/angular.js&#39;,<br>          &#39;vendor/angular-cookies.js&#39;,<br>          &#39;app/scripts/**/*.js&#39;<br>        ],<br>        dest: &#39;build/js/&#39;<br>      }]<br>    }<br>  }<br>});</pre><p>Luckily, webpack is flexible enough to replicate this behaviour using <a href="https://github.com/webpack/imports-loader">imports-loader</a>, <a href="https://github.com/webpack/exports-loader">exports-loader</a>, and <a href="https://webpack.github.io/docs/context.html">context</a>:</p><p>To make your webpack JS bundle behave identically to your old script-concatenation bundle, create a new <strong>.js</strong> file to serve as your webpack entry point, and use imports-loader and exports-loader to import dependencies and export values for your vendor scripts, e.g.</p><pre>// app/app.js</pre><pre>// Import legacy vendor scripts in the correct order<br>window._ = require(<br>  &#39;../vendor/lodash&#39;<br>);<br>window.$ = window.jQuery = require(<br>  &#39;../vendor/jquery&#39;<br>);<br>window.angular = require(<br>  &#39;exports?window.angular!../vendor/angular&#39;<br>);<br>require(<br>  &#39;imports?angular=&gt;window.angular!&#39; +<br>  &#39;../vendor/angular-cookies&#39;<br>);</pre><pre>// ... application scripts</pre><p>Since each vendor script has different dependencies and exported values, each must be inspected manually to determine whether &amp; how to use imports-loader and exports-loader. Be sure to assign global variables to `window` to ensure they are available in your application code.</p><p>If your application code also needs to be executed in a particular order, you can simply `require` each file in sequence, e.g.</p><pre>// app/app.js</pre><pre>// ... vendor scripts</pre><pre>require(&#39;./scripts/moduleA&#39;);<br>require(&#39;./scripts/moduleB&#39;);<br>require(&#39;./scripts/moduleC&#39;);<br>// ...<br>require(&#39;./scripts/main&#39;);</pre><p>Or, if your application code can be executed in any order (e.g. if you use <a href="https://docs.angularjs.org/api/ng/function/angular.module">AngularJS modules</a>), you can make use of webpack <a href="https://webpack.github.io/docs/context.html">context</a> to `require` all of these files at once:</p><pre>// app/app.js</pre><pre>// ... vendor scripts</pre><pre>/**<br> * `require` all modules in the given webpack context<br> */<br>function requireAll(context) {<br>  context.keys().forEach(context);<br>}</pre><pre>// Collect all angular modules<br>requireAll(require.context(<br>  &#39;./scripts&#39;,<br>  /* use subdirectories: */ true,<br>));</pre><p>For handling your JS files, that’s all there is to it! No changes to the vendor scripts or application code needed to be made; you only need to modify the webpack entry point. It’s important that you don’t modify the application code itself, to ensure that you can continue to build using your previous build system.</p><h4>Creating a CSS bundle</h4><p>Your CSS build step probably involves a preprocessor step, such as with <a href="http://stylus-lang.com/">Stylus</a> via <a href="https://github.com/gruntjs/grunt-contrib-stylus">grunt-contrib-stylus</a>:</p><pre>grunt.initConfig({<br>  stylus: {<br>    compile: {<br>      files: {<br>        src: &#39;app/css/main.styl&#39;,<br>        dest: &#39;build/css/main.css&#39;<br>      }<br>    }<br>  }<br>});</pre><p>webpack offers a set of loaders to handle CSS preprocessing and output:</p><ul><li><a href="https://github.com/shama/stylus-loader">stylus-loader</a> / <a href="https://github.com/jtangelder/sass-loader">sass-loader</a> / <a href="https://github.com/webpack/less-loader">less-loader</a>: runs the given preprocessor on your CSS file(s), returning the plain CSS</li><li><a href="https://github.com/webpack/css-loader">css-loader</a>: resolves `@import` / `url(https://medium.com/%E2%80%A6)` and returns the resulting CSS</li><li><a href="https://github.com/webpack/url-loader">url-loader</a> / <a href="https://github.com/webpack/file-loader">file-loader</a>: inlines or emits file contents in webpack’s output, like font or image `url(https://medium.com/%E2%80%A6)`s in CSS</li><li><a href="https://github.com/webpack/extract-text-webpack-plugin">extract-text-webpack-plugin</a>: moves your CSS to a separate output file, rather than inlining it in the HTML</li></ul><p>Configuring webpack with these loaders is left as an exercise for the reader, but once it’s configured, adding your stylesheet to the webpack build is as simple as adding a `require(…)` to your entry point:</p><pre>// app/app.js</pre><pre>require(&#39;./css/main.styl&#39;);</pre><pre>// ... vendor scripts</pre><pre>// ... application scripts</pre><p><em>Note</em>: the CSS build step doesn’t <em>necessarily</em> have to be part of the webpack build; for example, we could have opted to switch to a simple npm script invoking the `stylus` command. But we get some benefits by using webpack:</p><ul><li>Only webpack needs to be run to build both JS and CSS; no need to run a separate command to build CSS.</li><li>Image / font assets referenced in CSS are automatically included in webpack’s output; no need to copy them to your output folder and manage asset paths via some other method.</li><li>file-loader will automatically generate hashed filenames for long-term caching of the CSS file <em>and</em> included fonts / images.</li></ul><h4>Rendering JS / CSS asset paths to your HTML template</h4><p>The final step to be able to run both webpack and your old build system on the same codebase is to render the new webpack JS / CSS assets to your site’s HTML template.</p><p>You can capture the paths of all of webpack’s output assets using the <a href="https://webpack.github.io/docs/node.js-api.html#stats">`Stats` object that is returned when the build completes</a>. A simple way to pass that data to your HTML template is via <a href="https://www.npmjs.com/package/stats-webpack-plugin">stats-webpack-plugin</a>:</p><pre>// webpack.config.js</pre><pre>const StatsPlugin = require(&#39;stats-webpack-plugin&#39;);</pre><pre>module.exports = {<br>  // ...<br>  plugins: [<br>    // ...<br>    new StatsPlugin(&#39;webpack-stats.json&#39;, {<br>      chunks: false,<br>      modules: false,<br>      children: false,<br>      cached: false,<br>      reasons: false,<br>      source: false,<br>      errorDetails: false,<br>      chunkOrigins: false,<br>    })<br>  ]<br>};</pre><p>To easily switch back and forth between webpack and your old build system, you can simply render webpack’s asset paths if <em>webpack-stats.json</em> exists in your output folder, else fall back to your old build system’s asset paths.</p><p>For example, using PHP:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a5e6c1a6981919615fe97967e7f3f0c/href">https://medium.com/media/5a5e6c1a6981919615fe97967e7f3f0c/href</a></iframe><p>Those are all of the pieces necessary to get a minimal webpack build working! At this point, your project should be in this state:</p><ul><li>Run your old build step (e.g. `grunt build`), which will create JS / CSS assets in your build folder, which causes your site’s HTML template to render asset paths in the old way.</li><li>Run `webpack`, which will create JS / CSS assets and <em>webpack-stats.json</em> in your build folder, which causes your site’s HTML template to render asset paths using <em>webpack-stats.json</em>.</li></ul><h3>2. Use webpack during development and deprecate usage of the old build system</h3><p>During this stage, tell other developers on the project to use webpack instead of the old build system. If any problems arise (such as a misconfigured loader), you can resolve them safely without affecting production, since production continues to use the old build system for now.</p><p>At this stage, you should also configure replacements for any other tasks that the old build system does, such as <a href="http://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin">asset minification</a>, <a href="https://github.com/webpack/karma-webpack">karma test execution</a>, or language translations.</p><p>If you use a task runner like Grunt or Gulp, then <a href="https://medium.freecodecamp.com/why-i-left-gulp-and-grunt-for-npm-scripts-3d6853dd22b8#.2axfpr1bl">npm scripts are often a simpler alternative</a> to Grunt/Gulp tasks.</p><p>Here are some Grunt task alternatives that we use:</p><ul><li><a href="https://github.com/karma-runner/grunt-karma">grunt-karma</a>: <a href="https://karma-runner.github.io/latest/intro/configuration.html">Karma’s command-line interface</a> (`karma start`)</li><li><a href="https://github.com/rubenv/grunt-angular-gettext">grunt-angular-gettext</a>: <a href="https://github.com/huston007/angular-gettext-cli">angular-gettext-cli</a> + <a href="https://github.com/princed/angular-gettext-loader">angular-gettext-loader</a></li><li><a href="https://github.com/ericclemmons/grunt-angular-templates">grunt-angular-templates</a>: <a href="https://github.com/WearyMonkey/ngtemplate-loader">ngtemplate-l0ader</a></li></ul><p>At the end of this stage, you should have:</p><ul><li>An npm script to replace each build task, e.g. `npm run bundle:watch` to bundle &amp; watch during development, `npm run bundle:production` to bundle &amp; minify with language translations, `npm run karma` to run Karma tests</li><li>Thoroughly tested your entire application to ensure it works properly: watch out for missing scripts or stylesheets as these can cause your app to break.</li></ul><h3>3. Remove the deprecated build system, leaving only webpack</h3><p>This part is easy: simply configure your CI build script to run your new npm build scripts (like `npm test &amp;&amp; npm run bundle:production`), delete your old task runner configuration (Gruntfile.js / gulpfile.js / etc.), and remove now-unused dependencies from package.json.</p><p>You’ll also want to delete the code in your HTML template that falls back to your old build system if <em>webpack-stats.json</em> doesn’t exist.</p><p>If you’ve thoroughly tested your application in the previous stage, this stage should go off without a hitch. Of course, you should first deploy to a production-like test environment first to be safe.</p><h3>4. Update the codebase with improvements enabled by webpack</h3><p>Now that webpack is the only build system in place, we can address the issues originally described in <a href="https://medium.com/eventmobi/why-we-switched-to-webpack-69b7396f3ec5"><em>Why We Switched to webpack</em></a><em>:</em></p><h4>Resolving dependencies</h4><p>Remember how you setup the webpack entry point to import scripts globally, in dependency-first order?</p><pre>// app/app.js</pre><pre>require(&#39;./css/main.styl&#39;);</pre><pre>// Import legacy vendor scripts in the correct order<br>window._ = require(<br>  &#39;../vendor/lodash&#39;<br>);<br>// ...</pre><pre>require(&#39;./scripts/moduleA&#39;);<br>require(&#39;./scripts/moduleB&#39;);<br>require(&#39;./scripts/moduleC&#39;);<br>// ...<br>require(&#39;./scripts/main&#39;);</pre><p>It’s not ideal to continue importing them this way: ensuring the order is correct can be error-prone, and polluting the global namespace can be problematic.</p><p>Instead, you can now import dependencies <em>in the module where they are used</em>, e.g. if <strong>main.js</strong> depends on <strong>lodash.js</strong>, <strong>moduleA.js</strong>, and <strong>moduleC.js</strong>:</p><pre>// app/scripts/main.js</pre><pre>var _ = require(&#39;../../vendor/lodash&#39;);<br>var A = require(&#39;./moduleA&#39;);<br>var C = require(&#39;./moduleC&#39;);</pre><pre>// ...</pre><p>And if <strong>moduleB.js</strong> depends only on <strong>moduleA.js</strong>:</p><pre>// app/scripts/moduleB.js</pre><pre>var A = require(&#39;./moduleA&#39;);</pre><pre>// ...</pre><p>Once a module is imported explicitly by all of its dependents, it can be removed from the webpack entry point: transition one module at a time, and eventually your webpack entry point will be left with just:</p><pre>// app/app.js</pre><pre>require(&#39;./css/main.styl&#39;);</pre><pre>require(&#39;./scripts/main&#39;);</pre><h4>Using npm packages</h4><p>Now you can start using npm packages instead of copying 3rd-party vendor modules to the repository: upgrading packages becomes easier, shared transitive dependencies reduce code duplication, and you no longer need to create custom UMD bundles.</p><p>It should be as simple as doing an `npm install &lt;package&gt;`, then updating references to the module, e.g. change this:</p><pre>var _ = require(&#39;../../vendor/lodash&#39;);</pre><p>To this:</p><pre>var _ = require(&#39;lodash&#39;);</pre><p>Now that dependencies are explicit and we’re using npm packages, we’ve achieved (at least somewhat) the goal of <strong>making the codebase easier to understand</strong>. And we have <strong>improved dev-prod parity</strong> too, since webpack is now handling our module dependencies the same way in both development and production: the only difference being that the production build is minified.</p><p>Of course, we also <strong>made the codebase ready for a future </strong>where it’s easier to start using ES2015+ by turning on <a href="https://github.com/babel/babel-loader">babel-loader</a>, and using React and Redux becomes easier with the ability to use JSX and npm packages.</p><p>This was the transition that <a href="http://www.eventmobi.com/">EventMobi</a>’s primary event app codebase recently made successfully. If your project is in similar shape, I hope this has provided some insight on how to improve it!</p><p><em>[If you liked this article, click the little </em>❤️ <em>on the left so other folks know. If you want more posts like this, follow EventMobi publication with the (Follow) button on the right!</em></p><p><em>Finally if solving problems like this seems up your alley, we’re hiring! Checkout our current open positions at </em><a href="http://www.eventmobi.com/careers/"><em>http://www.eventmobi.com/careers/</em></a><em>]</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=203a1b431f7a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/eventmobi/how-to-incrementally-switch-to-webpack-203a1b431f7a">How to Incrementally Switch to webpack</a> was originally published in <a href="https://medium.com/eventmobi">EventMobi</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why We Switched to webpack]]></title>
            <link>https://medium.com/eventmobi/why-we-switched-to-webpack-69b7396f3ec5?source=rss-ff3d2977e897------2</link>
            <guid isPermaLink="false">https://medium.com/p/69b7396f3ec5</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[angularjs]]></category>
            <category><![CDATA[webpack]]></category>
            <dc:creator><![CDATA[Spencer Elliott]]></dc:creator>
            <pubDate>Mon, 29 Aug 2016 12:16:02 GMT</pubDate>
            <atom:updated>2016-08-30T15:06:36.554Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*HNx9A5pT5JJJ1_4YCFqXRw.png" /></figure><p>This is the first of a two-part series on why and how we switched our JavaScript bundling system from an ad hoc system of Grunt tasks and PHP, to a declarative webpack configuration. This post will cover the “why” we did it — Stay tuned for a post that covers the “how”!</p><blockquote>Hi! I’m Spencer, a Computer Science student at the University of Toronto. This was an initiative that I kicked off during my internship while working with the event app team at EventMobi.</blockquote><p>Until recently, <a href="http://www.eventmobi.com/">EventMobi</a>’s primary event app codebase had used an ad hoc system of Grunt tasks and PHP to bundle JavaScript for the browser:</p><ul><li>Scripts and stylesheets were collected in a dependencies-first order and piped through some preprocessors.</li><li>For production builds, another task would concatenate and minify scripts and stylesheets into one JS and one CSS artifact, but in development, we had PHP render each individual script and stylesheet into a new `&lt;script&gt;` / `&lt;link&gt;` on the page so that individual files can be found in the debugger.</li><li>Like the <a href="https://github.com/angular/angular-seed/tree/18a925eb74efcbb34d0e05576234b5d5fb43b8d8">angular-seed starter kit</a>, application code was separated into modules using `<a href="https://docs.angularjs.org/api/ng/function/angular.module">angular.module</a>`: each file had one `angular.module()` definition with a unique string name to identify the module. We relied on AngularJS’s module names and <a href="https://docs.angularjs.org/guide/di">dependency injection</a> to resolve dependencies between modules.</li></ul><p>This build system had served us well enough, but we eventually ran up against some pain points which could be addressed by using a module bundler like <a href="https://webpack.github.io/">webpack</a>:</p><h4>1. Resolving dependencies was tedious</h4><p>Scripts had to be collected in a specific order, otherwise dependencies would be out-of-order: first load jQuery, then load AngularJS, then load AngularJS plugins, etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R27qtgJbsMIM_tjF65680g.png" /><figcaption>Managing dependencies with &lt;script&gt; tags</figcaption></figure><p>With a module bundler, dependencies are imported using CommonJS `require` statements in the file where they are used, and combined into one JS bundle. So dependencies are guaranteed to be available: no need to render many `&lt;script&gt;` tags in a specific order.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sEEdBqa78Amyp4G8qUgkXw.png" /><figcaption>Managing dependencies with CommonJS modules</figcaption></figure><h4>2. Hard to use npm packages</h4><p>Importing packages from npm required us to manually create a <a href="https://github.com/umdjs/umd">UMD</a> bundle if one wasn’t available. Want to use <a href="https://github.com/angular-redux/ng-redux">ng-redux</a>? Create a UMD bundle and add it the the list of vendor scripts. Want to use <a href="https://www.npmjs.com/package/left-pad">left-pad</a>? Create a UMD bundle.</p><p>In Node.js it’s common, and <a href="https://github.com/sindresorhus/ama/issues/10#issuecomment-117766328">arguably just good design</a> to publish and consume tiny modules from npm like left-pad. But the overhead of creating and managing UMD bundles meant it was often easier to ‘reinvent the wheel’ by re-implementing common functionality than use an npm package.<br>Oh, and what about when two npm packages share a common dependency? There’s no simple way to share that common dependency between the two UMD bundles; that dependency is now duplicated in each bundle, wasting valuable bytes.</p><p>With a module bundler, npm packages can be used with a simple</p><pre>npm install &lt;name&gt;</pre><p>and</p><pre>require(&#39;&lt;name&gt;&#39;);</pre><p>No need to create UMD bundles. And if two npm packages share a common dependency, both will share the same code without duplication.</p><h4>3. Poor dev-prod parity</h4><p>Scripts loaded during development were sometimes different from scripts loaded in production, since the PHP `&lt;script&gt;`-rendering behaviour for development was separate from the Grunt script concatenation behaviour for production. This meant that sometimes our development build would work, but the production build would fail due to a missing script. In other words, poor <a href="http://12factor.net/dev-prod-parity">dev-prod parity</a>.</p><p>With a module bundler, in both development and production, a single JS bundle and a single CSS bundle are created, so there is no disparity between the two environments in how many `&lt;script&gt;`s are rendered.</p><p>The above presented us many challenges to solve, and as we started to look into potential solutions we had two major goals in mind:</p><h4>Goal #1: Make the codebase easier to read and understand</h4><p>Since we relied on AngularJS’s module names and <a href="https://docs.angularjs.org/guide/di">dependency injection</a> to resolve dependencies between modules, there were no explicit links between modules; locating the source of a function was often an arduous process of performing global searches on the codebase.</p><p>With a module bundler, it’s much easier to trace the origin of any piece of code when it’s imported via a `require()` call. The module path in the `require()` call <em>explicitly</em> tells us which file the dependency comes from.</p><h4>Goal #2: Make the codebase ready for a future where it’s simple to start using:</h4><ul><li><a href="https://babeljs.io/">Babel</a>: to use the latest ECMAScript while still supporting old browsers, and to support JSX syntax for…</li><li><a href="https://facebook.github.io/react/">React</a>: some parts of the app were very slow, like rendering long lists. Using a faster view layer like React for specific views would help solve this problem.</li><li><a href="http://redux.js.org/">Redux</a>: we recently started using Redux via a UMD bundle of ng-redux to help reason about the app’s complex state. This would be easier to manage if we didn’t have to maintain a UMD bundle for each individual redux addon and middleware we use.</li></ul><p>With a module bundler, enabling ECMA2015+ language features and JSX is as simple as enabling <a href="https://babeljs.io/">Babel</a>, e.g. via <a href="https://github.com/babel/babel-loader">babel-loader</a>, and using React, Redux, and its ecosystem of packages is simplified to `npm install` and `require()`.</p><p>This isn’t an exhaustive list of all of the reasons to use a module bundler like webpack, but these were the main reasons why we opted to switch.</p><p><em>Next week</em>, we will cover <strong><em>how</em></strong><em> </em>we switched to webpack, using an incremental approach to the migration.</p><p><em>[If you liked this article, click the little</em> ❤️<em> on the left so other folks know. If you want more posts like this, follow EventMobi publication with the (Follow) button on the right!</em></p><p><em>Finally if solving problems like this seems up your alley, we’re hiring! Checkout our current open positions at </em><a href="http://www.eventmobi.com/careers/"><em>http://www.eventmobi.com/careers/</em></a><em>]</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=69b7396f3ec5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/eventmobi/why-we-switched-to-webpack-69b7396f3ec5">Why We Switched to webpack</a> was originally published in <a href="https://medium.com/eventmobi">EventMobi</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>