I've got this extension method: ```public fun Stat...
# javascript
e
I've got this extension method:
Copy code
public fun Status.isSuccessful(): Boolean {
  return code == HttpStatuses.Ok
      || code == HttpStatuses.Created
      || code == HttpStatuses.Accepted
      || code == HttpStatuses.NoContent
      || code == HttpStatuses.ResetContent
      || code == HttpStatuses.PartialContent
}
And I didn't expect the compiled JS to be this convoluted 👀
Copy code
function isSuccessful(_this__u8e3s4) {
  var tmp;
  var tmp_0;
  var tmp_1;
  var tmp_2;
  var tmp_3;
  if (_this__u8e3s4.h19_1 === 200) {
    tmp_3 = true;
  } else {
    tmp_3 = _this__u8e3s4.h19_1 === 201;
  }
  if (tmp_3) {
    tmp_2 = true;
  } else {
    tmp_2 = _this__u8e3s4.h19_1 === 202;
  }
  if (tmp_2) {
    tmp_1 = true;
  } else {
    tmp_1 = _this__u8e3s4.h19_1 === 204;
  }
  if (tmp_1) {
    tmp_0 = true;
  } else {
    tmp_0 = _this__u8e3s4.h19_1 === 205;
  }
  if (tmp_0) {
    tmp = true;
  } else {
    tmp = _this__u8e3s4.h19_1 === 206;
  }
  return tmp;
}
Is this output by design?
j
Looks like it's maintaining the short-circuit dynamics of the language boolean operators. So I would say yes
e
And probably the compiler always works with a single return point, instead of returning early.
j
No you can generate multiple returns. I think it's more that it has to generate code which satisfies the entire expression in isolation which then flows into your single return.
e
it has to generate code which satisfies the entire expression
Do you think it's worth opening an issue for a possible optimization? Or should I just forget about it.
j
Usually their assumption is that if you care about size you are running one of the many tools in the JS ecosystem. For example, terser outputs
Copy code
h;console.log(!(200!==(h=200).h19_1&&201!==h.h19_1&&202!==h.h19_1&&204!==h.h19_1&&205!==h.h19_1)||206===h.h19_1)
for that code +
console.log(isSuccessful(200))
e
Makes sense. I've never tried integrating JS-first tooling in the compilation pipeline, but probably it's worth a try just for the experience
j
The Kotlin Gradle plugin supports webpack out of the box and you can basically run anything within webpack
e
I'll check the docs, thanks!
Ahh wait... I just recalled Webpack is used for the browser target only. Let me know if my assumption is wrong tho.
a
And with 1.9.20 it should be better, because we are making post-processing of JS AST now, to eliminate a lot of variables.
very nice 1
e
@Artem Kobzar that's cool! Is it already in Beta? Or will it (possibly) land in RC+? I mean, I don't have a size issue ATM, so even if it's ready for 2.0, no problem
In the meantime I'll test out the workaround in KT-35620, curious to see how well it works.
a
It should also work in beta
e
@Artem Kobzar than I think that is the result as I'm in Beta2. Not sure if I need to enable some flag tho
a
Hmm, I will ask the guy who made the feature and will come back to you
I've checked and seems like it's our fault. I believe we will fix it in the next version
e
Thank you!
h
Did you manage to use webpack with nodeJS` and`binaries.library` to remove dead code/minify the resulting code?
e
@hfhbd I still need to try out the suggestion from the linked issue, but currently the only way is to use the
browser
target. I suppose with
nodeJs
you'd have to integrate an additional step and use external tools after the build is done
h
I tried the browser target too but unfortunately, I get my problem regarding the
module.exports
again. Don't know yet if there is a web pack option to remove this module option (again).
e
Mmmh someone correct me if I'm wrong but Webpack seem to run only with
binaries.executable()
Yeah looked at the code and it runs only for executable configurations. That means in my case it is impossible to emulate.
Well it probably makes sense. The library is then used to produce an executable in the end, so the minification/processing step is delegated to the final consumer. Although one could also use a library as-is, especially in Node
h
Well, it does make sense, but you could minify your library to distribute a smaller binary (eg at npm) when using other dependencies with
implementation
.
e
but you could minify your library to distribute a smaller binary
Definitely. I've asked on the linked issue why libraries aren't covered