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

Adapt dart2wasm #2215

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion dio/lib/browser.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export 'src/adapters/browser_adapter.dart' show BrowserHttpClientAdapter;
export 'src/adapters/web_adapters/browser_adapter.dart'
show BrowserHttpClientAdapter;
export 'src/dio/dio_for_browser.dart' show DioForBrowser;
3 changes: 2 additions & 1 deletion dio/lib/src/adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import 'dart:typed_data';
import 'package:meta/meta.dart';

import 'adapters/io_adapter.dart'
if (dart.library.html) 'adapters/browser_adapter.dart' as adapter;
if (dart.library.js_util) 'adapters/web_adapters/web_adapter.dart'

Choose a reason for hiding this comment

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

This should be dart.library.js_interop as mentioned here: https://dart.dev/interop/js-interop/package-web#conditional-imports

and everywhere else in the dio package

as adapter;
import 'headers.dart';
import 'options.dart';
import 'redirect_record.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import 'dart:typed_data';

import 'package:meta/meta.dart';

import '../adapter.dart';
import '../dio_exception.dart';
import '../headers.dart';
import '../options.dart';
import '../utils.dart';
import '../../adapter.dart';
import '../../dio_exception.dart';
import '../../headers.dart';
import '../../options.dart';
import '../../utils.dart';

HttpClientAdapter createAdapter() => BrowserHttpClientAdapter();
HttpClientAdapter createWebAdapter() => BrowserHttpClientAdapter();

