GitHub LinkedIn RSS
Showing posts with label Developer Toolkit. Show all posts
Showing posts with label Developer Toolkit. Show all posts
Sunday, July 6, 2014

Sass


Remember in our first JavaScript Developer Toolkit article I've told that you would need to install Ruby. Today we'll see why and the reason is Sass.

Sass is an extension of CSS3 that helps you creating better stylesheets with less effort. Sass frees you from repetition and gives you tools to be creative. Since you can implement changes much faster, you’ll be able to take your design in a more boldly manner. Your stylesheets will be able to keep pace with changes, all while producing standards-based CSS you can use in any environment. As mentioned already, the Sass processor is written in Ruby. However Ruby environment has to be only installed on the development environment, so it doesn't matter how you write your server side. You only deploy the generated CSS files to production environment.
gem install sass
Sass offers a lot of features such as variables, nesting, importing, mixins and many more. I'll cover the most important of them, but make sure to read the reference to make the best of it.

Variables


One of the major benefits of Sass is the variables it brings to CSS. Variables allow you to name CSS values that you use repeatedly and then refer to them by name rather than repeating the value over and over. Variables can be declared globally or inside the scope of specific rule. The latter will only be active within the declared scope. As you can see it can be used even inside the rule, like we did in border.
$panel-color: #001122;
.panel {
 $width: 100px;
 width: $width;
 color: $panel-color;
 border: 1px $panel-color solid;
}

Nesting CSS rules


One of the most annoyingly repetitive aspects of CSS is writing selectors. When you’re writing a bunch of styles, that all target the same section of the page, you often need to write the same ID over and over again. Sass takes care of that by introducing nesting. Look at the following nested rules:
#content {
 article {
  div { color: #123 }
  &:hover { color: red }
 }
 aside {
  background-color: #eee
  :hover { color: red }
  border: {
   style: solid;
   width: 1px;
  }
 }
 > section { color: #555 }
}
When Sass engine runs, the nesting rules will be flattened and rewritten into regular css rules. Notice the difference between hover rules in lines 4 and 8. The engine replaces the & sign by the parent object, article, generating article:hover, whereas plain hover is appended to the parent generating article :hover. Sass also supports sibling and child selectors as well as nesting of CSS properties, border rule in our example.
#content > section { color: #555 }
#content article div { color: #123 }
#content article:hover { color: red }
#content aside {
 background-color: #eee;
 border-style: solid;
 border-width: 1px;
}
#content aside :hover { background-color: #eee }

Mixin


When you have a few small stylistic similarities throughout your site, colors and fonts that you use consistently, variables are a great way to keep track of them. But when your styles get more complicated, you need to be able to reuse more than just individual values. That's when it's time to use mixin. It groups the rules into one named block, to which we can pass parameters and make it even more flexible.
@mixin rounded-corners(
 $radius: 5px) {
 -moz-border-radius: $radius;
 -webkit-border-radius: $radius;
 border-radius: $radius;
}
.notice {
 border: 2px solid #00aa00;
 @include rounded-corners(6px);
}
The rule above will be transformed into this:
.notice {
 border: 2px solid #00aa00;
 -moz-border-radius: 6px;
 -webkit-border-radius: 6px;
 border-radius: 6px;
}
I hope you found some interest in the Sass and we'll give it a change. Truth is - once you do, you'll never let it go. Next time we'll talk about Compass - a cookbook of various CSS recipes.
Sunday, June 8, 2014

Bower and Require.js Integration


In the past we've talked about Bower package manager and God knows, I have been obsessively repeating myself about using Require.js and it's integration with object oriented programming. So how do they integrate with each other? In this article we'll cover the integration of both and see how they get along together.

Bower Initiation


Let's start by defining, which packages we want to use in our project by filling the bower.json. You can create one yourself or run bower init command for interactive creation.
{
    "name": "RequireJS Starter",
    "version": "1.0.0",
    "dependencies": {
        "jquery": "1.9",
        "underscore": null,
        "requirejs": null
    }
}
So we'll be needing jQuery, Underscore and of course Require.js. Putting null as a version number will download the latest version. Now run bower install to download all the packages into bower_components folder. You can change the location along with other thing using .bowerrc configuration file. After everything is in place, we'll configure our dependencies in our entry point JavaScript file and use the libraries once retrieved:
require.config({
    paths: {
        "jquery": "bower_components/jquery/jquery",
        "underscore": "bower_components/underscore/underscore"
    }
});
require(['jquery', 'underscore'], function($, _) {
    $('body').text(_.range(10));
});

