Skip to content

Commit

Permalink
try implement hash concat for wasm pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
Slixe committed Nov 2, 2024
1 parent acf0d32 commit 974f0da
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
14 changes: 11 additions & 3 deletions frb_dart/lib/src/cli/build_web/executor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ Future<void> executeBuildWeb(BuildWebArgs args) async {
final rustCrateName =
await _getRustCreateName(rustCrateDir: args.rustCrateDir);

await _executeWasmPack(args, rustCrateName: rustCrateName);
final contentHash =
await _getRustContentHash(rustCrateDir: args.rustCrateDir);

await _executeWasmPack(args,
rustCrateName: rustCrateName, contentHash: contentHash);

if (args.enableWasmBindgen) {
await _executeWasmBindgen(args, rustCrateName: rustCrateName);
Expand Down Expand Up @@ -149,8 +153,12 @@ Future<String> _getRustCreateName({required String rustCrateDir}) async {
return rustCrateName;
}

Future<int> _getRustContentHash({required String rustCrateDir}) async {
throw UnimplementedError();
}

Future<void> _executeWasmPack(BuildWebArgs args,
{required String rustCrateName}) async {
{required String rustCrateName, required int contentHash}) async {
await runCommand('wasm-pack', [
'build',
'-t',
Expand All @@ -159,7 +167,7 @@ Future<void> _executeWasmPack(BuildWebArgs args,
args.outputWasm,
'--no-typescript',
'--out-name',
rustCrateName,
'${rustCrateName}${contentHash.toRadixString(16)}',
if (!args.release) '--dev',
args.rustCrateDir,
'--',
Expand Down
3 changes: 2 additions & 1 deletion frb_dart/lib/src/loader/_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import 'dart:async';
import 'dart:io';

import 'package:flutter_rust_bridge/src/loader/_common.dart';
import 'package:flutter_rust_bridge/src/main_components/entrypoint.dart';
import 'package:flutter_rust_bridge/src/platform_types/_io.dart';

/// Load the [ExternalLibrary], with the following cases in mind:
/// 1. When `flutter run`, or when a real app is bundled.
/// 2. When running Flutter widget tests.
/// 3. When `dart test`, `dart run`, `dart compile exe`, etc.
FutureOr<ExternalLibrary> loadExternalLibrary(
ExternalLibraryLoaderConfig config) async {
BaseEntrypoint _, ExternalLibraryLoaderConfig config) async {
final ioDirectory = config.ioDirectory;
return loadExternalLibraryRaw(
nativeLibDirWhenNonPackaged:
Expand Down
6 changes: 4 additions & 2 deletions frb_dart/lib/src/loader/_web.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import 'dart:async';

import 'package:flutter_rust_bridge/src/loader/_common.dart';
import 'package:flutter_rust_bridge/src/main_components/entrypoint.dart';
import 'package:flutter_rust_bridge/src/platform_types/_web.dart';
import 'package:flutter_rust_bridge/src/wasm_module/_web.dart';

/// See `loadExternalLibrary` in the counterpart `_io.dart` for doc
FutureOr<ExternalLibrary> loadExternalLibrary(
ExternalLibraryLoaderConfig config) async {
BaseEntrypoint entrypoint, ExternalLibraryLoaderConfig config) async {
var hex = entrypoint.rustContentHash.toRadixString(16);
return loadExternalLibraryRaw(
moduleRoot: '${config.webPrefix}${config.stem}');
moduleRoot: '${config.webPrefix}${config.stem}${hex}');
}

/// Please see `loadExternalLibrary` for details
Expand Down
3 changes: 1 addition & 2 deletions frb_dart/lib/src/main_components/entrypoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,14 @@ abstract class BaseEntrypoint<A extends BaseApi, AI extends BaseApiImpl,
String get codegenVersion;

/// {@macro flutter_rust_bridge.only_for_generated_code}
@protected
int get rustContentHash;

/// {@macro flutter_rust_bridge.only_for_generated_code}
@protected
Future<void> executeRustInitializers();

Future<ExternalLibrary> _loadDefaultExternalLibrary() async =>
await loadExternalLibrary(defaultExternalLibraryLoaderConfig);
await loadExternalLibrary(this, defaultExternalLibraryLoaderConfig);

A _createDefaultApi(
BaseHandler handler,
Expand Down

0 comments on commit 974f0da

Please sign in to comment.