问题
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