ITADN

Migrate flutter_gpu_shaders and flutter_scene_importer hooks to DataAssets

#106Closedbdero 创建于 2026-05-10
importerbuildupstreamroadmap
B
bderocommented
Long-pending follow-up. Track the cutover from the current "write into the consumer's `build/` directory and reference the path from pubspec `flutter: assets:`" pattern to the proper [`DataAsset`](https://pub.dev/documentation/data_assets/latest/) flow that the `package:hooks` API supports. ## What's there today Both build hooks side-step the hook protocol's asset registration: - `flutter_gpu_shaders/lib/build.dart` writes the compiled shader bundle to `buildInput.packageRoot.resolve('build/shaderbundles/')`. The consumer (`flutter_scene`) lists `build/shaderbundles/base.shaderbundle` in its pubspec `flutter: assets:` block and loads it as `packages/flutter_scene/build/shaderbundles/base.shaderbundle`. There's an explicit `TODO(bdero)` in the source noting the intent to migrate when DataAssets are usable. - `flutter_scene_importer/lib/build_hooks.dart` does the same for `.model` files: writes into `<consumer>/build/models/`, consumer adds `- build/models/` as a pubspec asset directory. Both work, but they bypass the structured "this hook produces these files" contract. flutter_tools never sees the bundles or `.model` files as hook outputs, which is the root cause of the cache-invalidation gap I filed at https://github.com/flutter/flutter/issues/186305. ## What we'd do instead Each hook: ```dart import 'package:data_assets/data_assets.dart'; import 'package:hooks/hooks.dart'; await build(args, (input, output) async { final outFile = input.outputDirectory.resolve('shaders/base.shaderbundle'); // ...compile shader bundle to outFile... output.assets.data.add(DataAsset( package: input.packageName, name: 'shaders/base.shaderbundle', file: outFile, )); }); ``` flutter_tools picks it up via `BuildResult.encodedAssets`, filters to DataAssets, bundles each under `packages/<package>/<name>`, and writes the asset path into the manifest automatically. The consumer's pubspec stops needing the `flutter: assets:` entry. flutter_scene's `_kBaseShaderBundlePath` becomes `packages/flutter_scene/shaders/base.shaderbundle` (no `/build/` segment). ## Why we're not doing this yet `dartDataAssets` is `available` on master but **not** `enabledByDefault`, so consumers would need to run `flutter config --enable-dart-data-assets` once before their app builds. That's an extra setup step we don't want to ask people for, especially since the current path works without any config flag (`nativeAssets` itself is `enabledByDefault: true` everywhere). Doing the cutover before the feature stabilizes also risks re-migration if the Flutter team changes the protocol shape during the unstable phase. ## Trigger Migrate when `dartDataAssets` flips to `enabledByDefault: true` on at least the master channel (matching `nativeAssets`'s status). Watch [`packages/flutter_tools/lib/src/features.dart`](https://github.com/flutter/flutter/blob/master/packages/flutter_tools/lib/src/features.dart) for the change. ## Scope Coordinated release across: - `flutter_gpu_shaders` (the hook): switch to `output.assets.data.add(DataAsset(...))`. Add `data_assets` dependency. Major version bump because consumers must drop their pubspec entry. - `flutter_scene` (consumer of `flutter_gpu_shaders`): drop the `- build/shaderbundles/base.shaderbundle` line from pubspec. Update `_kBaseShaderBundlePath` in `lib/src/shaders.dart` to the new `packages/flutter_scene/shaders/...` path. - `flutter_scene_importer` (its own hook): switch the `.model` writer to `outputDirectory` + `DataAsset` registration. Update `buildModels` API since the output location is no longer caller-controlled. - `examples/flutter_app` (consumer of `flutter_scene_importer`): drop the `- build/models/` pubspec entry. Update any `Node.fromAsset(...)` paths if they currently reference the build path. - `flutter-scene-example`: same updates as the in-tree example. CHANGELOGs everywhere noting the consumer-side migration step (drop the pubspec entry, optionally rename load paths). ## Side benefit Once the migration lands and https://github.com/flutter/flutter/issues/186305 lands upstream, deleting a generated `.model` or `.shaderbundle` from disk will correctly invalidate the build hook cache instead of failing at the asset-bundling step with a misleading "asset not found" error. The current workaround (nuking every `.dart_tool/` and `build/` in the workspace) goes away.
关闭于 2026-06-10 3 条评论