Skip to content

Commit ee03604

Browse files
authored
Add a flag to allow the default Client to be tree shaken away. (#868)
1 parent 2039fb3 commit ee03604

File tree

8 files changed

+125
-5
lines changed

8 files changed

+125
-5
lines changed

.github/workflows/dart.yml

Lines changed: 73 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkgs/http/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## 0.13.6-dev
22

33
* `BrowserClient` throws an exception if `send` is called after `close`.
4+
* If `no_default_http_client=true` is set in the environment then disk usage
5+
is reduced in some circumstances.
46
* Require Dart 2.19
57

68
## 0.13.5

pkgs/http/lib/src/browser_client.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ import 'streamed_response.dart';
1515
/// Create a [BrowserClient].
1616
///
1717
/// Used from conditional imports, matches the definition in `client_stub.dart`.
18-
BaseClient createClient() => BrowserClient();
18+
BaseClient createClient() {
19+
if (const bool.fromEnvironment('no_default_http_client')) {
20+
throw StateError('no_default_http_client was defined but runWithClient '
21+
'was not used to configure a Client implementation.');
22+
}
23+
return BrowserClient();
24+
}
1925

2026
/// A `dart:html`-based HTTP client that runs in the browser and is backed by
2127
/// XMLHttpRequests.

pkgs/http/lib/src/client.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,17 @@ Client? get zoneClient {
210210
/// runWithClient(() => runApp(const MyApp()), clientFactory);
211211
/// }
212212
/// ```
213+
///
214+
/// If [runWithClient] is used and the environment defines
215+
/// `no_default_http_client=true` then generated binaries may be smaller e.g.
216+
/// ```shell
217+
/// $ flutter build appbundle --dart-define=no_default_http_client=true ...
218+
/// $ dart compile exe --define=no_default_http_client=true ...
219+
/// ```
220+
///
221+
/// If `no_default_http_client=true` is set then any call to the [Client]
222+
/// factory (i.e. `Client()`) outside of the [Zone] created by [runWithClient]
223+
/// will throw [StateError].
213224
R runWithClient<R>(R Function() body, Client Function() clientFactory,
214225
{ZoneSpecification? zoneSpecification}) =>
215226
runZoned(body,

pkgs/http/lib/src/io_client.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ import 'io_streamed_response.dart';
1212
/// Create an [IOClient].
1313
///
1414
/// Used from conditional imports, matches the definition in `client_stub.dart`.
15-
BaseClient createClient() => IOClient();
15+
BaseClient createClient() {
16+
if (const bool.fromEnvironment('no_default_http_client')) {
17+
throw StateError('no_default_http_client was defined but runWithClient '
18+
'was not used to configure a Client implementation.');
19+
}
20+
return IOClient();
21+
}
1622

1723
/// Exception thrown when the underlying [HttpClient] throws a
1824
/// [SocketException].

pkgs/http/mono_pkg.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ stages:
1515
- test: --platform chrome
1616
os:
1717
- linux
18+
- command: dart run --define=no_default_http_client=true test/no_default_http_client_test.dart
19+
os:
20+
- linux
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:http/http.dart' as http;
6+
import 'package:test/test.dart';
7+
8+
/// Tests that no [http.Client] is provided by default when run with
9+
/// `--define=no_default_http_client=true`.
10+
void main() {
11+
test('Client()', () {
12+
if (const bool.fromEnvironment('no_default_http_client')) {
13+
expect(http.Client.new, throwsA(isA<StateError>()));
14+
} else {
15+
expect(http.Client(), isA<http.Client>());
16+
}
17+
});
18+
}

tool/ci.sh

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)