Gruntfile not working when running the grunt build command
Asked Answered
R

1

16

I used yeoman to create my structure using "yo webapp", I excluded all extras during the installation process via npm and manually downloaded the bootstrap files.

I am trying to run the "grunt build" command which should take everything in the app folder (and its sub folders and their sub folder, etc) and create, compile, concatenate and minify the files into a dist folder in the parent directory (on the same level as app), as I am sure anyone who has used yeoman and grunt know.

For some reason it won't do it though and I have tried changing default paths, etc, etc in the gruntfile to try to make it work but it isn't working correctly (to be honest, saying it doesn't work at all is more apt, even though it says in the cmd that it does).

It completes the build task according the the cmd now (it didnt before, claiming there was an issue with the imagemin task, but I altered that one and it works now (or so it says)), but when I look in the dist folder, there is only an index.html and a styles file (which doesnt include some of the css files that it should...).

Here is the important part of my folder structure:

Site
├───.tmp
│   ├───spec
│   └───styles
├───app
    ├───fonts
    ├───images
    │   ├───home
    │   ├───payments
    │   └───profile
    ├───scripts
    │   ├───JS
    │   ├───PHP
    └───styles

The .tmp folder is automatically created for some reason, I assume to help grunt with something since it is made when I save files in the app folder and grunt is watching.

All I want is simple:

  • To be able to run "grunt build"
  • To then have grunt go through all folders and files
  • To concatenate, modify, recreate, move and create files as expected (if you have used yeoman and grunt together, you will know what I mean and expect, more aptly)
  • To output into the dist folder

If it helps, he filetypes in the folders are as you would expect, the font folder has [EOT, TTF, OTF, WOFF, SVG], images and its sub folders use [PNG, JPEG, GIF], scripts has [JS, PHP] in itself and its sub folders and styles has [SASS, SCSS, CSS], but obviously its just the CSS that I care about moving over to dist.

That may have complicated your mind but hopefully you know what I expect after working with yeoman and grunt before yourself and can help me get the tasks and movement going.

Here is my gruntfile as it stands:

// Generated on 2015-11-17 using
// generator-webapp 1.1.0
'use strict';

// # Globbing
// for performance reasons we're only matching one level down:
// 'test/spec/{,*/}*.js'
// If you want to recursively match all subfolders, use:
// 'test/spec/**/*.js'

module.exports = function (grunt) {

    // Time how long tasks take. Can help when optimizing build times
    require('time-grunt')(grunt);

    // Automatically load required grunt tasks
    require('jit-grunt')(grunt, {
        useminPrepare: 'grunt-usemin'
    });

    // Configurable paths
    var config = {
        app: 'app',
        dist: 'dist'
    };

    // Define the configuration for all the tasks
    grunt.initConfig({

        // Project settings
        config: config,

        // Watches files for changes and runs tasks based on the changed files
        watch: {
            bower: {
                files: ['bower.json'],
                tasks: ['wiredep']
            },
            babel: {
                files: ['<%= config.app %>/scripts/{,*/}*.js'],
                tasks: ['babel:dist']
            },
            babelTest: {
                files: ['test/spec/{,*/}*.js'],
                tasks: ['babel:test', 'test:watch']
            },
            gruntfile: {
                files: ['Gruntfile.js']
            },
            styles: {
                files: ['<%= config.app %>/styles/{,*/}*.css'],
                tasks: ['newer:copy:styles', 'postcss']
            }
        },

        browserSync: {
            options: {
                notify: false,
                background: true,
                watchOptions: {
                    ignored: ''
                }
            },
            livereload: {
                options: {
                    files: [
            '<%= config.app %>/{,*/}*.html',
            '.tmp/styles/{,*/}*.css',
            '<%= config.app %>/images/{,*/}*',
            '.tmp/scripts/{,*/}*.js'
          ],
                    port: 9000,
                    server: {
                        baseDir: ['.tmp', config.app],
                        routes: {
                            '/bower_components': './bower_components'
                        }
                    }
                }
            },
            test: {
                options: {
                    port: 9001,
                    open: false,
                    logLevel: 'silent',
                    host: 'localhost',
                    server: {
                        baseDir: ['.tmp', './test', config.app],
                        routes: {
                            '/bower_components': './bower_components'
                        }
                    }
                }
            },
            dist: {
                options: {
                    background: false,
                    server: '<%= config.dist %>'
                }
            }
        },

        // Empties folders to start fresh
        clean: {
            dist: {
                files: [{
                    dot: true,
                    src: [
            '.tmp',
            '<%= config.dist %>/*',
            '!<%= config.dist %>/.git*'
          ]
        }]
            },
            server: '.tmp'
        },

        // Make sure code styles are up to par and there are no obvious mistakes
        eslint: {
            target: [
        'Gruntfile.js',
        '<%= config.app %>/scripts/{,*/}*.js',
        '!<%= config.app %>/scripts/vendor/*',
        'test/spec/{,*/}*.js'
      ]
        },

        // Mocha testing framework configuration options
        mocha: {
            all: {
                options: {
                    run: true,
                    urls: ['http://<%= browserSync.test.options.host %>:<%= browserSync.test.options.port %>/index.html']
                }
            }
        },

        // Compiles ES6 with Babel
        babel: {
            options: {
                sourceMap: true
            },
            dist: {
                files: [{
                    expand: true,
                    cwd: '<%= config.app %>/scripts/*',
                    src: '{,*/}*.js',
                    dest: '.tmp/scripts',
                    ext: '.js'
        }]
            },
            test: {
                files: [{
                    expand: true,
                    cwd: 'test/spec',
                    src: '{,*/}*.js',
                    dest: '.tmp/spec',
                    ext: '.js'
        }]
            }
        },

        postcss: {
            options: {
                map: true,
                processors: [
          // Add vendor prefixed styles
          require('autoprefixer')({
                        browsers: ['> 1%', 'last 2 versions', 'Firefox ESR']
                    })
        ]
            },
            dist: {
                files: [{
                    expand: true,
                    cwd: '.tmp/styles/',
                    src: '{,*/}*.css',
                    dest: '.tmp/styles/'
        }]
            }
        },

        // Automatically inject Bower components into the HTML file
        wiredep: {
            app: {
                src: ['<%= config.app %>/index.html'],
                ignorePath: /^(\.\.\/)*\.\./
            }
        },

        // Renames files for browser caching purposes
        filerev: {
            dist: {
                src: [
          '<%= config.dist %>/scripts/{,*/}*.js',
          '<%= config.dist %>/styles/{,*/}*.css',
          '<%= config.dist %>/images/{,*/}*.*',
          '<%= config.dist %>/styles/fonts/{,*/}*.*',
          '<%= config.dist %>/*.{ico,png}'
        ]
            }
        },

        // Reads HTML for usemin blocks to enable smart builds that automatically
        // concat, minify and revision files. Creates configurations in memory so
        // additional tasks can operate on them
        useminPrepare: {
            options: {
                dest: '<%= config.dist %>'
            },
            html: '<%= config.app %>/index.html'
        },

        // Performs rewrites based on rev and the useminPrepare configuration
        usemin: {
            options: {
                assetsDirs: [
          '<%= config.dist %>',
          '<%= config.dist %>/images',
          '<%= config.dist %>/styles'
        ]
            },
            html: ['<%= config.dist %>/{,*/}*.html'],
            css: ['<%= config.dist %>/styles/{,*/}*.css']
        },

        // The following *-min tasks produce minified files in the dist folder
        imagemin: {
            dist: {
                files: [{
                    expand: true,
                    cwd: '<%= config.app %>/images/*',
                    src: '{,*/}*.*',
                    dest: '<%= config.dist %>/images'
        }]
            }
        },

        svgmin: {
            dist: {
                files: [{
                    expand: true,
                    cwd: '<%= config.app %>/images',
                    src: '{,*/}*.svg',
                    dest: '<%= config.dist %>/images'
        }]
            }
        },

        htmlmin: {
            dist: {
                options: {
                    collapseBooleanAttributes: true,
                    collapseWhitespace: true,
                    conservativeCollapse: true,
                    removeAttributeQuotes: true,
                    removeCommentsFromCDATA: true,
                    removeEmptyAttributes: true,
                    removeOptionalTags: true,
                    // true would impact styles with attribute selectors
                    removeRedundantAttributes: false,
                    useShortDoctype: true
                },
                files: [{
                    expand: true,
                    cwd: '<%= config.dist %>',
                    src: '{,*/}*.html',
                    dest: '<%= config.dist %>'
        }]
            }
        },
        cssmin: {
            dist: {
                files: {
                    '<%= config.dist %>/styles/main.css': [
                 '.tmp/styles/{,*/}*.css',
                 '<%= config.app %>/styles/{,*/}*.css'
               ]
                }
            }
        },
        uglify: {
            dist: {
                files: {
                    '<%= config.dist %>/scripts/scripts.js': [
                 '<%= config.dist %>/scripts/scripts.js'
               ]
                }
            }
        },
        concat: {
            dist: {}
        },

        // Copies remaining files to places other tasks can use
        copy: {
            dist: {
                files: [{
                    expand: true,
                    dot: true,
                    cwd: '<%= config.app %>',
                    dest: '<%= config.dist %>',
                    src: [
            '*.{ico,png,txt}',
            'images/{,*/}*.webp',
            '{,*/}*.html',
            'styles/fonts/{,*/}*.*'
          ]
        }]
            },
            styles: {
                expand: true,
                dot: true,
                cwd: '<%= config.app %>/styles',
                dest: '.tmp/styles/',
                src: '{,*/}*.css'
            }
        },

        // Run some tasks in parallel to speed up build process
        concurrent: {
            server: [
        'babel:dist',
        'copy:styles'
      ],
            test: [
        'babel',
        'copy:styles'
      ],
            dist: [
        'babel',
        'copy:styles',
        'imagemin',
        'svgmin'
      ]
        }
    });


    grunt.registerTask('serve', 'start the server and preview your app', function (target) {

        if (target === 'dist') {
            return grunt.task.run(['build', 'browserSync:dist']);
        }

        grunt.task.run([
      'clean:server',
      'wiredep',
      'concurrent:server',
      'postcss',
      'browserSync:livereload',
      'watch'
    ]);
    });

    grunt.registerTask('server', function (target) {
        grunt.log.warn('The `server` task has been deprecated. Use `grunt serve` to start a server.');
        grunt.task.run([target ? ('serve:' + target) : 'serve']);
    });

    grunt.registerTask('test', function (target) {
        if (target !== 'watch') {
            grunt.task.run([
        'clean:server',
        'concurrent:test',
        'postcss'
      ]);
        }

        grunt.task.run([
      'browserSync:test',
      'mocha'
    ]);
    });

    grunt.registerTask('build', [
    'clean:dist',
    'wiredep',
    'useminPrepare',
    'concurrent:dist',
    'postcss',
    'concat',
    'cssmin',
    'uglify',
    'copy:dist',
    'filerev',
    'usemin',
    'htmlmin'
  ]);

    grunt.registerTask('default', [
    'newer:eslint',
    'test',
    'build'
  ]);
};

I would take screenshots to show you the problem but as I said, when I run "grunt build", it passes according to the CMD but does not actually do what is expected, as I explained.

If you need any clarifications, let me know.

Cheers,

-- SD

Rodgerrodgers answered 17/11, 2015 at 23:27 Comment(9)
A couple questions: 1. "I used yeoman to create my structure using yo webapp". So are you having problems starting a project from scratch? 2. When you run grunt, do you get any error message?Tadzhik
Id recommend you reinstall Node and the associated packages(yeoman etc)Sarcophagus
@Tadzhik 1) No, no, I have a project that has began, I was merely explaining how I made the project at the start since yo created a different gruntfile dependant on your generator used, in my case it was the "webapp" generator. 2) Not anymore as I explained, but to be fair, as explained, I messed with paths and stuff trying what I could, now it passes and runs but doesnt actually do what it is meant to, as I explained.Rodgerrodgers
@Sarcophagus Tried that not long after posting this, didn't help though and since it was on the latest versions of all the packages, I felt it was a null thing to point out that I did, seeing as nothing actually changed in versioning or anything.Rodgerrodgers
You can start with updating your packages and updating npm. You might want to try running: "npm update -g npm". This will update npm itself. You can even try running this command again: "npm install -g yo bower grunt-cli gulp"Tadzhik
I already updated npm and all the packages, as I explained in my last comment to @ihaveitnow.Rodgerrodgers
If all you want to do is have it concat, minify/uglify, and move to dist on build, that is a pretty simple gruntfile to create yourself. Start from scratch. Build it incrementally testing each task along the way.Ganof
Can you show us what is in the dist that gets built?Becket
I did yo webapp and replaced webapp gruntfile with your gruntfile. grunt build seems to be working fine. do you have repo with complete source code?Thermolabile
H
1

You never said if it a clean webapp ever worked for you. Since you have already tried re installing yeoman etc and have updated the npm modules I would try to generate a new webapp project. And without touching it run grunt build to see what happens.

yo webapp
bower install
grunt build

If it doesn't work please post the error you are getting. If it works, then copy your current app files/folders into the new project and then try again it.

Halfcocked answered 13/1, 2016 at 3:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.