-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Expand file tree
/
Copy pathwasm-esm.ts
More file actions
80 lines (71 loc) · 2.8 KB
/
wasm-esm.ts
File metadata and controls
80 lines (71 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { readFile, writeFile } from 'node:fs/promises';
import assert from 'node:assert/strict';
import { ng } from '../../utils/process';
import { prependToFile, replaceInFile } from '../../utils/fs';
import { updateJsonFile, useSha } from '../../utils/project';
import { installWorkspacePackages } from '../../utils/packages';
import { executeBrowserTest } from '../../utils/puppeteer';
/**
* Compiled and base64 encoded WASM file for the following WAT:
* ```
* (module
* (import "./values" "getValue" (func $getvalue (result i32)))
* (export "multiply" (func $multiply))
* (export "subtract1" (func $subtract))
* (func $multiply (param i32 i32) (result i32)
* local.get 0
* local.get 1
* i32.mul
* )
* (func $subtract (param i32) (result i32)
* call $getvalue
* local.get 0
* i32.sub
* )
* )
* ```
*/
const importWasmBase64 =
'AGFzbQEAAAABEANgAAF/YAJ/fwF/YAF/AX8CFQEILi92YWx1ZXMIZ2V0VmFsdWUAAAMDAgECBxgCCG11bHRpcGx5AAEJc3VidHJhY3QxAAIKEQIHACAAIAFsCwcAEAAgAGsLAC8EbmFtZQEfAwAIZ2V0dmFsdWUBCG11bHRpcGx5AghzdWJ0cmFjdAIHAwAAAQACAA==';
const importWasmBytes = Buffer.from(importWasmBase64, 'base64');
export default async function () {
// Add WASM file to project
await writeFile('src/app/multiply.wasm', importWasmBytes);
await writeFile(
'src/app/multiply.wasm.d.ts',
'export declare function multiply(a: number, b: number): number; export declare function subtract1(a: number): number;',
);
// Add requested WASM import file
await writeFile('src/app/values.js', 'export function getValue() { return 100; }');
// Use WASM file in project
await prependToFile(
'src/app/app.ts',
`
import { multiply, subtract1 } from './multiply.wasm';
`,
);
await replaceInFile('src/app/app.ts', "'test-project'", 'multiply(4, 5) + subtract1(88)');
// Remove Zone.js from polyfills and make zoneless
await updateJsonFile('angular.json', (json) => {
// Remove bundle budgets to avoid a build error due to the expected increased output size
// of a JIT production build.
json.projects['test-project'].architect.build.options.polyfills = [];
});
await ng('build');
// Update E2E test to check for WASM execution
await executeBrowserTest({ expectedTitleText: 'Hello, 32' });
// Setup prerendering and build to test Node.js functionality
await ng('add', '@angular/ssr', '--skip-confirmation', '--skip-install');
await useSha();
await installWorkspacePackages();
await ng('build', '--configuration', 'development', '--prerender');
const content = await readFile('dist/test-project/browser/index.html', 'utf-8');
assert.match(content, /Hello, 32/);
}