emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.76k stars 3.3k forks source link

Postprocessing JS #18740

Open jeswr opened 1 year ago

jeswr commented 1 year ago

The current JS that emscripten generates uses some old conventions + often generates unused code; I have seen a few issues going around offering manual updates of some of these things.

I wanted to note that a combination of tools like lebab, eslint and rollup could be used in a post processing step to tidy up the .js files that emscripten emits.

In particular, with the default eslint and rollup configs I just ran on a generated file called swipl.js

lebab --replace swipl.js --transform arrow,arrow-return,for-of,arg-rest,arg-spread,obj-method,obj-shorthand,no-strict,exponent,multi-var,let,class,commonjs,template,default-param,destruct-param,includes
eslint swipl.js
rollup swipl.js -o out.js

The out.js was about 10% smaller on the project I tried and has various code smells removed such as limiting the use of var, removing unused variables etc.

sbc100 commented 1 year ago

Interesting. Were you able to improve code size even with --closure=1 and -O3? I think we generally worry more about code size than code smells in the generated JS, since the output JS can kind of be considered machine-generated code (of course it would be nice if looked nice too)

jeswr commented 1 year ago

Will double check my settings - the main code removed seemed to be unused variable declarations in the Javascript.

sbc100 commented 1 year ago

Yeah I think we generally rely on closure compiler to remove all of those. Or we try not to generate them in first place (by using our fake C pre-processor thing).