0

I'm writing an sbt plugin that creates a JS file from a specific StaticAnnotation type; instead of including all annotations found on the classpath, I would prefer to only emit annotations on classes that were actually linked by fastOptJS / fullOptJS. Can I leverage the Scala.js linker tools or sbt plugin to this end?

jokade
  • 197
  • 5

1 Answers1

0

Yes, you can. The Scala.js Tools API (the linker tools) offers a data type called Analysis which contains everything you need.

You can construct an Analysis with Analyzer.computeReachability. You have to give it the appropriate ClassInfo for the .sjsir files on your classpath.

This has been done in the callgraph sbt plugin which, although still under development, shows how to construct an Analysis for an sbt project: https://github.com/lionelfleury/scala-js-call-graph/blob/065506bf9a9c12ccc54fed056fed878e6f18b328/sbt-scalajs-callgraph/src/main/scala/ch/epfl/sbtplugin/CallGraphPlugin.scala#L28-L49

This particular implementation selects the Analysis after optimizations have been performed (unless the program does not link, in which case it falls back to the analysis before optimizations are performed). Depending on your use case, you might prefer one or the other, but it is more likely that you want the version after optimizations.

Once you have an Analysis, you can simply list its classInfo members, and filter out those for which .isNeededAtAll is false. Again, depending on your case, you want to use more refined tests, such as .isAnySubclassInstantiated.

sjrd
  • 21,805
  • 2
  • 61
  • 91
  • Great, thanks for the detailed explanation. One question though: how do I get a core.tools.logging.Logger from an sbt.Logger? – jokade May 31 '16 at 15:55
  • Like this: https://github.com/scala-js/scala-js/blob/master/sbt-plugin/src/main/scala/scala/scalajs/sbtplugin/Implicits.scala (although, now that I see that, we should probably not expose those things as implicits ^^) – sjrd May 31 '16 at 19:44