Git Product home page Git Product logo

require-bower-grunt-boilerplate's Introduction

require-bower-grunt-boilerplate

A boilerplate for a front-end build system setup using requirejs, bower, gruntjs and of course LESS

You must have nodejs installed in order to use this tool. http://nodejs.org/

Lets get started:

cd build
npm install

The File Watcher

I've setup automated tasks for both CSS and JS compilation. The compiled files will sit in /assets/css/dist and /assets/js/dist respectively.

You can trigger the file watcher using the following command: (assuming you're in the build folder)

grunt watch

Once the file watcher is initiated it will watch both assets/js and assets/css folders for any changes. Meaning if you change or add any files inside either of those folders, it will run the build tools and output the compiled file(s) in the respective /dist subfolder.

Since the LESS compiler is fairly straight foward. lets talk a bit more about the JS compiler.

Javascript Management

This system is using both bower and requirejs to help manage the js dependencies.

lets take a look at /assets/js/config.js

requirejs.config({
    baseUrl: '/assets/js/',
    waitSeconds: 0,
    paths: {
        jquery: '../../build/bower_components/jquery/dist/jquery',
        parsleyjs: '../../build/bower_components/parsleyjs/dist/parsley',
        site: 'app/site'
    },
    shim: {
        site: ['jquery']
    },
    packages: [

    ]
});

This is our config file which we use to keep track of dependencies and other assets.

This is where bower comes into play. Using bower install we can safely generate new libs and plugins into our folder structure as well as automatically add them to our config file.

For example, lets cd to our build folder and run bower install colorbox

The file watcher will recognize these changes and update the config file to look like this.

requirejs.config({
    baseUrl: '/assets/js/',
    waitSeconds: 0,
    paths: {
        jquery: '../../build/bower_components/jquery/dist/jquery',
        parsleyjs: '../../build/bower_components/parsleyjs/dist/parsley',
        site: 'app/site',
        colorbox: '../../build/bower_components/colorbox/jquery.colorbox',
    },
    shim: {
        site: [
            'jquery'
        ]
    },
    packages: [

    ]
});

Now that we've updated our config.js we need to tell the build tool which assets we're going to include in our production file. This is pretty simple and we do it inside of /assets/js/main.js which looks like this.

require(['./config'], function(){
    require([
        'parsleyjs',
        'colorbox',
        'site']);
});

Notice that I'm not including jquery, even though it's required for both colorbox, and /app/site.js. We don't need to do this inside of main.js because we're already defining it inside of config.js. This allows us to define dependencies for our plugins/files using:

  shim: {
site: [
'jquery'
]
},
.

Assuming that the changes you make are error free, and you save /assets/js/main.js your production ready js file will be placed inside /assets/js/dist/

Debugging JS

So we've now covered how we can manage our JS assets and get them ready for production. Lets talk about how we can troubleshoot/debug any issues we run into along the way.

below I've provided a very basic php conditional script that looks for a querysting yourdomain.com/?dev.

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if(false !== strpos($url,'dev')) { 
<script data-main="assets/js/main.js"  src="assets/js/require.min.js"></script>
} else {
<script src="assets/js/require.min.js"></script>
<script src="assets/js/dist/main.min.js?v=1"></script>
}

By using the data-main attribute inside of the require.min.js script tag and pointing it to the main.js file, requirejs will load each dependency individually.

<script data-main="assets/js/main.js"  src="assets/js/require.min.js"></script>

Having each script load individually will make it much easier to pin point any errors being returned

JS Unit Tests

I've setup a JS unit testing folder, which can be found here: /tests/. Inside of this folder, we can add unit tests using the popular unit testing framework qunit. These tests will run in the headless browser phantom.js and can be initiated by using grunt watch or grunt test.

If you end up using grunt watch for unit tesing, it will watch the /tests folder and re-run the unit tests upon any change.

Other Defined Grunt Tasks

I've created a number of other commands that mimick the watcher, to help troubleshoot, or get a different output.

To compile your LESS into unminfied css, run:

grunt less:dev

To compile your LESS like the watcher, run:

grunt less:prod

To compile your JS like the watcher, run:

grunt buildjs

To update your config.js file with any new bower updates without the watcher, run:

grunt package

To run unit tests via the /tests/ folder, run:

grunt test

You can make edits to the watcher, and other grunt processes via

/build/gruntfile.js

For windows users, I've also included batch files inside of /build that run the node commands.

Cool tricks

For those not to fimilar with bower, try checking out their package list.

http://bower.io/search/

require-bower-grunt-boilerplate's People

Contributors

beakshy avatar

Watchers

James Cloos avatar Tyler avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.