# Did anybody ever use Prepack or Google closure compiler with Elm 0.18's compiled JavaScript?

**URL:** <https://discourse.elm-lang.org/t/did-anybody-ever-use-prepack-or-google-closure-compiler-with-elm-0-18s-compiled-javascript/2030>\
**Category:** Learn\
**Created:** [September 21, 2018, 9:38pm UTC](https://discourse.elm-lang.org/t/did-anybody-ever-use-prepack-or-google-closure-compiler-with-elm-0-18s-compiled-javascript/2030 "2018-09-21T21:38:28Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![DevotionGeo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/devotiongeo/32/3558_2.png) [@DevotionGeo](https://discourse.elm-lang.org/u/DevotionGeo)\
**Post date:** [September 21, 2018, 9:38pm UTC](https://discourse.elm-lang.org/t/did-anybody-ever-use-prepack-or-google-closure-compiler-with-elm-0-18s-compiled-javascript/2030/1 "2018-09-21T21:38:28Z")

</div>

I tried both of them to optimize / remove dead code, and both failed, because of some error. The Elm file compiled to JavaScript was a simple hello world example

---

<div class="post-metadata">

**Author:** ![Hector](https://avatars.discourse-cdn.com/v4/letter/h/43a26b/32.png) [@Hector](https://discourse.elm-lang.org/u/Hector)\
**Post date:** [September 21, 2018, 10:15pm UTC](https://discourse.elm-lang.org/t/did-anybody-ever-use-prepack-or-google-closure-compiler-with-elm-0-18s-compiled-javascript/2030/2 "2018-09-21T22:15:02Z")

</div>

I use closure compiler [version v20171203](https://github.com/google/closure-compiler/wiki/Binary-Downloads#v20171203) with `--compilation_level SIMPLE_OPTIMIZATIONS` It gives a few warnings but works fine. Newer versions can’t process my code and output errors. Note that SIMPLE\_OPTIMIZATIONS doesn’t remove dead code.

---

<div class="post-metadata">

**Author:** ![pdamoc](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/pdamoc/32/36_2.png) [@pdamoc](https://discourse.elm-lang.org/u/pdamoc)\
**Post date:** [September 22, 2018, 5:55am UTC](https://discourse.elm-lang.org/t/did-anybody-ever-use-prepack-or-google-closure-compiler-with-elm-0-18s-compiled-javascript/2030/3 "2018-09-22T05:55:53Z")

</div>

> [@DevotionGeo](#):
>
> I tried both of them to optimize / remove dead code

Use `uglifyjs` or, if you don’t want to bother with the options, use `elm-minify` (which uses `uglifyjs` behind the scene).

Sample command:

```bash
uglifyjs elm.js --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' | uglifyjs --mangle --output=elm.min.js

```

---

<div class="post-metadata">

**Author:** ![DevotionGeo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/devotiongeo/32/3558_2.png) [@DevotionGeo](https://discourse.elm-lang.org/u/DevotionGeo)\
**Post date:** [September 22, 2018, 7:20am UTC](https://discourse.elm-lang.org/t/did-anybody-ever-use-prepack-or-google-closure-compiler-with-elm-0-18s-compiled-javascript/2030/4 "2018-09-22T07:20:52Z")

</div>

I used `elm-minify` and it worked fine. The concept of Prepack is different than minification or dead-code-removal (which Google Closure Compiler does). It eliminate computations which can be done on compile time, so the app runs faster during runtime (having to deal with less computations). Google Closure compiler may also be doing something similar when the “advanced” option is chosen (I’m not sure about that).  
Though it’s not necessary, but I thought if somebody already did it, getting the benefits for free is a good thing. 🙂

---

<div class="post-metadata">

**Author:** ![pdamoc](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/pdamoc/32/36_2.png) [@pdamoc](https://discourse.elm-lang.org/u/pdamoc)\
**Post date:** [September 22, 2018, 9:27am UTC](https://discourse.elm-lang.org/t/did-anybody-ever-use-prepack-or-google-closure-compiler-with-elm-0-18s-compiled-javascript/2030/5 "2018-09-22T09:27:05Z")

</div>

Out of curiosity, I tried using `prepack` on a simple Counter app. It worked in that it produced some `.js` file that behaved properly. The funny thing is that `elm-minify` on the prepack version produced a large minified version than running it on the compiler output.

so `Main.elm > compiler > prepack > uglifyjs` produces a file that is larger than `Main.elm > compiler > uglifyjs`.

I have tested it on the `elm-spa-example` and the difference remains. (93k for the uglifyjs only vs 114k for prepack + uglifyjs)

It would be wonderful to have compile time function evaluation native in the elm compiler when the `--optimize` would be used. This would allow for even smaller sizes but it is not a trivial optimization.

---

<div class="post-metadata">

**Author:** ![dmy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/dmy/32/702_2.png) [@dmy](https://discourse.elm-lang.org/u/dmy)\
**Post date:** [September 22, 2018, 9:27am UTC](https://discourse.elm-lang.org/t/did-anybody-ever-use-prepack-or-google-closure-compiler-with-elm-0-18s-compiled-javascript/2030/6 "2018-09-22T09:27:36Z")

</div>

> [@pdamoc](#):
>
> It would be wonderful to have compile time function evaluation native in the elm compiler when the `--optimize` would be used. This would allow for even smaller sizes but it is not a trivial optimization.

This is interesting. I guess the compiler would have to run the elm code to do that, and therefore it would have to run some javascript (requiring nodejs), wouldn’t it?

---

<div class="post-metadata">

**Author:** ![pdamoc](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/pdamoc/32/36_2.png) [@pdamoc](https://discourse.elm-lang.org/u/pdamoc)\
**Post date:** [September 22, 2018, 10:10am UTC](https://discourse.elm-lang.org/t/did-anybody-ever-use-prepack-or-google-closure-compiler-with-elm-0-18s-compiled-javascript/2030/7 "2018-09-22T10:10:16Z")

</div>

> [@dmy](#):
>
> I guess the compiler would have to run the elm code to do that, and therefore it would have to run some javascript (requiring nodejs), wouldn’t it?

Not necessarily. I imagine that a restricted version of this functionality could be implemented without going through JS. Imagine implementing an `eval` function that would be able to evaluate some Elm code by using Haskell’s functions. It might not be able to evaluate every piece of Elm code but it doesn’t need to. The compile time execution can be provided for a restricted set of elm functions and ignored for what it cannot do. For example, it can provide execution of operations on `Int`, `Float`, `String` and maybe on very simple custom types.

In any case, it would not be trivial and I’m not familiar with the compiler implementation so I might be talking nonsense.

---

<div class="post-metadata">

**Author:** ![joakin](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/joakin/32/171_2.png) [@joakin](https://discourse.elm-lang.org/u/joakin)\
**Post date:** [September 22, 2018, 11:02am UTC](https://discourse.elm-lang.org/t/did-anybody-ever-use-prepack-or-google-closure-compiler-with-elm-0-18s-compiled-javascript/2030/8 "2018-09-22T11:02:15Z")

</div>

I believe that prepack targets speed of execution and initial parse/execution optimizations so it is like an optimizing compiler, rather than a minifier.

---

<div class="post-metadata">

**Author:** ![dmy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/dmy/32/702_2.png) [@dmy](https://discourse.elm-lang.org/u/dmy)\
**Post date:** [September 22, 2018, 11:44am UTC](https://discourse.elm-lang.org/t/did-anybody-ever-use-prepack-or-google-closure-compiler-with-elm-0-18s-compiled-javascript/2030/9 "2018-09-22T11:44:00Z")

</div>

Then I wonder what it would do with the js equivalent of:

```auto
range : List Int
range = List.range 0 42000

```

Would it pre-compute it?

---

<div class="post-metadata">

**Author:** ![joakin](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/joakin/32/171_2.png) [@joakin](https://discourse.elm-lang.org/u/joakin)\
**Post date:** [September 22, 2018, 12:33pm UTC](https://discourse.elm-lang.org/t/did-anybody-ever-use-prepack-or-google-closure-compiler-with-elm-0-18s-compiled-javascript/2030/10 "2018-09-22T12:33:51Z")

</div>

If it is in the top level and run on the first pass, then yes. See [example](https://prepack.io/repl.html#BQMwrgdgxgLglgewgAmASmQbwFDOb5AcwBsEAjAQ2IDoAnCiQgU2QF5kBBW+gT2AFYADILTUQcYsWAQwk0QFsKAB2DAA+gBpkcDKwB82tNgC+adAG4gA)

---

<div class="post-metadata">

**Author:** ![madasebrof](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/madasebrof/32/363_2.png) [@madasebrof](https://discourse.elm-lang.org/u/madasebrof)\
**Post date:** [September 22, 2018, 5:19pm UTC](https://discourse.elm-lang.org/t/did-anybody-ever-use-prepack-or-google-closure-compiler-with-elm-0-18s-compiled-javascript/2030/11 "2018-09-22T17:19:32Z")

</div>

We use the google closure compiler with great success for Elm 0.18! We run it using the `webpack-closure-compiler` for webpack, but only for production builds (for speed reasons).

[https://www.npmjs.com/package/webpack-closure-compiler](https://www.npmjs.com/package/webpack-closure-compiler)

But you do need the correct version of Java installed locally for it to work correctly–the JDK.

[https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html](https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [October 2, 2018, 5:19pm UTC](https://discourse.elm-lang.org/t/did-anybody-ever-use-prepack-or-google-closure-compiler-with-elm-0-18s-compiled-javascript/2030/12 "2018-10-02T17:19:36Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