Automagical wire-up


This is how it works according to the writter of bower-requirejs plugin. It looks at the libraries installed by Bower and creates a JavaScript file with require.config call with all the paths in place. Just run the following line in your bash console:
./node_modules/.bin/bower-requirejs -c destination/config.js
It has some interesting flags you can use, all of them well described in the documentation.

CDN Integration


But my mother told me local libraries are bad. She was right! We should always try to use CDN for common libraries, since chances are it had been already downloaded to user's computer and cached version would be used instead of downloading fresh version from our server. Let's edit our code and add cdnjs urls as a first priority. I prefer them over others because they offer the biggest variety of libraries and it's always good practice to work with a single provider. We'll leave our versions as a fallback if something bad happens to the CDN:
require.config({
    paths: {
        "jquery": ["http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min", "bower_components/jquery/jquery"],
        "underscore": ["http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min", "bower_components/underscore/underscore"]
    },
    waitSeconds: 10
});
require(['jquery', 'underscore'], function($, _) {
    $('body').text(_.range(10));
});
Notice the waitSeconds parameter we've added configuring how much time to wait for the CDN's versions. Hope things started to clear bit by bit. What to use and how to use it. I'll continue to write articles on this topic including Grunt integration.
Sunday, June 1, 2014

CSS and JavaScript Live Editing


As planned we're continuing our tradition to present JavaScript Developer Toolkit. Last time we've talked about Emmet and today's article I'd like to devote to HTML/CSS/JavaScript editing.

How often do you change your CSS rules and JavaScript within your browser? How often do copy/paste the changes made into appropriate resource files? It's time to do this Zen style.

IDE integration


This is my preferable way to develop and live-editing the changes. Let me show you why. Wouldn't it be cool if you could develop using your beloved IDE with all the neat plugins it offers and see the changes reflected live in the browser? Well - you can, with both Sublime Text and Webstorm we've talked about during our previous JavaScript toolkit article. You do this by installing JetBrains IDE Chrome extension for Webstorm:


or Emmet LiveStyle for Sumblime Text:


Look into their sites and see how properly things should to be configured. Both tools only offer support for Chrome, so Firefox fans will have to wait for now...

Browser Extensions


As time passed, passionate developers have been creating bunch of extensions both for Chrome and Firefox to enable this functionality. The most prominent ones are Chrome DevTools Autosave and EditCSS. However this work has not only inspired the developer community, but also the creators of both most commonly used browsers. As a result as of Firefox 11 and Chrome 28 the functionality has been bundled with the browsers, making the extensions obsolete.

Browser built-in capabilities


While Firefox has done some progress and enabled the live editing of CSS using it's Style Editor, the real advancement came from Google camp with the introduction of workspaces. It allows one to save both CSS rules and JavaScript code and even supports Sass debugging of Sass (.scss) files in the Sources panel, and view the results without having to leave DevTools or refresh the page. When you inspect an element whose styles are provided by a Sass-generated CSS file, the Elements panel displays a link to the .scss file, not the generated .css file. We'll talk more about Sass and Compass in the future. Find the time to watch the whole presentation about this feature:

Sunday, May 4, 2014

JavaScript Developer Toolkit - Emmet


This is a second article about JavaScript developer's toolkit and today we're going to talk about Emmet. If you've missed the first article about the useful tools for JavaScript developers, have a look at it here.

What is Emmet?


Essentially it gives you shortcuts you can type that expand into full HTML or CSS. Like nav>a*5 will expand into a <nav> tag with eight links inside it with empty hrefs.
<nav>
 <a href=""></a>
 <a href=""></a>
 <a href=""></a>
 <a href=""></a>
 <a href=""></a>
