JSPack.scala 2.08 KB
Newer Older
1
package org.openmole.gui.server.jscompile
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

/*
 * Copyright (C) 02/10/14 // mathieu.leclaire@openmole.org
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

20
import org.openmole.tool.stream._
Mathieu's avatar
Mathieu committed
21
22
23
24
25
import org.scalajs.linker.interface._

import scala.concurrent.Await
import scala.concurrent.duration.Duration
import org.scalajs.linker._
26
import java.io.File
27

Mathieu's avatar
Mathieu committed
28
import org.openmole.tool.file._
29
import org.openmole.core.workspace._
Romain Reuillon's avatar
Romain Reuillon committed
30
import org.scalajs.logging.{ NullLogger, ScalaConsoleLogger }
31
32
33

object JSPack {

Mathieu's avatar
Mathieu committed
34
35
  implicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global

36
  def link(inputDirectory: File, outputJSFile: File, optimizedJS: Boolean)(implicit newFile: TmpDirectory): Unit =
Mathieu's avatar
Mathieu committed
37
38
    newFile.withTmpFile("lib", ".jar") { jar 

39
      JSPack.getClass.getClassLoader.getResourceAsStream("scalajs-library.jar") copy jar
40
41

      // Obtain VirtualScalaJSIRFile's from the input classpath
Mathieu's avatar
Mathieu committed
42
      val irCache = StandardImpl.irFileCache().newCache
43

Mathieu's avatar
Mathieu committed
44
45
46
47
48
49
50
51
52
53
      val result = (for {
        (containers, _)  PathIRContainer.fromClasspath(Seq(jar.toPath, inputDirectory.toPath))
        sjsirFiles  irCache.cached(containers)
        config = StandardConfig()
          .withOptimizer(true)
          .withSourceMap(true)
          .withClosureCompilerIfAvailable(optimizedJS)

        linker = StandardImpl.linker(config)
        _  linker.link(sjsirFiles, Nil, LinkerOutput(PathOutputFile(outputJSFile.toPath)), NullLogger)
Mathieu's avatar
Mathieu committed
54
      } yield ())
Mathieu's avatar
Mathieu committed
55
56
57
58

      Await.result(result, Duration.Inf)
    }
}