Skip to content

Commit 086ea02

Browse files
committed
feat(option): add optimization and transformation 🚸
the options by default ```json { optimization: { level: 2, // -O2 shrinkLevel: 1 // -Os }, transformation: { passes: [ "duplicate-function-elimination", "inlining-optimizing", "remove-unused-module-elements", "memory-packing" ] }, debug: false } ```
1 parent 9f55fff commit 086ea02

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

src/index.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import assert from 'assert';
22

33
import { getOptions } from 'loader-utils';
44
import validate from '@webpack-contrib/schema-utils';
5-
import { readBinary, setDebugInfo } from 'binaryen';
5+
import {
6+
readBinary,
7+
setOptimizeLevel,
8+
setShrinkLevel,
9+
setDebugInfo,
10+
} from 'binaryen';
611

712
import schema from './options.json';
813

@@ -20,7 +25,43 @@ export default function loader(source) {
2025
target: options,
2126
});
2227

23-
// `..|| false` since docs not clear enough about the default value
28+
if (options.optimization) {
29+
const o = options.optimization;
30+
if (o.level) setOptimizeLevel(o.level);
31+
if (o.shrinkLevel) setShrinkLevel(o.shrinkLevel);
32+
}
33+
34+
if (options.transformation) {
35+
// #region helpers function
36+
const tf = options.transformation;
37+
const runOnFunction = (passes) => {
38+
if (tf.function) wasmModule.runPassesOnFunction(tf.function, passes);
39+
};
40+
const runPassses = (passes) => {
41+
wasmModule.runPasses(passes); // ⬅ ️since it only accept array (based on docs)
42+
runOnFunction(passes);
43+
};
44+
// #endregion
45+
46+
// #region global transformation
47+
if (tf.passes) {
48+
if (Array.isArray(tf.passes)) runPassses(tf.passes);
49+
else runPassses([tf.passes]); // ⬅ ️also accept string
50+
}
51+
// WARNING: docs not clear what default level ⤵️ optimization
52+
else if (tf.function) wasmModule.optimizeFunction(tf.function); // ⬅ ️run default passes
53+
// #endregion
54+
55+
// #region transformation on each specific functions
56+
// if (Array.isArray(options.transformation)) {
57+
// // TODO: support different transformation on different function
58+
// /* TRACK: waiting binaryen.js support runPasses on different function
59+
// currently it support on single function or global transformation */
60+
// }
61+
// #endregion
62+
}
63+
64+
// `..|| false` ⤵️ since docs not clear enough about the default value
2465
setDebugInfo(options.debug || false);
2566
wasmModule.optimize();
2667

0 commit comments

Comments
 (0)