简体   繁体   中英

grunt htmlmin: don't specify filename in the gruntfile.js

my task is the following:

    htmlmin : {
        dist : {
            options : {
                removeComments : true,
                collapseWhitespace : true
            },
            files : {
                'index.html' : 'index-src.html'
            }
        }
    },

this works fine when i have just one html file on my site, so this processes index-src.html into minified index.html .

what if i have 100 other html files to process? i don't want to manually list them in my gruntfile.

how can i abstract the file name and tell grunt to minify my src file to the corresponding production file? in my case they are:

source file is [name]-src.html production file is [name].html

i'm guessing it's just a matter of syntax, but i don't know what to write. thanks! :)

See the Globbing Patterns section of the Grunt Documentation.

I believe you'll just have to change your param files object to:

'index.html' : '*-src.html'

Update

Re-reading your question, I realized you needed a 1-1 file conversion for dynamic source and destination file names.

For that see Building the files object dynamically

I have yet to use this in my project but the syntax looks straight forward. You may need to change your src vs production naming convention to a folder based convention.

  • /source/name.html (source folder)
  • /build/name.html (destination folder)

Example

files: [
    {
      expand: true,     // Enable dynamic expansion.
      cwd: 'source/',      // Src matches are relative to this path.
      src: ['*-src.html'], // Actual pattern(s) to match.
      dest: 'build/',   // Destination path prefix.
      ext: '.html',   // Dest filepaths will have this extension.
      extDot: 'first'   // Extensions in filenames begin after the first dot
    }
]
module.exports = function (grunt) {

    // 1. All configuration goes here
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        concat: {
            controlCss: {
                src: ['UI.controls/assets/css/*.css'],
                dest: 'UI.controls/assets/css/min/production.css'
            },

            controlJs: {
                src: ['UI.controls/assets/js/*.js'],
                dest: 'UI.controls/assets/js/min/production.js'
            },

            coreJs: {
                src: ['UI.core/assets/js/*.js'],
                dest: 'UI.core/assets/js/min/production.js'
            },
            dist: {
                src: ['UI.controls/assets/templates/*.htm'],
                dest: 'UI.controls/assets/templates/min/production.min.htm'
            }
        },

        cssmin: {
            controlCss: {
                src: 'UI.controls/assets/css/min/production.css',
                dest: 'UI.controls/assets/css/min/production.min.css'
            }
        },

        uglify: {
            controlJs: {
                src: 'UI.controls/assets/js/min/production.js',
                dest: 'UI.controls/assets/js/min/production.min.js'
            },

            coreJs: {
                src: 'UI.core/assets/js/min/production.js',
                dest: 'UI.core/assets/js/min/production.min.js'
            }
        },

        htmlmin: {
            dist: {
                options: {
                    removeComments: true,
                    collapseWhitespace: true
                },
                expand: true,
                cwd: 'build',
                src: ['UI.controls/assets/templates/*.htm'],
                dest: 'UI.controls/assets/templates/min/production.min.htm'
            }
        }

  });

    // 2. Where we tell Grunt we plan to use this plug-in.
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-htmlmin');

    // 3. Where we tell Grunt what to do when we type "grunt" into the terminal.
    grunt.registerTask('default', ['concat', 'cssmin', 'uglify', 'htmlmin']);

};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM