Skip to content

Commit

Permalink
Add a 'no npm' example
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Jun 9, 2022
1 parent 6e436ac commit a3b9a39
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
10 changes: 10 additions & 0 deletions examples/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ lazy val mario =
.settings(commonSettings: _*)
.settings(name := "mario")

lazy val nonpm =
(project in file("no-npm"))
.enablePlugins(ScalaJSPlugin)
.settings(commonSettings: _*)
.settings(
name := "No NPM",
scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.NoModule) }
)

lazy val subcomponents =
(project in file("subcomponents"))
.enablePlugins(ScalaJSPlugin)
Expand All @@ -127,6 +136,7 @@ lazy val exampleProjects: List[String] =
"http",
"indigo",
"mario",
"nonpm",
"subcomponents",
"websocket"
)
Expand Down
13 changes: 13 additions & 0 deletions examples/no-npm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Tyrian counter example - No NPM Version!

This is yet another version of the Counter example, but this one is just plain old HTML + JavaScript. No NPM, no bundlers, nothing.

You will need a static http server unfortunately, you can install one globally with ...npm (there are others, I like this one):

```npm install -g http-server```

To run your http server, `cd` into this directory (`tyrian/examples/no-npm`) using your terminal, and then run:

```http-server -c-1```

Then navigate to [http://localhost:8080/](http://localhost:8080/) in your browser of choice.
17 changes: 17 additions & 0 deletions examples/no-npm/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<title>Tyrian Counter Example (No NPM)</title>
</head>

<body>
<div id="myapp"></div>
<script type="text/javascript" src="./target/scala-3.1.2/no-npm-fastopt.js"></script>
<script type="text/javascript">
TyrianApp.launch("myapp");
</script>
</body>

</html>
38 changes: 38 additions & 0 deletions examples/no-npm/src/main/scala/example/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package example

import cats.effect.IO
import tyrian.Html.*
import tyrian.*

import scala.scalajs.js.annotation.*

@JSExportTopLevel("TyrianApp")
object Main extends TyrianApp[Msg, Model]:

def init(flags: Map[String, String]): (Model, Cmd[IO, Msg]) =
(Model.init, Cmd.None)

def update(model: Model): Msg => (Model, Cmd[IO, Msg]) =
case Msg.Increment => (model + 1, Cmd.None)
case Msg.Decrement => (model - 1, Cmd.None)

def view(model: Model): Html[Msg] =
div(
button(onClick(Msg.Decrement))("-"),
div(model.toString),
button(onClick(Msg.Increment))("+")
)

def subscriptions(model: Model): Sub[IO, Msg] =
Sub.None

opaque type Model = Int
object Model:
def init: Model = 0

extension (i: Model)
def +(other: Int): Model = i + other
def -(other: Int): Model = i - other

enum Msg:
case Increment, Decrement

0 comments on commit a3b9a39

Please sign in to comment.