Unverified Commit 8f365a3b authored by joshualitt's avatar joshualitt Committed by GitHub

[web] Update build to use generated JS runtime for Dart2Wasm. (#118359)

parent 947b694f
...@@ -334,6 +334,7 @@ class Dart2WasmTarget extends Dart2WebTarget { ...@@ -334,6 +334,7 @@ class Dart2WasmTarget extends Dart2WebTarget {
@override @override
List<Source> get outputs => const <Source>[ List<Source> get outputs => const <Source>[
Source.pattern('{OUTPUT_DIR}/main.dart.wasm'), Source.pattern('{OUTPUT_DIR}/main.dart.wasm'),
Source.pattern('{OUTPUT_DIR}/main.dart.mjs'),
]; ];
// TODO(jacksongardner): override `depfiles` once dart2wasm begins producing // TODO(jacksongardner): override `depfiles` once dart2wasm begins producing
...@@ -348,7 +349,9 @@ class WebReleaseBundle extends Target { ...@@ -348,7 +349,9 @@ class WebReleaseBundle extends Target {
final WebRendererMode webRenderer; final WebRendererMode webRenderer;
final bool isWasm; 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 @override
String get name => 'web_release_bundle'; String get name => 'web_release_bundle';
...@@ -362,11 +365,13 @@ class WebReleaseBundle extends Target { ...@@ -362,11 +365,13 @@ class WebReleaseBundle extends Target {
List<Source> get inputs => <Source>[ List<Source> get inputs => <Source>[
Source.pattern('{BUILD_DIR}/$outputFileName'), Source.pattern('{BUILD_DIR}/$outputFileName'),
const Source.pattern('{PROJECT_DIR}/pubspec.yaml'), const Source.pattern('{PROJECT_DIR}/pubspec.yaml'),
if (isWasm) Source.pattern('{BUILD_DIR}/$wasmJSRuntimeFileName'),
]; ];
@override @override
List<Source> get outputs => <Source>[ List<Source> get outputs => <Source>[
Source.pattern('{OUTPUT_DIR}/$outputFileName'), Source.pattern('{OUTPUT_DIR}/$outputFileName'),
if (isWasm) Source.pattern('{OUTPUT_DIR}/$wasmJSRuntimeFileName'),
]; ];
@override @override
...@@ -376,20 +381,20 @@ class WebReleaseBundle extends Target { ...@@ -376,20 +381,20 @@ class WebReleaseBundle extends Target {
'web_resources.d', 'web_resources.d',
]; ];
bool shouldCopy(String name) =>
// Do not copy the deps file.
(name.contains(outputFileName) && !name.endsWith('.deps')) ||
(isWasm && name == wasmJSRuntimeFileName);
@override @override
Future<void> build(Environment environment) async { Future<void> build(Environment environment) async {
for (final File outputFile in environment.buildDir.listSync(recursive: true).whereType<File>()) { for (final File outputFile in environment.buildDir.listSync(recursive: true).whereType<File>()) {
final String basename = globals.fs.path.basename(outputFile.path); final String basename = globals.fs.path.basename(outputFile.path);
if (!basename.contains(outputFileName)) { if (shouldCopy(basename)) {
continue; outputFile.copySync(
} environment.outputDir.childFile(globals.fs.path.basename(outputFile.path)).path
// Do not copy the deps file. );
if (basename.endsWith('.deps')) {
continue;
} }
outputFile.copySync(
environment.outputDir.childFile(globals.fs.path.basename(outputFile.path)).path
);
} }
if (isWasm) { if (isWasm) {
...@@ -533,16 +538,6 @@ class WebBuiltInAssets extends Target { ...@@ -533,16 +538,6 @@ class WebBuiltInAssets extends Target {
} }
if (isWasm) { 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'); final File bootstrapFile = environment.outputDir.childFile('main.dart.js');
bootstrapFile.writeAsStringSync(wasm_bootstrap.generateWasmBootstrapFile()); bootstrapFile.writeAsStringSync(wasm_bootstrap.generateWasmBootstrapFile());
} }
......
...@@ -13,7 +13,7 @@ String generateWasmBootstrapFile() { ...@@ -13,7 +13,7 @@ String generateWasmBootstrapFile() {
let moduleInstance; let moduleInstance;
try { try {
const dartModulePromise = WebAssembly.compileStreaming(fetch("main.dart.wasm")); 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, {}); moduleInstance = await dart2wasm_runtime.instantiate(dartModulePromise, {});
} catch (exception) { } catch (exception) {
console.error(`Failed to fetch and instantiate wasm module: ${exception}`); console.error(`Failed to fetch and instantiate wasm module: ${exception}`);
......
...@@ -865,14 +865,11 @@ void main() { ...@@ -865,14 +865,11 @@ void main() {
})); }));
test('wasm build copies and generates specific files', () => testbed.run(() async { 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') globals.fs.file('bin/cache/flutter_web_sdk/canvaskit/canvaskit.wasm')
.createSync(recursive: true); .createSync(recursive: true);
await WebBuiltInAssets(globals.fs, globals.cache, true).build(environment); 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.childFile('main.dart.js').existsSync(), true);
expect(environment.outputDir.childDirectory('canvaskit') expect(environment.outputDir.childDirectory('canvaskit')
.childFile('canvaskit.wasm') .childFile('canvaskit.wasm')
......
...@@ -47,11 +47,11 @@ void main() { ...@@ -47,11 +47,11 @@ void main() {
'web_wasm' 'web_wasm'
)); ));
for (final String filename in const <String>[ for (final String filename in const <String>[
'dart2wasm_runtime.mjs',
'flutter.js', 'flutter.js',
'flutter_service_worker.js', 'flutter_service_worker.js',
'index.html', 'index.html',
'main.dart.wasm', 'main.dart.wasm',
'main.dart.mjs',
'main.dart.js', 'main.dart.js',
]) { ]) {
expect(appBuildDir.childFile(filename), exists); expect(appBuildDir.childFile(filename), exists);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment