使用 grunt-file-append 将文本附加到多个文件



如何使用grunt-file-append将文本附加到多个文件

https://www.npmjs.com/package/grunt-file-append

grunt.initConfig({
  file_append: {
    default_options: {
      files: [
        {
          append: "text to append",
          prepend: "text to prepend",
          input: '/path/to/input/file'
          output: 'path/to/output/file'
        }
      ]
    }
  }
})

如果我以这种方式编写函数,则附加到多个文件时会出现错误。

grunt.initConfig({
  file_append: {
    default_options: {
      files: [
        {
          append: "text to append",
          prepend: "text to prepend",
          input: './path/to/input/*.html'
          output: 'path/to/output/*.html'
        }
      ]
    }
  }
})

我收到以下错误:

Running "file_append:default_option" (file_append) task
>> Source file "./path/to/output/*.html" not found.
Warning: Task "file_append:default_option" failed. Use --force to continue.
Aborted due to warnings.

仅附加到单个文件有效,但不适用于多个文件,我在这里做错了什么。

我认为它不应该起作用。正如你在github代码中看到的grunt-file-append:

prepend = file.prepend || ""
append  = file.append  || ""
fileContent = grunt.file.read filepath
value = "#{ prepend }#{ fileContent }#{ append }"
grunt.file.write filepath, value

它只读取一个文件并在其上附加/预置。

你试过咕噜咕��

正如

@jmartins提到的,代码只是没有设置为处理"something/*.html",我认为附加多个文件(除了修改源代码)的唯一方法是在数组中有多个对象:

file_append: {
        default_options: {
            files: [{
                prepend: 'something',
                input: '<%= config.dist %>/<%= config.distScripts %>/script1.js',
                output: '<%= config.dist %>/<%= config.distScripts %>/script1.js'
            }, {
                prepend: 'something',
                input: '<%= config.dist %>/<%= config.distScripts %>/script2.js',
                output: '<%= config.dist %>/<%= config.distScripts %>/script2.js'
            }, {
                prepend: 'something',
                input: '<%= config.dist %>/<%= config.distScripts %>/script3.js',
                output: '<%= config.dist %>/<%= config.distScripts %>/script3.js'
            }]
        }
    }

但是,当您有很多文件要更新时,这并不好,因此,如果有很多文件,或者没有有限的列表,并且您真的不想不断更新 grunt 文件,那么简单地更新源代码以执行所需的操作可能会更容易。

这是我动态添加脚本标签和 id 的方式

使用咕噜声替换用指定的替换替换所有文本

最新更新