首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Gruntfile在运行grunt build命令时不工作

Gruntfile在运行grunt build命令时不工作
EN

Stack Overflow用户
提问于 2015-11-18 07:27:13
回答 1查看 4.5K关注 0票数 16

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

我正在尝试运行"grunt build“命令,该命令将获取app文件夹(以及它的子文件夹和子文件夹等)中的所有内容,并创建、编译、连接和缩小文件到父目录中的dist文件夹(与app处于同一级别),我相信任何使用过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正在观看。

我想要的很简单:

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

<代码>F210

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

这可能会让你的头脑变得复杂,但希望你知道我在和yeoman和going一起工作后的期望,并能帮助我完成任务和移动。

下面是我的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通过,但实际上并没有像我解释的那样做预期的事情。

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

干杯,

-- SD

EN

回答 1

Stack Overflow用户

发布于 2016-01-13 11:21:02

你从来没有说过它是否是一个干净的网络应用程序对你有效。由于您已经尝试重新安装yeoman等,并更新了npm模块,因此我将尝试生成一个新的webapp项目。在不接触它的情况下,运行grunt build,看看会发生什么。

yo webapp
bower install
grunt build

如果它不能工作,请发布你得到的错误。如果工作正常,请将当前的应用程序文件/文件夹复制到新项目中,然后重试。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33769037

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档