-
Notifications
You must be signed in to change notification settings - Fork 3
/
esgleam.gleam
281 lines (251 loc) · 7.01 KB
/
esgleam.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import gleam/io
import gleam/string
import gleam/list
import gleam/option.{type Option, None, Some}
import gleam/result
import gleam/string_builder.{append, append_builder, from_strings}
import simplifile
import esgleam/internal
import esgleam/mod/install
import esgleam/mod/platform.{get_exe_name}
/// Kind of output
pub type Kind {
/// Executes the `main()` function in your entry point
Script
/// Only exports the code
Library
}
/// Output format of generated JavaScript
pub type Format {
/// ECMAScript module
Esm
/// CommonJS
Cjs
/// immediately-invoked function expression
Iife
}
/// [Platform](https://esbuild.github.io/api/#platform)
pub type Platform {
/// Build platform independed code
Neutral
/// Build browser compatable code (default)
Browser
/// Build for Node
Node
}
/// Config for esbuild
pub type Config {
Config(
/// Output directory
outdir: String,
/// List of entry points supplied
entry_points: List(String),
/// Output format for JavaScript. See [`Format`](#Format)
/// default `Esm`
format: Format,
/// Kind for output
/// default `Library`
kind: Kind,
/// default `False`
minify: Bool,
/// List of [target environments](https://esbuild.github.io/api/#target)
/// default `[]`
target: List(String),
/// The root directory for the dev server (run server when defined)
/// default `None`
serve: Option(String),
/// Generate sourcemap for JavaScript
/// default `False`
sourcemap: Bool,
/// Platform for the output
/// See [esbuild/platform](https://esbuild.github.io/api/#platform) for what each type does
/// default `Neutral`
platform: Option(Platform),
/// Enable watchmode. Bundles files on change
/// Note: `gleam build` need to be run manually
/// default `False`
watch: Bool,
/// raw flags to pass to esbuild
/// default `""`
raw: String,
)
}
/// Start to create a build script
/// ```gleam
/// > esbuild.new("./dist/static")
/// Config("./dist/static", ...)
/// ```
pub fn new(outdir path: String) -> Config {
Config(
outdir: path,
entry_points: [],
format: Esm,
kind: Library,
minify: False,
target: [],
serve: None,
sourcemap: False,
platform: None,
watch: False,
raw: "",
)
}
/// [Entry points](https://esbuild.github.io/api/#entry-points)
/// Can be use multiple times
pub fn entry(config: Config, path: String) -> Config {
Config(..config, entry_points: [path, ..config.entry_points])
}
/// Output format. see [`Format`](#format)
pub fn format(config: Config, format: Format) {
Config(..config, format: format)
}
/// Kind for output. see [`Kind`](#kind)
/// if set to `Script`, will ignore all entries except the first and will call its `main` function.
pub fn kind(config: Config, kind: Kind) {
Config(..config, kind: kind)
}
/// [Target](https://esbuild.github.io/api/#target) for transpiled JavaScript.
/// Can be used mutiple times
/// ```gleam
/// > esbuild.new("./dist/static")
/// > |> esbuild.target("es2020")
/// > |> esbuild.target("firefox110")
/// > |> esbuild.target("edge90")
/// ```
pub fn target(config: Config, target: String) {
Config(..config, target: [target, ..config.target])
}
/// Create minified JavaScript
pub fn minify(config: Config, do_minify: Bool) {
Config(..config, minify: do_minify)
}
/// Start a development server on http://127.0.0.1:8000 with `path` being `/`
pub fn serve(config: Config, dir path: String) {
Config(..config, serve: Some(path))
}
/// Generate code for a specified platform
pub fn platform(config: Config, platform platform: Platform) {
Config(..config, platform: Some(platform))
}
// #TODO: Setup file watcher for all targets
/// Note: There is no file watcher for Gleam files so you have to manually run `gleam build` on change.
pub fn watch(config: Config, do_watch: Bool) {
Config(..config, watch: do_watch)
}
/// Raw CLI argument to pass to esbuild
pub fn raw(config: Config, args: String) {
Config(..config, raw: string.append(to: " ", suffix: args))
}
/// Executes `esbuild` with the provided config parsed as CLI arguments.
pub fn bundle(config: Config) {
simplifile.create_directory_all(config.outdir)
|> result.map(fn(_) { do_bundle(config) })
}
fn do_bundle(config: Config) {
let entries_list =
list.map(config.entry_points, fn(entry) {
"./build/dev/javascript/"
<> internal.get_project_name()
<> "/"
<> string.replace(entry, ".gleam", with: ".mjs")
})
let assert Ok(first_entry_rel) =
list.first(config.entry_points)
|> result.map(string.replace(_, ".gleam", with: ".mjs"))
let script_path = {
"./build/dev/javascript/"
<> internal.get_project_name()
<> "/"
<> "gleam.main.mjs"
}
let entries = case config.kind {
Script -> {
let content =
"import { main } from \"" <> "./" <> first_entry_rel <> "\";main?.();"
let assert Ok(_) = simplifile.write(content, to: script_path)
script_path
}
Library -> string.join(entries_list, with: " ")
}
let exe_path = "./build/dev/bin/package/bin/" <> get_exe_name()
let cmd =
string_builder.from_string(exe_path <> " ")
|> append(entries)
|> append(" --bundle")
|> append_builder(case config.kind {
Script ->
from_strings([
" --outfile="
<> config.outdir
<> "/"
<> internal.get_project_name()
<> ".mjs",
])
Library -> from_strings([" --outdir=", config.outdir])
})
|> if_true(config.minify, " --minify")
|> if_some(
option.map(config.platform, fn(platform) {
case platform {
Neutral -> "neutral"
Node -> "node"
Browser -> "browser"
}
}),
" --platform=",
)
|> if_true(config.watch, " --watch")
|> append(" --format=")
|> append(format_to_string(config.format))
|> if_true(!list.is_empty(config.target), " --target=")
|> append(string.join(config.target, with: ","))
|> if_some(config.serve, flag: " --servedir=")
|> append(config.raw)
|> string_builder.to_string
case simplifile.is_file(exe_path) {
False -> install.fetch()
True -> Nil
}
io.println("$ " <> cmd)
case config.watch {
True -> watch_gleam()
False -> fn() { Nil }
}
internal.exec_shell(cmd)
}
fn format_to_string(format: Format) {
case format {
Esm -> "esm"
Cjs -> "cjs"
Iife -> "iife"
}
}
fn if_true(
builder: string_builder.StringBuilder,
predicate: Bool,
value: String,
) -> string_builder.StringBuilder {
case predicate {
True -> value
False -> ""
}
|> append(builder, _)
}
fn if_some(
builder: string_builder.StringBuilder,
option opt: Option(String),
flag flag: String,
) -> string_builder.StringBuilder {
case opt {
Some(value) -> flag <> value
None -> ""
}
|> append(builder, _)
}
@target(javascript)
@external(javascript, "./ffi_esgleam.mjs", "watch_gleam")
pub fn watch_gleam() -> fn() -> Nil
@target(erlang)
pub fn watch_gleam() -> fn() -> Nil {
fn() { Nil }
}