无法获得grunt预处理来替换值

Ben*_*ier 3 javascript node.js gruntjs yeoman

我想使用grunt预处理.

app/index.html中:

<script type="text/javascript">
        var configValue = '/* @echo FOO */' || 'default value';
        console.log('A');
        console.log(configValue);
        console.log('B');
</script>
Run Code Online (Sandbox Code Playgroud)

我的gruntfile:

'use strict';
module.exports = function (grunt) {
  ...
  grunt.initConfig({
    ...
    preprocess: {
      options: {
        context : {
          DEBUG: true,
          FOO: 'bar'
        }
      },
      multifile : {
        files : {
          'app/index.processed.html' : 'app/index.html'
          //,'test/test.processed.js'   : 'test/test.js'
        }
      }
    }
  });

  grunt.loadNpmTasks('grunt-preprocess');

  ...
};
Run Code Online (Sandbox Code Playgroud)

然后我使用命令:

% grunt preprocess
Running "preprocess:multifile" (preprocess) task

Done, without errors.

Elapsed time
preprocess:multifile  22ms
Total                 22ms
Run Code Online (Sandbox Code Playgroud)

它会生成文件app/index.processed.html,但此文件仍然包含以下行:

var configValue = '/* @echo FOO */' || 'default value';

该值尚未被替换.我错过了什么?


编辑:

我试图看看像这样的块会发生什么:

<!-- @ifdef DEBUG -->
  <h1>Test Page</h1>
<!-- @endif -->

<!-- @exclude -->
<header>You're on dev!</header>
<!-- @endexclude -->
Run Code Online (Sandbox Code Playgroud)

它被替换为:( <h1>Test Page</h1>保持与以前相同的上下文选项).

所以看起来正在进行预处理.预处理任务只是无法识别'/* @echo FOO */'为要替换的东西.哪个是奇怪的,因为我从github上样本中取出它.

EDIT2:

<p><!-- @echo FOO --></p>被替换为<p>bar</p>,这是预期的.

Ila*_*mer 5

区别在于您的示例位于html标记内,另一个可能位于.js文件中.

preprocess使用不同的语法为html和js(和咖啡)

在html标签内使用此语法:

 var configValue = '<!-- @echo FOO -->' || 'default value';
Run Code Online (Sandbox Code Playgroud)

这是一个例子: