Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
[web] Update build to use generated JS runtime for Dart2Wasm. (#118359)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshualitt committed Jan 12, 2023
1 parent 947b694 commit 8f365a3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 25 deletions.
35 changes: 15 additions & 20 deletions packages/flutter_tools/lib/src/build_system/targets/web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ class Dart2WasmTarget extends Dart2WebTarget {
@override
List<Source> get outputs => const <Source>[
Source.pattern('{OUTPUT_DIR}/main.dart.wasm'),
Source.pattern('{OUTPUT_DIR}/main.dart.mjs'),
];

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

String get outputFileName => isWasm ? 'main.dart.wasm' : 'main.dart.js';
String get outputFileNameNoSuffix => 'main.dart';
String get outputFileName => '$outputFileNameNoSuffix${isWasm ? '.wasm' : '.js'}';
String get wasmJSRuntimeFileName => '$outputFileNameNoSuffix.mjs';

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

@override
List<Source> get outputs => <Source>[
Source.pattern('{OUTPUT_DIR}/$outputFileName'),
if (isWasm) Source.pattern('{OUTPUT_DIR}/$wasmJSRuntimeFileName'),
];

@override
Expand All @@ -376,20 +381,20 @@ class WebReleaseBundle extends Target {
'web_resources.d',
];

bool shouldCopy(String name) =>
// Do not copy the deps file.
(name.contains(outputFileName) && !name.endsWith('.deps')) ||
(isWasm && name == wasmJSRuntimeFileName);

@override
Future<void> build(Environment environment) async {
for (final File outputFile in environment.buildDir.listSync(recursive: true).whereType<File>()) {
final String basename = globals.fs.path.basename(outputFile.path);
if (!basename.contains(outputFileName)) {
continue;
}
// Do not copy the deps file.
if (basename.endsWith('.deps')) {
continue;
if (shouldCopy(basename)) {
outputFile.copySync(
environment.outputDir.childFile(globals.fs.path.basename(outputFile.path)).path
);
}
outputFile.copySync(
environment.outputDir.childFile(globals.fs.path.basename(outputFile.path)).path
);
}

if (isWasm) {
Expand Down Expand Up @@ -533,16 +538,6 @@ class WebBuiltInAssets extends Target {
}

if (isWasm) {
final String dartSdkPath =
globals.artifacts!.getArtifactPath(Artifact.engineDartSdkPath);
final File dart2wasmRuntime = fileSystem.directory(dartSdkPath)
.childDirectory('bin')
.childFile('dart2wasm_runtime.mjs');
final String targetPath = fileSystem.path.join(
environment.outputDir.path,
'dart2wasm_runtime.mjs');
dart2wasmRuntime.copySync(targetPath);

final File bootstrapFile = environment.outputDir.childFile('main.dart.js');
bootstrapFile.writeAsStringSync(wasm_bootstrap.generateWasmBootstrapFile());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ String generateWasmBootstrapFile() {
let moduleInstance;
try {
const dartModulePromise = WebAssembly.compileStreaming(fetch("main.dart.wasm"));
dart2wasm_runtime = await import('./dart2wasm_runtime.mjs');
dart2wasm_runtime = await import('./main.dart.mjs');
moduleInstance = await dart2wasm_runtime.instantiate(dartModulePromise, {});
} catch (exception) {
console.error(`Failed to fetch and instantiate wasm module: ${exception}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -865,14 +865,11 @@ void main() {
}));

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

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

expect(environment.outputDir.childFile('dart2wasm_runtime.mjs').existsSync(), true);
expect(environment.outputDir.childFile('main.dart.js').existsSync(), true);
expect(environment.outputDir.childDirectory('canvaskit')
.childFile('canvaskit.wasm')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ void main() {
'web_wasm'
));
for (final String filename in const <String>[
'dart2wasm_runtime.mjs',
'flutter.js',
'flutter_service_worker.js',
'index.html',
'main.dart.wasm',
'main.dart.mjs',
'main.dart.js',
]) {
expect(appBuildDir.childFile(filename), exists);
Expand Down

0 comments on commit 8f365a3

Please sign in to comment.