javascript - 运行 grunt build 命令时 Gruntfile 不工作

标签 javascript gruntjs yeoman gruntfile

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

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

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

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

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

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

.tmp 文件夹是出于某种原因自动创建的,我想是为了帮助 grunt 做一些事情,因为它是在我将文件保存在应用程序文件夹中并且 grunt 正在观看时创建的。

我想要的很简单:

  • 能够运行“grunt build”
  • 然后让 grunt 遍历所有文件夹和文件
  • 按预期连接、修改、重新创建、移动和创建文件(如果您一起使用过 yeoman 和 grunt,您会更恰本地理解我的意思和期望)
  • 输出到dist文件夹

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

这可能让您的想法变得复杂,但希望您知道我在与 yeoman 和 grunt 合作之后的期望,并可以帮助我完成任务和移动。

这是我的 gruntfile:

// 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 通过,但实际上并没有像我解释的那样执行预期的操作。

如果您需要任何说明,请告诉我。

干杯,

-- 标清

最佳答案

您从未说过它是否适合您使用干净的网络应用程序。由于您已经尝试重新安装 yeoman 等并更新了 npm 模块,我会尝试生成一个新的 webapp 项目。在不碰它的情况下运行 grunt build 看看会发生什么。

yo webapp
bower install
grunt build

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

关于javascript - 运行 grunt build 命令时 Gruntfile 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33769037/

相关文章:

node.js - 无法在 OSX 10.6.8 上安装 Grunt

javascript - 咕噜连接 : Use file instructions a la Codekit

javascript - Grunt Watch 任务不必要地重复步骤

javascript - Yeoman + Grunticon : Gruntfile. js 问题

node.js - 如何在 Yeoman Ember 应用程序中设置/获取环境特定变量

JavaScript/jQuery : How to Chain a Method that uses console. 日志()

javascript - 创建纯 css javascript 库/包的最佳实践是什么

javascript - 如何将点击从不可见的 div 传播到嵌入式 flash?

javascript - Yeoman 更改图片文件名,而不是在 HTML 中更新

javascript - AngularJS 使用 ng-class 切换多个元素的类