Search code examples
commonjsscala.js

How to convert Scala.js application to CommonJS module?


Is there any standard way to use Scala.js application as a libriary in CommonJS environment? And if not, can I patch generated js file for that purpose?


Solution

  • Scala.js 0.6.13 and onward

    Put this in your build file:

    scalaJSModuleKind := ModuleKind.CommonJSModule
    

    Scala.js 0.6.5 to 0.6.12

    Put this in your build file (explanation see below):

    scalaJSOutputWrapper := ("var __ScalaJSEnv = { exportsNamespace: exports };", "")
    

    Scala.js 0.5.4 to 0.6.4

    You can tell Scala.js where to send the stuff it exports. To make a CommonJS module, simply prepend this:

    var __ScalaJSEnv = {
       exportsNamespace: exports
    };
    

    to the .js file produced by fastOptJS/fullOptJS.

    Pre Scala.js 0.5.4

    Please upgrade :)