How to generate a pure JavaScript file with Jade

别等时光非礼了梦想. 提交于 2019-12-22 08:10:08

问题


I know that Jade is for producing HTML instead of JavaScript, but for a project I'm working, it would be great, and a huge time saver, if I could do this without adding a pipe in every line:

|   (function(){
|       //Some JavaScript Code
|       var foo="#{bar}";
|       console.log("This a JavaScript file generated with Jade");
|   })()

The idea is to use the output of this template as the source of a regular JavaScript include like this:

<script type="application/javascript" src="foo.generated.js"></script>

So doing something like this:

script(type="application/javascript").
    (function(){
        //Some JavaScript Code
        var foo="#{bar}";
        console.log("This a JavaScript file generated with Jade");
    })()

won't solve my issue, because I need to output pure JavaScript with no DOM container element.

Is there another way to do this without adding pipes to every line? Or I have to assume that Jade was designed to produce only HTML, give up, and find other solution without Jade?

Thanks in advance!


回答1:


Produce all XML, include HTML. And jade was not designed to cast large blocks of javascript. The best thing you can do is create a method for obtaining these large blocks, and modifying variables. As it is doing in Example.




回答2:


I think you should use:

res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Content-Type', 'text/javascript; charset=utf-8');
res.end(`(function(){
    //Some JavaScript Code
    var foo="${bar}";
    console.log("This a JavaScript file generated with Jade");
})()`);


来源:https://stackoverflow.com/questions/19008500/how-to-generate-a-pure-javascript-file-with-jade

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!