从SJSIR“手动”构建JS [英] Build JS from SJSIR `manually`

查看:107
本文介绍了从SJSIR“手动”构建JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在运行时从sjsir文件构建一个js文件来实现一个插件系统,这样在编译时就无法完成编译。我曾经使用以下代码在0.6.3中实现相同的过程,但它似乎已被弃用。你建议使用什么算法来实现0.6.13的相同动作?
谢谢

I need to build a js file from sjsir files at runtime to implement a system of plugins, so that it can't be done at compile time with the rest of my compilation. I used to implement the same process in 0.6.3 with the following code, but it seems to be deprecated. What algorithm do you recommand to achieve the same action with 0.6.13 ? Thanks

val scalajsLib = copyJar("scalajs-library_2.11-0.6.3")

val semantics = org.scalajs.core.tools.sem.Semantics.Defaults

val partialClasspath =
  PartialClasspathBuilder.build(collection.immutable.Seq(scalajsLib, src))

val completeClasspath = partialClasspath.resolve()

val optimizer = new ScalaJSOptimizer(semantics)

val logger = new ScalaConsoleLogger

val out = WritableFileVirtualJSFile(
    new java.io.File(target, JS_FILE))
if (optimized) {
  val sems = semantics.optimized

  new ScalaJSClosureOptimizer(sems).optimizeCP(
    new ScalaJSOptimizer(sems),
    completeClasspath,
    ScalaJSClosureOptimizer.Config(out),
    logger
  )
} else {
  optimizer.optimizeCP(
    completeClasspath,
    ScalaJSOptimizer.Config(out, checkIR = false, wantSourceMap = !optimized),
    logger
  )
}


推荐答案

事实上,Tools API在0.6.5中发生了巨大的变化。它变得更加简单,并且能够在未来以非破坏性的方式发展。

The Tools API has dramatically changed in 0.6.5, indeed. It became much simpler and more able to evolve in non-breaking ways in the future.

您的上述代码可以使用新API编写,如下所示:

Your code above can be written with the new API as follows:

import java.io.File

import org.scalajs.core.tools.io._
import org.scalajs.core.tools.sem._
import org.scalajs.core.tools.linker.backend.{OutputMode, ModuleKind}
import org.scalajs.core.tools.linker.Linker
import org.scalajs.core.tools.logging.ScalaConsoleLogger

def link(inputClasspath: Seq[File], outputJSFile: File): Unit = {
  // Obtain VirtualScalaJSIRFile's from the input classpath
  val irCache = new IRFileCache().newCache
  val irContainers = IRFileCache.IRContainer.fromClasspath(inputClasspath)
  val sjsirFiles = irCache.cached(irContainers)

  // A bunch of options. Here we use all the defaults
  val semantics = Semantics.Defaults
  val outputMode = OutputMode.Default
  val moduleKind = ModuleKind.NoModule
  val linkerConfig = Linker.Config()

  // Actual linking
  val linker = Linker(semantics, outputMode, moduleKind, linkerConfig)
  val logger = new ScalaConsoleLogger
  linker.link(sjsirFiles, WritableFileVirtualJSFile(outputJSFile), logger)
}

你可以调用链接具有以下参数的函数,与上面的代码段完全匹配:

And you can call that link function with the following arguments, to exactly match your above snippet:

link(Seq(scalajsLib, src), new java.io.File(target, JS_FILE))

如果您打算在同一个进程中的同一个类路径上多次调用此方法,建议缓存并重用实例 irCache linker 跨运行,因为这将大大加快流程。

If you intend to call this method several times on the same classpath within the same process, it is advised to cache and reuse the instances irCache and linker across runs, as this will considerably speed up the process.

另见工具API的Scaladoc

这篇关于从SJSIR“手动”构建JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