Advertisement
Guest User

Gruntfile.js

a guest
Nov 28th, 2014
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. module.exports = function (grunt) {
  4.  
  5.   // Load grunt tasks automatically
  6.   require('load-grunt-tasks')(grunt);
  7.  
  8.   // Time how long tasks take. Can help when optimizing build times
  9.   require('time-grunt')(grunt);
  10.  
  11.   // Turn on the stack trace by default
  12.   grunt.option('stack', true);
  13.  
  14.   // Define the configuration for all the tasks
  15.   grunt.initConfig({
  16.  
  17.     // Read the package file
  18.     pkg: grunt.file.readJSON('package.json'),
  19.  
  20.     // Project settings
  21.     config: {
  22.       // Configurable paths
  23.       app: {
  24.         root: 'app',
  25.         scripts: '<%= config.app.root %>/scripts',
  26.         styles: '<%= config.app.root %>/styles',
  27.         templates: '<%= config.app.root %>/templates',
  28.         snippets: '<%= config.app.root %>/snippets',
  29.         img: '<%= config.app.root %>/images'
  30.       },
  31.       dist: {
  32.         site: 'site',
  33.         assets: 'assets',
  34.         scripts: '<%= config.dist.assets %>/scripts',
  35.         styles: '<%= config.dist.assets %>/styles',
  36.         templates: '<%= config.dist.site %>/templates',
  37.         snippets: '<%= config.dist.site %>/snippets',
  38.         img: '<%= config.dist.assets %>/images'
  39.       },
  40.       localhost: 'http://craigmdennis.local'
  41.  
  42.     },
  43.  
  44.     banner: '/*\n' +
  45.             'Author: <%= pkg.author %>\n' +
  46.             'Twitter: <%= pkg.twitter %>\n' +
  47.             'Description: <%= pkg.description %>\n' +
  48.             'Version: <%= pkg.version %>\n' +
  49.             '*/\n',
  50.  
  51.     // Watches files for changes and runs tasks based on the changed files
  52.     watch: {
  53.       jade: {
  54.         files: ['<%= config.app.templates %>/*.jade', '<%= config.app.snippets %>/*.jade'],
  55.         tasks: ['jadephp:server']
  56.       },
  57.       coffee: {
  58.         files: ['<%= config.app.scripts %>/{,*/}*.{coffee,litcoffee,coffee.md}'],
  59.         tasks: ['coffee', 'jshint:server']
  60.       },
  61.       js: {
  62.         files: ['<%= config.app.scripts %>/{,*/}*.js'],
  63.         tasks: ['newer:copy', 'jshint:server']
  64.       },
  65.       gruntfile: {
  66.         files: ['Gruntfile.js'],
  67.         tasks: ['jshint:gruntfile', 'default']
  68.       },
  69.       assets: {
  70.         files: ['<%= config.app.img %>/{,*/}*.{webp,svg}', '<%= config.app.assets %>/fonts/{,*/}*.*'],
  71.         tasks: ['newer:copy:dist']
  72.       },
  73.       sass: {
  74.         files: ['<%= config.app.styles %>/{,*/}*.{scss,sass}'],
  75.         tasks: ['sass', 'autoprefixer']
  76.       },
  77.       styles: {
  78.         files: ['<%= config.app.styles %>/{,*/}*.css'],
  79.         tasks: ['newer:copy:server', 'autoprefixer']
  80.       },
  81.       livereload: {
  82.         options: {
  83.           livereload: '<%= connect.options.livereload %>'
  84.         },
  85.         files: [
  86.           'content/**/*.{md,txt}',
  87.           '<%= config.dist.templates %>/{,*/}*.php',
  88.           '<%= config.dist.snippets %>/{,*/}*.php',
  89.           '.tmp/styles/{,*/}*.css',
  90.           '.tmp/scripts/{,*/}*.js',
  91.           '<%= config.app.img %>/{,*/}*.*'
  92.         ]
  93.       }
  94.     },
  95.  
  96.     // The actual grunt server settings
  97.     connect: {
  98.       options: {
  99.         port: 9000,
  100.         livereload: 35729,
  101.         hostname: '0.0.0.0'
  102.       },
  103.       livereload: {
  104.         options: {
  105.           open: '<%= config.localhost %>',
  106.           base: [
  107.             '.tmp',
  108.             '<%= config.app.root %>',
  109.             '/'
  110.           ]
  111.         }
  112.       },
  113.       dist: {
  114.         options: {
  115.           open: '<%= config.localhost %>',
  116.           base: '<%= config.dist.site %>',
  117.           livereload: false
  118.         }
  119.       }
  120.     },
  121.  
  122.     // Empties folders to start fresh
  123.     clean: {
  124.       dist: [
  125.         '.tmp',
  126.         '<%= config.dist.templates %>/*',
  127.         '<%= config.dist.snippets %>/*',
  128.         '<%= config.dist.assets %>'
  129.       ],
  130.       cache: [
  131.         '<%= config.dist.site %>/cache/*'
  132.       ]
  133.     },
  134.  
  135.     // Compile Jade to HTML
  136.     jadephp: {
  137.       options: {
  138.         pretty: true
  139.       },
  140.       // Compile templates straight to php in dist
  141.       server: {
  142.         files: [
  143.           {
  144.             expand: true,
  145.             cwd: '<%= config.app.templates %>/',
  146.             dest: '<%= config.dist.templates %>',
  147.             src: '*.jade',
  148.             ext: '.php'
  149.           },{
  150.             expand: true,
  151.             cwd: '<%= config.app.snippets %>/',
  152.             dest: '<%= config.dist.snippets %>',
  153.             src: '*.jade',
  154.             ext: '.php'
  155.           }
  156.         ]
  157.       },
  158.       // Compile templates to HTML in .tmp so we can run useminPrepare
  159.       dist: {
  160.         files: [
  161.           {
  162.             expand: true,
  163.             cwd: '<%= config.app.templates %>/',
  164.             dest: '.tmp/templates',
  165.             src: '*.jade',
  166.             ext: '.html'
  167.           },{
  168.             expand: true,
  169.             cwd: '<%= config.app.snippets %>/',
  170.             dest: '.tmp/snippets',
  171.             src: '*.jade',
  172.             ext: '.html'
  173.           }
  174.         ]
  175.       },
  176.     },
  177.  
  178.     // Make sure code styles are up to par and there are no obvious mistakes
  179.     jshint: {
  180.       options: {
  181.         jshintrc: '.jshintrc',
  182.         reporter: require('jshint-stylish')
  183.       },
  184.       gruntfile: {
  185.         src: 'Gruntfile.js'
  186.       },
  187.       server: {
  188.         options: {
  189.           force: true
  190.         },
  191.         src: '.tmp/scripts/{,*/}.js'
  192.       },
  193.       dist: {
  194.         src: '<%= jshint.server.src %>'
  195.       }
  196.     },
  197.  
  198.     // Compiles CoffeeScript to JavaScript
  199.     coffee: {
  200.       options: {
  201.         sourceMap: false
  202.       },
  203.       dist: {
  204.         files: [{
  205.           expand: true,
  206.           cwd: '<%= config.app.scripts %>/',
  207.           src: '{,*/}*.{coffee,litcoffee,coffee.md}',
  208.           dest: '.tmp/scripts',
  209.           ext: '.js'
  210.         }]
  211.       },
  212.     },
  213.  
  214.  
  215.     // Compiles Sass to CSS and generates necessary files if requested
  216.     sass: {
  217.       options: {
  218.         style: 'expanded',
  219.         sourcemap: false,
  220.         quiet: true
  221.       },
  222.       dist: {
  223.         files: [{
  224.           expand: true,
  225.           cwd: '<%= config.app.styles %>/',
  226.           src: '{,*/}*.scss',
  227.           dest: '.tmp/styles',
  228.           ext: '.css'
  229.         }]
  230.       }
  231.     },
  232.  
  233.     // Add vendor prefixed styles
  234.     autoprefixer: {
  235.       options: {
  236.         browsers: ['last 1 version']
  237.       },
  238.       dist: {
  239.         files: [{
  240.           expand: true,
  241.           cwd: '.tmp/styles/',
  242.           src: '{,*/}*.css',
  243.           dest: '.tmp/styles/'
  244.         }]
  245.       }
  246.     },
  247.  
  248.     // Automatically inject Bower components into the HTML file
  249.     bowerInstall: {
  250.       app: {
  251.         src: ['<%= config.app.snippets %>/{,*/}*.jade'],
  252.         ignorePath: '<%= config.app.root %>/'
  253.       },
  254.       sass: {
  255.         src: ['<%= config.app.styles %>/{,*/}*.{scss,sass}'],
  256.         ignorePath: '<%= config.app.root %>/bower_components/'
  257.       }
  258.     },
  259.  
  260.     // Reads HTML for usemin blocks to enable smart builds that automatically
  261.     // concat, minify and revision files. Creates configurations in memory so
  262.     // additional tasks can operate on them
  263.     useminPrepare: {
  264.       options: {
  265.         dest: '<%= config.dist.assets %>'
  266.       },
  267.       html: ['.tmp/templates/{,*/}*.html', '.tmp/snippets/{,*/}*.html']
  268.     },
  269.  
  270.     // Performs rewrites based on rev and the useminPrepare configuration
  271.     usemin: {
  272.       options: {
  273.         assetDirs: '<%= config.dist.assets %>',
  274.         basedir: '',
  275.         dirs: '<%= config.app.root %>'
  276.       },
  277.       html: ['.tmp/templates/{,*/}*.html', '.tmp/snippets/{,*/}*.html'],
  278.       css: '.tmp/styles/{,*/}*.css'
  279.     },
  280.  
  281.     // The following *-min tasks produce minified files in the dist folder
  282.     imagemin: {
  283.       dist: {
  284.         files: [{
  285.           expand: true,
  286.           cwd: '<%= config.app.img %>/',
  287.           src: '{,*/}*.{gif,jpeg,jpg,png}',
  288.           dest: '<%= config.dist.img %>'
  289.         }]
  290.       }
  291.     },
  292.  
  293.     uglify: {
  294.       options: {
  295.         sourceMap: false
  296.       }
  297.     },
  298.  
  299.     cssmin: {
  300.       options: {
  301.         banner: '<%= banner %>',
  302.         keepSpecialComments: false,
  303.         report: 'min'
  304.       }
  305.     },
  306.  
  307.     // Copies remaining files to places other tasks can use
  308.     copy: {
  309.       server: {
  310.         files: [
  311.           {
  312.             // Copy any .css files to the temp directory
  313.             expand: true,
  314.             dot: true,
  315.             cwd: '<%= config.app.styles %>/',
  316.             dest: '.tmp/styles/',
  317.             src: '{,*/}*.css'
  318.           },
  319.           {
  320.             // Copy any js files to the temp directory
  321.             expand: true,
  322.             dot: true,
  323.             cwd: '<%= config.app.scripts %>/',
  324.             dest: '.tmp/scripts/',
  325.             src: '{,*/}*.js'
  326.           },
  327.           {
  328.             // Copy any important top-level files to the temp directory
  329.             expand: true,
  330.             dot: true,
  331.             cwd: '<%= config.app.root %>/',
  332.             dest: '<%= config.dist.site %>',
  333.             src: [
  334.               '*.{ico,png,txt,svg}',
  335.               '.htaccess',
  336.               '*.php',
  337.             ]
  338.           }
  339.         ]
  340.       },
  341.       dist: {
  342.         files: [
  343.           {
  344.             // Copy files that aren't compressed with imagemin
  345.             expand: true,
  346.             dot: true,
  347.             cwd: '<%= config.app.root %>/',
  348.             dest: '<%= config.dist.assets %>',
  349.             src: [
  350.               'images/{,*/}*.{webp,svg}',
  351.               'fonts/{,*/}*.*'
  352.             ]
  353.           },
  354.           {
  355.             // Copy HTML tempaltes and convert them to php
  356.             expand: true,
  357.             dot: true,
  358.             cwd: '.tmp/templates',
  359.             dest: '<%= config.dist.templates %>',
  360.             src: '*.html',
  361.             ext: '.php'
  362.           },
  363.           {
  364.             // Copy HTML snippets and convert them to php
  365.             expand: true,
  366.             dot: true,
  367.             cwd: '.tmp/snippets',
  368.             dest: '<%= config.dist.snippets %>',
  369.             src: '*.html',
  370.             ext: '.php'
  371.           },
  372.           {
  373.             src: '<%= config.app.root %>/bower_components/jquery/dist/jquery.min.js',
  374.             dest: '<%= config.dist.scripts %>/vendor/jquery-1.11.1.min.js'
  375.           }
  376.         ]
  377.       }
  378.     },
  379.  
  380.     // Renames files for browser caching purposes
  381.     rev: {
  382.       dist: {
  383.         files: {
  384.           src: [
  385.             '<%= config.dist.scripts %>/{,*/}*.js',
  386.             '<%= config.dist.styles %>/{,*/}*.css',
  387.             '<%= config.dist %>/fonts/{,*/}*.*',
  388.             '<%= config.dist %>/*.{ico,png}',
  389.  
  390.             // Don't rev images until I can figure out how to support CDN
  391.             '!<%= config.dist.img %>/{,*/}*.*'
  392.           ]
  393.         }
  394.       }
  395.     },
  396.  
  397.     // Generates a custom Modernizr build that includes only the tests you
  398.     // reference in your app
  399.     modernizr: {
  400.       dist: {
  401.         devFile: '<%= config.app.root %>/bower_components/modernizr/modernizr.js',
  402.         outputFile: '<%= config.dist.scripts %>/vendor/modernizr-custom.js',
  403.         files: {
  404.           src: [
  405.             '<%= config.dist.scripts %>/*.js',
  406.             '<%= config.dist.styles %>/{,*/}*.css'
  407.           ]
  408.         },
  409.         matchCommunityTests: true,
  410.         uglify: true,
  411.         extra: {
  412.           shiv: false,
  413.           load: false
  414.         }
  415.       }
  416.     },
  417.  
  418.     bump: {
  419.       options: {
  420.         files: ['package.json', 'bower.json'],
  421.         push: true,
  422.         pushTo: 'origin',
  423.         createTag: true,
  424.         tagName: 'v%VERSION%',
  425.         tagMessage: 'Version %VERSION%',
  426.         commitFiles: ['<%= bump.options.files %>', 'CHANGELOG.md'],
  427.         commitMessage: 'Bumped version to v%VERSION%'
  428.       }
  429.     },
  430.  
  431.     changelog: {
  432.       options: {
  433.         editor: 'sublime -w'
  434.       }
  435.     },
  436.  
  437.     // Run some tasks in parallel to speed up build process
  438.     concurrent: {
  439.       options: {
  440.         logConcurrentOutput: true,
  441.         limit: 5
  442.       },
  443.       server: [
  444.         'newer:sass',
  445.         'newer:jadephp:server',
  446.         'newer:coffee',
  447.         'newer:copy',
  448.       ],
  449.       dist: [
  450.         'jadephp:dist',
  451.         'sass',
  452.         'coffee',
  453.         'copy:server',
  454.         'imagemin'
  455.       ]
  456.     }
  457.   });
  458.  
  459.   grunt.registerTask('default', [
  460.     'clean',
  461.     'concurrent:server',
  462.     'autoprefixer',
  463.   ]);
  464.  
  465.   grunt.registerTask('serve', [
  466.     'default',
  467.     'connect:livereload',
  468.     'watch'
  469.   ]);
  470.  
  471.   grunt.registerTask('build', [
  472.     'clean',
  473.     'concurrent:dist',
  474.     'useminPrepare',
  475.     'jshint',
  476.     'autoprefixer',
  477.     'concat',
  478.     'uglify',
  479.     'cssmin',
  480.     'modernizr',
  481.     // 'rev',
  482.     'usemin',
  483.     'copy:dist',
  484.     'connect:dist'
  485.   ]);
  486.  
  487. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement