运行grunt构建命令时Gruntfile无法正常工作

时间:2015-11-17 23:27:13

标签: javascript gruntjs yeoman gruntfile

我使用yeoman使用“yo webapp”创建我的结构,我在安装过程中通过npm排除了所有额外内容并手动下载了引导文件。

我正在尝试运行“grunt build”命令,该命令应该包含app文件夹(及其子文件夹及其子文件夹等)中的所有内容,并创建,编译,连接和缩小文件到dist文件夹中父目录(与应用程序处于同一级别),因为我确信使用过yeoman和grunt的人都知道。

由于某种原因,它不会这样做,我已经尝试更改gruntfile中的默认路径等,以尝试使其工作,但它无法正常工作(说实话,说它没有工作根本就更合适,即使它在cmd中说它确实如此)。

它现在根据cmd完成构建任务(之前没有,声称imagemin任务存在问题,但我改变了那个,现在可以正常工作(或者它说)),但是当我查看时dist文件夹,只有一个index.html和一个样式文件(它不包括它应该的一些css文件......)。

以下是我的文件夹结构的重要部分:

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

.tmp文件夹是出于某种原因自动创建的,我假设帮忙哼了一声,因为它是在我将应用程序文件夹中的文件保存并且grunt正在观看时制作的。

我想要的只是简单:

  • 能够运行“grunt build”
  • 然后让grunt浏览所有文件夹和文件
  • 按预期连接,修改,重新创建,移动和创建文件(如果你曾经一起使用过yeoman和grunt,你就会更清楚地知道我的意思和期望)
  • 输入到dist文件夹

如果有帮助,文件夹中的文件类型就像你期望的那样,字体文件夹有[EOT,TTF,OTF,WOFF,SVG],图像及其子文件夹使用[PNG,JPEG,GIF],脚本有[JS,PHP]本身及其子文件夹和样式有[SASS,SCSS,CSS],但显然它只是我关心转移到dist的CSS。

这可能让你的思绪变得复杂,但希望你能够在我自己与自耕农和咕噜一起工作之后知道我的期望,并且可以帮助我完成任务和运动。

这是我的grunt文件:

// 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'
  ]);
};

我会截取屏幕截图向您展示问题,但正如我所说,当我运行“grunt build”时,它会根据CMD传递,但实际上并没有按照我的解释那样做。

如果您需要任何澄清,请告诉我。

干杯,

- SD

1 个答案:

答案 0 :(得分:1)

你从来没有说过它是否适用于干净的webapp。既然你已经尝试重新安装yeoman等并更新了npm模块,我会尝试生成一个新的webapp项目。并且不接触它就会运行grunt build以查看会发生什么。

yo webapp
bower install
grunt build

如果它不起作用,请发布您收到的错误。如果可行,则将当前的应用程序文件/文件夹复制到新项目中,然后再试一次。