/// The default [HttpClientAdapter] for Web platforms.
class BrowserHttpClientAdapter implements HttpClientAdapter {
Expand Down
32 changes: 32 additions & 0 deletions dio/lib/src/adapters/web_adapters/wasm_adapter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'dart:typed_data';

import 'package:dio/dio.dart';

import '../../dio/dio_for_browser.dart';

final _err = UnsupportedError(
'If you want to build with WASM, Please see package [dio_wasm_adapter].',
);

HttpClientAdapter createWebAdapter() => _WasmHttpClientAdapter();

/// Empty [HttpClientAdapter]'s implements for WASM.
///
/// **Why we need this?** We want to create a [DioForBrowser] for WASM, but
/// we need to use conditionally import to avoid `dart:html` be dependent
/// when compile-time.
class _WasmHttpClientAdapter implements HttpClientAdapter {
@override
void close({bool force = false}) {
throw _err;
}

@override
Future<ResponseBody> fetch(
RequestOptions options,
Stream<Uint8List>? requestStream,
Future<void>? cancelFuture,
) {
throw _err;
}
}
5 changes: 5 additions & 0 deletions dio/lib/src/adapters/web_adapters/web_adapter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:dio/dio.dart';

import 'wasm_adapter.dart' if (dart.library.html) 'browser_adapter.dart';

HttpClientAdapter createAdapter() => createWebAdapter();
2 changes: 1 addition & 1 deletion dio/lib/src/compute/compute.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import 'dart:async';

import 'compute_io.dart' if (dart.library.html) 'compute_web.dart' as _c;
import 'compute_io.dart' if (dart.library.js_util) 'compute_web.dart' as _c;

/// Signature for the callback passed to [compute].
///
Expand Down
2 changes: 1 addition & 1 deletion dio/lib/src/dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:async';
import 'adapter.dart';
import 'cancel_token.dart';
import 'dio/dio_for_native.dart'
if (dart.library.html) 'dio/dio_for_browser.dart';
if (dart.library.js_util) 'dio/dio_for_browser.dart';
import 'dio_mixin.dart';
import 'headers.dart';
import 'options.dart';
Expand Down
4 changes: 2 additions & 2 deletions dio/lib/src/dio/dio_for_browser.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import '../adapters/browser_adapter.dart';
import '../adapters/web_adapters/web_adapter.dart';
import '../cancel_token.dart';
import '../dio.dart';
import '../dio_mixin.dart';
Expand All @@ -15,7 +15,7 @@ class DioForBrowser with DioMixin implements Dio {
/// It's mostly just one Dio instance in your application.
DioForBrowser([BaseOptions? options]) {
this.options = options ?? BaseOptions();
httpClientAdapter = BrowserHttpClientAdapter();
httpClientAdapter = createAdapter();
}

@override
Expand Down
2 changes: 1 addition & 1 deletion dio/lib/src/dio_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import 'headers.dart';
import 'interceptors/imply_content_type.dart';
import 'options.dart';
import 'progress_stream/io_progress_stream.dart'
if (dart.library.html) 'progress_stream/browser_progress_stream.dart';
if (dart.library.js_util) 'progress_stream/browser_progress_stream.dart';
import 'response.dart';
import 'response/response_stream_handler.dart';
import 'transformer.dart';
Expand Down
2 changes: 1 addition & 1 deletion dio/lib/src/multipart_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'dart:convert';
import 'package:http_parser/http_parser.dart';

import 'multipart_file/io_multipart_file.dart'
if (dart.library.html) 'multipart_file/browser_multipart_file.dart';
if (dart.library.js_util) 'multipart_file/browser_multipart_file.dart';
import 'utils.dart';

/// A file to be uploaded as part of a [MultipartRequest]. This doesn't need to
Expand Down
3 changes: 3 additions & 0 deletions dio/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import 'dart:developer' as dev;
import 'options.dart';
import 'parameter.dart';

const kIsWasm = bool.fromEnvironment('dart.library.js_util') &&
!bool.fromEnvironment('dart.library.html');

// See https://github.com/flutter/flutter/pull/112122.
const kIsWeb = bool.hasEnvironment('dart.library.js_util')
? bool.fromEnvironment('dart.library.js_util')
Expand Down
29 changes: 29 additions & 0 deletions plugins/dio_wasm_adapter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
build/
10 changes: 10 additions & 0 deletions plugins/dio_wasm_adapter/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: "5dcb86f68f239346676ceb1ed1ea385bd215fba1"
channel: "stable"

project_type: package
3 changes: 3 additions & 0 deletions plugins/dio_wasm_adapter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.0.1

* TODO: Describe initial release.
1 change: 1 addition & 0 deletions plugins/dio_wasm_adapter/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO: Add your license here.
17 changes: 17 additions & 0 deletions plugins/dio_wasm_adapter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# dio_wasm_adapter

## Who it applies to
If you want to build web with WASM or just want to use adapter based on `package:web`.

## Get started

### Install

Add the `dio_wasm_adapter` package to your
[pubspec dependencies](https://pub.dev/packages/dio_wasm_adapter/install).

### Example

```dart
final dioClient = createDio();
```
1 change: 1 addition & 0 deletions plugins/dio_wasm_adapter/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: ../../analysis_options.yaml
43 changes: 43 additions & 0 deletions plugins/dio_wasm_adapter/example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
**/ios/Flutter/.last_build_id
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.pub-cache/
.pub/
/build/

# Symbolication related
app.*.symbols

# Obfuscation related
app.*.map.json

# Android Studio will place build artifacts here
/android/app/debug
/android/app/profile
/android/app/release
36 changes: 36 additions & 0 deletions plugins/dio_wasm_adapter/example/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: "5dcb86f68f239346676ceb1ed1ea385bd215fba1"
channel: "stable"

project_type: app

# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
base_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
- platform: android
create_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
base_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
- platform: ios
create_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
base_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
- platform: web
create_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
base_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1

# User provided section

# List of Local paths (relative to this file) that should be
# ignored by the migrate tool.
#
# Files that are not part of the templates will be ignored by default.
unmanaged_files:
- 'lib/main.dart'
- 'ios/Runner.xcodeproj/project.pbxproj'
16 changes: 16 additions & 0 deletions plugins/dio_wasm_adapter/example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# example

A new Flutter project.

## Getting Started

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

- [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab)
- [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook)

For help getting started with Flutter development, view the
[online documentation](https://docs.flutter.dev/), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
1 change: 1 addition & 0 deletions plugins/dio_wasm_adapter/example/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: ../analysis_options.yaml
13 changes: 13 additions & 0 deletions plugins/dio_wasm_adapter/example/android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
gradle-wrapper.jar
/.gradle
/captures/
/gradlew
/gradlew.bat
/local.properties
GeneratedPluginRegistrant.java

# Remember to never publicly share your keystore.
# See https://flutter.dev/docs/deployment/android#reference-the-keystore-from-the-app
key.properties
**/*.keystore
**/*.jks
58 changes: 58 additions & 0 deletions plugins/dio_wasm_adapter/example/android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
plugins {
id "com.android.application"
id "kotlin-android"
// The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
id "dev.flutter.flutter-gradle-plugin"
}

def localProperties = new Properties()
def localPropertiesFile = rootProject.file("local.properties")
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader("UTF-8") { reader ->
localProperties.load(reader)
}
}

def flutterVersionCode = localProperties.getProperty("flutter.versionCode")
if (flutterVersionCode == null) {
flutterVersionCode = "1"
}

def flutterVersionName = localProperties.getProperty("flutter.versionName")
if (flutterVersionName == null) {
flutterVersionName = "1.0"
}

android {
namespace = "com.example.example"
compileSdk = flutter.compileSdkVersion
ndkVersion = flutter.ndkVersion

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId = "com.example.example"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdk = flutter.minSdkVersion
targetSdk = flutter.targetSdkVersion
versionCode = flutterVersionCode.toInteger()
versionName = flutterVersionName
}

buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig = signingConfigs.debug
}
}
}

flutter {
source = "../.."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<!-- The INTERNET permission is required for development. Specifically,
the Flutter tool needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
Loading