Skip to content

Commit 1b33ba9

Browse files
authored
Support compiling to wasm in build_web_compilers (#3727)
Add `dart2wasm` as a compiler option for `build_web_compilers`.
1 parent 59a8aa6 commit 1b33ba9

26 files changed

+500
-70
lines changed

_test/build.dart2wasm.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
targets:
2+
$default:
3+
builders:
4+
build_web_compilers:entrypoint:
5+
options:
6+
compiler: dart2wasm
7+
dart2wasm_args:
8+
- --enable-asserts
9+
- -O0
10+
release_options:
11+
compiler: dart2wasm
12+
dart2wasm_args:
13+
- --enable-asserts
14+
- -O4
15+
generate_for:
16+
- web/main.dart
17+
- web/sub/main.dart
18+
- test/configurable_uri_test.dart
19+
- test/configurable_uri_test.dart.browser_test.dart
20+
- test/hello_world_test.dart
21+
- test/hello_world_test.dart.browser_test.dart
22+
- test/hello_world_deferred_test.dart
23+
- test/hello_world_deferred_test.dart.browser_test.dart
24+
- test/hello_world_custom_html_test.dart
25+
- test/hello_world_custom_html_test.dart.browser_test.dart
26+
- test/other_test.dart.browser_test.dart
27+
- test/sub-dir/subdir_test.dart
28+
- test/sub-dir/subdir_test.dart.browser_test.dart

_test/dart_test.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ timeout: 16x
33
# package.
44
concurrency: 1
55

6+
tags:
7+
integration:
8+
# This tag is used for integration tests - we don't need special options at the
9+
# moment, but want to avoid warnings from the test runner about using undefined
10+
# targets.
11+
612
override_platforms:
713
chrome:
814
settings:

_test/lib/app.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'dart:html';
5+
import 'package:web/web.dart';
66

77
void startApp({String? text}) {
88
text ??= 'Hello World!';
9-
var component = DivElement()..text = text;
9+
var component = HTMLDivElement()..text = text;
1010
document.body!.append(component);
1111
}

_test/pkgs/provides_builder/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: provides_builder
22
resolution: workspace
33
environment:
4-
sdk: ^3.5.0
4+
sdk: ^3.6.0-165.0.dev
55
publish_to: none
66

77
dependencies:

_test/pubspec.yaml

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
name: _test
22
publish_to: none
3-
resolution: workspace
3+
# This package can't be part of the workspace because it needs to use the local
4+
# build_web_compilers, which also isn't part of the workspace. See the top-
5+
# level pubspec for details.
6+
#resolution: workspace
47

58
environment:
6-
sdk: ^3.5.0
9+
sdk: ^3.6.0-165.0.dev
10+
11+
dependencies:
12+
web: ^1.0.0
713

814
dev_dependencies:
915
analyzer: any
@@ -15,9 +21,29 @@ dev_dependencies:
1521
build_runner_core: any
1622
build_test: any
1723
build_web_compilers: any
24+
dart_flutter_team_lints: ^3.1.0
1825
io: ^1.0.0
1926
path: ^1.8.0
2027
provides_builder:
2128
path: pkgs/provides_builder/
2229
test: ^1.16.0
30+
test_descriptor: ^2.0.1
2331
test_process: ^2.0.0
32+
33+
dependency_overrides:
34+
build:
35+
path: ../build
36+
build_config:
37+
path: ../build_config
38+
build_modules:
39+
path: ../build_modules
40+
build_resolvers:
41+
path: ../build_resolvers
42+
build_runner:
43+
path: ../build_runner
44+
build_runner_core:
45+
path: ../build_runner_core
46+
build_test:
47+
path: ../build_test
48+
build_web_compilers:
49+
path: ../build_web_compilers

_test/test/common/message_export.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
export 'message.dart'
66
if (dart.library.io) 'message_io.dart'
7-
if (dart.library.html) 'message_html.dart';
7+
if (dart.library.html) 'message_html.dart'
8+
if (dart.library.js_interop) 'message_js_without_html.dart';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
String get message => 'Hello World from WebAssembly!';

_test/test/common/utils.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,18 +167,22 @@ Future<TestProcess> _runTests(String executable, List<String> scriptArgs,
167167
List<String>? buildArgs,
168168
List<String>? testArgs}) async {
169169
usePrecompiled ??= true;
170-
testArgs ??= [];
171-
testArgs.addAll(['-p', 'chrome', '-r', 'expanded']);
172170
if (usePrecompiled) {
173171
var args = scriptArgs.toList()
174172
..add('test')
175173
..add('--verbose')
176174
..addAll(buildArgs ?? [])
177175
..add('--')
178-
..addAll(testArgs);
176+
..addAll([
177+
...?testArgs,
178+
'-p',
179+
'chrome',
180+
'-r',
181+
'expanded',
182+
]);
179183
return TestProcess.start(executable, args);
180184
} else {
181-
var args = ['run', 'test', '--pub-serve', '8081', ...testArgs];
185+
var args = ['run', 'test', '--pub-serve', '8081', ...?testArgs];
182186
return TestProcess.start('dart', args);
183187
}
184188
}

