Skip to content

Commit

Permalink
Merge pull request #58 from mkurz/fallback
Browse files Browse the repository at this point in the history
Handle multiple extracted versions of same webjar
  • Loading branch information
mkurz committed Jan 29, 2024
2 parents 24f13f3 + 8d34c66 commit f237abb
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 16 deletions.
12 changes: 3 additions & 9 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,9 @@ developers += Developer(
)

libraryDependencies ++= Seq(
"org.webjars.npm" % "jshint" % "2.13.6",

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

addSbtJsEngine("1.3.5")
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/jshint-shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
"use strict";

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

var SOURCE_FILE_MAPPINGS_ARG = 2;
var OPTIONS_ARG = 4;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
lazy val root = (project in file(".")).enablePlugins(SbtWeb)

WebKeys.reporter := new TestBuild.TestReporter(target.value)

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import sbt._
import java.util.function.Supplier

object TestBuild {

class TestLogger(target: File) extends xsbti.Logger {

def error(msg: Supplier[String]): Unit = {
if (msg.get().contains("Missing semicolon.")) {
IO.touch(target / "missing-semi-error")
}
}

def warn(msg: Supplier[String]): Unit = {}
def info(msg: Supplier[String]): Unit = {}
def debug(msg: Supplier[String]): Unit = {}
def trace(t: Supplier[Throwable]): Unit = {}
}

class TestReporter(target: File) extends sbt.internal.inc.LoggedReporter(-1, new TestLogger(target))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
addSbtPlugin("com.github.sbt" % "sbt-jshint" % sys.props("project.version"))

// When the same webjar (= same name) from different groupIds (org.webjars[.npm|bower]?)
// are are on the classpath, those webjars get extracted into subfolders which are named by the version of the webjar.
// However, if there is just one type (npm, bower, classic) of a webjar on the classpath, NO subfolders gets created.
// Now, because in a project we don't know which other webjars get pulled in from other dependencies, it can happen
// that subfolders get created, or may not. However, require(...) can't know that and therefore has to look in both places.
// To test that the lookup is correct, we force this scripted test to create subfolders by pulling in the same webjar
// but from different groupIds (the other scripted test does not do that and therefore no subfolders get created there)
// 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
// and no subfolder will be forced for that case but the newest version will be choosen. Like normal dependency resolution.
libraryDependencies ++= Seq(
// Pulling in the classic and the npm webjar to subfolders for this webjar will be created
"org.webjars.npm" % "jshint" % "2.9.7",
"org.webjars" % "jshint" % "2.9.1",

// Same here, we pull in the bower and the npm one so subfolders will be created
// ("1.9.2/ and 2.5.1 etc.)
"org.webjars" % "strip-json-comments" % "1.0.2",
"org.webjars.npm" % "strip-json-comments" % "0.1.1",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function a() {
return 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Lint an valid js file and see that linting errors are reported.
-> assets
$ exists target/missing-semi-error

> set JshintKeys.config := Some(file("test.jshintrc"))
> assets

$ exists project/target/node-modules/webjars/jshint/2.13.6/bin/jshint
$ exists project/target/node-modules/webjars/jshint/2.9.1/jshint.js

$ exists project/target/node-modules/webjars/strip-json-comments/1.0.2-1/strip-json-comments.js
$ exists project/target/node-modules/webjars/strip-json-comments/1.0.4/strip-json-comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"asi" : true
}
5 changes: 0 additions & 5 deletions src/sbt-test/sbt-jshint-plugin/test/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
resolvers ++= Seq(
Resolver.mavenLocal,
Resolver.sonatypeRepo("snapshots"),
)

addSbtPlugin("com.github.sbt" % "sbt-jshint" % sys.props("project.version"))
5 changes: 5 additions & 0 deletions src/sbt-test/sbt-jshint-plugin/test/test
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ $ exists target/missing-semi-error

> set JshintKeys.config := Some(file("test.jshintrc"))
> assets

$ exists project/target/node-modules/webjars/jshint/bin/jshint

$ exists project/target/node-modules/webjars/strip-json-comments/1.0.2-1/strip-json-comments.js
$ exists project/target/node-modules/webjars/strip-json-comments/1.0.4/strip-json-comments.js

0 comments on commit f237abb

Please sign in to comment.