Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add conditional export to support --wasm build #3363

Merged
merged 2 commits into from
Nov 30, 2024

Conversation

koji-1009
Copy link
Contributor

Added if (dart.library.html) to src/web/channel_legacy.dart export.

With this change, src/web/channel_legacy.dart will only be referenced if dart:html is available, and builds with the --wasm option will succeed.


If we write the following migration code from IndexedDB to WasmDB, we need to do two imports, drift/wasm.dart and drift/web.dart.

import 'package:drift/drift.dart';
import 'package:drift/wasm.dart';
import 'package:drift/web.dart';

const _databaseName = 'nice_db';

LazyDatabase getDatabase() => LazyDatabase(() async {
      final result = await WasmDatabase.open(
        databaseName: _databaseName,
        sqlite3Uri: Uri.parse('sqlite3.wasm'),
        driftWorkerUri: Uri.parse('drift_worker.dart.js'),
        initializeDatabase: () async {
          final storage = await DriftWebStorage.indexedDbIfSupported(
            _databaseName,
          );
          await storage.open();

          final blob = await storage.restore();
          await storage.close();

          return blob;
        },
      );

      return result.resolvedExecutor;
    });

In a build targeting the web, the current code work. (flutter run -d chrome)
However, in builds with the --wasm option, code using dart:html is not available. (flutter run -d chrome --wasm)

$ flutter run -d chrome --wasm
Launching lib/main.dart on Chrome in debug mode...
Target dart2wasm failed: ProcessException: Process exited abnormally with exit code 64:
../../../../.pub-cache/hosted/pub.dev/drift-2.22.0/lib/src/web/channel_legacy.dart:1:8: Error: Dart library 'dart:html' is not available on this platform.
import 'dart:html';
       ^
Context: The unavailable library 'dart:html' is imported through these packages:

    main.dart => package:example => package:encrypt_file_loader => package:drift => dart:html

Copy link
Owner

@simolus3 simolus3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I think this is a good way to ease the migration, as we can continue to keep this setup until dart:html is removed for good.

@koji-1009
Copy link
Contributor Author

The code migration support via extentsion on dynamic looks great. I was not aware of that!
Thanks for the review!

@simolus3 simolus3 merged commit 4f55cb7 into simolus3:develop Nov 30, 2024
15 checks passed
@koji-1009 koji-1009 deleted the fix/support_wasm_compile branch November 30, 2024 22:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants