【问题标题】:How do I replace template variables on copy with grunt?如何用 grunt 替换副本上的模板变量?
【发布时间】:2014-04-30 16:29:53
【问题描述】:

我正在使用load-grunt-configgrunt-contrib-copy,我正在尝试让复制任务用“进程”选项替换一些模板标签。

我知道可以替换模板标签(来自 grunt-contrib-copy 文档),但我无法让它工作。我要做的是将“style.css”中的字符串<%= init.project.name %>替换为同名的模板变量(由用户使用grunt-prompt输入)。

在复制时,我希望 grunt 用它在内存中的值替换 style.css 文件中的模板变量。但是当我使用下面的代码时它不会这样做。有谁知道我做错了什么?

Grunt 复制任务

// -------------------------------------
// Grunt copy
// -------------------------------------

module.exports = {

  // ----- Copy files for initialization (selected with grunt prompt) ----- //

  init: {
    files: [{
      cwd: 'init/php/templates',
      src: '<%= init.php.templates %>',
      dest: 'src/php/templates',
      expand: true
    }, {
      cwd: 'init/php/includes',
      src: '<%= init.php.includes %>',
      dest: 'src/php/includes',
      expand: true
    }, {
      cwd: 'init/js',
      src: '<%= init.scripts %>',
      dest: 'src/js',
      expand: true
    }, {
      cwd: 'init/css',
      src: 'style.css',
      dest: 'src/css',
      expand: true,
      options: {
        process: function (content, srcpath) {
          return grunt.template.process(content);
        }
      }
    }]
  }
};

Css 文件(wordpress)

/*
Theme Name: <%= init.project.name %>
Theme URI: 
Description: Description
Version: 1.0
Author: Name
Author URI: uri
Tags: Tags
*/

我试过this 答案,但processContent 已被process 取代,而且这个答案似乎不再起作用(即使将processContent 更改为process)。

【问题讨论】:

    标签: javascript node.js gruntjs lodash


    【解决方案1】:

    你把选项放错了地方,把它移出来,它应该可以工作。

    module.exports = function (grunt) {
      return {
          init: {
            files: [{
              cwd: 'init/php/templates',
              src: '<%= init.php.templates %>',
              dest: 'src/php/templates',
              expand: true
            }, {
              cwd: 'init/php/includes',
              src: '<%= init.php.includes %>',
              dest: 'src/php/includes',
              expand: true
            }, {
              cwd: 'init/js',
              src: '<%= init.scripts %>',
              dest: 'src/js',
              expand: true
            }, {
              cwd: 'init/css',
              src: 'style.css',
              dest: 'src/css',
              expand: true
            }],
            options: {
              process: function (content, srcpath) {
                return grunt.template.process(content);
              }
            }
          }
      }
    };
    

    更新为适合load-grunt-config

    【讨论】:

    • 我已经尝试过了,但它给了我以下错误:“警告:处理...文件时出错”。其中 ... 文件是被复制的文件之一(但不是包含模板标签的文件)。开启此选项时复制的所有文件是否都需要包含模板标签?你知道我们能做些什么吗?
    • 将复制所有文件。该错误似乎是另一个问题。变量init.php.templates、includes和scripts是什么...?
    • 看起来确实如此,但是当我删除选项块时,一切都按预期工作。当我包含选项块时,复制任务遇到的第一个文件出现错误(这是一个不包含模板标签的文件,只有 style.css 文件包含)。变量来自another grunt task,按预期工作。
    • 可能grunt 未定义并导致错误。根据文件 load-grunt-config 写道,您可以通过这种方式传递 grunt 对象。 module.exports = function (grunt) { return { init: {...} } };
    • 嘿@Sam,您是否测试了关于不包含模板标签的文件的假设?查看它是否适用于包含的选项块,并且仅存在包含模板标签的文件。
    猜你喜欢
    • 1970-01-01
    • 2019-10-05
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    • 2019-09-05
    • 1970-01-01
    • 2023-04-04
    • 2018-02-06
    相关资源
    最近更新 更多