Skip to content

Commit

Permalink
Change Webjars path aliasing to only use the artifactId
Browse files Browse the repository at this point in the history
  • Loading branch information
gitblit committed Jun 27, 2015
1 parent e6f818d commit 9b9f95e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
Expand Up @@ -57,15 +57,19 @@ public WebjarsResourceHandler(String urlPath) {

@Override
public URL getResourceUrl(String resourcePath) {
String resourceName;
String artifactPath = resourcePath.substring(0, resourcePath.indexOf('/', resourcePath.indexOf('/') + 1) + 1);
String resourceName = getResourceBasePath() + "/" + resourcePath;
String artifactPath = resourcePath.substring(0, resourcePath.indexOf('/') + 1);
if (pathAliases.containsKey(artifactPath)) {
String artifactVersion = pathAliases.get(artifactPath);
String aliasedPath = resourcePath.replace(artifactPath, artifactVersion);
log.trace("Replaced Webjar path {} with {}", resourcePath, aliasedPath);
resourceName = getResourceBasePath() + "/" + aliasedPath;
} else {
resourceName = getResourceBasePath() + "/" + resourcePath;

// Do not replace already fixed-version paths.
// i.e. skip replacing first path segment of "/jquery/1.11.1/jquery.min.js"
// BUT do replace first path segment of "jquery/jquery.min.js".
if (!resourcePath.startsWith(artifactVersion)) {
String aliasedPath = resourcePath.replace(artifactPath, artifactVersion);
log.trace("Replaced Webjar path {} with {}", resourcePath, aliasedPath);
resourceName = getResourceBasePath() + "/" + aliasedPath;
}
}

URL url = this.getClass().getClassLoader().getResource(resourceName);
Expand All @@ -77,9 +81,10 @@ public URL getResourceUrl(String resourcePath) {
}

/**
* Scans the classpath for Webjars metadata and creates an alias registry of "current" path to "fixed" path.
* Scans the classpath for Webjars metadata and creates an alias registry of path aliases to fixed version paths
* for all Webjars on the classpath.
* <p>
* This allows specifications of a path like "jquery/current/jquery.min.js" which will be substituted
* This allows specifications of a path like "jquery/jquery.min.js" which will be substituted
* at runtime as "jquery/1.11.1/jquery.min.js".</p>
*
* @return a map of path pathAliases
Expand All @@ -100,8 +105,8 @@ private Map<String, String> indexWebjars() {
String artifactId = pom.getProperty("artifactId");
String version = pom.getProperty("version");
String fixedPath = artifactId + '/' + version + '/';
String currentPath = artifactId + "/current/";
pathAliases.put(currentPath, fixedPath);
String aliasPath = artifactId + '/';
pathAliases.put(aliasPath, fixedPath);
}

for (Map.Entry<String, String> alias : pathAliases.entrySet()) {
Expand Down
Expand Up @@ -8,16 +8,16 @@

<title>${title}</title>

<link href="${webjarsAt('bootstrap/current/css/bootstrap.min.css')}" rel="stylesheet">
<link href="${webjarsAt('font-awesome/current/css/font-awesome.min.css')}" rel="stylesheet">
<link href="${webjarsAt('bootstrap/css/bootstrap.min.css')}" rel="stylesheet">
<link href="${webjarsAt('font-awesome/css/font-awesome.min.css')}" rel="stylesheet">
<link href="${publicAt('css/style.css')}" rel="stylesheet">
</head>
<body>
<div class="container">
<#nested/>

<script src="${webjarsAt('jquery/current/jquery.min.js')}"></script>
<script src="${webjarsAt('bootstrap/current/js/bootstrap.min.js')}"></script>
<script src="${webjarsAt('jquery/jquery.min.js')}"></script>
<script src="${webjarsAt('bootstrap/js/bootstrap.min.js')}"></script>
</div>
</body>
</html>
Expand Down
Expand Up @@ -8,18 +8,18 @@

<title>${title}</title>

<link href="${webjarsAt('bootstrap/current/css/bootstrap.min.css')}" rel="stylesheet">
<link href="${webjarsAt('font-awesome/current/css/font-awesome.min.css')}" rel="stylesheet">
<link href="${webjarsAt('bootstrap/css/bootstrap.min.css')}" rel="stylesheet">
<link href="${webjarsAt('font-awesome/css/font-awesome.min.css')}" rel="stylesheet">
<link href="${publicAt('css/style.css')}" rel="stylesheet">
<base href="${contextPath}/">
</head>
<body>
<div class="container">
<#nested/>

<script src="${webjarsAt('jquery/current/jquery.min.js')}"></script>
<script src="${webjarsAt('bootstrap/current/js/bootstrap.min.js')}"></script>
<script src="${webjarsAt('angularjs/current/angular.min.js')}"></script>
<script src="${webjarsAt('jquery/1.11.1/jquery.min.js')}"></script>
<script src="${webjarsAt('bootstrap/js/bootstrap.min.js')}"></script>
<script src="${webjarsAt('angularjs/angular.min.js')}"></script>
<script src="${publicAt('js/crudNgApp.js')}"></script>
</div>
</body>
Expand Down

0 comments on commit 9b9f95e

Please sign in to comment.