How to use different JS output for different pages?

Let's say I have two pages that contain a lot of code, a lot of libraries, etc., but have some differences. For a specific example, I turn on jQuery and on each page there is another function in the "finished document" (aka $(function() { ... }) ).

With JS, that would be easy. I would include jQuery on each page and on each page should have a different <script> snippet or include script-behind-page-A.js in pageA.html and script-behing-page-B.js in pageB.html .

How can I achieve the same result using ClojureScript?

I suspect that the compilation output is so great that it is best to have one big javascript ball emitted by the compiler. In this case, he clearly cannot have two different “finished documents”.

Is the proposed thread so that the code contains mainly functions that allow you to do something, initialize several state variables and initialize each page individually using simple JS as needed?

+4
source share
1 answer

I think the recommended approach would be what is explained in this ClojureScript tutorial from Mimmo Cosenza :

  • Create one large JS output file (so you optimize / gzip it when it lives)
  • Use different namespaces for functions, make sure you export at least one “entry point function” for each page
  • In each HTML file, call the desired entry point function, for example:

     <!-- on the bottom of welcome.html --> <script src="js/output.js"></script> <script>myapp.welcome.init();</script> <!-- on the bottom of login.html --> <script src="js/output.js"></script> <script>myapp.login.init();</script> 

This is described in detail in part 6 of the textbook .

+5
source

All Articles