简体   繁体   中英

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:

|   (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!

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.

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

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