WebStorm Essentials - Sample Chapter
WebStorm Essentials - Sample Chapter
$ 29.99 US
19.99 UK
P U B L I S H I N G
C o m m u n i t y
E x p e r i e n c e
WebStorm Essentials
Build efficient HTML, CSS, and JavaScript applications using the
powerful WebStorm IDE
D i s t i l l e d
Den Patin
pl
Sa
m
Stefan Rosca
WebStorm Essentials
WebStorm Essentials
ee
Stefan Rosca
Den Patin
on the client and the server. Likes to tweak things, loves a good challenge and
benchmarks everything.
During the last years he specializes in front end development and worked on
several projects at Word of Social, SynerTrade and VisualDNA.
He can be reached at http://uk.linkedin.com/in/roscastefan/.
Den Patin is a senior software engineer with a hankering for writing clean code
and creating elaborate yet user-friendly web applications. For over four years at and
outside work, he has been utilizing a vast array of technologies for both frontend
(HTML, CSS, JavaScript, CoffeeScript, jQuery, and AngularJS) and backend (Node.js,
Ruby, and PHP).
Den holds a master's degree of science in applied informatics and a bachelor's degree
of science in computer science and engineering and specializes in computational
linguistics. His hobbies, besides programming, comprise of studying and teaching
foreign languages and playing the piano.
During the last couple of years, Den developed and tailored various navigation and
location-based web applications at T-Systems RUS.
To get in touch with him, you can visit his website, http://dpat.in.
Preface
Exploit the functional power of WebStorm to build better JavaScript applications!
Developers call WebStorm "the smartest JavaScript IDE", and this couldn't be any
truer. It outperforms all its competitors and offers some very interesting features
such as intelligent code assistance, debugging and testing, seamless integration of
various tools, VCS integration, and many more.
This book is aimed at helping the reader get the benefit of all the features and
possibilities that WebStorm brings to the world of frontend development.
We will go through all current technologies, building everything from simple
websites to single page applications with Angular and Meteor and native mobile
applications using HTML5, JavaScript, and CSS. We will also go through some of
WebStorm's power features that will help us boost our productivity and enhance
developer experience!
Preface
Chapter 4, Using Package Managers and Build Automation Tools, describes using package
managers and building systems for your application by means of WebStorm's
built-in features.
Chapter 5, AngularJS, React, Express, and Meteor Developing Your Web Application,
focuses on the state-of-the-art technologies of the web industry and describes
the process of building a typical application in them using the power of
WebStorm features.
Chapter 6, Immersing Yourself in Mobile App Development, shows you how to use
JavaScript, HTML, and CSS to develop a mobile application and how to set up the
environment to test run this mobile application.
Chapter 7, Analyzing and Debugging Your Code, shows how to perform the debugging,
tracing, profiling, and code style checking activities directly in WebStorm.
Chapter 8, Testing Your Applications, presents a couple of proven ways to easily perform
application testing in WebStorm using some of the most popular testing libraries.
Chapter 9, Getting to Know Additional yet Powerful Features, is about a second
portion of powerful features provided within WebStorm. In this chapter, we
focus on some of WebStorm's power features that help us boost productivity and
developer experience.
Getting Started
with WebStorm
WebStorm is a full-fledged JavaScript integrated development environment (IDE)
engineered by JetBrains for client-side and server-side web development. It is built
on the basis of IntelliJ IDEA; WebStorm inherits all its features and peculiarities with
which you can work conveniently with web-oriented languages, such as JavaScript,
HTML, and CSS, as well as utilize essential-for-developers capabilities, such as
refactoring, debugging, and version control management. Besides, WebStorm
natively supports Node.js and comprises a bunch of innovations from previous
versions, such as support for AngularJS, CoffeeScript and TypeScript, LESS, and
SASS, and other great features.
This book is intended for you to learn about the newest features that WebStorm
10 provides and use WebStorm to exploit cutting-edge web technologies. You will
see how to develop high-quality web applications and discover best practices and
timesaving hacks in the web development process. Ultimately, you will gain all the
skills required to revolutionize your web development experience.
In this chapter, you will do the following:
[1]
TypeScript: This new version of WebStorm comes with support for versions
1.4 and 1.5 and a built-in compiler
Grunt: You can easily navigate and edit the jobs inside WebStorm. The grunt
integration has been reworked to provide a consistent experience whether or
not you decide to use Grunt or Gulp for your project
Live Dart analysis view: You can now perform on-the-fly analysis for your
code through the Dart Analysis Server. All the results will be displayed
directly in the editor.
At the time of writing this book, JetBrains is already preparing to release WebStorm
11 that will bring cool features, such as Yeoman integration, advanced NPM
integration, Webpack support, AngularJS 2 support, and improved JSX support to
name a few.
Installing WebStorm
I believe that you are intrigued by all these features and are now longing to try
out WebStorm 10 to leverage them. We need to install the IDE. You can find the
download package directly on the WebStorm website at https://www.jetbrains.
com/webstorm/.
One of the strongest advantages of WebStorm is that it's cross-platform. You can run
WebStorm on the following operating systems:
[2]
Chapter 1
Depending on the OS, the installation process varies slightly, but it still
remains simple.
On a Mac machine, you should:
1. Mount the downloaded WebStorm-10*.dmg disk image file as another disk in
your system.
2. Copy WebStorm to your Applications folder.
On a Windows machine, you should:
1. Run the downloaded WebStorm-10*.exe file that starts the installation
wizard.
2. Follow all steps suggested by the wizard.
On a Linux machine, you should:
1. Unpack the downloaded WebStorm-10*.tar.gz file using the
following command:
tar xfz WebStorm-10*.tar.gz
Configuring WebStorm
You need to configure a couple of things to complete the installation and proceed to
work in the IDE.
[3]
First, it's suggested you import your settings from a previous version. However,
since we assume here that you are new to WebStorm, click on the I don't have a
previous version of WebStorm or I don't want to import settings button, as shown
in the following screenshot:
Then, you get a popup of the WebStorm License Activation dialog, where you can
activate your license if it exists, buy it, or run a 30-day WebStorm trial. Read the
license agreement and accept all its terms. The following screenshot shows this:
[4]
Chapter 1
When you are done with the license, in the WebStorm Initial Configuration dialog,
you can set the Keymap scheme depending on what is habitual to you, the IDE
theme, and the Editor colors and fonts depending on what you prefer moredark
or bright colors. Use the Click to preview section to evaluate whether the theme and
colors you have set fit your needs and preferences or not. The shortcuts in this book
are based on the Mac OS X 10.5+ scheme. The following screenshot captures this
discussion aptly:
[5]
You can either create a new project, open an existing one, check out the code from
version control systems, or choose a setting action to perform from the Configure
drop-down list. To get familiar with the WebStorm user interface, let's create a new,
simple HTML project.
[6]
Chapter 1
3. Then, leave the Project type field with the Empty project valuefor now,
we are just going to immerse in the interface and not create something
really usefuland click on the OK button. The following screenshot
depicts these steps:
Instead of simply introducing them, let's create a very simple yet rather interesting
projectthat of automatically locating you with Google Maps. In the preceding
image, you can see the final version of what we are going to do. So, after completing
this section, you will have the same view.
[7]
The Menu line (1), which has a bunch of options, including the standard
File, Edit, View options and the WebStorm-specific Code, Refactor, Run,
and VCS ones.
The Project tool panel (4) on the left-hand side, where you will see
your project structure and file. You now only have a line with the
project name, yet in the image, you can already see the hierarchical
structure of our project. The Project tool panel correlates with another
interface element, Tool tabs (3), which I will describe a bit later.
The Editor panel (5) on the right-hand side, where you will type
your code.
Slightly above these two panels, you can find Navigation toolbar (2). For
now, you have it with a sole breadcrumb, MyFirstWebProject, prepended
with a typical folder icon and appended with a right-arrow shape, yet in
the image, there are three breadcrumbs, where each one stands for the next
hierarchy level. The Navigation toolbar is great for quick navigation and
better understanding of the project file hierarchy.
At the very bottom, you can find Status bar (7), which contains some
auxiliary information, such as denoting the current caret position in the
editor (in the line:column format), line ending type (Windows-styled,
Mac-styled, and so on), encoding the current file in the editor, and so on.
Your project is as yet empty. Let's get down to business! Add an HTML file to the
project by right-clicking on the project name in the Project tool window, and name
it map.html or simply map. This is encapsulated in the following screenshot:
[8]
Chapter 1
In the editor, you see simple HTML5 starter code. There, let's indicate where our
map will be displayed. It will be a simple div element with the map ID:
<div id="map"></div>
In the body section, start typing <div>. As you type each symbol, what you see
is that WebStorm suggests appropriate tags within the current content. You can
navigate through the drop-down tag list with a mouse and select a tag with the Enter
key in the same way that you can add attributes to the tag. When you type >, you
will find that WebStorm automatically closes the tag if it is a pair tag. You should
notice that WebStorm puts the cursor directly where you will further write the tag
or attribute value; it slightly yet pleasantly saves some time. While you work on
WebStorm, there is no need to save your files; by default, it will watch for changes in
your files when you change your frame and save them automatically. This behavior
can be set up/ modified in the Preferences | Appearances & Behavior | System
Settings dialog
Now we need JavaScript code to render the map. To add it, first create a new
directory, js, in the project root directory and add there a JavaScript file, map.js.
It opens in a new tab and contains a comment on the author and the creation time.
Fill the file with the following code:
var map;
function initialize() {
var mapOptions = {
zoom: 10
};
map = new google.maps.Map(document.getElementById('map'),
mapOptions);
if(navigator.geolocation) {
navigator.geolocation.
getCurrentPosition(function(position) {
var pos = new
google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
var infowindow = new google.maps.InfoWindow({
map: map,
position: pos,
content: 'I am here!'
});
map.setCenter(pos);
[9]
The code we just created uses the Google Maps API's features, renders the map on
your HTML page, locates you, and shows an I am here! message there.
Let's now pay attention to one more workspace element. It is located at almost the
bottom of the IDE (6). Doesn't this remind you of something? Yes, it is a standard
Terminal of your OS, which is integrated into WebStorm. You can activate it in
the bottom-left corner by either clicking on the Terminal button, or selecting the
icon, as
Terminal option in the drop-down option list when clicking on the
shown in the following screenshot:
[ 10 ]
Chapter 1
The terminal opens, and you can use it as if you were using the terminal built in to
your OS. I want you to create one more directorycssto keep the map.css file via
the WebStorm Terminal. Simply execute the following commands, and both the css
directory and map.css file appear in the Project tool panel:
$ mkdir css
$ cd css
You will now have a new directory named css. In this folder, create a new CSS file
named map.css from the context dialog or File | New menu. There is no advantage
in using the terminal in preference to context menu manipulations, or vice versa.
Which you use is up to you, but since we are exploring an instrument, WebStorm,
that is new to you, I will nevertheless prefer using its interface as much as possible.
Now double-click on the map.css file in the project structure hierarchy and fill it
with the following code:
html, body, #map {
height: 100%;
margin: 0;
padding: 0
}
Now we need to add the finishing touchwe should tell HTML to use our CSS and
JavaScript files as well as point out where the Google Maps API is located. We can do
it by simply adding the link and script tags to the head section. In the same way that
it was described, you can use WebStorm's code completion feature to speed up your
code typing. I want to mention one more feature now which is really essential here
(we will talk about WebStorm's smart code completion in detail in the next chapter).
It concerns those attributes that point to the other project filesin our case, it is the
src and href attributes. There is no need to manually search for files to point at from
these tags (it is especially appreciable in huge projects)WebStorm does everything
for you. When already being between quotes and waiting to input an attribute
value, you can simply press on Ctrl + Space (on Windows) / Ctrl + Space (on Mac),
and WebStorm displays only those files in the drop-down list that are possible in
this context. If there are too many files, you can simply start typing the name of the
file and WebStorm's smart completion does its work, too. Of course, it is a scintilla
of what WebStorm can offer you. You will become familiar with a bundle of smart
techniques in the next chapter.
So, the resulting HTML code is the following:
<!DOCTYPE html>
<html>
<head lang="en">
[ 11 ]
The last yet not least important interface element is Tool tabs (3), which I have
already mentioned, but only now, we can see the benefits of using it. Earlier,
we were on the Project tab, where we could see the file and directory hierarchy.
Change the tab for the Structure one, and for each filemap.html, map.js, and
map.cssyou will see the following:
[ 12 ]
Chapter 1
Yes, each icon is a shortcut to a browser executable, and if you click on any one,
the corresponding browser opens with the current page. Note that only installed
browsers will open.
The page with your current location opens at the address http://localhost:63342/
MyFirstWebProject/map.html. Don't be afraid if you see a blank page. First, you
need to allow your browser to get your location as it will ask about it. Then, you will
see a map wherein your location will be indicated with the words I am here!.
The only question that remains is, what is the strange number after the localhost
word? WebStorm provides the facility of a simple HTTP server running on the
default port 63342. You can change the port in IDE Settings.
[ 13 ]
For the first case, to open the settings window, you can simply press either
Ctrl + Alt + S (Windows) or + , (Mac), and you will get the following screen:
To access project settings, select File | Default Settings, and you will get something
similar to the WebStorm settings, but with a reduced list of configurable options.
[ 14 ]
Chapter 1
I should say at once that settings are not a thing that matters
every day, so you needn't think of it as something to
necessarily be utterly and completely configuredin most
cases, everything is already tuned up; moreover, if something
needs configuring, it will pop up as an information balloon
with a direct link to a setting.
If you need to configure something, what you will need to do is quickly find
the necessary setting. To do that, start typing its name in the search field, and,
as you type, the first matching setting gets highlighted, and the corresponding
page is displayed.
But let's stroll further through the settings. I am not aiming to describe all the
settings, but still I am going to cover the most important ones.
[ 15 ]
If you want to change a color for a certain section of your IDE, you can go to the
Colors & Fonts subsection of the Editor section and try various parameters there so as
to find the ones that suit you. You can observe the changes in real time by clicking on
the Apply button. This discussion is aptly depicted in the following screenshot:
Keymap
The keymap is that very thing that developers must know and feel on the tips of their
fingers because the proficiency level to use shortcut keys is directly proportional to
the speed of development. It is evident that each person has his own preferences of
what key combination to use in each case. So, the IDE should take into account the
necessity to customize the keymap so as to suit a larger number of developers.
WebStorm offers you comprehensive keymap management, which you can access
in the Keymap subsection of the Appearance & Behavior section. For each menu
item and command accessible inside the IDE, you get an opportunity to change the
default key combination. Of course, WebStorm prevents you from overwriting and
overriding the already existing combinations with a warning message.
To add a new shortcut or to change an existing one, you need to just select what
you are going to process and click on the Remove or/and Add Keyboard Shortcut
buttondepending on what you intend to do. Then, simply press the combination
that you would like to set for a certain action, and that's all.
This is great news for those who migrated from one OS to another or from one IDE to
WebStorm. If you are already used to a keymap specific to your previous workspace,
you need to first check the Keymap options list, where you can reliably find an
option that satisfies you. Among the options are Emacs, Visual Studio, Eclipse,
NetBeans, Mac OS X, Linux, Windows, and so on.
[ 16 ]
Chapter 1
Code Style
In the Code Style subsection of the Editor section, you can see the possibility of
customizing the appearance of each language in which you an write the code using
WebStorm: JavaScript, CoffeeScript, CSS, Haml, JSON, Dart, XML, YAML,
and so on.
Settings about the languages that you can change are uncountable. You can set the
indent size, indent type, and if you are using tabs, their size, where to put or not put
spaces, how to perform wrapping, and lot of other aspects. On the customization
panel for each language is a preview of what you get if you change something.
[ 17 ]
Plugins
Plugins are an important part of WebStorm because they can dramatically expand
the core functionality of the IDE.
Be selective and do not install the plugins one by one.
Remember that the more plugins you install, the more the
time it will take you to launch the IDE, and the poorer the
performance it will have. But don't be afraid of installing
pluginseach one adds just a couple of dozens of
milliseconds to the startup time of your IDE, so you will not
experience performance degradation unless you install only
necessary plugins. Up to 10-15 plugins is fine for performance
as well as enough for a typical project.
You can manage plugins in the Plugins section of the Preferences window, as shown
in the following screenshot:
The plugins that are checked are already installed and accessible inside the IDE. If
you need more plugins, there are two ways of getting them. You can either browse
them in online repositories or install already downloaded or engineered plugins
directly from the disk.
[ 18 ]
Chapter 1
The difference between the Install JetBrains plugin window and the Browse
repositories window is that the second one allows you to install any plugin
available in online repositories, whether it be third-party or of JetBrains.
There is nothing about it in the installation process of a plugin. You need to just find
what you want and simply click on the Install Plugin from disk button.
Version Control
Any development process, be it web, desktop, or whatever else, is inconceivable
without version control. That commits IDEs to integrate various version control
systems, and WebStorm is not an exception. WebStorm supports Git, Subversion,
Mercurial, CVS, and Perforce besides providing a binding with your GitHub
account. Besides, WebStorm provides you with the facility of the so-called "local
history", which enables you to track any changes in the code by means of the IDE,
without using dedicated VCSes. Despite the differences between these VCSes,
WebStorm allows you to use them in similar ways. As an IDE, WebStorm, of course,
takes a lot of issues, so dealing with VCSes is visual and becomes even simpler than
via the standard command line.
You can decide which actions require confirmation (in the following image), specify
which ones can run in the background, configure what is to be ignored, manage
history cache handling, and so on. Then, you will be able to unnoticeably yet
skillfully manage your VCS using just shortcut keys. Version Control is depicted
in the following screenshot:
[ 19 ]
Proxy
You may need to use WebStorm at work. A lot of companies are using internal
networks, so it may turn out in a way that you will not be able to perform most
Internet-oriented actions without a proxy. There may also be something else, which
prevents you from comfortably working on the Internet. So, let's learn how to set up
a proxy in WebStorm.
Instead of manually searching where the proxy settings are located, let's query the
search box about it, entering the word proxy there. It will instantly not only show
where the proxy settings are, but also filter out only the menu sections containing
"proxy". Moreover, it highlights all the parameters that contain the word "proxy",
so we can rapidly find what we are searching for.
For the proxy to process connections is a small matter. You need to fill out the blank
fields as displayed here and click on the Apply button:
Summary
In this chapter, you learned how to install and then prepare WebStorm for the
development process. We implemented a simple web application, and by the
example of the files that we created and the actions we performed, we scrutinized the
WebStorm interface and the means to customize the most significant elements of it.
In the next chapter, we are going to discover a bunch of the most essential and smart
features that drastically improve your efficiency and, thus, makes WebStorm really
great for web development.
[ 20 ]
www.PacktPub.com
Stay Connected: