Benjamin Crouzier
Benjamin Crouzier

Reputation: 41875

Cannot get grunt preprocess to replace value

I want to use grunt preprocess.

In app/index.html:

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

My 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');

  ...
};

Then I use the command:

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

Done, without errors.

Elapsed time
preprocess:multifile  22ms
Total                 22ms

It generates the file app/index.processed.html, but this file still has the line:

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

The value has not been replaced. What am I missing ?


Edit:

I have tried to see what happens to a block like this:

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

<!-- @exclude -->
<header>You're on dev!</header>
<!-- @endexclude -->

And it is replaced with: <h1>Test Page</h1> (keeping the same context options as before).

So it looks like the preprocessing is happening. The preprocess task is just not recognising '/* @echo FOO */' as something to be replaced. Which is wierd, because I took it from the sample on github.

Edit2:

<p><!-- @echo FOO --></p> is replaced with <p>bar</p>, which is expected.

Upvotes: 3

Views: 2957

Answers (1)

Ilan Frumer
Ilan Frumer

Reputation: 32357

The difference is that your example is inside a html tag and the other one is probably inside a .js file.

preprocess uses differnt syntax for html and js ( and coffee)

use this syntax inside html tags:

 var configValue = '<!-- @echo FOO -->' || 'default value';

here is an example:

Upvotes: 5

Related Questions