【问题标题】:How to stop Grunt.js from minifying my Yeoman webapp如何阻止 Grunt.js 缩小我的 Yeoman webapp
【发布时间】:2014-02-27 18:32:10
【问题描述】:

我正在尝试使用 generator-webapp(sass w/compass + boostrap-sass) 构建我的 webapp。但是,我不希望它缩小任何文件。 (至少只是css,但理想情况下什么都不会被缩小)。在我的工作中,我将做一些在 .NET/C# 以及但主要是前端完成的后端,因此我需要不缩小 HTML 以便我可以将前端导入 Visual Studio 并成为能够操纵它。

目前,我正在尝试配置我的 gruntfile 以停止缩小,但我很难找到解决方案。

这是我的 gruntfile(它只是生成的默认文件)

// Generated on 2014-02-27 using generator-webapp 0.4.7
'use strict';

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

module.exports = function (grunt) {

// Load grunt tasks automatically
require('load-grunt-tasks')(grunt);

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

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

    // Project settings
    yeoman: {
        // Configurable paths
        app: 'app',
        dist: 'dist'
    },

    // Watches files for changes and runs tasks based on the changed files
    watch: {
        js: {
            files: ['<%= yeoman.app %>/scripts/{,*/}*.js'],
            tasks: ['jshint'],
            options: {
                livereload: true
            }
        },
        jstest: {
            files: ['test/spec/{,*/}*.js'],
            tasks: ['test:watch']
        },
        gruntfile: {
            files: ['Gruntfile.js']
        },
        compass: {
            files: ['<%= yeoman.app %>/styles/{,*/}*.{scss,sass}'],
            tasks: ['compass:server', 'autoprefixer']
        },
        styles: {
            files: ['<%= yeoman.app %>/styles/{,*/}*.css'],
            tasks: ['newer:copy:styles', 'autoprefixer']
        },
        livereload: {
            options: {
                livereload: '<%= connect.options.livereload %>'
            },
            files: [
                '<%= yeoman.app %>/{,*/}*.html',
                '.tmp/styles/{,*/}*.css',
                '<%= yeoman.app %>/images/{,*/}*.{gif,jpeg,jpg,png,svg,webp}'
            ]
        }
    },

    // The actual grunt server settings
    connect: {
        options: {
            port: 9000,
            livereload: 35729,
            // Change this to '0.0.0.0' to access the server from outside
            hostname: 'localhost'
        },
        livereload: {
            options: {
                open: true,
                base: [
                    '.tmp',
                    '<%= yeoman.app %>'
                ]
            }
        },
        test: {
            options: {
                port: 9001,
                base: [
                    '.tmp',
                    'test',
                    '<%= yeoman.app %>'
                ]
            }
        },
        dist: {
            options: {
                open: true,
                base: '<%= yeoman.dist %>',
                livereload: false
            }
        }
    },

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

    // Make sure code styles are up to par and there are no obvious mistakes
    jshint: {
        options: {
            jshintrc: '.jshintrc',
            reporter: require('jshint-stylish')
        },
        all: [
            'Gruntfile.js',
            '<%= yeoman.app %>/scripts/{,*/}*.js',
            '!<%= yeoman.app %>/scripts/vendor/*',
            'test/spec/{,*/}*.js'
        ]
    },


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




    // Compiles Sass to CSS and generates necessary files if requested
    compass: {
        options: {
            sassDir: '<%= yeoman.app %>/styles',
            cssDir: '.tmp/styles',
            generatedImagesDir: '.tmp/images/generated',
            imagesDir: '<%= yeoman.app %>/images',
            javascriptsDir: '<%= yeoman.app %>/scripts',
            fontsDir: '<%= yeoman.app %>/styles/fonts',
            importPath: '<%= yeoman.app %>/bower_components',
            httpImagesPath: '/images',
            httpGeneratedImagesPath: '/images/generated',
            httpFontsPath: '/styles/fonts',
            relativeAssets: false,
            assetCacheBuster: false
        },
        dist: {
            options: {
                generatedImagesDir: '<%= yeoman.dist %>/images/generated'
            }
        },
        server: {
            options: {
                debugInfo: true
            }
        }
    },

    // Add vendor prefixed styles
    autoprefixer: {
        options: {
            browsers: ['last 1 version']
        },
        dist: {
            files: [{
                expand: true,
                cwd: '.tmp/styles/',
                src: '{,*/}*.css',
                dest: '.tmp/styles/'
            }]
        }
    },

    // Automatically inject Bower components into the HTML file
    'bower-install': {
        app: {
            html: '<%= yeoman.app %>/index.html',
            ignorePath: '<%= yeoman.app %>/'
        }
    },

    // Renames files for browser caching purposes
    rev: {
        dist: {
            files: {
                src: [
                    '<%= yeoman.dist %>/scripts/{,*/}*.js',
                    '<%= yeoman.dist %>/styles/{,*/}*.css',
                    '<%= yeoman.dist %>/images/{,*/}*.{gif,jpeg,jpg,png,webp}',
                    '<%= yeoman.dist %>/styles/fonts/{,*/}*.*'
                ]
            }
        }
    },

    // 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: '<%= yeoman.dist %>'
        },
        html: '<%= yeoman.app %>/index.html'
    },

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

    // The following *-min tasks produce minified files in the dist folder
    imagemin: {
        dist: {
            files: [{
                expand: true,
                cwd: '<%= yeoman.app %>/images',
                src: '{,*/}*.{gif,jpeg,jpg,png}',
                dest: '<%= yeoman.dist %>/images'
            }]
        }
    },
    svgmin: {
        dist: {
            files: [{
                expand: true,
                cwd: '<%= yeoman.app %>/images',
                src: '{,*/}*.svg',
                dest: '<%= yeoman.dist %>/images'
            }]
        }
    },
    htmlmin: {
        dist: {
            options: {
                collapseBooleanAttributes: true,
                collapseWhitespace: true,
                removeAttributeQuotes: true,
                removeCommentsFromCDATA: true,
                removeEmptyAttributes: true,
                removeOptionalTags: true,
                removeRedundantAttributes: true,
                useShortDoctype: true
            },
            files: [{
                expand: true,
                cwd: '<%= yeoman.dist %>',
                src: '{,*/}*.html',
                dest: '<%= yeoman.dist %>'
            }]
        }
    },

    // By default, your `index.html`'s <!-- Usemin block --> will take care of
    // minification. These next options are pre-configured if you do not wish
    // to use the Usemin blocks.
    // cssmin: {
    //     dist: {
    //         files: {
    //             '<%= yeoman.dist %>/styles/main.css': [
    //                 '.tmp/styles/{,*/}*.css',
    //                 '<%= yeoman.app %>/styles/{,*/}*.css'
    //             ]
    //         }
    //     }
    // },
    // uglify: {
    //     dist: {
    //         files: {
    //             '<%= yeoman.dist %>/scripts/scripts.js': [
    //                 '<%= yeoman.dist %>/scripts/scripts.js'
    //             ]
    //         }
    //     }
    // },
    // concat: {
    //     dist: {}
    // },

    // Copies remaining files to places other tasks can use
    copy: {
        dist: {
            files: [{
                expand: true,
                dot: true,
                cwd: '<%= yeoman.app %>',
                dest: '<%= yeoman.dist %>',
                src: [
                    '*.{ico,png,txt}',
                    '.htaccess',
                    'images/{,*/}*.webp',
                    '{,*/}*.html',
                    'styles/fonts/{,*/}*.*',
                    'bower_components/' + (this.includeCompass ? 'sass-' : '') + 'bootstrap/' + (this.includeCompass ? 'fonts/' : 'dist/fonts/') +'*.*'
                ]
            }]
        },
        styles: {
            expand: true,
            dot: true,
            cwd: '<%= yeoman.app %>/styles',
            dest: '.tmp/styles/',
            src: '{,*/}*.css'
        }
    },


    // Generates a custom Modernizr build that includes only the tests you
    // reference in your app
    modernizr: {
        devFile: '<%= yeoman.app %>/bower_components/modernizr/modernizr.js',
        outputFile: '<%= yeoman.dist %>/bower_components/modernizr/modernizr.js',
        files: [
            '<%= yeoman.dist %>/scripts/{,*/}*.js',
            '<%= yeoman.dist %>/styles/{,*/}*.css',
            '!<%= yeoman.dist %>/scripts/vendor/*'
        ],
        uglify: true
    },

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


grunt.registerTask('serve', function (target) {
    if (target === 'dist') {
        return grunt.task.run(['build', 'connect:dist:keepalive']);
    }

    grunt.task.run([
        'clean:server',
        'concurrent:server',
        'autoprefixer',
        'connect:livereload',
        'watch'
    ]);
});

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

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

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

grunt.registerTask('build', [
    'clean:dist',
    'useminPrepare',
    'concurrent:dist',
    'autoprefixer',
    'concat',
    'cssmin',
    'uglify',
    'copy:dist',
    'modernizr',
    'rev',
    'usemin',
    'htmlmin'
]);

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

感谢您的帮助!

【问题讨论】:

标签: node.js gruntjs minify yeoman


【解决方案1】:

我同样希望修改 Yeoman webapp 构建,以便结果(javascript、css 和 index.html)不会被缩小。我的方法是尝试只修改 Gruntfile。我想避免直接手动对 Yeoman 提供的 index.html 结构进行任何更改。而且我想避免对生成的构建文件进行任何构建后的手动更改(太乏味)。只是想构建和去 - 没有缩小。

这是我想出的,以防它可能对其他人有所帮助。我将其缩小到对 Gruntfile 进行两次更改,如下所示。

首先,我修改了 UseminPrepare 配置以连接但不 uglify js 文件或缩小 css 文件:

在 UseminPrepare 中,我改变了这个:

js: ['concat', 'uglifyjs'],
css: ['cssmin']

到这里:

js: ['concat'],
css: ['cssmin']

其次,我通过删除“uglify”和“htmlmin”任务修改了构建任务系列,但有些矛盾的是,您必须将“UseminPrepare”和“usemin”任务留在系列中。通过这样做,您可以利用 usemin 进行连接和复制,但避免将其缩小。

通过注释掉 'uglify' 和 'htmlmin' 来更改构建任务(不要忘记确保 usemin 任务,现在是本系列的最后一个任务,不以逗号结尾 :)):

grunt.registerTask('build', [
 'clean:dist',
 'bowerInstall',
 'useminPrepare',
 'concurrent:dist',
 'autoprefixer',
 'concat',
 'ngmin',
 'copy:dist',
 'cdnify',
 'cssmin',
 //'uglify',
 'rev',
 'usemin'
 //'htmlmin'
]);

现在只需运行grunt build 即可完成。为我工作。

我希望这可能对其他人有所帮助。

【讨论】:

  • +1 表示方向,但对我来说,它并没有像描述的那样工作。为了使 grunt 构建正常工作,我还必须注释掉“cssmin”
  • 非常感谢。此外,如果这个建议的解决方案对某人不起作用,他或她应该尝试将 Gruntfile.js 恢复为原始文件,然后再次执行这些步骤。
  • 如果我按照描述进行构建,我会看到“没有找到“cssmin”目标。”。如果我按照@TomasPanik 的建议删除cssmin,我会看到“...views/main.html 404(未找到)”。不适合我。
  • @adamdport - 如果您像这样编写 preparemin 任务,则不必从构建任务中注释掉 cssmin - useminPrepare: { html: ['/index.html '],选项:{ dest:'',流程:{ html:{ 步骤:{ js:['concat'],css:['cssmin']},发布:{}} } } },
【解决方案2】:

如果您删除 htmlmin 任务,它应该会停止缩小 HTML。如果您想停止缩小其他内容,也可以删除其他 *min 任务。您需要从底部的注册任务中删除它们。但是,如果您不打算使用它们,也可以删除随附的块。

【讨论】:

  • 我确实尝试过,但是我的 CSS 没有编译/移动到我的 dist 文件夹。不过我会再试一次。
  • 如果是这种情况,请尝试不要注释掉 useminPrepare 和 usemin。看起来他们实际上并没有进行缩小,而只是为缩小做好准备。看起来 cssmin 本身并没有移动任何文件。
  • 只注释掉 'cssmin' 和 'htmlmin',除了我的 css 仍然不能复制到 dist 之外,一切正常。
  • 我认为您可能需要保留 cssmin。看起来你是如何设置的,你将所有的 CSS 编译到一个文件中。要改变这一点,您需要更新您的 HTML 以便它单独使用 CSS 文件,然后将文件添加到您的复制块以将所有 CSS 文件复制到您的 dist 目录。根据您希望 CSS 分离的程度,这可能不值得。
  • 所以我保留 cssmin,这会导致我的 scss 被编译并复制到我的 dist 文件夹。 “要更改它,您需要更新您的 HTML 以便它单独使用 CSS 文件,然后将文件添加到您的复制块以将所有 CSS 文件复制到您的 dist 目录”是什么意思?跨度>
【解决方案3】:

如果您将输出样式设置为压缩,则必须像这样删除它

dist:
  options:
    **outputStyle: 'compressed'**
    debugInfo: false
    noLineComments: true

dist:
  options:
    **#outputStyle: 'compressed'** #remove it
    debugInfo: false
    noLineComments: true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多