Skip to content

Commit ab440bc

Browse files
joshualittesouthren
authored andcommitted
[web] Update build to use generated JS runtime for Dart2Wasm. (flutter#118359)
1 parent 67a91d3 commit ab440bc

File tree

4 files changed

+17
-25
lines changed

4 files changed

+17
-25
lines changed

packages/flutter_tools/lib/src/build_system/targets/web.dart

+15-20
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ class Dart2WasmTarget extends Dart2WebTarget {
334334
@override
335335
List<Source> get outputs => const <Source>[
336336
Source.pattern('{OUTPUT_DIR}/main.dart.wasm'),
337+
Source.pattern('{OUTPUT_DIR}/main.dart.mjs'),
337338
];
338339

339340
// TODO(jacksongardner): override `depfiles` once dart2wasm begins producing
@@ -348,7 +349,9 @@ class WebReleaseBundle extends Target {
348349
final WebRendererMode webRenderer;
349350
final bool isWasm;
350351

351-
String get outputFileName => isWasm ? 'main.dart.wasm' : 'main.dart.js';
352+
String get outputFileNameNoSuffix => 'main.dart';
353+
String get outputFileName => '$outputFileNameNoSuffix${isWasm ? '.wasm' : '.js'}';
354+
String get wasmJSRuntimeFileName => '$outputFileNameNoSuffix.mjs';
352355

353356
@override
354357
String get name => 'web_release_bundle';
@@ -362,11 +365,13 @@ class WebReleaseBundle extends Target {
362365
List<Source> get inputs => <Source>[
363366
Source.pattern('{BUILD_DIR}/$outputFileName'),
364367
const Source.pattern('{PROJECT_DIR}/pubspec.yaml'),
368+
if (isWasm) Source.pattern('{BUILD_DIR}/$wasmJSRuntimeFileName'),
365369
];
366370

367371
@override
368372
List<Source> get outputs => <Source>[
369373
Source.pattern('{OUTPUT_DIR}/$outputFileName'),
374+
if (isWasm) Source.pattern('{OUTPUT_DIR}/$wasmJSRuntimeFileName'),
370375
];
371376

372377
@override
@@ -376,20 +381,20 @@ class WebReleaseBundle extends Target {
376381
'web_resources.d',
377382
];
378383

384+
bool shouldCopy(String name) =>
385+
// Do not copy the deps file.
386+
(name.contains(outputFileName) && !name.endsWith('.deps')) ||
387+
(isWasm && name == wasmJSRuntimeFileName);
388+
379389
@override
380390
Future<void> build(Environment environment) async {
381391
for (final File outputFile in environment.buildDir.listSync(recursive: true).whereType<File>()) {
382392
final String basename = globals.fs.path.basename(outputFile.path);
383-
if (!basename.contains(outputFileName)) {
384-
continue;
385-
}
386-
// Do not copy the deps file.
387-
if (basename.endsWith('.deps')) {
388-
continue;
393+
if (shouldCopy(basename)) {
394+
outputFile.copySync(
395+
environment.outputDir.childFile(globals.fs.path.basename(outputFile.path)).path
396+
);
389397
}
390-
outputFile.copySync(
391-
environment.outputDir.childFile(globals.fs.path.basename(outputFile.path)).path
392-
);
393398
}
394399

395400
if (isWasm) {
@@ -533,16 +538,6 @@ class WebBuiltInAssets extends Target {
533538
}
534539

535540
if (isWasm) {
536-
final String dartSdkPath =
537-
globals.artifacts!.getArtifactPath(Artifact.engineDartSdkPath);
538-
final File dart2wasmRuntime = fileSystem.directory(dartSdkPath)
539-
.childDirectory('bin')
540-
.childFile('dart2wasm_runtime.mjs');
541-
final String targetPath = fileSystem.path.join(
542-
environment.outputDir.path,
543-
'dart2wasm_runtime.mjs');
544-
dart2wasmRuntime.copySync(targetPath);
545-
546541
final File bootstrapFile = environment.outputDir.childFile('main.dart.js');
547542
bootstrapFile.writeAsStringSync(wasm_bootstrap.generateWasmBootstrapFile());
548543
}

packages/flutter_tools/lib/src/web/file_generators/wasm_bootstrap.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ String generateWasmBootstrapFile() {
1313
let moduleInstance;
1414
try {
1515
const dartModulePromise = WebAssembly.compileStreaming(fetch("main.dart.wasm"));
16-
dart2wasm_runtime = await import('./dart2wasm_runtime.mjs');
16+
dart2wasm_runtime = await import('./main.dart.mjs');
1717
moduleInstance = await dart2wasm_runtime.instantiate(dartModulePromise, {});
1818
} catch (exception) {
1919
console.error(`Failed to fetch and instantiate wasm module: ${exception}`);

packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart

-3
Original file line numberDiff line numberDiff line change
@@ -865,14 +865,11 @@ void main() {
865865
}));
866866

867867
test('wasm build copies and generates specific files', () => testbed.run(() async {
868-
globals.fs.file('bin/cache/dart-sdk/bin/dart2wasm_runtime.mjs')
869-
.createSync(recursive: true);
870868
globals.fs.file('bin/cache/flutter_web_sdk/canvaskit/canvaskit.wasm')
871869
.createSync(recursive: true);
872870

873871
await WebBuiltInAssets(globals.fs, globals.cache, true).build(environment);
874872

875-
expect(environment.outputDir.childFile('dart2wasm_runtime.mjs').existsSync(), true);
876873
expect(environment.outputDir.childFile('main.dart.js').existsSync(), true);
877874
expect(environment.outputDir.childDirectory('canvaskit')
878875
.childFile('canvaskit.wasm')

packages/flutter_tools/test/integration.shard/flutter_build_wasm_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ void main() {
4747
'web_wasm'
4848
));
4949
for (final String filename in const <String>[
50-
'dart2wasm_runtime.mjs',
5150
'flutter.js',
5251
'flutter_service_worker.js',
5352
'index.html',
5453
'main.dart.wasm',
54+
'main.dart.mjs',
5555
'main.dart.js',
5656
]) {
5757
expect(appBuildDir.childFile(filename), exists);

0 commit comments

Comments
 (0)