</nav>
Or, try div.module*2>p*2>lorem and press tab. Firstly it will convert div.module*2 into two <div> elements and associate them with a module css class. After that it will look at >p*2 and create two <p> elements. In the end >lorem will fill the paragraphs with famous lorem ipsum text. Take a look at the produced code below:
<div class="module">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.
     Accusantium aliquid, autem eligendi explicabo possimus
        similique vitae! Asperiores ducimus ea illum perspiciatis
        placeat similique soluta, totam! Alias aut maiores
        omnis quo.
    </p>
    <p>Ab alias aspernatur eius, ex natus non obcaecati quo
     veritatis voluptatem voluptatum? Ab assumenda consectetur
        deserunt, doloremque ea et odio. Animi beatae eaque facere
        itaque nemo nobis obcaecati quis sequi.
    </p>
</div>
<div class="module">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.
     Ad aperiam eos esse explicabo nostrum, numquam rem
        veniam. Blanditiis commodi cum error eum incidunt ipsam
        laboriosam natus quod veritatis voluptate, voluptatibus?
 </p>
    <p>Ab architecto atque deleniti dicta dolor dolores, enim
     explicabo ipsum, libero natus nulla officiis, rerum
        tempora temporibus unde! Aliquid aspernatur eaque fuga
        molestiae nisi omnis quis repellendus reprehenderit sunt
        voluptatum.
    </p>
</div> 
There are also a bunch of editor navigation shortcuts like move to the next edit point, reflect css value, split/join tag and more. All of them are beautifully demonstrated on Emmet documentation section and even can be played with.

Emmet works with various code editors and is already bundled-in with Webstorm. It can as well be easily installed on Sublime Text, of which we talked in our previous article.

Check out for the next article in the series of developer toolkit. In fact I think I'm going to keep the tradition and post an article about the useful tools every first weekend of the month.
Sunday, April 13, 2014

What Is Bower And Why You Need It


So you've heard everyone talking about this mythical creature called Bower. But what exactly is it? Better to see once than to hear 100 times, so before we proceed any further take a peek at their site.


The problem


Recall how you used to manage, and maybe still do, your 3rd party libraries. Firstly you went to their official site or maybe github repository. After finding the latest minified version, you would get something like this - xxx-1.0.1.min.js, then downloaded and saved it into your project. At last referencing it in the HTML would do the trick by copy pasting the script tag - <script src="//code.jquery.com/jquery-1.11.0.min.js">.

But what would have happened if a new version came up? Well, if you were aware of the update, you would repeat the process again and put xxx-1.0.2.min.js in your library folder. Then you would change the reference in your HTMLs or JavaScript files. You could of course rename the file to xxx.js and omitting the version and 'min' suffix, but then you'd need to enter the file itself to verify the version; that is if the version was written there of course.

All this should have been done just for one library, but you well know that today's applications depend upon dozens of 3rd libraries. Each of them has its own version and keeping track of their changes has become a tedious if not impossible task for a man who just wanted to use some cool library. This is where Bower comes to help.


The solution


Basically, Bower is a package manager of JavaScript libraries. It was designed specifically to maintain the front-end related libraries like JQuery, Underscore and so on. You can use Bower to download libraries from the command line, without having to manually download each project from their respective sites. Bower keeps track of these packages in a manifest file called bower.json. Once you downloaded the package, the usage is up to you - Bower doesn't interfere nor assists with the loading process of the libraries.


The rumors


There is a lot of confusion among newcomers to the JavaScript package management world about the purpose of each tool and needs they come to solve. Let's talk about the most common rumors.

Bower vs npm


npm is most commonly used for managing Node.js modules, but some front-end libraries has crept into its repositories and may be downloaded from there as well.

Bower is created solely for the front-end and is optimized with that in mind. The biggest difference is that npm does nested dependency tree (size heavy) while Bower requires a flat dependency tree (puts the burden of dependency resolution on the user).

A nested dependency tree means that your dependencies can have its own dependencies which can have their own, and so on. This is really great on the server where you don't have to care much about space and latency. It lets you not have to care about dependency conflicts as all your dependencies use e.g. their own version of Underscore. This obviously doesn't work that well on the front-end.

Bower vs Requirejs


Bower is intended to keep track of the packages, whereas Requirejs for managing the dependencies inside the project. Once you've downloaded all the required libraries using the Bower, you would still need to reference all of them in your code in the right order. This is what Requirejs does.

The usage


Let us see how to install and use the Bower. In order to install it, just get it from the npm using the following code:

npm install -g bower

If you don't know what npm is or don't have one installed on your machine, please read the JavaScript developer toolkit article. Go to the project's folder and initiate the bower configuration file. The tool will ask you couple of question like naming and versions before it starts. To do this, type the following:

