gruntjs / grunt

Grunt: The JavaScript Task Runner
http://gruntjs.com/
Other
12.27k stars 1.5k forks source link

Is there a way to keep Template Strings From Being Processed? #1462

Open Izzmo opened 8 years ago

Izzmo commented 8 years ago

Simple question: is it possible to keep template strings from getting processed?

I shouldn't have to do this:

replacements: [{
   from: 'something',
   to: '<script src="<ConfigurationManager.AppSettings("something")>"></script>'
},{
   from: 'ConfigurationManager.AppSettings("something")',
   to: '%=ConfigurationManager.AppSettings("something")%'
}]
nycdotnet commented 8 years ago

The documentation for lodash indicates that \\ can be used to escape the open and close angle brackets.

https://lodash.com/docs#template

so for example:

replacements: [{
   from: 'something',
   to: '<script src="\\<%=ConfigurationManager.AppSettings("something")%\\>"></script>'
}]
Izzmo commented 8 years ago

@nycdotnet the problem is that those output like:

<script src="\<%=ConfigurationManager.AppSettings("something")%\>"></script>

instead of

<script src="<%=ConfigurationManager.AppSettings("something")%>"></script>

Not sure why it's keeping the extra slash in there.

ArmorDarks commented 8 years ago

It's most likely upstream issue. Did you try to test it with native lodash _.template(...)? Does it produce those slashes too?

Izzmo commented 8 years ago

@ArmorDarks Sort of.

This adds a slash (making it a single backslash outputs fine though..):

let x = _.template('test <%= test %>!');
x({ test: '<script src="\\<%=ConfigurationManager.AppSettings("something")%\\>"></script>' });

// output: "test <script src="\<%=ConfigurationManager.AppSettings("something")%\>"></script>!"
ArmorDarks commented 8 years ago

Oh, now I see why it doesn't work.

Here is example from docs:

// Use backslashes to treat delimiters as plain text.
var compiled = _.template('<%= "\\<%- value %\\>" %>');
compiled({ 'value': 'ignored' });
// ➜ '<%- value %>'

Please, note that you should wrap escaped template into template tags too to make it work.

_.template('\\<%- value %\\>')()
\\ returns `\<%- "value" %\>`

_.template('<%= "\\<%- value %\\>" %>')()
\\ returns `<%- value %>`

Though, what is really strange, while '<%= "\\<%- value %\\>" %>' works in lodash templates, Grunt for some reasons completely ignores escaping and trying to process whole template, which results in Warning: An error occurred while processing a template (value is not defined).

So, in the end of all, it's Grunt's issue.

Izzmo commented 8 years ago

@ArmorDarks thanks for writing that out, that helps. So, now, where in the Grunt code should this be fixed, do you think?