Bundle a local Roboto fallback for no-CDN web builds#184275
Bundle a local Roboto fallback for no-CDN web builds#184275samanzamani wants to merge 1 commit intoflutter:masterfrom
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request introduces a mechanism to bundle a local Roboto font fallback for web builds when the kUseLocalCanvasKitFlag is enabled. The WebReleaseBundle target was modified to verify the presence of the Roboto font in the FontManifest.json and, if absent, copy the font from the engine source to the assets directory. Review feedback recommends refactoring hardcoded engine font paths into constants in both the implementation and test files to enhance maintainability and adhere to readability principles.
| final File sourceRobotoFont = environment.fileSystem.file( | ||
| environment.fileSystem.path.join( | ||
| Cache.flutterRoot!, | ||
| 'engine', | ||
| 'src', | ||
| 'flutter', | ||
| 'txt', | ||
| 'third_party', | ||
| 'fonts', | ||
| 'Roboto-Regular.ttf', | ||
| ), | ||
| ); |
There was a problem hiding this comment.
The path to the Roboto font is constructed from multiple hardcoded string literals. This is brittle and can lead to breakage if the engine's internal directory structure changes. It also impacts readability.
To improve maintainability and align with the style guide's principle to "Optimize for readability", I recommend extracting this path into a private, top-level constant. This provides a single source of truth for the path and makes the code easier to understand and update.
Here's how you could refactor it:
-
Add a constant at the top of the file:
const String _kEngineRobotoFontPath = 'engine/src/flutter/txt/third_party/fonts/Roboto-Regular.ttf';
-
Use this constant in the
_bundleLocalRobotoFallbackmethod:final File sourceRobotoFont = environment.fileSystem.file( environment.fileSystem.path.join( Cache.flutterRoot!, _kEngineRobotoFontPath, ), );
This change will make the dependency on this specific engine file more explicit and easier to manage in the future.
References
- Optimize for readability: Code is read more often than it is written. (link)
| globals.fs | ||
| .file('engine/src/flutter/txt/third_party/fonts/Roboto-Regular.ttf') | ||
| .createSync(recursive: true); |
There was a problem hiding this comment.
Similar to the implementation file, the path to the mock Roboto font is hardcoded here. This makes the test dependent on a magic string that mirrors the implementation.
For better maintainability and readability, you could extract this path into a constant within the test file. This would make the test easier to read and update if the source path changes.
Example:
const String _kEngineRobotoFontPathForTest = 'engine/src/flutter/txt/third_party/fonts/Roboto-Regular.ttf';
// in setUp
globals.fs
.file(_kEngineRobotoFontPathForTest)
.createSync(recursive: true);
Problem
Solution
Tests
00:00 +0 -1: loading test/general.shard/build_system/targets/web_test.dart [E]
Failed to load "test/general.shard/build_system/targets/web_test.dart": Does not exist.
00:00 +0 -1: Some tests failed.
Failing tests:
test/general.shard/build_system/targets/web_test.dart: loading test/general.shard/build_system/targets/web_test.dart