问题描述
我知道Jade用于生成HTML而不是JavaScript,但是对于我正在工作的项目,如果我可以在不添加每行的管道的情况下执行此操作,那将会很棒,节省大量时间:
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");
| })()
想法是使用此模板的输出作为常规JavaScript的来源包括:
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>
所以做这样的事情:
script(type="application/javascript").
(function(){
//Some JavaScript Code
var foo="#{bar}";
console.log("This a JavaScript file generated with Jade");
})()
无法解决我的问题,因为我需要输出没有DOM容器元素的纯JavaScript。
won't solve my issue, because I need to output pure JavaScript with no DOM container element.
还有另一种方法可以在不向每一行添加管道的情况下执行此操作吗?或者我必须假设Jade只是为了制作HTML而放弃,并在没有Jade的情况下找到其他解决方案?
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?
提前致谢!
推荐答案
我认为你应该使用:
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");
})()`);
这篇关于如何使用Jade生成纯JavaScript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!