![](http://habrastorage.org/getpro/habr/avatars/90b/a64/ff5/90ba64ff556c1b7a00a914ce925bdd34.jpg)
Automated template for front-end projects
![](https://habrastorage.org/files/49d/5bc/3d7/49d5bc3d7a1c45328c4ae3c94453c862.jpeg)
Man invented computers to perform most of the repetitive tasks for us. This allows us to save a lot of time and use it to the maximum benefit. And, since this article is about front-end development, there are a lot of such tasks in this area: compilation of css and js preprocessors, sprite assembly, image optimization, file minification, etc.
Now prepros , codekit , grunt help us automate these tasks , gulp and similar tools.
I had experience with each of the 4 listed, but I stopped at gulp for the following reasons:
- Gulp is less demanding on system resources than GUI programs;
- he is very fast. Compared to gulp, grunt is slow. So slow that I managed to switch from the editor to the browser and refresh the page, and the new styles were still not compiled. Gulp does it much better;
- The way to describe tasks in gulp seemed easier to me.
With the beginning of a new project, I had to do the same job: install gulp and the necessary modules, write tasks, organize the file structure of the project and create the necessary initial files. In each project, I applied the previously used solutions. Thus, I have gathered a decent set that automates most of the routine tasks and allows me to concentrate on the development.
At some point, I decided to create a database of developments on the basis of which you can quickly deploy work on a new project. And I want to share this template with you.
This template is not a panacea. Each has its own needs and, probably, for the maximum benefit, you will have to configure it for yourself.
What's under the hood?
Automated Tasks
- Stylus compilation ;
- Compilation CoffeeScript ;
- Compilation of Jade templates;
- Build sprites and style generation for them (I previously published an article about this);
- Adding vendor prefixes to properties;
- Minification of css and js;
- Image optimization.
File structure
The first level of the project is folders
builder
, src
and built
. builder
- the folder in which lies gulpfile
with the gulp tasks, where the modules will lie node
and from where I will run all the commands in the console. The folder
src
contains all the source files of the project, and the folder built
is created and changed automatically when executing gulp commands.Folder contents src
assets
styles
- project stylesimages
design
- images designed for website designcontent
- images used in page content
scripts
local
- scripts written as part of this projectvendor
- third-party libraries
templates
pages
- page templatesblocks
- blocks from which pages will be collected
How does it work?
In
gulpfile.coffee
described tasks that perform a particular job. Task can be called individually and in groups. Group calling is the most common user case. All gulp plugins are loaded automatically from
package.json
using the plugin gulp-load-plugins
. This reduces the volume gulpfile.coffee
. All paths to files and folders are placed in variables and described in the file
config.yml
.Tasks
sprite
- sprite generation based on the pictures that are in the folderconfig.paths.src.sprites.images.all
;coffee
- compilation.сoffee
from the folderlocal
;vendor
- transfer scripts from the foldervendor
tobuilt
;stylus
- compilation.styl
;images
- Transfer pictures to abuilt
folder;jade
- compilation of.jade
templates;autoprefixer
- add vendor prefixes (default settings);scripts:min
- minification of scripts;styles:min
- minification of styles;images:min
- image optimization;watch
- Tracking changes in files and launching the necessary task.
Task groups
default
- compiles templates, scripts, styles, collects sprites;dev
- includes thedefault
taskwatch
;minify
- compresses files and optimizes images that were created after workdefault
;prod
- task that runs on the server or before uploading to the server. It executes the groupdefault
and then launchesminify
.
System requirements
- node.js;
- gulp;
- coffee-script - to run gulpfile.coffee. If desired, you can translate
.coffee
to.js
.
Installation
- If you do not have node.js installed, download and install. Together with node.js, you will have a package manager installed
npm
; - Then install
coffee-script
andgulp
by entering the command in the console (you will need administrator rights)npm i -g coffee-script gulp
After these steps, you can begin to initialize the project itself.
- Download and unzip the archive with the template;
- in the console, open the folder
builder
and enternpm i
. The process of downloading and installing all of thepackage.json
modules listed will go .
After the installation is completed, you can safely print
gulp
in the console and the task group will workdefault
Problems
If at startup the
gulp
system gives an error saying that the module was not found coffee-script/register
, you need to set the NODE_PATH variable, which makes it clear to the system where the global modules are. Links to solutions:
- http://stackoverflow.com/questions/9587665/nodejs-cannot-find-installed-module-on-windows
- http://stackoverflow.com/questions/12594541/npm-global-install-cannot-find-module
Conclusion
The template will be constantly updated. In the near future I am going to add the following functionality:
- mixin kit for Stylus;
- JSHint;
- concatenation of js;
- bower.
There are also plans to prepare a grunt version with a similar functional.
Quick links
- Template repository - https://github.com/Krekotun/fabium
- node.js
- Gulp
- Stylus
- CoffeeScript
- Jade