Search code examples
javascriptspringrequirejswebjars

How to resolve multiple RequireJS config files from Webjars


In a spring-boot application i obtained several webjars (basically javascript,css and resource files in jar packages), all which are specified in a maven pom file.

pom.xml

<dependencies>
...
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>requirejs</artifactId>
        <version>2.1.16</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>angularjs</artifactId>
        <version>${angularjs.version}</version>
    </dependency>
...
</dependencies>

Spring-boot helps by exposing each script path. For example bootstrap is available using

webjars/bootstrap/bootstrap-version/bootstrap.js

Ok this is pretty cool i can now obtain each script in my html and js files.

But here's the thing, each of those webjars has it's own webjars-requirejs.js file with configs for module exposure and path and dependencies resolution.

I believe it's purpose is to be used, and not copied in some users custom require.config

webjars/bootstrap/version/webjars-requirejs.js

/*global requirejs */

// Ensure any request for this webjar brings in jQuery.
requirejs.config({
paths: { 
    "bootstrap": webjars.path("bootstrap", "js/bootstrap"),
    "bootstrap-css": webjars.path("bootstrap", "css/bootstrap")  
  },
  shim: { "bootstrap": [ "jquery" ] }
});

Question

Noticing the webjars.path() method i can't figure it's scope and how it will get resolved if i obtain several webjars-requirejs.js files with their require.config() from my own custom require.config().
How can i manipulate those webjars-requirejs.js files in one custom javascript file?


Solution

  • The webjars-requirejs.js files are legacy artifacts. The newer RequireJS metadata is in the WebJar's pom.xml file. The webjars-locator library has some RequireJS utilities that make it easy to work with this metadata. The WebJar Docs include some details on how to use this.