<?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 Luís Antunes on Medium]]></title>
        <description><![CDATA[Stories by Luís Antunes on Medium]]></description>
        <link>https://medium.com/@luis.antunes?source=rss-cdc3b6604eb1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6NVsS6j4ouLV3HmMNDzp4g.jpeg</url>
            <title>Stories by Luís Antunes on Medium</title>
            <link>https://medium.com/@luis.antunes?source=rss-cdc3b6604eb1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 09:28:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@luis.antunes/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[Project scaffolding + Webpack 2 + React + PostCSS — Step by step (2/2)]]></title>
            <link>https://medium.com/@luis.antunes/project-scaffolding-webpack-2-react-postcss-step-by-step-2-2-b4b64d33024?source=rss-cdc3b6604eb1------2</link>
            <guid isPermaLink="false">https://medium.com/p/b4b64d33024</guid>
            <category><![CDATA[webpack-2]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[postcss]]></category>
            <category><![CDATA[scaffolding]]></category>
            <dc:creator><![CDATA[Luís Antunes]]></dc:creator>
            <pubDate>Sat, 04 Feb 2017 07:45:49 GMT</pubDate>
            <atom:updated>2017-02-04T07:53:07.944Z</atom:updated>
            <content:encoded><![CDATA[<p>Hey again. In the <a href="https://medium.com/p/f74be26159ef">previous post</a> we provided some basic configurations of webpack 2, along with a simple folder structure that helps us stay organised, covering the first 3 points below.</p><ol><li>Project architecture</li><li>Global and local dependencies and basic folder structure</li><li>Webpack 2 basic configuration</li><li>React</li><li>PostCSS with some basic plugins for globals, grid, etc</li></ol><p>This one aims to give understanding on how we can configure React + PostCSS, covering the last two bullets above.</p><p>If you are reading this, you probably already know these tools. So, without any further delays lets get to business.</p><h3>4. React</h3><p>When we speak about react we all tend to think about JSX. In the Webpack domain we think about loaders, because they transform stuff into another stuff. In this case JSX to Javascript. Anyway, we will not use any loader to transform JSX, but we will use Babel to transpile code to ES5 and take advantage of the React preset to deal with the JSX.</p><p><strong>4.1 Dependencies</strong></p><p>Lets start by adding the dependencies.</p><pre>// react dependencies<br>yarn add react react-dom react-router</pre><pre>// html management<br>yarn add --dev html-webpack-plugin</pre><pre>// babel dependencies<br>yarn add --dev babel-core babel-loader babel-preset-react babel-preset-latest</pre><pre>// in the project root<br>touch .babelrc</pre><p>First lets put add the following in the .babelrc:</p><pre>{<br>    &quot;presets&quot;: [<br>        ...<br>        &quot;react&quot;<br>    ]<br>}</pre><p>So, like any other preset in babel, react preset brings a couple of plugins for dealing jsx transformation and such.</p><p><strong>4.2 webpack.config.js</strong></p><p><strong>html-webpack-plugin</strong> may not be so obvious here. This manages script injection in the html for us.</p><p>Lets configure it, in the plugins.js:</p><pre>const HtmlWebpack = require(&#39;html-webpack-plugin&#39;);<br>...<br>const HtmlWebpackPlugin = new HtmlWebpack({<br>    template: path.join(__dirname, &#39;../../src/index.html&#39;),<br>    filename: &#39;index.html&#39;,<br>    inject: &#39;body&#39;, // inject at the bottom of the body tag<br>});</pre><pre>module.exports = {<br>    ...<br>    HtmlWebpackPlugin: HtmlWebpackPlugin<br>}</pre><p>And in the webpack.config.js we have the babel loader:</p><pre>module.exports = env =&gt; {<br>    return {<br>        plugins: [<br>            ...<br>            plugins.HtmlWebpackPlugin<br>        ],<br>        ...<br>        module: {<br>            loaders: [{<br>                test: /\.(js)$/,<br>                exclude: /node_modules/,<br>                loader: &#39;babel&#39;,<br>                query: {<br>                    cacheDirectory: true<br>                }<br>            }]<br>        }<br>    }<br>}</pre><p>Pretty simple. We just tell to exclude node_modules, because we don’t need the burden of parsing external dependencies.</p><p><strong>4.3 webpack-dev-server</strong></p><p>Ok, lets configure our webpack dev server because we need to have a development server. In the webpack-dev-server.js below ./tools/build:</p><pre>/**<br> * Just for development purposes<br> */<br>const webpack = require(&#39;webpack&#39;);<br>const WebpackDevServer = require(&#39;webpack-dev-server&#39;);<br>const webpackConfig = require(&#39;./webpack.config&#39;);<br>const path = require(&#39;path&#39;);</pre><pre>const config = {<br> // dynamic route refreshing<br>    historyApiFallback: {<br>        disableDotRule: true<br>    },<br>    contentBase: path.join(__dirname, &#39;../../src/&#39;),<br>};</pre><pre>// always dev environment when running webpack dev server<br>const env = {<br>    dev: process.env.NODE_ENV<br>};</pre><pre>// create the server<br>const server = new WebpackDevServer(webpack(webpackConfig(env)), config);</pre><pre>// browse at <a href="http://localhost:3000/">http://localhost:3000/</a><br>server.listen(3000, &#39;localhost&#39;);</pre><p>The code above is pretty much self explanatory. We are creating a web server taking into account the configuration from webpack and the environment. <strong>Did you notice the module.exports in webpack.config.js? It can be used to pass the environment as a parameter.</strong></p><p>Since we are with our hands dirty, lets go and change our package.json, like so:</p><pre>...<br>&quot;scripts&quot;: {<br>    &quot;build&quot;: &quot;webpack -p --config ./tools/build/webpack.config.js --display-error-details&quot;,<br>    &quot;start&quot;: &quot;node tools/build/webpack-dev-server&quot;<br>},<br>....</pre><p><strong>display-error-details</strong> is just to get more terminal info about the errors that may arise.</p><p><strong>4.4 Entrypoints</strong></p><p>Did you notice in 4.2 that we told <strong>html-webpack-plugin</strong> that we had a template called index.html. So, lets add it below too in ./src.</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br> &lt;head&gt;<br>  &lt;meta http-equiv=&quot;Content-type&quot; content=&quot;text/html; charset=utf-8&quot;/&gt;<br>  &lt;title&gt;Webpack-2-starter&lt;/title&gt;<br> &lt;/head&gt;<br> &lt;body&gt;<br>  &lt;div id=&quot;App&quot;&gt;&lt;/div&gt;<br> &lt;/body&gt;<br>&lt;/html&gt;</pre><p>Our app is going to be injected in the div with the id=”App”. Speaking of app, lets change our app.js to include react and company.</p><pre>&#39;use strict&#39;;</pre><pre>import React from &#39;react&#39;;<br>import { render } from &#39;react-dom&#39;;</pre><pre>render(<br>  &lt;h1&gt;Hello, World!&lt;/h1&gt;,<br>  document.getElementById(&#39;App&#39;)<br>);</pre><p>Now, lets give our new scripts a try:</p><pre>npm run build</pre><p>The last will build all scripts into one nice little bundle along with the index.html referencing it. The one below will run dev server with the same webpack config file created before.</p><pre>npm run start</pre><p>And thats it, we’ve got react working.</p><h3>5. PostCSS</h3><blockquote>PostCSS is a tool for transforming styles with JS plugins. These plugins can lint your CSS, support variables and mixins, transpile future CSS syntax, inline images, and more.</blockquote><p>The beauty of it is that you can write your own Plugin to do whatever ticks you. For example, someone was getting crazy when a fellow colleague didn’t browse the color palette and added precisely a color that closely resembled one that already existed. Yes, that’s right, someone wrote a plugin called <a href="https://github.com/SlexAxton/css-colorguard">css-colorguard</a> to do just that; find similar colours.</p><p>In order to get PostCSS up and running, first we need to tell WebPack how to handle css.</p><pre>yarn add --dev css-loader extract-text-webpack-plugin file-loader postcss postcss-loader postcss-cssnext postcss-import</pre><p>Imports are now managed by the css-loader.</p><blockquote>extract-text-webpack-plugin extracts moves every require(&quot;style.css&quot;) in entry chunks into a separate css output file</blockquote><p>PostCSS is installed along with postcss-loader which allow us to use PostCSS with WebPack.</p><p>Next we use try to be future proof in terms of CSS syntax by using postcss-next plugin.</p><blockquote>postcss-next is a PostCSS plugin to use tomorrow’s CSS syntax, today.</blockquote><blockquote>postcss-import let us inline content.</blockquote><p>So, lets get started with the configurations. First lets add a configuration file next to our webpack configuration file named postcss.config.js.</p><pre>module.exports = {<br>  plugins: {<br>    &#39;postcss-import&#39;: {},<br>    &#39;postcss-cssnext&#39;: {<br>      browsers: [&#39;last 2 versions&#39;, &#39;&gt; 5%&#39;],<br>    },<br>  },<br>};</pre><p>The order up there is important, one should have postcss-import first because other plugins may take the advantage of working on the AST as if there were only a single file to process.</p><p>Autoprefixer enables automatic vendor prefixes on the css rules. It uses <a href="http://caniuse.com/"><strong>caniuse</strong></a> in order to enable/disable features.</p><p>Since we are adding loaders, lets separate them into a new file for loaders like we did with the plugins, so it can stay a little cleaner. Lets move all the loaders there, like so:</p><pre>const plugins = require(&#39;./plugins&#39;);</pre><pre>const BabelLoader = {<br>    test: /\.(js)$/,<br>    exclude: /node_modules/,<br>    loader: &#39;babel&#39;,<br>    query: {<br>        cacheDirectory: true<br>    }<br>};</pre><pre>const CSSLoader = {<br>    test: /\.css$/,<br>    loader: plugins.ExtractTextPlugin.extract({<br>        loader: &#39;css-loader?importLoaders=1!postcss-loader&#39;<br>    })<br>};</pre><pre>module.exports = {<br>    BabelLoader: BabelLoader,<br>    CSSLoader: CSSLoader<br>}</pre><p>The resulting webpack.config.js is as follows:</p><pre>&#39;use strict&#39;;</pre><pre>const webpack = require(&#39;webpack&#39;);<br>const path = require(&#39;path&#39;);<br>const paths = require(&#39;./paths&#39;);<br>const plugins = require(&#39;./plugins&#39;);<br>const loaders = require(&#39;./loaders&#39;);</pre><pre>module.exports = env =&gt; {<br>    return {<br>        context: path.resolve(__dirname, &#39;../../&#39;),<br>        entry: {<br>            app: paths.APP,<br>        },<br>        output: {<br>            filename: &#39;[name].[hash].js&#39;,<br>            path: path.resolve(__dirname, &#39;../../public/dist&#39;),<br>            publicPath: paths.ASSETS // useful if we need a CDN<br>        },<br>        plugins: [<br>            plugins.DefinePlugin,<br>            plugins.HtmlWebpackPlugin<br>        ],<br>        module: {<br>            loaders: [<br>                loaders.BabelLoader,<br>                loaders.CSSLoader<br>            ]<br>        }<br>    };<br>};</pre><p>You can see the result in <a href="https://github.com/luisantunesdeveloper/webpack-2-starter">https://github.com/luisantunesdeveloper/webpack-2-starter</a></p><p>Thats all folks. Thanks for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b4b64d33024" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Project scaffolding + Webpack 2 + React + PostCSS — Step by step (1/2)]]></title>
            <link>https://medium.com/@luis.antunes/project-scaffolding-webpack-2-react-postcss-step-by-step-1-2-f74be26159ef?source=rss-cdc3b6604eb1------2</link>
            <guid isPermaLink="false">https://medium.com/p/f74be26159ef</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[webpack-2]]></category>
            <category><![CDATA[postcss]]></category>
            <category><![CDATA[scaffolding]]></category>
            <dc:creator><![CDATA[Luís Antunes]]></dc:creator>
            <pubDate>Sat, 04 Feb 2017 07:43:31 GMT</pubDate>
            <atom:updated>2017-02-04T07:52:07.769Z</atom:updated>
            <content:encoded><![CDATA[<p>So, you are tired of reading boilerplates and every time you open them up, it seams so much more complicated than it should be? You came to the right place. This is the first of two posts that tries to explain the basics of compiling all these front end technologies into one complete and working project. These posts don’t try to expose in-depth of the aforementioned technologies. We already have plenty on the internet for that.</p><p>Ok, let’s get to it. Here is the plan.</p><ol><li>Project architecture</li><li>Global, local dependencies and basic folder structure</li><li>Webpack 2 basic configuration</li><li>React</li><li>PostCSS with some basic plugins for globals, grid, etc</li></ol><p>We will cover steps 1, 2 and 3 in this post. The steps 4 and 5 are for the next.</p><p>Lets get to it.</p><h3>1. Project architecture</h3><p>For starters we should answer the following question:</p><ul><li><strong>What is a component?</strong></li></ul><blockquote>A software component is a unit of composition with contractually specified interfaces and explicit context dependencies only. A software component can be deployed independently and is subject to composition by third parties. <a href="https://people.eecs.berkeley.edu/~newton/Classes/EE290sp99/lectures/ee290aSp994_1/tsld009.htm">https://people.eecs.berkeley.edu/~newton/Classes/EE290sp99/lectures/ee290aSp994_1/tsld009.htm</a></blockquote><p>So, a component should only be dependent in terms of the context where it applies.</p><p>Component = JS + Styles + Tests + Whatever is component dependent.</p><pre>├── .gitignore<br>├── LICENCE<br>├── README.md<br>├── docs<br>├── package.json<br>├── public<br>│   ├── assets<br>│   └── dist<br>│       └── app.bundle.js<br>├── src<br>│   ├── app.js<br>│   ├── components<br>│   │   ├── component1<br>│   │   │   ├── README.md<br>│   │   │   ├── config.js<br>│   │   │   ├── logic.js<br>│   │   │   ├── logic.spec.js<br>│   │   │   └── style.sass<br>│   │   ├── component2<br>│   │   │   ├── logic.js<br>│   │   │   ├── logic.spec.js<br>│   │   │   └── style.sass<br>│   │   └── layout<br>│   │       ├── logic.js<br>│   │       ├── logic.spec.js<br>│   │       └── style.sass<br>│   └── core<br>│       ├── helpers<br>│       ├── navigation<br>│       │   ├── history.js<br>│       │   └── router.js<br>│       └── test<br>│           └── setup.js<br>├── targets<br>│   ├── firebase.js<br>│   └── heroku.js<br>├── tools<br>│   └── config<br>│       ├── paths.js<br>│       ├── plugins.js<br>│       └── webpack.config.js<br>├── utils<br>│   └── i18n<br>│       ├── en.json<br>│       └── pt.json<br>└── yarn.lock</pre><p>The idea behind this folder structure is to isolate each component with their responsibilities and to be capable of identifying or correlate each section of the project just by looking to the directory name.</p><p>Notice that everything is a component, even the layout.</p><h3>2. Global, local dependencies and basic folder structure</h3><p>We are going to use <strong>yarn</strong>, well because it’s new and I want to give it a try. Someone told me that it’s faster, has cleaner syntax and in general follows the same road as npm. It also throws some goodies like licence inspection, interactive upgrades and so forth.</p><p>So, if you don’t have <a href="https://yarnpkg.com/en/docs/install">yarn</a>, lets go get it. For other OS check the link.</p><pre>brew update<br>brew install yarn</pre><p>Lets go and create a directory to hold our project</p><pre>mkdir webpack-2-starter &amp;&amp; cd webpack-2-starter</pre><p>Start a new project with yarn, this is similar to npm. Just follow along and answer the questions.</p><pre>yarn init</pre><p>Until here, nothing new. Lets install webpack 2 and the webpack dev server.</p><pre>yarn global add webpack@2.1.0-beta.25 webpack-dev-server@2.1.0-beta.10<br>yarn add --dev webpack@2.1.0-beta.25 webpack-dev-server@2.1.0-beta.10</pre><p>After this we can create our folder structure with the following commands.</p><pre>mkdir -p dir1/dir2/dir3<br>touch dir1/dir2/dir3/file1</pre><p>The mkdir with -p option is used to create the parents if they don’t exist. Very handy to create our folder structure. The touch command creates a file in the given path.</p><p>Don’t forget to exclude node_modules in .gitignore and commit your project.</p><h3>3. Webpack 2 basic configuration</h3><p>Like everything in the world of “computers”, we have inputs and outputs. Webpack is not so different. Lets add a basic configuration to webpack.config.js.</p><p><strong>3.1 Inputs and outputs</strong></p><pre>&#39;use strict&#39;;</pre><pre>const webpack = require(&#39;webpack&#39;);<br>const path = require(&#39;path&#39;);<br>const ASSET_PATH = require(&#39;./paths&#39;).ASSET_PATH;</pre><pre>module.exports = {<br>    context: path.resolve(__dirname, &#39;../../src&#39;),<br>    entry: {<br>        app: &quot;./app.js&quot;,<br>    },<br>    output: {<br>        filename: &quot;[name].bundle.js&quot;,<br>        path: path.resolve(__dirname, &#39;../../public/dist&#39;),<br>        publicPath: ASSET_PATH // useful if we need a CDN<br>    }<br>};</pre><p><strong>Context</strong> is used to set an absolute path to resolve configuration and loaders.</p><p><strong>Entry</strong> serves as an entry point to the application.</p><p><strong>Output</strong> defines the place where we put our bundle, and let us define the path and the filename which gets the name from the entry key we define, in this app.</p><p>We also add a file with the paths used on the configurations.</p><pre>&#39;use strict&#39;;</pre><pre>// Whatever comes as an environment variable, otherwise use root<br>const ASSET_PATH = process.env.ASSET_PATH || &#39;/&#39;;</pre><pre>module.exports = {<br>    ASSET_PATH: ASSET_PATH<br>}</pre><p>Sometimes we want to customize the webpack build process. For that we can use plugins. <strong>DefinePlugin</strong> lets us define constants at compile time.</p><p><strong>UglifyJsPlugin</strong> lets us mangle the code. Later we should separate dev from production configuration.</p><pre>&#39;use strict&#39;;</pre><pre>const webpack = require(&#39;webpack&#39;);<br>const paths = require(&#39;./paths&#39;);</pre><pre>// This makes it possible for us to safely use env vars on our code<br>const DefinePlugin = new webpack.DefinePlugin({<br>    &#39;process.env.ASSETS&#39;: JSON.stringify(paths.ASSETS)<br>});</pre><pre>const UglifyJsPlugin = new webpack.optimize.UglifyJsPlugin({<br>    compress: {<br>        warnings: true<br>    }<br>});</pre><pre>module.exports = {<br>    DefinePlugin: DefinePlugin,<br>    UglifyJsPlugin: UglifyJsPlugin<br>}</pre><p>We use them on webpack.config.js file.</p><pre>...<br>const plugins = require(&#39;./plugins&#39;);</pre><pre>module.exports = {<br>    ...<br>    plugins: [<br>        plugins.DefinePlugin,<br>        plugins.UglifyJsPlugin<br>    ]<br>};</pre><p><strong>3.2 ES6</strong></p><p>Luckily for us we don’t have to deal with an additional loader because Webpack 2 already does this for us, which is kinda nice.</p><pre>{<br>    &quot;presets&quot;: [<br>        [&quot;es2015&quot;, { &quot;modules&quot;: false }]<br>    ]<br>}</pre><p>{ modules: false } enables <a href="http://www.2ality.com/2015/12/webpack-tree-shaking.html">Tree Shaking</a> to remove unused exports from your bundle to bring down the file size.</p><p>More info on <a href="https://webpack.js.org/">webpack 2</a>.</p><p>And thats it. In the <a href="https://medium.com/p/b4b64d33024">next part</a> we will focus on React + PostCSS.</p><p>You can see the result in <a href="https://github.com/luisantunesdeveloper/webpack-2-starter">https://github.com/luisantunesdeveloper/webpack-2-starter</a></p><p>Thanks for reading. See you next time.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f74be26159ef" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>