<?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 Jonathan Potter on Medium]]></title>
        <description><![CDATA[Stories by Jonathan Potter on Medium]]></description>
        <link>https://medium.com/@jonathanpotter_80804?source=rss-59ed3061333a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*DLNOGwWYroR_MWygi-NZwQ.jpeg</url>
            <title>Stories by Jonathan Potter on Medium</title>
            <link>https://medium.com/@jonathanpotter_80804?source=rss-59ed3061333a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 15 Nov 2026 20:15:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jonathanpotter_80804/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[Step-by-Step Webpack 2 Setup for Personal Projects]]></title>
            <link>https://medium.com/@jonathanpotter_80804/step-by-step-webpack-2-setup-for-personal-projects-f0b74072daa1?source=rss-59ed3061333a------2</link>
            <guid isPermaLink="false">https://medium.com/p/f0b74072daa1</guid>
            <category><![CDATA[webpack-2]]></category>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Jonathan Potter]]></dc:creator>
            <pubDate>Fri, 24 Feb 2017 08:25:56 GMT</pubDate>
            <atom:updated>2017-02-24T18:46:01.016Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yanSshN4FldFx4wimzNtqQ.jpeg" /></figure><p>Webpack 2, es6, scss, uglification and sourcemaps</p><p>A few days ago I decided to put together a new side project. The first thing I did was put together a basic setup with Webpack 2 using es6, scss, and uglification with sourcemaps. There are definitely fancier things you can do, but this will get you going with the basics.</p><p>You can check out the finished product here: <a href="https://github.com/jonathan-potter/basic-webpack-2-setup">https://github.com/jonathan-potter/basic-webpack-2-setup</a></p><h3>Get some Javascript running:</h3><h4>terminal</h4><p>Here I just get a bunch of files ready to go.</p><pre>mkdir basic-webpack-2-setup<br>cd basic-webpack-2-setup<br>git init<br>touch index.html<br>mkdir javascript<br>mkdir css<br>touch javascript/index.js<br>touch css/app.scss<br>touch package.json<br>touch webpack.config.js<br>touch .babelrc<br>echo node_modules &gt; .gitignore</pre><h4>index.html</h4><p>All we’re doing here is setting up a skeleton for an HTML page. We set the title and add a script tag pointing to the bundled javascript file we will be building.</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>  &lt;head&gt;<br>    &lt;meta charset=&quot;utf-8&quot;&gt;<br>    &lt;title&gt;Basic Webpack 2 Setup&lt;/title&gt;<br>  &lt;/head&gt;<br>  &lt;body&gt;</pre><pre>    &lt;script src=&quot;build/bundle.js&quot;&gt;&lt;/script&gt;<br>  &lt;/body&gt;<br>&lt;/html&gt;</pre><h4>package.json</h4><p>This needs to be here for our npm packages. The interesting point here is the start script. The start entry in the scripts section means all we’ll need to do to start the server is call npm start in the terminal.</p><pre>{<br>  &quot;name&quot;: &quot;basic-webpack-2-setup&quot;,<br>  &quot;version&quot;: &quot;0.0.1&quot;,<br>  &quot;description&quot;: &quot;&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;start&quot;: &quot;webpack-dev-server&quot;<br>  },<br>  &quot;author&quot;: &quot;your-name&quot;,<br>  &quot;license&quot;: &quot;&quot;<br>}</pre><h4>webpack.config.js</h4><p>There is quite a bit going on here so i’ll just hit the high points. entry points to the top level javascript file we will be using. output sets where our bundled javascript file will be placed. Proper source-mapping is also set up.</p><pre>const path = require(&#39;path&#39;)</pre><pre>module.exports = {<br>  entry: &#39;./javascript/index&#39;,<br>  output: {<br>    path: __dirname,<br>    filename: &#39;build/bundle.js&#39;,<br>    sourceMapFilename: &#39;sourcemap&#39;<br>  },<br>  resolve: {<br>    extensions: [&#39;.js&#39;, &#39;.jsx&#39;],<br>    alias: {<br>      css: path.resolve(__dirname, &#39;css&#39;),<br>      javascript: path.resolve(__dirname, &#39;javascript&#39;)<br>    }<br>  },<br>  module: {},<br>  devtool: &#39;#inline-source-map&#39;,<br>  plugins: []<br>}</pre><h4>javascript/index.js</h4><p>At this point all we want to do is make sure our javascript is working. console.log will do fine for that purpose.</p><pre>console.log(&quot;it&#39;s alive!&quot;)</pre><h4>Terminal</h4><p>Install webpack and its dev server and start the server.</p><pre>npm i webpack webpack-dev-server --save-dev<br>npm start</pre><h4>Open the browser to <a href="http://localhost:8080/">http://localhost:8080</a></h4><p>You should see “it’s alive!” in the dev console (cmd-option-i for mac users).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t19aHpbKcV5fIjF5XOMvqw.png" /><figcaption>our javascript is running!</figcaption></figure><h3>Get some css (scss) working:</h3><p>Awesome! Javascript is up and running so it’s time to get our styles in too.</p><h4>css/app.scss</h4><p>This is just enough to show that our scss is building correctly.</p><pre>$reddish-color: #fcc;</pre><pre>body {<br>  background: $reddish-color;<br>}</pre><h4>javascript/index.js</h4><p>Add a css import to the javascript file.</p><pre>import &#39;css/app.scss&#39;</pre><pre>console.log(&quot;it&#39;s alive!&quot;)</pre><h4>index.html</h4><p>Add a link tag to pull in our new css build file.</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>  &lt;head&gt;<br>    ...<br>    &lt;link rel=&quot;stylesheet&quot; href=&quot;build/style.css&quot;&gt;<br>  &lt;/head&gt;<br>  ...<br>&lt;/html&gt;</pre><h4>webpack.config.js</h4><p>Add the ExtractTextPlugin and some loaders to make sure our css is built separately from our javascript.</p><pre>const path = require(&#39;path&#39;)<br>const ExtractTextPlugin = require(&#39;extract-text-webpack-plugin&#39;)</pre><pre>module.exports = {<br>  ...,<br>  module: {<br>    rules: [<br>      {<br>        test: /\.css|.scss$/,<br>        use: ExtractTextPlugin.extract({<br>          loader: [<br>            { loader: &#39;css-loader&#39;, options: { sourceMap: true } },<br>            { loader: &#39;sass-loader&#39;, options: { sourceMap: true } }<br>          ]<br>        })<br>      }<br>    ]<br>  },<br>  ...,<br>  plugins: [<br>    new ExtractTextPlugin({<br>      filename: &#39;build/style.css&#39;,<br>      allChunks: true<br>    })<br>  ]<br>}</pre><h4>terminal</h4><p>Kill the server, install the required dependencies, and start the server up again.</p><pre># kill the server (ctrl-c)</pre><pre>npm install extract-text-webpack-plugin@beta --save-dev<br>npm install sass-loader node-sass css-loader --save-dev</pre><pre>npm start # start the server again</pre><h4>open browser to <a href="http://localhost:8080/">http://localhost:8080</a></h4><p>Now you should see our nice reddish color in the background of the page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w-0wI0v-aqDYSVxtgl8-LA.png" /><figcaption>The reddish color is showing up!</figcaption></figure><h3>Next well uglify the code:</h3><p>The last step I’ll cover is uglifing the code. The main benefits are reducing the size of our javascript file and making it harder for people to read our code.</p><h4>webpack.config.js</h4><p>Use the UglifyJsPlugin and add the babel-loader for javascript files.</p><pre>...<br>const webpack = require(&#39;webpack&#39;)<br>...</pre><pre>module.exports = {<br>  module: {<br>    rules: [<br>      {<br>        test: /\.js$|.jsx$/,<br>        exclude: /node_modules/,<br>        use: &#39;babel-loader&#39;<br>      },<br>      ...<br>    ]<br>  },<br>  plugins: [<br>    ...,<br>    new webpack.optimize.UglifyJsPlugin({<br>      sourceMap: true<br>    })<br>  ]<br>}</pre><h4>.babelrc</h4><p>This just makes sure babel is doing all the things we may want it to do.</p><pre>{<br>  &quot;presets&quot;: [&quot;es2015&quot;, &quot;stage-2&quot;]<br>}</pre><h4>terminal</h4><pre># kill the server (ctrl-c)</pre><pre>npm i babel-core babel-loader --save-dev<br>npm i babel-preset-es2015 babel-preset-stage-2 --save-dev</pre><pre>npm start</pre><h4>open browser to <a href="http://localhost:8080/build/bundle.js">http://localhost:8080/build/bundle.js</a></h4><p>If you open the built javascript file directly you’ll be able to see that it has been compacted.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K5iLw3oX9aFxelZgN8l9Kg.png" /><figcaption>bundle.js with minified code!</figcaption></figure><p>note: uglifying is not recommended while attempting to debug! It is easy to turn off: just comment out the plugin in webpack.config.js and restart the server.</p><h3>Good luck</h3><p>I hope this helps someone wade through the morass of docs and get up and running a bit faster. Let me know: <a href="https://twitter.com/PotterRawr">https://twitter.com/PotterRawr</a> :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f0b74072daa1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>