npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@scala-js/vite-plugin-scalajs

v1.0.0

Published

Vite plugin for integration of Scala.js

Downloads

2,335

Readme

vite-plugin-scalajs

A Vite plugin for Scala.js.

Usage

We assume that you have an existing Vite and Scala.js sbt project. If not, follow the accompanying tutorial.

Install the plugin as a development dependency:

$ npm install -D @scala-js/vite-plugin-scalajs

Tell Vite to use the plugin in vite.config.js:

import { defineConfig } from "vite";
import scalaJSPlugin from "@scala-js/vite-plugin-scalajs";

export default defineConfig({
  plugins: [scalaJSPlugin()],
});

Finally, import the Scala.js output from a .js or .ts file with

import 'scalajs:main.js';

which will execute the main method of the Scala.js application.

The sbt project must at least be configured to use ES modules. For the best feedback loop with Vite, we recommend to emit small modules for application code. If your application lives in the my.app package, configure the sbt project with the following settings:

scalaJSLinkerConfig ~= {
  _.withModuleKind(ModuleKind.ESModule)
    .withModuleSplitStyle(
      ModuleSplitStyle.SmallModulesFor(List("my.app")))
},

Configuration

The plugin supports the following configuration options:

export default defineConfig({
  plugins: [
    scalaJSPlugin({
      // path to the directory containing the sbt build
      // default: '.'
      cwd: '.',

      // sbt project ID from within the sbt build to get fast/fullLinkJS from
      // default: the root project of the sbt build
      projectID: 'client',

      // URI prefix of imports that this plugin catches (without the trailing ':')
      // default: 'scalajs' (so the plugin recognizes URIs starting with 'scalajs:')
      uriPrefix: 'scalajs',
    }),
  ],
});

Importing @JSExportTopLevel Scala.js members

@JSExportTopLevel("foo") members in the Scala.js code are exported from the modules that Scala.js generates. They can be imported in .js and .ts files with the usual JavaScript import syntax.

For example, given the following Scala.js definition:

import scala.scalajs.js
import scala.scalajs.js.annotation._

@JSExportTopLevel("ScalaJSLib")
class ScalaJSLib extends js.Object {
  def square(x: Double): Double = x * x
}

we can import and use it as

import { ScalaJSLib } from 'scalajs:main.js';

const lib = new ScalaJSLib();
console.log(lib.square(5)); // 25

Exports in other modules

By default, @JSExportTopLevel("Foo") exports Foo from the main module, which is why we import from scalajs:main.js. We can also split the Scala.js exports into several modules. For example,

import scala.scalajs.js
import scala.scalajs.js.annotation._

@JSExportTopLevel("ScalaJSLib", "library")
class ScalaJSLib extends js.Object {
  def square(x: Double): Double = x * x
}

can be imported with

import { ScalaJSLib } from 'scalajs:library.js';

The Scala.js documentation contains more information about module splitting.