Skip to content

Commit f237abb

Browse files
authored
Merge pull request #58 from mkurz/fallback
Handle multiple extracted versions of same webjar
2 parents 24f13f3 + 8d34c66 commit f237abb

File tree

10 files changed

+75
-16
lines changed

10 files changed

+75
-16
lines changed

build.sbt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,9 @@ developers += Developer(
1414
)
1515

1616
libraryDependencies ++= Seq(
17-
"org.webjars.npm" % "jshint" % "2.13.6",
18-
19-
// Multiple strip-json-comments dependencies will be downloaded by webjars extractor.
20-
// Therefore we need to add the version we want to use to the require(...) statement.
21-
// E.g. when running the scripted test by hand you will find the webjars in:
22-
// src/sbt-test/sbt-jshint-plugin/test$ ls -a1 ./project/target/node-modules/webjars/strip-json-comments/
23-
// 1.0.2-1
24-
// 1.0.4
25-
"org.webjars" % "strip-json-comments" % "1.0.2-1" // sync with "var stripJsonComments = require(...)" in src/main/resources/jshint-shell.js
17+
"org.webjars.npm" % "node-require-fallback" % "1.0.0",
18+
"org.webjars.npm" % "jshint" % "2.13.6", // sync with src/main/resources/jshint-shell.js
19+
"org.webjars" % "strip-json-comments" % "1.0.2-1", // sync with src/main/resources/jshint-shell.js
2620
)
2721

2822
addSbtJsEngine("1.3.5")

src/main/resources/jshint-shell.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
"use strict";
2020

2121
var args = process.argv;
22+
var requireIfExists = require('node-require-fallback');
2223
var console = require("console");
2324
var fs = require("fs");
24-
var jshint = require("jshint");
25-
var stripJsonComments = require("strip-json-comments/1.0.2-1"); // version of sub-folder defined in build.sbt's libraryDependencies
25+
var jshint = requireIfExists("jshint/2.13.6", "jshint"); // sync with build.sbt
26+
var stripJsonComments = requireIfExists("strip-json-comments/1.0.2-1", "strip-json-comments"); // sync with build.sbt
2627

2728
var SOURCE_FILE_MAPPINGS_ARG = 2;
2829
var OPTIONS_ARG = 4;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
lazy val root = (project in file(".")).enablePlugins(SbtWeb)
2+
3+
WebKeys.reporter := new TestBuild.TestReporter(target.value)
4+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import sbt._
2+
import java.util.function.Supplier
3+
4+
object TestBuild {
5+
6+
class TestLogger(target: File) extends xsbti.Logger {
7+
8+
def error(msg: Supplier[String]): Unit = {
9+
if (msg.get().contains("Missing semicolon.")) {
10+
IO.touch(target / "missing-semi-error")
11+
}
12+
}
13+
14+
def warn(msg: Supplier[String]): Unit = {}
15+
def info(msg: Supplier[String]): Unit = {}
16+
def debug(msg: Supplier[String]): Unit = {}
17+
def trace(t: Supplier[Throwable]): Unit = {}
18+
}
19+
20+
class TestReporter(target: File) extends sbt.internal.inc.LoggedReporter(-1, new TestLogger(target))
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
addSbtPlugin("com.github.sbt" % "sbt-jshint" % sys.props("project.version"))
2+
3+
// When the same webjar (= same name) from different groupIds (org.webjars[.npm|bower]?)
4+
// are are on the classpath, those webjars get extracted into subfolders which are named by the version of the webjar.
5+
// However, if there is just one type (npm, bower, classic) of a webjar on the classpath, NO subfolders gets created.
6+
// Now, because in a project we don't know which other webjars get pulled in from other dependencies, it can happen
7+
// that subfolders get created, or may not. However, require(...) can't know that and therefore has to look in both places.
8+
// To test that the lookup is correct, we force this scripted test to create subfolders by pulling in the same webjar
9+
// but from different groupIds (the other scripted test does not do that and therefore no subfolders get created there)
10+
// btw: dependency eviction within the same type of webjar still works correctly, so e.g. 0.2 wins over 0.1 within the same type of webjar
11+
// and no subfolder will be forced for that case but the newest version will be choosen. Like normal dependency resolution.
12+
libraryDependencies ++= Seq(
13+
// Pulling in the classic and the npm webjar to subfolders for this webjar will be created
14+
"org.webjars.npm" % "jshint" % "2.9.7",
15+
"org.webjars" % "jshint" % "2.9.1",
16+
17+
// Same here, we pull in the bower and the npm one so subfolders will be created
18+
// ("1.9.2/ and 2.5.1 etc.)
19+
"org.webjars" % "strip-json-comments" % "1.0.2",
20+
"org.webjars.npm" % "strip-json-comments" % "0.1.1",
21+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function a() {
2+
return 1
3+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Lint an valid js file and see that linting errors are reported.
2+
-> assets
3+
$ exists target/missing-semi-error
4+
5+
> set JshintKeys.config := Some(file("test.jshintrc"))
6+
> assets
7+
8+
$ exists project/target/node-modules/webjars/jshint/2.13.6/bin/jshint
9+
$ exists project/target/node-modules/webjars/jshint/2.9.1/jshint.js
10+
11+
$ exists project/target/node-modules/webjars/strip-json-comments/1.0.2-1/strip-json-comments.js
12+
$ exists project/target/node-modules/webjars/strip-json-comments/1.0.4/strip-json-comments.js
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"asi" : true
3+
}
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
resolvers ++= Seq(
2-
Resolver.mavenLocal,
3-
Resolver.sonatypeRepo("snapshots"),
4-
)
5-
61
addSbtPlugin("com.github.sbt" % "sbt-jshint" % sys.props("project.version"))

src/sbt-test/sbt-jshint-plugin/test/test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@ $ exists target/missing-semi-error
44

55
> set JshintKeys.config := Some(file("test.jshintrc"))
66
> assets
7+
8+
$ exists project/target/node-modules/webjars/jshint/bin/jshint
9+
10+
$ exists project/target/node-modules/webjars/strip-json-comments/1.0.2-1/strip-json-comments.js
11+
$ exists project/target/node-modules/webjars/strip-json-comments/1.0.4/strip-json-comments.js

0 commit comments

Comments
 (0)