_test/test/configurable_uri_test.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import 'package:test/test.dart';
66

77
import 'common/message.dart'
88
if (dart.library.io) 'common/message_io.dart'
9-
if (dart.library.html) 'common/message_html.dart';
9+
if (dart.library.html) 'common/message_html.dart'
10+
if (dart.library.js_interop) 'common/message_js_without_html.dart';
1011

1112
import 'common/message_export.dart' as exported;
1213

@@ -19,7 +20,17 @@ void main() {
1920
test('exports', () {
2021
expect(exported.message, contains('Javascript'));
2122
});
22-
}, testOn: 'browser');
23+
}, testOn: 'js');
24+
25+
group('wasm', () {
26+
test('imports', () {
27+
expect(message, contains('WebAssembly'));
28+
});
29+
30+
test('exports', () {
31+
expect(exported.message, contains('WebAssembly'));
32+
});
33+
}, testOn: 'dart2wasm');
2334

2435
group('vm', () {
2536
test('imports', () {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
@TestOn('vm')
6+
@Tags(['integration'])
7+
library;
8+
9+
import 'dart:async';
10+
11+
import 'package:test/test.dart';
12+
import 'package:test_descriptor/test_descriptor.dart' as d;
13+
14+
import 'common/utils.dart';
15+
16+
// This doesn't actually change anything since we're using precompiled tests,
17+
// but it gets the compiler selector in tests right.
18+
const _testArgs = ['-c', 'dart2wasm'];
19+
20+
void main() {
21+
group('Can run tests using dart2wasm', () {
22+
test('via build.yaml config flag', () async {
23+
await expectTestsPass(
24+
usePrecompiled: true,
25+
buildArgs: [
26+
'--config=dart2wasm',
27+
'--output=${d.sandbox}',
28+
],
29+
testArgs: _testArgs,
30+
);
31+
await _expectWasCompiledWithDart2Wasm();
32+
}, onPlatform: {'windows': const Skip('flaky on windows')});
33+
34+
test('via --define flag', () async {
35+
await expectTestsPass(
36+
usePrecompiled: true,
37+
buildArgs: [
38+
'--define',
39+
'build_web_compilers:entrypoint=compiler=dart2wasm',
40+
'--define',
41+
'build_web_compilers:entrypoint=dart2wasm_args='
42+
'["--enable-asserts", "-E--enable-experimental-ffi"]',
43+
'--output=${d.sandbox}',
44+
],
45+
testArgs: _testArgs,
46+
);
47+
await _expectWasCompiledWithDart2Wasm();
48+
}, onPlatform: {'windows': const Skip('flaky on windows')});
49+
50+
test('via --release mode', () async {
51+
await expectTestsPass(
52+
usePrecompiled: true,
53+
buildArgs: [
54+
'--release',
55+
'--config=dart2wasm',
56+
'--output=${d.sandbox}',
57+
],
58+
testArgs: _testArgs,
59+
);
60+
await _expectWasCompiledWithDart2Wasm();
61+
}, onPlatform: {'windows': const Skip('flaky on windows')});
62+
});
63+
}
64+
65+
Future<void> _expectWasCompiledWithDart2Wasm() async {
66+
await d.dir(
67+
'test',
68+
[
69+
d.file(
70+
'hello_world_deferred_test.dart.browser_test.wasm',
71+
anything,
72+
),
73+
],
74+
).validate();
75+
}

0 commit comments

Comments
 (0)