javascriptaemclient-library

How can I call specific js file of a client library?


I have organized all my js files into a common clientlib category. For example

 ->etc
   ->designs
    ->myapp
     -> clientlibs
      -> base
        -> js
          -> app1.js
          -> app2.js
          -> app3.js

So when I access /etc/designs/myapp/clientlibs/base.js, it returns one big merged js with content from all included js. Is there a way to access a specific js(say app2.js) from this base.js. We can use relative path like /etc/designs/myapp/clientlibs/base/js/app2.js and it works.

But is there better way (maybe using selectors like /clientlibs/base.app2.js) to return this js? Or is this to only way to access the specific js?


Solution

  • Using the relative path as you did is the way to access a specific .js file. Client libs aren't designed to be accessed via selectors. Ideally you will want to always load JavaScript via a client lib because it improves performance to combine scripts into a single HTTP request (more HTTP requests take longer to load than just 1). It also compresses the scripts to make them smaller. So you can have you source scripts with full white space and comments and formatting in files like app1.js, but then load them via the base client lib which removes the white space and comments that you don't need in the script end users download.

    If you want only a part of the client lib, you could define a 2nd client lib that only includes the parts you want. You can have the same source file (such as app1.js) included in multiple different client libs. For your use case this sounds like the best approach.

    For debugging you can always add the ?debugClientLibs=true query string to see individual files again in the browser temporarily.