From 49d83f489236073212ce4c7ba8c6c19ec1a1037b Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Wed, 11 Sep 2024 00:02:32 +0200 Subject: [PATCH] Compile with dart2js and dart2wasm (#3737) --- _test/build.both.yaml | 23 ++ _test/test/dart2wasm_integration_test.dart | 23 ++ build_web_compilers/CHANGELOG.md | 6 +- build_web_compilers/README.md | 202 +++++++-- .../lib/src/dart2js_bootstrap.dart | 16 +- .../lib/src/dart2wasm_bootstrap.dart | 49 +-- .../lib/src/dev_compiler_bootstrap.dart | 3 +- .../lib/src/web_entrypoint_builder.dart | 388 ++++++++++++++---- build_web_compilers/pubspec.yaml | 2 +- .../test/dart2js_bootstrap_test.dart | 20 +- .../test/dart2wasm_bootstrap_test.dart | 103 +++++ .../test/dev_compiler_bootstrap_test.dart | 18 +- .../test/entrypoint_options_test.dart | 97 +++++ 13 files changed, 786 insertions(+), 164 deletions(-) create mode 100644 _test/build.both.yaml create mode 100644 build_web_compilers/test/dart2wasm_bootstrap_test.dart create mode 100644 build_web_compilers/test/entrypoint_options_test.dart diff --git a/_test/build.both.yaml b/_test/build.both.yaml new file mode 100644 index 000000000..2a1800020 --- /dev/null +++ b/_test/build.both.yaml @@ -0,0 +1,23 @@ +targets: + $default: + builders: + build_web_compilers:entrypoint: + options: + compilers: + dart2wasm: + dart2js: + loader: .dart.js + generate_for: + - web/main.dart + - web/sub/main.dart + - test/configurable_uri_test.dart + - test/configurable_uri_test.dart.browser_test.dart + - test/hello_world_test.dart + - test/hello_world_test.dart.browser_test.dart + - test/hello_world_deferred_test.dart + - test/hello_world_deferred_test.dart.browser_test.dart + - test/hello_world_custom_html_test.dart + - test/hello_world_custom_html_test.dart.browser_test.dart + - test/other_test.dart.browser_test.dart + - test/sub-dir/subdir_test.dart + - test/sub-dir/subdir_test.dart.browser_test.dart diff --git a/_test/test/dart2wasm_integration_test.dart b/_test/test/dart2wasm_integration_test.dart index 6a1acd093..7a7777978 100644 --- a/_test/test/dart2wasm_integration_test.dart +++ b/_test/test/dart2wasm_integration_test.dart @@ -59,6 +59,29 @@ void main() { ); await _expectWasCompiledWithDart2Wasm(); }, onPlatform: {'windows': const Skip('flaky on windows')}); + + test('when also enabling dart2js', () async { + await expectTestsPass( + usePrecompiled: true, + buildArgs: [ + '--release', + '--config=both', + '--output=${d.sandbox}', + ], + testArgs: _testArgs, + ); + await _expectWasCompiledWithDart2Wasm(); + + await d.dir( + 'test', + [ + d.file( + 'hello_world_deferred_test.dart.browser_test.dart2js.js', + startsWith('// Generated by dart2js'), + ), + ], + ).validate(); + }, onPlatform: {'windows': const Skip('flaky on windows')}); }); } diff --git a/build_web_compilers/CHANGELOG.md b/build_web_compilers/CHANGELOG.md index 94184e358..8430acc28 100644 --- a/build_web_compilers/CHANGELOG.md +++ b/build_web_compilers/CHANGELOG.md @@ -1,8 +1,10 @@ -## 4.0.12-wip +## 4.1.0-wip -- Account for dartdevc snapshot in the Dart SDK changing to an AOT snapshot. - Support compiling to WebAssembly by using `dart2wasm` as a value for the compiler option. +- Support compiling with multiple compilers (e.g. `dart2js` and `dart2wasm`) + with a new `compilers` option. +- Account for dartdevc snapshot in the Dart SDK changing to an AOT snapshot. - Bump the min sdk to 3.6.0-165.0.dev. ## 4.0.11 diff --git a/build_web_compilers/README.md b/build_web_compilers/README.md index 1606e7d1f..d03d6ab24 100644 --- a/build_web_compilers/README.md +++ b/build_web_compilers/README.md @@ -39,14 +39,35 @@ then all you need is the `dev_dependency` listed above. ## Configuration -### Configuring the default compiler - By default, this package uses the [Dart development compiler][] (_dartdevc_, also known as _DDC_) to compile Dart to JavaScript. In release builds (running -the build tool with `--release`, the default compiler is `dart2js`). +the build tool with `--release`), `dart2js` is used as a compiler. + +In addition to compiling to JavaScript, this package also supports compiling to +WebAssembly. Currently, `dart2wasm` needs to be enabled with builder options. +To understand the impact of these options, be aware of differences between +compiling to JavaScript and compiling to WebAssembly: + +1. Dart has two compilers emitting JavaScript: `dart2js` and `dartdevc` (which + supports incremental rebuilds but is typically only used for development). + For both JavaScript compilers, `build_web_compilers` generates a primary + entrypoint script and additional module files or source maps depending on + compiler options. +2. Compiling with `dart2wasm` generates a WebAssembly module (a `.wasm` file) + and a JavaScript module (a `.mjs` file) exporting functions to instantiate + that module. `dart2wasm` alone generates no entrypoint file that could be + added as a `