bower init

Once everything is set up, start downloading packages by typing their name:

bower install jquery

or typing the github repository directly:

bower install https://github.com/jquery/jquery

Bower supports many options to make your life easier like downloading specific version or auto-update all or some packages at once. All these can be read about on their site.
Saturday, April 6, 2013

JavaScript Developer Toolkit


The world has changed as it must. JavaScript developers are no longer considered the outcasts of developer community, but a boutique club gaining followers like no one else. However with its popularity and chic, comes the complexity and burden of maintenance. A JavaScript developer can no longer write one's code in a text editor and rely on ad-hock solutions copy pasted from various forums, but surround oneself with professional tools specifically designed and customized for the language. In this article, I'll present must have tools, which will assist any JavaScript developer in the crafting of awesomeness.

Environment


Your OS is largely dependent upon our habits or company's policy, however for server side developers, I would strongly suggest migrating towards Unix base OS like OS X or Linux of some sort - my preference is Ubuntu. The reason behind the choice is the enviroment on which your node.js application runs on, which is Unix machine. The sooner you work on the compatible environment the better. Surely you can build your code on Windows and test it later on Unix, but with the quality of latest Windows versions - does it really worth the trouble :)?

Even as a solely front-end developer, these days you’ll certainly run into a project that may have node or may use Ruby and knowing how to operate in these environments plus keeping them up to date can be a huge advantage to you or your team members.

Node.js


Node.js is a cross-platform runtime environment and a library for running applications written in JavaScript outside the browser - on the server. You can read more about it on wikipedia and their site. To install the node, just follow the instructions on their download section.

npm


Basically npm is an online repository for the publishing of Node.js projects. It also allows you to interact with downloaded packages in terms of versioning and dependencies. npm is installed as part of node's installation, so no work here. In order to install some package in you project's destination, async for example, just type the following:

npm install async

If you want to install the package globally, that is to use it in all your projects, just add -g flag after the install command. You can update a specific package by running the following:

npm update async

or all installed ones by removing the package name.
Using the npm you'll be able to install some useful tools like Yeoman, Grunt, Bower and many more. All of them will be discussed in the following articles.

Ruby


Ruby is definitely another environment you’ll encounter during your work as a JavaScript developer. Why Ruby, you may ask? There are a lot of cool tools already built in Ruby, which JavaScript community members use with impunity. The most popular are Compass and Sass, but many more exist and will be reviewed in the following articles. To install the Ruby environment, go to the download section and follow the instructions. The usage of Ruby is similar to npm described above, just change the npm command to gem.

Integrated development environment (IDE)


For many years JavaScript code has been written using tools, which were designed for other languages. .NET developers used Visual Studio, Java and PHP was written with NetBeans or Eclipse. None of these tools were designed to write extensive code in JavaScript, yet along support its frameworks like Angular and Ember. And don't get me wrong, I love Eclipse and it's my number one IDE for Java projects, it's just not very good when it comes to JavaScript ecosystem. The void has started to fill only recently, by tools popping up like mushrooms after the rain.

I would like to put a spotlight on two of them: Sublime Text and WebStorm. Notice though, I only mention Sublime Text because of its popularity among the tutorial writers because of its lightness and ease of use for small pieces of codes. It is however a code editor, rather than a IDE like WebStorm. Here, the writer put it in a correct form:
"If you want a flexible, powerful, extensible programming text editor that is lightning fast and you don't mind switching to other windows for code checking, debugging, and deployment, then look no farther than Sublime Text."
WebStorm on the other hand offers a wide set of tools ranging from source control to extensive debugging using spy.js integration. Subline Text's price should also play a role in a comparison. Putting a price tag of 70$ on a super buffed text editor is a bit ambitions in my point of view, but maybe I'm in the wrong. In order to work with WebStorm, you'll have to pay from 59$ to 99$, depending on the license. It also has a start-up 50% discount and is offered completely free for a open source projects. Have a look at both and choose what fits you best.

These are the basics, where you should start. I'll be covering additional tools and plugins like Emmet, if possible those which work with both WebStorm and Sublime Text.

**The tools presented here are by no means better nor superior to others present on the market. The list is compiled based on my subjective opinion. If you found/know of better tools, you're welcome to comment below.