-
Notifications
You must be signed in to change notification settings - Fork 43
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
[native_assets_builder] Add hook/link.dart
// Tree shaking
#153
Comments
Notes from discussion with @mkustermann: Maybe we should not
Instead, we should consider
Step 2 and 3 would be similar to C++ builds where the build-step is separate from the linking step. The data-flow in 2 would be as meta-data currently works. A reason to make it work similar to a C++ build is that it would be easier to migrate the logic in Bazel, if Dart were to ever move to the Bazel build system. Also g3 requires custom build rules instead of a Dart script, so staying closer to what other build systems do is beneficial there as well. This approach should also cover the use case for We have multiple options for what file this tree-shaking script should be in:
@mosuem Any thoughts about this approach? |
Having a build phase and tree-shake phase does raise the question what the CLI interface should be for the tree-shake phase.
Theoretically, we could do the code tree-shaking also in the tree-shake phase instead of baking it in to the SDK. |
What does But generally, I believe this would correspond to the initial idea of having a I have no thoughts on whether having a second script is better or worse than different calls to |
Dependencies get the "meta data" / "config" from the dependants. E.g. |
Notes from discussion with @mosuem: The interaction between
build.dart meta data output is keyed on target package, and link.dart of the target package gets the meta data.
Data assets output by either build.dart or link.dart should be bundled in Dart and Flutter apps and accessible by https://api.flutter.dev/flutter/services/AssetBundle-class.html. TODO: figure out what the different bundles mean in Flutter. The implementation plan would have multiple stages:
edit: This plan only works when deferred loading is not considered. Deferred loading means something else in dart2js than in Flutter, which makes it hard to abstract over it. |
Sounds good. The deferred loading can be just delegated to the packages own |
If I remember correctly, the shaker would output:
We still need to flash out how that works with the different deferred loading mechanisms in Flutter and dart2js.
Did you give this aspect more thought yet @mosuem? |
Or did we want to depend on "component name"? But also that requires code generation. E.g. a package author does not know what the component name is where their file ends up in when that package is used inside an app that defines a split in components. |
The shaker would output whatever the package needs to load the component. So for package:messages, the
I don't think it is such a large file, it should be basically |
Having |
link.dart
// Tree shaking
link.dart
// Tree shakinghook/link.dart
// Tree shaking
Both of these can be done in a link hook. And the first one of these can be done by the Dart/Flutter SDK if all uses of a certain asset ID are const. @mosuem suggested we should have some kind of enum for
@mkustermann suggested to tie this behavior to what hook the asset is output from:
|
I would even introduce two modes instead of enum Treeshake {
/// Never treeshake by the SDK, only manually in link hooks.
never,
/// Treeshake if `loadBytes` never has a non-const arguments passed
/// and no const argument matches the asset id.
safe,
/// Treeshake even if `loadBytes` has non-const arguments passed, but
/// none of the known const ones matches the asset id.
unsafe;
} When using the third mode, it is entirely possible that assets are treeshaken which are actually used, which would only be noticed at runtime. But in the safe mode, any package in the dependency tree which does a single +1 on the defaults. |
And then we should probably add a warning on the stderr or stdout when building and one of the hooks reports assets with |
The
hook/build.dart
is run before kernel compilation.We should consider running a
hook/link.dart
after kernel compilation. In AOT we would have access to tree-shaking information that would enable us to:Use cases:
For use case 2, we want to standardize the annotations and output format for what is reachable, so that any user can implement tree-shaking for any type of resource they want to bundle (e.g. icons, images, localization, nothing is special.)
Flutter tracking issue:
hook/link.dart
support in flutter_tools flutter/flutter#146263Dart tracking issue:
hook/link.dart
support in dartdev sdk#55369The text was updated successfully, but these errors were encountered: