Skip to content

Commit

Permalink
Add logic for minifying the resulting Javascript files.
Browse files Browse the repository at this point in the history
  • Loading branch information
EdSchouten committed Apr 22, 2019
1 parent c5db643 commit 3ba2519
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ Javascript file will be named `${name}.js`.

**Note:** When the compilation mode (`-c`) is equal to `dbg`, the
resulting Javascript file will have the time traveling debugger enabled.
When the compilation mode is `opt`, optimizations are performed.
When the compilation mode is `opt`, optimizations are performed and the
resulting code is minified using UglifyJS.

### `elm_library()`

Expand Down
73 changes: 62 additions & 11 deletions elm/def.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,63 @@ def _do_elm_make(

def _elm_binary_impl(ctx):
js_file = ctx.actions.declare_file(ctx.attr.name + ".js")
_do_elm_make(
ctx,
ctx.files.main[0],
ctx.attr.deps,
[],
[],
[js_file],
js_file.path,
"",
"",
)
if ctx.var["COMPILATION_MODE"] == "opt":
# Step 1: Compile the Elm code.
js1_file = ctx.actions.declare_file(ctx.attr.name + ".1.js")
_do_elm_make(
ctx,
ctx.files.main[0],
ctx.attr.deps,
[],
[],
[js1_file],
js1_file.path,
"",
"",
)

# Step 2: Compress the resulting Javascript.
js2_file = ctx.actions.declare_file(ctx.attr.name + ".2.js")
ctx.actions.run(
mnemonic = "UglifyJS",
executable = ctx.executable._uglifyjs,
arguments = [
js1_file.path,
"--compress",
"pure_funcs=[F2,F3,F4,F5,F6,F7,F8,F9,A2,A3,A4,A5,A6,A7,A8,A9],pure_getters,keep_fargs=false,unsafe_comps,unsafe",
"--output",
js2_file.path,
],
inputs = [js1_file],
outputs = [js2_file],
)

# Step 3: Mangle the resulting Javascript.
ctx.actions.run(
mnemonic = "UglifyJS",
executable = ctx.executable._uglifyjs,
arguments = [
js2_file.path,
"--mangle",
"--output",
js_file.path,
],
inputs = [js2_file],
outputs = [js_file],
)
else:
# Don't attempt to compress the code after building.
_do_elm_make(
ctx,
ctx.files.main[0],
ctx.attr.deps,
[],
[],
[js_file],
js_file.path,
"",
"",
)
return [DefaultInfo(files = depset([js_file]))]

elm_binary = rule(
Expand All @@ -94,6 +140,11 @@ elm_binary = rule(
single_file = True,
default = Label("@com_github_edschouten_rules_elm//elm:compile.py"),
),
"_uglifyjs": attr.label(
cfg = "host",
default = Label("@npm//uglify-js/bin:uglifyjs"),
executable = True,
),
},
toolchains = [_TOOLCHAIN],
implementation = _elm_binary_impl,
Expand Down

0 comments on commit 3ba2519

Please sign in to comment.