|
1 |
| - |
| 1 | +assemblyscript-typescript-loader |
| 2 | +================= |
2 | 3 |
|
3 | 4 | A webpack loader for compiles typescript with [AssemblyScript](https://github.com/AssemblyScript/assemblyscript) and bundles it as wasm or btyes string,it as well with [Marauder](https://github.com/SinaMFE/webpack-marauder) System
|
| 5 | + |
| 6 | + |
| 7 | +<h2 align="center">Install</h2> |
| 8 | + |
| 9 | +```bash |
| 10 | +npm i assemblyscript-typescript-loader --save |
| 11 | +``` |
| 12 | + |
| 13 | +<h2 align="center"><a href="#">Usage</a></h2> |
| 14 | + |
| 15 | +**webpack.config.js** |
| 16 | +```js |
| 17 | +module.exports = { |
| 18 | + module: { |
| 19 | + rules: [ |
| 20 | + { |
| 21 | + test: /\.ts?$/, |
| 22 | + loader: 'assemblyscript-typescript-loader', |
| 23 | + include:/assemblyscript/,//to avoid a conflict with other ts file who use 'ts-load',so you can division them with prop 'include' |
| 24 | + options: { |
| 25 | + limit: 1000, |
| 26 | + name: `static/assembly/[name].[hash:8].wasm` |
| 27 | + } |
| 28 | + } |
| 29 | + ] |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +``` |
| 35 | +You can import ts or asc when coding, and this loader can transform it into wasm; |
| 36 | + |
| 37 | +**assemblyscript/moduleEntry.ts** |
| 38 | + |
| 39 | +```ts |
| 40 | + |
| 41 | +var w: u32, // width |
| 42 | + h: u32, // height |
| 43 | + s: u32; // total size |
| 44 | + |
| 45 | +/** Initializes width and height. */ |
| 46 | +export function init(w_: u32, h_: u32): void { |
| 47 | + w = w_; |
| 48 | + h = h_; |
| 49 | + s = w * h; |
| 50 | +} |
| 51 | + |
| 52 | +/** Performs one step. */ |
| 53 | +export function step(): void { |
| 54 | + var hm1 = h - 1, |
| 55 | + wm1 = w - 1; |
| 56 | + for (var y: u32 = 0; y < h; ++y) { |
| 57 | + var ym1 = select<u32>(hm1, y - 1, y == 0), |
| 58 | + yp1 = select<u32>(0, y + 1, y == hm1); |
| 59 | + for (var x: u32 = 0; x < w; ++x) { |
| 60 | + var xm1 = select<u32>(wm1, x - 1, x == 0), |
| 61 | + xp1 = select<u32>(0, x + 1, x == wm1); |
| 62 | + var n = ( |
| 63 | + load<u8>(ym1 * w + xm1) + load<u8>(ym1 * w + x) + load<u8>(ym1 * w + xp1) + |
| 64 | + load<u8>(y * w + xm1) + load<u8>(y * w + xp1) + |
| 65 | + load<u8>(yp1 * w + xm1) + load<u8>(yp1 * w + x) + load<u8>(yp1 * w + xp1) |
| 66 | + ); |
| 67 | + if (load<u8>(y * w + x)) { |
| 68 | + if (n < 2 || n > 3) |
| 69 | + store<u8>(s + y * w + x, 0); |
| 70 | + } else if (n == 3) |
| 71 | + store<u8>(s + y * w + x, 1); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +``` |
| 77 | +**file.js** |
| 78 | +```js |
| 79 | +import asmPromise from "./assemblyscript/moduleEntry.ts"; |
| 80 | +asmPromise.then(function(asmModule){ |
| 81 | + // here you can use the wasm.exports |
| 82 | + asmModule.step(); |
| 83 | +}) |
| 84 | +``` |
| 85 | + |
| 86 | +<h2 align="center">Options</h2> |
| 87 | + |
| 88 | +|Name|Type|Default|Description| |
| 89 | +|:--:|:--:|:-----:|:----------| |
| 90 | +|**`name`**|`{String\|Function}`|`[hash].[ext]`|Configure a custom filename template for your file| |
| 91 | +|**`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; |
| 92 | +|**`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| |
| 93 | +|**`outputPath`**|`{String\|Function}`|`'undefined'`|Configure a custom `output` path for your file| |
| 94 | + |
| 95 | +### `name` |
| 96 | + |
| 97 | +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 |
| 98 | + |
| 99 | +#### `{String}` |
| 100 | + |
| 101 | +**webpack.config.js** |
| 102 | +```js |
| 103 | +{ |
| 104 | + loader: 'assemblyscript-typescript-loader', |
| 105 | + options: { |
| 106 | + name: '[path][name].wasm' |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +#### `{Function}` |
| 112 | + |
| 113 | +**webpack.config.js** |
| 114 | +```js |
| 115 | +{ |
| 116 | + loader: 'assemblyscript-typescript-loader', |
| 117 | + options: { |
| 118 | + name (file) { |
| 119 | + if (env === 'development') { |
| 120 | + return '[path][name].wasm' |
| 121 | + } |
| 122 | + return '[hash].wasm' |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | +``` |
| 127 | +#### `placeholders` |
| 128 | + |
| 129 | +|Name|Type|Default|Description| |
| 130 | +|:--:|:--:|:-----:|:----------| |
| 131 | +|**`[ext]`**|`{String}`|`file.extname`|The extension of the resource| |
| 132 | +|**`[name]`**|`{String}`|`file.basename`|The basename of the resource| |
| 133 | +|**`[path]`**|`{String}`|`file.dirname`|The path of the resource relative to the `context`| |
| 134 | +|**`[hash]`**|`{String}`|`md5`|The hash of the content, hashes below for more info| |
| 135 | +|**`[N]`**|`{String}`|``|The `n-th` match obtained from matching the current file name against the `regExp`| |
| 136 | + |
| 137 | +#### `hashes` |
| 138 | + |
| 139 | +`[<hashType>:hash:<digestType>:<length>]` optionally you can configure |
| 140 | + |
| 141 | +|Name|Type|Default|Description| |
| 142 | +|:--:|:--:|:-----:|:----------| |
| 143 | +|**`hashType`**|`{String}`|`md5`|`sha1`, `md5`, `sha256`, `sha512`| |
| 144 | +|**`digestType`**|`{String}`|`hex`|`hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`| |
| 145 | +|**`length`**|`{Number}`|`9999`|The length in chars| |
| 146 | + |
| 147 | +By default, the path and name you specify will output the file in that same directory and will also use that same URL path to access the file. |
| 148 | + |
| 149 | + |
| 150 | +### `publicPath` |
| 151 | + |
| 152 | +**webpack.config.js** |
| 153 | +```js |
| 154 | +{ |
| 155 | + loader: 'assemblyscript-typescript-loader', |
| 156 | + options: { |
| 157 | + name: '[path][name].wasm', |
| 158 | + publicPath: 'assembly/' |
| 159 | + } |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +### `outputPath` |
| 164 | + |
| 165 | +**webpack.config.js** |
| 166 | +```js |
| 167 | +{ |
| 168 | + loader: 'assemblyscript-typescript-loader', |
| 169 | + options: { |
| 170 | + name: '[path][name].wasm', |
| 171 | + outputPath: 'assembly/' |
| 172 | + } |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +### `useRelativePath` |
| 177 | + |
| 178 | +`useRelativePath` should be `true` if you wish to generate a relative URL to the for each file context. |
| 179 | + |
| 180 | +```js |
| 181 | +{ |
| 182 | + loader: 'assemblyscript-typescript-loader', |
| 183 | + options: { |
| 184 | + useRelativePath: process.env.NODE_ENV === "production" |
| 185 | + } |
| 186 | +} |
| 187 | +``` |
0 commit comments