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

3
使用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.

追加到单个文件是有效的,但对于多个文件无效, 这里我做错了什么。

3个回答

3
正如@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文件。


3

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

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

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

您尝试过grunt-contrib-concat吗?


我已经使用了grunt contrib concat,但我是grunt的新手,不知道如何将这两个插件合并在一起。 - patz
有没有办法我可以使用Grunt Contrib Contact并使用它附加或前置标签。 - patz
是的,请查看 bannerfooter 选项:https://github.com/gruntjs/grunt-contrib-concat#banner - jmartins
1
如果grunt-contrib-concat不符合您的要求,请尝试在文件开头和结尾添加要替换的字符串,并使用grunt-string-replace进行替换。 - jmartins
所以横幅和页脚对我来说是有效的,我的问题是我的字符串是一个带有id字段的脚本标签“<script type ='text' id =''>”,我必须为我将连接的每个文件插入不同的id。 - patz
显示剩余2条评论

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接