Skip to content

Commit

Permalink
feat: quick cli for bundle and serve
Browse files Browse the repository at this point in the history
  • Loading branch information
Enderchief committed Dec 29, 2023
1 parent 569cf4e commit fdb4c75
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 8 deletions.
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,28 @@

## Quick start

1. Create a Gleam project as you would normally.
1. Create a Gleam project as you would normally and make sure you have a file in `src` with the name of your project (as specified in `gleam.toml`)

2. Install `esbuild`
```sh
$ gleam run -m esgleam/install
```

3.
```sh
$ gleam run -m esgleam/bundle
```
See `/dist` for your bundled code

4. To start a development server
```sh
$ gleam run -m esgleam/serve
```

## Advanced Usage

(Follow steps 1-2)

3. Create `/src/build.gleam` with the following
```gleam
import esgleam
Expand All @@ -27,12 +42,7 @@ pub fn main() {
}
```

4. Build for JavaScript
```sh
$ gleam build --target=javascript
```

5. Run your build script (in any target)
5. Run your build script
```sh
$ gleam run -m build
```
Expand Down
4 changes: 3 additions & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
name = "esgleam"
version = "0.1.0"
version = "0.2.0"
gleam = ">= 0.32.0"

description = "esbuild for Gleam"
licences = ["Apache-2.0"]
repository = { type = "github", user = "Enderchief", repo = "esgleam" }
# links = [{ title = "Website", href = "https://gleam.run" }]

target = "javascript"

[dependencies]
gleam_stdlib = "~> 0.34"
simplifile = "~> 1.1"
Expand Down
9 changes: 9 additions & 0 deletions src/esgleam/bundle.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import esgleam
import esgleam/internal

/// Bundles using ESM to the `./dist` directory.
pub fn main() {
esgleam.new(outdir: "./dist")
|> esgleam.entry(internal.get_project_name() <> ".gleam")
|> esgleam.bundle
}
21 changes: 21 additions & 0 deletions src/esgleam/internal.gleam
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import gleam/regex
import gleam/result
import gleam/list
import gleam/option.{type Option, Some}
import simplifile
@target(javascript)
import gleam/string

Expand Down Expand Up @@ -34,3 +39,19 @@ pub fn exec_shell(command: String, cwd: String) -> Nil {
|> os_cmd
Nil
}

pub fn get_project_name() -> String {
let assert Ok(project_name) =
simplifile.read("./gleam.toml")
|> result.map(with: fn(file) {
let assert Ok(re) = regex.from_string("name *= *\"(\\w[\\w_]*)\"")
let assert Ok(match) =
regex.scan(re, file)
|> list.first

let assert Ok(first_maybe) = list.first(match.submatches)
let assert Some(name): Option(String) = first_maybe
name
})
project_name
}
10 changes: 10 additions & 0 deletions src/esgleam/serve.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import esgleam
import esgleam/internal

/// Serves the `/dist` directory as `/` on http://127.0.0.1:8000.
pub fn main() {
esgleam.new(outdir: "./dist")
|> esgleam.entry(internal.get_project_name() <> ".gleam")
|> esgleam.serve(dir: "./dist")
|> esgleam.bundle
}

0 comments on commit fdb4c75

Please sign in to comment.