Skip to content

Commit 071fc26

Browse files
author
fengwan
committed
feat(options from AS): How about more control for compiler options? #8
How about more control for compiler options? #8
1 parent 3f2ca87 commit 071fc26

File tree

7 files changed

+300
-13
lines changed

7 files changed

+300
-13
lines changed

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,41 @@ asmPromise().then(function(asmModule){
8989

9090
<h2 align="center">Options</h2>
9191

92+
[The loader supports some of the AS options here](https://github.com/AssemblyScript/assemblyscript/wiki/Using-the-compiler)
93+
9294
|Name|Type|Default|Description|
9395
|:--:|:--:|:-----:|:----------|
9496
|**`name`**|`{String\|Function}`|`[hash].[ext]`|Configure a custom filename template for your file|
9597
|**`limit`**|`{Int}`|`undefined`|Byte limit to the wasm file,if the size is smaller then limit value ,the wasm will bundled into js ,or the wasm file will build into dist ,well runtime , bundled js will fetch it and return the Promise object;
9698
|**`publicPath`**|`{String\|Function}`|[`__webpack_public_path__ `](https://webpack.js.org/api/module-variables/#__webpack_public_path__-webpack-specific-)|Configure a custom `public` path for your file|
9799
|**`outputPath`**|`{String\|Function}`|`'undefined'`|Configure a custom `output` path for your file|
98-
100+
|inherited options from AS |
101+
|**`optimize`**|`{String}`|`'-0'`|-O Uses defaults. Equivalent to -O2s<br>-O0 Equivalent to --optimizeLevel 0<br>-O1 Equivalent to --optimizeLevel 1<br>-O2 Equivalent to --optimizeLevel 2<br>-O2s Equivalent to --optimizeLevel 2 but with --shrinkLevel 1<br>-O2z Equivalent to --optimizeLevel 2 but with --shrinkLevel 2<br>-O3 Equivalent to --optimizeLevel 3<br>-Oz Equivalent to -O but with --shrinkLevel 2 and same as -O2z<br>-O3s Equivalent to -O3 with --shrinkLevel 1<br>-O3z Equivalent to -O3 with --shrinkLevel 2|
102+
|**`optimizeLevel`**|`{Int}`|``| How much to focus on optimizing code. [0-3]|
103+
|**`shrinkLevel`**|`{Int}`|``|How much to focus on shrinking code size. [0-2, s=1, z=2]|
104+
|**`validate`**|`{Boolean}`|`false`|Validates the module using Binaryen. Exits if invalid.|
105+
|**`sourceMap`**|`{Boolean}`|`false`|Enables source map generation. Optionally takes the URL used to reference the source map from the binary file.|
106+
|**`debug`**|`{Boolean}`|`false`|Enables debug information in emitted binaries.|
107+
|**`noTreeShaking`**|`{Boolean}`|`false`|Disables compiler-level tree-shaking, compiling everything.|
108+
|**`noAssert`**|`{Boolean}`|`false`|Replaces assertions with just their value without trapping.|
109+
|**`noEmit`**|`{Boolean}`|`false`|Performs compilation as usual but does not emit code.|
110+
|**`importMemory`**|`{Boolean}`|`false`|Imports the memory instance provided by the embedder.|
111+
|**`memoryBase`**|`{Int}`|`0`|Sets the start offset of compiler-generated static memory.|
112+
|**`importTable`**|`{Boolean}`|`0`|Imports the function table instance provided by the embedder.|
113+
|**`noLib`**|`{Boolean`|`false`|Does not include the shipped standard library.|
114+
|**`lib`**|`{String}`|`0`|Adds one or multiple paths to custom library components and uses exports of all top-level files at this path as globals.|
115+
|**`use`**|`{String}`|``|Aliases a global object under another name, e.g., to switch the default 'Math' implementation used: --use Math=JSMath|
116+
|**`trapMode`**|`{String}`|``|Sets the trap mode to use. <br>allow Allow trapping operations. This is the default.<br> clamp Replace trapping operations with clamping semantics. <br>js Replace trapping operations with JS semantics.|
117+
|**`runPasses`**|`{String}`|``|Specifies additional Binaryen passes to run after other <br> optimizations, if any. See: Binaryen/src/passes/pass.cpp|
118+
|**`enable`**|`{String}`|``|Enables additional (experimental) WebAssembly features.<br>sign-extension Enables sign-extension operations<br>mutable-global Enables mutable global imports and exports<br>bulk-memory Enables fast bulk memory operations|
119+
|**`transform`**|`{String}`|``|Specifies the path to a custom transform to 'require'.|
120+
|**`measure`**|`{Int}`|`0`|Prints measuring information on I/O and compile times.|
121+
|~~`binaryFile`~~|`{String}`|``|Specifies the binary output file (.wasm).|
122+
|~~`textFile`~~|`{String}`|``|Specifies the text output file (.wat).|
123+
|~~`asmjsFile`~~|`{String}`|``|Specifies the asm.js output file (.js).|
124+
|~~`idlFile`~~|`{String}`|``|Specifies the WebIDL output file (.webidl).|
125+
|~~`tsdFile`~~|`{String}`|``|Specifies the TypeScript definition output file (.d.ts).|
126+
|~~`noColors`~~|`{Int}`|`0`|Disables terminal colors.|
99127
### `{name}`
100128
101129
You can configure a custom filename template for your file using the query parameter `name`. For instance, to copy a file from your `context` directory into the output directory retaining the full directory structure, you might use

src/index.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import schema from "./options.bytes.json";
77
import schema4file from "./options.file.json";
88
import ts from "typescript";
99
import validateOptions from "schema-utils";
10-
10+
import optionUtils from "./optionsUtils";
1111
let wasmFooterPath = __dirname + "/wasmFooter.js";
1212
let wasmFooter = fs.readFileSync(wasmFooterPath, "utf-8");
1313

@@ -97,15 +97,18 @@ export default function loader(source) {
9797
path.parse(this.resourcePath).name + ".wasm"
9898
);
9999
mkDirsSync(buildTempPath);
100+
let params = [
101+
path.relative(process.cwd(), this.resourcePath),
102+
"-o",
103+
path.relative(process.cwd(), targetPath)
104+
// ,
105+
// "--optimize",
106+
// "--validate",
107+
// "--sourceMap"
108+
];
109+
optionUtils(params,options);
100110
asc.main(
101-
[
102-
path.relative(process.cwd(), this.resourcePath),
103-
"-o",
104-
path.relative(process.cwd(), targetPath),
105-
"--optimize",
106-
"--validate",
107-
"--sourceMap"
108-
],
111+
params,
109112
function(err) {
110113
if (err) throw err;
111114
var distStates = fs.statSync(targetPath);

src/options.bytes.json

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,95 @@
1212
},
1313
"encoding": {
1414
"type": "string"
15-
}
15+
},
16+
"optimize": {
17+
"type": "string",
18+
"alias": "O"
19+
},
20+
"optimizeLevel": {
21+
"description": "How much to focus on optimizing code. [0-3]",
22+
"type": "number"
23+
},
24+
"shrinkLevel": {
25+
"description": "How much to focus on shrinking code size. [0-2, s=1, z=2]",
26+
"type": "number"
27+
},
28+
"validate": {
29+
"description": "Validates the module using Binaryen. Exits if invalid.",
30+
"type": "boolean",
31+
"alias": "c",
32+
"default": false
33+
},
34+
"sourceMap": {
35+
36+
"type": "string"
37+
},
38+
"debug": {
39+
"description": "Enables debug information in emitted binaries.",
40+
"type": "boolean",
41+
"default": false
42+
},
43+
"noTreeShaking": {
44+
"description": "Disables compiler-level tree-shaking, compiling everything.",
45+
"type": "boolean",
46+
"default": false
47+
},
48+
"noAssert": {
49+
"description": "Replaces assertions with just their value without trapping.",
50+
"type": "boolean",
51+
"default": false
52+
},
53+
"noEmit": {
54+
"description": "Performs compilation as usual but does not emit code.",
55+
"type": "boolean",
56+
"default": false
57+
},
58+
"importMemory": {
59+
"description": "Imports the memory instance provided by the embedder.",
60+
"type": "boolean",
61+
"default": false
62+
},
63+
"memoryBase": {
64+
"description": "Sets the start offset of compiler-generated static memory.",
65+
"type": "number",
66+
"default": 0
67+
},
68+
"importTable": {
69+
"description": "Imports the function table instance provided by the embedder.",
70+
"type": "boolean",
71+
"default": false
72+
},
73+
"noLib": {
74+
"description": "Does not include the shipped standard library.",
75+
"type": "boolean",
76+
"default": false
77+
},
78+
"lib": {
79+
"type": "string"
80+
},
81+
"use": {
82+
"type": "string",
83+
"alias": "u"
84+
},
85+
"trapMode": {
86+
"type": "string",
87+
"default": "allow"
88+
},
89+
"runPasses": {
90+
"type": "string"
91+
},
92+
"enable": {
93+
"type": "string"
94+
},
95+
"transform": {
96+
"description": "Specifies the path to a custom transform to 'require'.",
97+
"type": "string"
98+
},
99+
"measure": {
100+
"description": "Prints measuring information on I/O and compile times.",
101+
"type": "boolean",
102+
"default": false
103+
}
16104
},
17105
"additionalProperties": true
18106
}

src/options.json

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
{
2+
"optimize": {
3+
"description": [
4+
"Optimizes the module. Also has the usual shorthands:",
5+
"",
6+
" -O Uses defaults. Equivalent to -O2s",
7+
" -O0 Equivalent to --optimizeLevel 0",
8+
" -O1 Equivalent to --optimizeLevel 1",
9+
" -O2 Equivalent to --optimizeLevel 2",
10+
" -O3 Equivalent to --optimizeLevel 3",
11+
" -Oz Equivalent to -O but with --shrinkLevel 2",
12+
" -O3s Equivalent to -O3 with --shrinkLevel 1 etc.",
13+
""
14+
],
15+
"type": "string",
16+
"alias": "O"
17+
},
18+
"optimizeLevel": {
19+
"description": "How much to focus on optimizing code. [0-3]",
20+
"type": "number"
21+
},
22+
"shrinkLevel": {
23+
"description": "How much to focus on shrinking code size. [0-2, s=1, z=2]",
24+
"type": "number"
25+
},
26+
"validate": {
27+
"description": "Validates the module using Binaryen. Exits if invalid.",
28+
"type": "bool",
29+
"alias": "c",
30+
"default": false
31+
},
32+
"sourceMap": {
33+
"description": [
34+
"Enables source map generation. Optionally takes the URL",
35+
"used to reference the source map from the binary file."
36+
],
37+
"type": "string"
38+
},
39+
"debug": {
40+
"description": "Enables debug information in emitted binaries.",
41+
"type": "bool",
42+
"default": false
43+
},
44+
"noTreeShaking": {
45+
"description": "Disables compiler-level tree-shaking, compiling everything.",
46+
"type": "bool",
47+
"default": false
48+
},
49+
"noAssert": {
50+
"description": "Replaces assertions with just their value without trapping.",
51+
"type": "bool",
52+
"default": false
53+
},
54+
"noEmit": {
55+
"description": "Performs compilation as usual but does not emit code.",
56+
"type": "bool",
57+
"default": false
58+
},
59+
"importMemory": {
60+
"description": "Imports the memory instance provided by the embedder.",
61+
"type": "bool",
62+
"default": false
63+
},
64+
"memoryBase": {
65+
"description": "Sets the start offset of compiler-generated static memory.",
66+
"type": "number",
67+
"default": 0
68+
},
69+
"importTable": {
70+
"description": "Imports the function table instance provided by the embedder.",
71+
"type": "bool",
72+
"default": false
73+
},
74+
"noLib": {
75+
"description": "Does not include the shipped standard library.",
76+
"type": "bool",
77+
"default": false
78+
},
79+
"lib": {
80+
"description": [
81+
"Adds one or multiple paths to custom library components and",
82+
"uses exports of all top-level files at this path as globals."
83+
],
84+
"type": "string"
85+
},
86+
"use": {
87+
"description": [
88+
"Aliases a global object under another name, e.g., to switch",
89+
"the default 'Math' implementation used: --use Math=JSMath"
90+
],
91+
"type": "string",
92+
"alias": "u"
93+
},
94+
"trapMode": {
95+
"description": [
96+
"Sets the trap mode to use.",
97+
"",
98+
" allow Allow trapping operations. This is the default.",
99+
" clamp Replace trapping operations with clamping semantics.",
100+
" js Replace trapping operations with JS semantics.",
101+
""
102+
],
103+
"type": "string",
104+
"default": "allow"
105+
},
106+
"runPasses": {
107+
"description": [
108+
"Specifies additional Binaryen passes to run after other",
109+
"optimizations, if any. See: Binaryen/src/passes/pass.cpp"
110+
],
111+
"type": "string"
112+
},
113+
"enable": {
114+
"description": [
115+
"Enables additional (experimental) WebAssembly features.",
116+
"",
117+
" sign-extension Enables sign-extension operations",
118+
" mutable-global Enables mutable global imports and exports",
119+
""
120+
],
121+
"type": "string"
122+
},
123+
"transform": {
124+
"description": "Specifies the path to a custom transform to 'require'.",
125+
"type": "string"
126+
},
127+
"measure": {
128+
"description": "Prints measuring information on I/O and compile times.",
129+
"type": "bool",
130+
"default": false
131+
}
132+
}

src/optionsUtils.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import schema from "./options.bytes.json";
2+
function isEmpty(obj){
3+
if(typeof obj == "undefined" || obj == null || obj == ""){
4+
return true;
5+
}else{
6+
return false;
7+
}
8+
}
9+
export default function(params,options){
10+
for(var o in options){
11+
let so = schema[o];
12+
let oo = options[o];
13+
if(!so){
14+
continue;
15+
}
16+
if(so.type=="bool"){
17+
if(oo==true){
18+
params.push("--"+o);
19+
}
20+
}
21+
if(so.type=="string"){
22+
if(isEmpty(oo)){
23+
continue;
24+
}
25+
params.push("--"+o);
26+
params.push(oo);
27+
}
28+
if(so.type=="number"){
29+
if(oo==0){
30+
continue;
31+
}
32+
params.push("--"+o);
33+
params.push(oo);
34+
}
35+
}
36+
}

test/__snapshots__/loader.test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ exports[`Loader Defaults 1`] = `
66
var p = new Promise(function(resolve){
77
var compatibleModule;
88
if (typeof WebAssembly !== 'undefined') {
9-
var buffer = new ArrayBuffer(447);
9+
var buffer = new ArrayBuffer(444);
1010
var uint8 = new Uint8Array(buffer);
11-
uint8.set([0,97,115,109,1,0,0,0,1,9,2,96,2,127,127,0,96,0,0,3,4,3,0,1,1,4,4,1,112,0,1,5,3,1,0,0,6,16,3,127,1,65,0,11,127,1,65,0,11,127,1,65,0,11,7,32,4,6,109,101,109,111,114,121,2,0,5,116,97,98,108,101,1,0,4,105,110,105,116,0,0,4,115,116,101,112,0,1,9,7,1,0,65,0,11,1,2,10,186,2,3,17,0,32,0,36,0,32,1,36,1,35,0,35,1,108,36,2,11,161,2,1,8,127,35,1,65,1,107,33,6,35,0,65,1,107,33,7,3,64,2,64,32,0,35,1,79,13,0,32,0,65,1,107,32,6,32,0,27,33,4,65,0,32,0,65,1,106,32,0,32,6,70,27,33,5,65,0,33,1,3,64,2,64,32,1,35,0,79,13,0,65,0,32,1,65,1,106,32,1,32,7,70,27,34,2,32,5,35,0,108,106,45,0,0,32,5,35,0,108,32,1,106,45,0,0,32,1,65,1,107,32,7,32,1,27,34,3,32,5,35,0,108,106,45,0,0,32,0,35,0,108,32,2,106,45,0,0,32,0,35,0,108,32,3,106,45,0,0,32,4,35,0,108,32,2,106,45,0,0,32,4,35,0,108,32,3,106,45,0,0,32,4,35,0,108,32,1,106,45,0,0,106,106,106,106,106,106,106,33,2,32,0,35,0,108,32,1,106,45,0,0,4,64,32,2,65,2,72,34,3,4,127,32,3,5,32,2,65,3,74,11,4,64,35,2,32,0,35,0,108,106,32,1,106,65,0,58,0,0,11,5,32,2,65,3,70,4,64,35,2,32,0,35,0,108,106,32,1,106,65,1,58,0,0,11,11,32,1,65,1,106,33,1,12,1,11,11,32,0,65,1,106,33,0,12,1,11,11,11,3,0,1,11,0,31,16,115,111,117,114,99,101,77,97,112,112,105,110,103,85,82,76,13,115,116,101,112,46,119,97,115,109,46,109,97,112]);
11+
uint8.set([0,97,115,109,1,0,0,0,1,9,2,96,2,127,127,0,96,0,0,3,4,3,0,1,1,4,4,1,112,0,1,5,3,1,0,0,6,21,4,127,1,65,0,11,127,1,65,0,11,127,1,65,0,11,127,0,65,8,11,7,32,4,6,109,101,109,111,114,121,2,0,5,116,97,98,108,101,1,0,4,105,110,105,116,0,0,4,115,116,101,112,0,1,9,7,1,0,65,0,11,1,2,10,211,2,3,17,0,32,0,36,0,32,1,36,1,35,0,35,1,108,36,2,11,187,2,1,10,127,35,1,65,1,107,33,0,35,0,65,1,107,33,1,2,64,65,0,33,2,3,64,32,2,35,1,73,69,13,1,2,64,32,0,32,2,65,1,107,32,2,65,0,70,27,33,3,65,0,32,2,65,1,106,32,2,32,0,70,27,33,4,2,64,65,0,33,5,3,64,32,5,35,0,73,69,13,1,2,64,32,1,32,5,65,1,107,32,5,65,0,70,27,33,6,65,0,32,5,65,1,106,32,5,32,1,70,27,33,7,32,3,35,0,108,32,6,106,45,0,0,32,3,35,0,108,32,5,106,45,0,0,106,32,3,35,0,108,32,7,106,45,0,0,106,32,2,35,0,108,32,6,106,45,0,0,106,32,2,35,0,108,32,7,106,45,0,0,106,32,4,35,0,108,32,6,106,45,0,0,106,32,4,35,0,108,32,5,106,45,0,0,106,32,4,35,0,108,32,7,106,45,0,0,106,33,8,32,2,35,0,108,32,5,106,45,0,0,4,64,32,8,65,2,72,34,9,4,127,32,9,5,32,8,65,3,74,11,4,64,35,2,32,2,35,0,108,106,32,5,106,65,0,58,0,0,11,5,32,8,65,3,70,4,64,35,2,32,2,35,0,108,106,32,5,106,65,1,58,0,0,11,11,11,32,5,65,1,106,33,5,12,0,0,11,0,11,11,32,2,65,1,106,33,2,12,0,0,11,0,11,11,2,0,11]);
1212
var WebAssemblyModule = function(deps) {
1313
var defaultDeps = {
1414
'global': { },

test/fixtures/temp/assembly/step.wasm

-3 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)