简体   繁体   中英

How to find out the current building task in a grunt .tmpl file

I assemble a single index.html file from an index.tmpl file with grunt.template.process().

Now, I basically have two different builds: A development build (unminified), and a release build (minified). Based on the task that I pass to grunt via grunt devel or grunt release , I want to change what script to inline within the index.tmpl. Right now, I do it manually, and uncomment the corresponding line depending on my needs.

index.tmpl:

<%
var ownJsFile = grunt.config('uglify').dist.dest;
//var ownJsFile = grunt.config('concat').dev.dest;
...
%>

But I would rather have something like:

if (TASK == 'release')
    var ownJsFile = grunt.config('uglify').dist.dest;
else
    var ownJsFile = grunt.config('concat').dev.dest;

But how to get the TASK inside the template?

You can pass arbitrary data to grunt.template.process(template, { data: ... } ) . For example, you can inject the task name using grunt.task.current.name. Here is an example:

https://gist.github.com/sheenobu/5722360

It doesn't matter if the task is registered twice under release-task and devel-task, as long as we don't repeat ourselves. That said, binding template behavior to task name is not the best. You could, instead, pass in minify=true or uglify=true to grunt.template.process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM