How to import UglifyJS from Scala.js?

2019-09-14 05:49发布

问题:

I can use jQuery from Scala.js quite well:

jsDependencies += "org.webjars" % "jquery" % "2.2.1" / "jquery.js" minified "jquery.min.js"

Now I would like to use UglifyJS and its webjar structure seems a bit different. There seems to be no minified js file, and even the main file which probably contains the complete library looks different, it has no .js extension and it starts with following lines:

#! /usr/bin/env node
// -*- js -*-

"use strict";

var UglifyJS = require("../tools/node");

Can this webjar be used from Scala.js at all, and if it can, how? I have tried following, but it did not work:

jsDependencies += "org.webjars" % "uglifyjs" % "2.7.4" / "bin/uglifyjs"

With this line SBT error was:

[error] (test:resolvedJSDependencies) org.scalajs.core.tools.jsdep.JSLibResolveException: Some references to JS libraries could not be resolved:

[error] - Missing JS library: bin/uglifyjs

[error] originating from: scalafromjs:test, scalafromjs:compile

回答1:

Uglify is published as uglify-js, not uglifyjs. Also, a webjar was not created for version 2.7.4, however there is one for 2.7.5.

You can easily request automatic creation of a webjar for 2.7.4 in a few clicks on http://www.webjars.org/npm if you need it that specific version.

If you go to the webjars website and search for "uglify-js", you'll see the dependency config you need to use: "org.webjars.npm" % "uglify-js" % "2.7.5" (plus the file you want).


Note that as of 0.6.14 Scala.js supports npm modules using scalajs-bundler.

That new system is by far superior to webjars – you have ALL npm modules and versions available, no need to trigger creation yourself (wait till you get into webjar dependencies!), no need to wait for the index to be updated, etc.