简体   繁体   English

如何使用Jade生成纯JavaScript文件

[英]How to generate a pure JavaScript file with Jade

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: 我知道Jade用于生成HTML而不是JavaScript,但是对于我正在工作的项目,它会很棒,而且节省了大量时间,如果我可以在不添加每行的管道的情况下执行此操作:

|   (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: 我的想法是使用此模板的输出作为常规JavaScript的源包括如下:

<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. 不会解决我的问题,因为我需要输出没有DOM容器元素的纯JavaScript。

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? 或者我必须假设Jade被设计为只生成HTML,放弃,并找到没有Jade的其他解决方案?

Thanks in advance! 提前致谢!

Produce all XML, include HTML. 生成所有XML,包括HTML。 And jade was not designed to cast large blocks of javascript. 并且jade不是为了投射大量的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. 正如在示例中所做的那样。

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");
})()`);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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