Javascript grunt htmlmin:don';t在gruntfile.js中指定文件名

Javascript grunt htmlmin:don';t在gruntfile.js中指定文件名,javascript,gruntjs,grunt-contrib-htmlmin,Javascript,Gruntjs,Grunt Contrib Htmlmin,我的任务如下: htmlmin : { dist : { options : { removeComments : true, collapseWhitespace : true }, files : { 'index.html' : 'index-src.html' }

我的任务如下:

    htmlmin : {
        dist : {
            options : {
                removeComments : true,
                collapseWhitespace : true
            },
            files : {
                'index.html' : 'index-src.html'
            }
        }
    },
当我的站点上只有一个html文件时,这很好用,因此这会将
index src.html
处理为缩小的
index.html

如果我有100个其他
html
文件要处理怎么办?我不想在GrunFile中手动列出它们

如何提取文件名并告诉grunt将src文件缩小为相应的生产文件?就我而言,它们是:

源文件是
[name]-src.html
生产文件是
[name].html

我猜这只是一个语法问题,但我不知道该写什么。 谢谢!:)

请参阅Grunt文档的一节

我相信您只需将param files对象更改为:

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

更新

重新阅读您的问题,我意识到您需要对动态源文件名和目标文件名进行1-1文件转换

那你看

我还没有在我的项目中使用它,但是语法看起来很简单。您可能需要将src与产品命名约定更改为基于文件夹的约定

  • /source/name.html
    (源文件夹)
  • /build/name.html
    (目标文件夹)
范例

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

};