DevDay 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
- grunt - atomic grunt tasks configuration
- includes - HTML partials like
head.html,scripts.html, etc. - scss - Sass 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.jsis 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 DevDay project into dev-day directory.
First, change the directory to your cloned project:
cd dev-day
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 Sass 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
base.scss- normalized base styleslayout.scss- main page structureutilities.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 filemixins.scss- mixins file
- vendor - styles overwriting/replacing library ones
The following approach is recommended when creating styles:
- Use
main.scssonly 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
.articleas a main CSS class followed by.article-title,.article-meta, etc. and with.article--featuredvariant that will have slightly different color scheme, you will do everyone a favour by placing it inscss/components/_article.scssfile 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 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-appearanceor-webkit-font-smoothingare not a part of standard and need to be written with prefixes by you).
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.jsfile<!-- build:js js/plugins.min.js --> <script src="bower_components/jquery/dist/jquery.min.js"></script> <script src="bower_components/jquery-colorbox/jquery.colorbox-min.js"></script> <!-- endbuild --> <script src="js/main.js"></script>
-
Go to
src/bower_components/jquery-colorboxand copy images fromexample1/imagesfolder tosrc/img/colorboxfolder. -
Get
example1/colorbox.cssfrom the same dir, rename it to_colorbox.scss, store it insrc/scss/vendorfolder and adjust to your needs if needed. -
Import
_colorbox.scssinmain.scss@import "vendor/colorbox";
-
Replace all instances of
images/in_colorbox.scsswith../img/colorbox/ -
Run the
grunt buildtask orgrunttask -
Now you can use Colorbox in your HTML files and initiate it from
src/js/main.js