testproj is a Front-end project created with XH Generator, a Yeoman generator for scaffolding web projects.
The following software needs to be installed if you want to use develop & build project created with XH Generator. These installations need to be done just once so you can skip this section if you have the software already installed.
(Note: As a command line replacement at Windows we recommend ConEmu.)
Install Node.js so you can work with npm
, Node package manager.
Then install Grunt's command line interface (CLI) globally:
npm install -g grunt-cli
For managing certain dependencies like Bootstrap, you will need Bower, another package manager. Install it from the command line too:
npm install -g bower
Also make sure that git is installed as some bower packages require it to be fetched and installed. On Windows ensure that Git is installed in your PATH by selecting Run Git from the Windows Command Prompt option during installation (check this screenshot).
The meaning of files and folders in generated project structure are as follows:
- dist - production / preview files are automatically generated here, this is where you check your work in a browser.
- node_modules - Node.js modules for various Grunt tasks, usually you don’t have to do anything about this folder
- src - source files, development is done here
- bower_components - 3rd party libraries managed via Bower
- designs - place to store design previews, sprite source files & so on
- grunt - atomic grunt tasks configuration
- includes - HTML partials like
head.html
,scripts.html
, etc. - scss - SCSS files
main.scss
- main file where other stylesheets are imported- common - common styles for most of pages
- components - styles for page modules/components; this is where most of your styles will go
- setup - various configurations and preprocesor helpers
- vendor - styles overwriting/replacing library ones
- js
main.js
is a main JS file in project
home.html
, etc. - HTML files composed from HTML partials
index.html
- project index with project pages listedGruntfile.js
- Grunt file with various automation tasks defined in itbower.json
- Bower dependencies in the projectpackage.json
- npm packages dependencies.yo-rc.json
- Yeoman generator configuration file.bowerrc
- configuration file for Bower.editorconfig
- EditorConfig configuration file to achieve consistent coding style.gitattributes
- Git configuration file to force Unix line ending in all text files.gitignore
- default Git ignore files and folders.jshitrc
- JSHint configuration
On a typical project, you will work in src
folder and check your work in dist
folder so you don’t have to touch other files. For more info about working with styles structure go to Writing styles section.
If you are joining an existing project which was set up using XH Generator (assuming that you have all prerequisites installed), all you need to do is to clone the existing repository and install Bower and npm dependencies.
Let's imagine you have cloned/unpacked testproj project into testproj
directory.
First, change the directory to your cloned project:
cd testproj
After that install Bower depedencies:
bower install
Then install npm dependencies:
npm install
Finally, (re)generate preview files in the dist folders:
grunt build
If everything went ok, the preview files will be generated and you will be able to check your work in the dist
folder.
Now the project is set up and you can continue like described in the Development section.
When you have the basic setup done, you can start development. To re-compile HTML / SCSS file in real time you can use default task. Type
grunt
and this will start a task that will watch for changes in files and recompile them as needed. Additionaly, development server will be started and BrowserSync scripts injected.
To rebuild the whole project, use the grunt build task again
grunt build
To validate HTML, use the following task
grunt validate
In general, it’s not recommended that you work directly with files in the dist
. The files in dist
folder are automatically generated from the source files in src
folder and by default dist
folder is ignored in version control system.
HTML and CSS files are prettified for consistent formatting and a table of contents from imported SCSS stylesheets is generated at the beginning of main.css
for better overview.
The following source files are generated in src/scss
folders:
main.scss
- main file where other stylesheets are imported- common - common styles for most of pages
_layout.scss
- main page structure_utilites.scss
- utility classes (image replacement, hide, etc.)
- components - styles for page modules/components; this is where most of your styles will go
- setup - various configurations and preprocesor helpers
_variables.scss
- variables file_mixins.scss
- mixins file
- vendor - styles overwriting/replacing library ones
The following approach is recommended when creating styles:
- Use
main.scss
only for importing other stylesheets. Do not write styles directly in this file! - Use variables and mixins files to store your variables and mixins.
- Depending on your preferences for styles organization, you can organize them according modules & components (recommended, use components folder), or pages. A good practice is to name file the same as main class used for that component, for example if you create a component representing an article with
.article
as a main CSS class followed by.article-title
,.article-meta
, etc. and with.article--featured
variant that will have slightly different color scheme, you will do everyone a favour by placing it inscss/components/_article.scss
file instead of.scss/components/_text.scss
- If you find yourself overwriting/replacing default library styles, put them into vendor folder. A good examples of that are replacing library custom select or lightbox styles with your own or overwriting some Bootstrap styles that were not configurable.
- Comment main sections and subsections appropriately.
- By default grunt-autoprefixer is enabled in project, which mean that you don't need to write prefixes for the standard CSS3 properties. It uses Can I Use database. However, please note that some popular properties (like
-webkit-appearance
or-webkit-font-smoothing
are not a part of standard and need to be written with prefixes by you).
LibSass is much faster than Ruby Sass, however some features of Ruby Sass may not yet be ported there or a bit faulty. Sometimes project requiremens force you to choose Ruby version over LibSass as some features of the libraries you would like to use may not be available in LibSass (like automatic sprite generation from Compass).
You can browse or add LibSass issues at LibSass GitHub page.
Let’s say you want to add Colorbox to your project. The following example shows how you can add it as a Bower package and merge its JS file into common plugins.min.js
file.
-
First, install it via Bower
bower install jquery-colorbox --save-dev
-
Then link it in
src/includes/scripts.html
. This will ensure that the library will be added toplugins.min.js
file<script src="//code.jquery.com/jquery-1.11.1.min.js"></script> <script>window.jQuery || document.write('<script src="js/jquery.min.js">\x3C/script>')</script> <!-- build:js js/plugins.min.js --> <script src="bower_components/jquery-colorbox/jquery.colorbox-min.js"></script> <!-- endbuild --> <script src="js/main.js"></script>
-
Go to
src/bower_components/jquery-colorbox
and copy images fromexample1/images
folder tosrc/img/colorbox
folder. -
Get
example1/colorbox.css
from the same dir, rename it to_colorbox.scss
, store it insrc/scss/vendor
folder and adjust to your needs if needed. -
Import
_colorbox.scss
inmain.scss
@import "vendor/colorbox";
-
Replace all instances of
images/
in_colorbox.scss
with../img/colorbox/
-
Run the
grunt build
task orgrunt
task -
Now you can use Colorbox in your HTML files and initiate it from
src/js/main.js
Vector graphics is increasingly more popular in web development due to its prefect look no matter the scale. As such you will probably find yourself using SVG files or icon fonts more and more often. However, not all browsers support SVGs out of the box, so fallbacks are needed. Currently XH Generator supports automatic optimization of SVG files (along with various other raster image formats) and PNG fallbacks creation. The caveat for correct automatic fallbacks is that SVG viewport needs to have proper size (PNG file will have the same dimensions). Also, if something seems off you can play with optimization settings in grunt/contrib-imagemin.js
task.