Advertisement
Guest User

gruntfile

a guest
Oct 13th, 2013
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2. var LIVERELOAD_PORT = 35729;
  3. var lrSnippet = require('connect-livereload')({port: LIVERELOAD_PORT});
  4. var mountFolder = function (connect, dir) {
  5.     return connect.static(require('path').resolve(dir));
  6. };
  7.  
  8. // # Globbing
  9. // for performance reasons we're only matching one level down:
  10. // 'test/spec/{,*/}*.js'
  11. // use this if you want to match all subfolders:
  12. // 'test/spec/**/*.js'
  13. // templateFramework: 'lodash'
  14.  
  15. module.exports = function (grunt) {
  16.     // show elapsed time at the end
  17.     require('time-grunt')(grunt);
  18.     // load all grunt tasks
  19.     require('load-grunt-tasks')(grunt);
  20.  
  21.     // configurable paths
  22.     var yeomanConfig = {
  23.         app: 'app',
  24.         dist: 'dist'
  25.     };
  26.  
  27.     grunt.initConfig({
  28.         yeoman: yeomanConfig,
  29.         watch: {
  30.             options: {
  31.                 nospawn: true
  32.             },
  33.             coffee: {
  34.                 files: ['<%= yeoman.app %>/scripts/{,*/}*.coffee'],
  35.                 tasks: ['coffee:dist']
  36.             },
  37.             coffeeTest: {
  38.                 files: ['test/spec/{,*/}*.coffee'],
  39.                 tasks: ['coffee:test']
  40.             },
  41.             compass: {
  42.                 files: ['<%= yeoman.app %>/styles/{,*/}*.{scss,sass}'],
  43.                 tasks: ['compass']
  44.             },
  45.             livereload: {
  46.                 options: {
  47.                     livereload: LIVERELOAD_PORT
  48.                 },
  49.                 files: [
  50.                     '<%= yeoman.app %>/*.html',
  51.                     '{.tmp,<%= yeoman.app %>}/styles/{,*/}*.css',
  52.                     '{.tmp,<%= yeoman.app %>}/scripts/{,*/}*.js',
  53.                     '<%= yeoman.app %>/images/{,*/}*.{png,jpg,jpeg,gif,webp}',
  54.                     '<%= yeoman.app %>/scripts/templates/*.{ejs,mustache,hbs}'
  55.                 ]
  56.             },
  57.             jst: {
  58.                 files: [
  59.                     '<%= yeoman.app %>/scripts/templates/*.ejs'
  60.                 ],
  61.                 tasks: ['jst']
  62.             }
  63.         },
  64.         connect: {
  65.             options: {
  66.                 port: 9000,
  67.                 // change this to '0.0.0.0' to access the server from outside
  68.                 hostname: 'localhost'
  69.             },
  70.             livereload: {
  71.                 options: {
  72.                     middleware: function (connect) {
  73.                         return [
  74.                             lrSnippet,
  75.                             mountFolder(connect, '.tmp'),
  76.                             mountFolder(connect, yeomanConfig.app)
  77.                         ];
  78.                     }
  79.                 }
  80.             },
  81.             test: {
  82.                 options: {
  83.                     port: 9001,
  84.                     middleware: function (connect) {
  85.                         return [
  86.                             mountFolder(connect, '.tmp'),
  87.                             mountFolder(connect, 'test'),
  88.                             mountFolder(connect, yeomanConfig.app)
  89.                         ];
  90.                     }
  91.                 }
  92.             },
  93.             dist: {
  94.                 options: {
  95.                     middleware: function (connect) {
  96.                         return [
  97.                             mountFolder(connect, yeomanConfig.dist)
  98.                         ];
  99.                     }
  100.                 }
  101.             }
  102.         },
  103.         open: {
  104.             server: {
  105.                 path: 'http://localhost:<%= connect.options.port %>'
  106.             }
  107.         },
  108.         clean: {
  109.             dist: ['.tmp', '<%= yeoman.dist %>/*'],
  110.             server: '.tmp'
  111.         },
  112.         jshint: {
  113.             options: {
  114.                 jshintrc: '.jshintrc'
  115.             },
  116.             all: [
  117.                 'Gruntfile.js',
  118.                 '<%= yeoman.app %>/scripts/{,*/}*.js',
  119.                 '!<%= yeoman.app %>/scripts/vendor/*',
  120.                 'test/spec/{,*/}*.js'
  121.             ]
  122.         },
  123.         mocha: {
  124.             all: {
  125.                 options: {
  126.                     run: true,
  127.                     urls: ['http://localhost:<%= connect.options.port %>/index.html']
  128.                 }
  129.             }
  130.         },
  131.         coffee: {
  132.             dist: {
  133.                 files: [{
  134.                     // rather than compiling multiple files here you should
  135.                     // require them into your main .coffee file
  136.                     expand: true,
  137.                     cwd: '<%= yeoman.app %>/scripts',
  138.                     src: '{,*/}*.coffee',
  139.                     dest: '.tmp/scripts',
  140.                     ext: '.js'
  141.                 }]
  142.             },
  143.             test: {
  144.                 files: [{
  145.                     expand: true,
  146.                     cwd: 'test/spec',
  147.                     src: '{,*/}*.coffee',
  148.                     dest: '.tmp/spec',
  149.                     ext: '.js'
  150.                 }]
  151.             }
  152.         },
  153.         compass: {
  154.             options: {
  155.                 sassDir: '<%= yeoman.app %>/styles',
  156.                 cssDir: '.tmp/styles',
  157.                 imagesDir: '<%= yeoman.app %>/images',
  158.                 javascriptsDir: '<%= yeoman.app %>/scripts',
  159.                 fontsDir: '<%= yeoman.app %>/styles/fonts',
  160.                 importPath: '<%= yeoman.app %>/bower_components',
  161.                 relativeAssets: true
  162.             },
  163.             dist: {},
  164.             server: {
  165.                 options: {
  166.                     debugInfo: true
  167.                 }
  168.             }
  169.         },
  170.         // not enabled since usemin task does concat and uglify
  171.         // check index.html to edit your build targets
  172.         // enable this task if you prefer defining your build targets here
  173.         /*uglify: {
  174.             dist: {}
  175.         },*/
  176.         useminPrepare: {
  177.             html: '<%= yeoman.app %>/index.html',
  178.             options: {
  179.                 dest: '<%= yeoman.dist %>'
  180.             }
  181.         },
  182.         usemin: {
  183.             html: ['<%= yeoman.dist %>/{,*/}*.html'],
  184.             css: ['<%= yeoman.dist %>/styles/{,*/}*.css'],
  185.             options: {
  186.                 dirs: ['<%= yeoman.dist %>']
  187.             }
  188.         },
  189.         imagemin: {
  190.             dist: {
  191.                 files: [{
  192.                     expand: true,
  193.                     cwd: '<%= yeoman.app %>/images',
  194.                     src: '{,*/}*.{png,jpg,jpeg}',
  195.                     dest: '<%= yeoman.dist %>/images'
  196.                 }]
  197.             }
  198.         },
  199.         cssmin: {
  200.             dist: {
  201.                 files: {
  202.                     '<%= yeoman.dist %>/styles/main.css': [
  203.                         '.tmp/styles/{,*/}*.css',
  204.                         '<%= yeoman.app %>/styles/{,*/}*.css'
  205.                     ]
  206.                 }
  207.             }
  208.         },
  209.         htmlmin: {
  210.             dist: {
  211.                 options: {
  212.                     /*removeCommentsFromCDATA: true,
  213.                     // https://github.com/yeoman/grunt-usemin/issues/44
  214.                     //collapseWhitespace: true,
  215.                     collapseBooleanAttributes: true,
  216.                     removeAttributeQuotes: true,
  217.                     removeRedundantAttributes: true,
  218.                     useShortDoctype: true,
  219.                     removeEmptyAttributes: true,
  220.                     removeOptionalTags: true*/
  221.                 },
  222.                 files: [{
  223.                     expand: true,
  224.                     cwd: '<%= yeoman.app %>',
  225.                     src: '*.html',
  226.                     dest: '<%= yeoman.dist %>'
  227.                 }]
  228.             }
  229.         },
  230.         copy: {
  231.             dist: {
  232.                 files: [{
  233.                     expand: true,
  234.                     dot: true,
  235.                     cwd: '<%= yeoman.app %>',
  236.                     dest: '<%= yeoman.dist %>',
  237.                     src: [
  238.                         '*.{ico,txt}',
  239.                         '.htaccess',
  240.                         'images/{,*/}*.{webp,gif}',
  241.                         'styles/fonts/{,*/}*.*'
  242.                     ]
  243.                 }]
  244.             }
  245.         },
  246.         bower: {
  247.             all: {
  248.                 rjsConfig: '<%= yeoman.app %>/scripts/main.js'
  249.             }
  250.         },
  251.         jst: {
  252.             compile: {
  253.                 files: {
  254.                     '.tmp/scripts/templates.js': ['<%= yeoman.app %>/scripts/templates/*.ejs']
  255.                 }
  256.             }
  257.         },
  258.         rev: {
  259.             dist: {
  260.                 files: {
  261.                     src: [
  262.                         '<%= yeoman.dist %>/scripts/{,*/}*.js',
  263.                         '<%= yeoman.dist %>/styles/{,*/}*.css',
  264.                         '<%= yeoman.dist %>/images/{,*/}*.{png,jpg,jpeg,gif,webp}',
  265.                         '/styles/fonts/{,*/}*.*'
  266.                     ]
  267.                 }
  268.             }
  269.         }
  270.     });
  271.  
  272.     grunt.registerTask('createDefaultTemplate', function () {
  273.         grunt.file.write('.tmp/scripts/templates.js', 'this.JST = this.JST || {};');
  274.     });
  275.  
  276.     grunt.registerTask('server', function (target) {
  277.         if (target === 'dist') {
  278.             return grunt.task.run(['build', 'open', 'connect:dist:keepalive']);
  279.         } else if (target === 'test') {
  280.             return grunt.task.run([
  281.                 'clean:server',
  282.                 'coffee',
  283.                 'createDefaultTemplate',
  284.                 'jst',
  285.                 'compass:server',
  286.                 'connect:test:keepalive'
  287.             ]);
  288.         }
  289.  
  290.         grunt.task.run([
  291.             'clean:server',
  292.             'coffee:dist',
  293.             'createDefaultTemplate',
  294.             'jst',
  295.             'compass:server',
  296.             'connect:livereload',
  297.             'open',
  298.             'watch'
  299.         ]);
  300.     });
  301.  
  302.     grunt.registerTask('test', [
  303.         'clean:server',
  304.         'coffee',
  305.         'createDefaultTemplate',
  306.         'jst',
  307.         'compass',
  308.         'connect:test',
  309.         'mocha'
  310.     ]);
  311.  
  312.     grunt.registerTask('build', [
  313.         'clean:dist',
  314.         'coffee',
  315.         'createDefaultTemplate',
  316.         'jst',
  317.         'compass:dist',
  318.         'useminPrepare',
  319.         'imagemin',
  320.         'htmlmin',
  321.         'concat',
  322.         'cssmin',
  323.         'uglify',
  324.         'copy',
  325.         'rev',
  326.         'usemin'
  327.     ]);
  328.  
  329.     grunt.registerTask('default', [
  330.         'jshint',
  331.         'test',
  332.         'build'
  333.     ]);
  334. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement