【问题标题】:Grunt: change file extension at buildGrunt:在构建时更改文件扩展名
【发布时间】:2015-04-02 05:04:38
【问题描述】:

我想编写一个 Grunt 任务,在构建过程中,它会复制我拥有的所有 .html 文件,并在 /dist 中创建它的 .asp 版本。

我一直在尝试使用grunt-contrib-copy 来完成此操作,这就是我所拥有的:

copy: {
  //some other tasks that work...

  //copy an .asp version of all .html files
  asp: {
    files: [{
      expand: true,
      dot: true,
      cwd: '<%= config.app %>',
      src: ['{,*/}*.html'],
      dest: '<%= config.dist %>',
      option: {
        process: function (content, srcpath) {
          return srcpath.replace(".asp");
        }
      }
    }]
  } //end asp task
},

我知道process 函数实际上并不正确...我尝试了一些不同的正则表达式以使其无济于事。当我运行asp 任务时,Grunt CLI 说我已经复制了 2 个文件,但找不到它们。任何帮助表示赞赏。

【问题讨论】:

    标签: gruntjs grunt-contrib-copy


    【解决方案1】:

    您可以使用rename 函数来做到这一点。

    例如:

    copy: {
      //some other tasks that work...
    
      //copy an .asp version of all .html files
      asp: {
        files: [{
          expand: true,
          dot: true,
          cwd: '<%= config.app %>',
          src: ['{,*/}*.html'],
          dest: '<%= config.dist %>',
          rename: function(dest, src) {
             return dest + src.replace(/\.html$/, ".asp");
          }
        }]
      } //end asp task
    },
    

    这应该可行。

    【讨论】:

    • 确实rename 方法有效,但通过仔细查看 Grunt 文档,我发现您还可以使用 ext 属性完成简单的文件扩展名更改,如下所示:ext: '.asp'
    • 请注意,您可能希望使用 path.sep() 加入 dest 和 src 参数,因为现在您加入的文件和文件夹没有斜线 somefoldersomefile.asp 而不是 somefolder/somefile.asp
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    相关资源
    最近更新 更多