[web:tools] always use CanvasKit from the cache when building web apps (#93002)

This commit is contained in:
Yegor 2021-11-03 17:48:25 -07:00 committed by GitHub
parent 6cd39cce1d
commit 773789344e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 9 deletions

View File

@ -410,9 +410,10 @@ class WebReleaseBundle extends Target {
/// These assets can be cached forever and are only invalidated when the /// These assets can be cached forever and are only invalidated when the
/// Flutter SDK is upgraded to a new version. /// Flutter SDK is upgraded to a new version.
class WebBuiltInAssets extends Target { class WebBuiltInAssets extends Target {
const WebBuiltInAssets(this.fileSystem); const WebBuiltInAssets(this.fileSystem, this.cache);
final FileSystem fileSystem; final FileSystem fileSystem;
final Cache cache;
@override @override
String get name => 'web_static_assets'; String get name => 'web_static_assets';
@ -431,7 +432,15 @@ class WebBuiltInAssets extends Target {
@override @override
Future<void> build(Environment environment) async { Future<void> build(Environment environment) async {
final Directory flutterWebSdk = globals.artifacts.getHostArtifact(HostArtifact.flutterWebSdk) as Directory; // TODO(yjbanov): https://github.com/flutter/flutter/issues/52588
//
// Update this when we start building CanvasKit from sources. In the
// meantime, get the Web SDK directory from cache rather than through
// Artifacts. The latter is sensitive to `--local-engine`, which changes
// the directory to point to ENGINE/src/out. However, CanvasKit is not yet
// built as part of the engine, but fetched from CIPD, and so it won't be
// found in ENGINE/src/out.
final Directory flutterWebSdk = cache.getWebSdkDirectory();
final Directory canvasKitDirectory = flutterWebSdk.childDirectory('canvaskit'); final Directory canvasKitDirectory = flutterWebSdk.childDirectory('canvaskit');
for (final File file in canvasKitDirectory.listSync(recursive: true).whereType<File>()) { for (final File file in canvasKitDirectory.listSync(recursive: true).whereType<File>()) {
final String relativePath = fileSystem.path.relative(file.path, from: canvasKitDirectory.path); final String relativePath = fileSystem.path.relative(file.path, from: canvasKitDirectory.path);
@ -443,9 +452,10 @@ class WebBuiltInAssets extends Target {
/// Generate a service worker for a web target. /// Generate a service worker for a web target.
class WebServiceWorker extends Target { class WebServiceWorker extends Target {
const WebServiceWorker(this.fileSystem); const WebServiceWorker(this.fileSystem, this.cache);
final FileSystem fileSystem; final FileSystem fileSystem;
final Cache cache;
@override @override
String get name => 'web_service_worker'; String get name => 'web_service_worker';
@ -454,7 +464,7 @@ class WebServiceWorker extends Target {
List<Target> get dependencies => <Target>[ List<Target> get dependencies => <Target>[
const Dart2JSTarget(), const Dart2JSTarget(),
const WebReleaseBundle(), const WebReleaseBundle(),
WebBuiltInAssets(fileSystem), WebBuiltInAssets(fileSystem, cache),
]; ];
@override @override

View File

@ -49,7 +49,7 @@ List<Target> _kDefaultTargets = <Target>[
const ReleaseBundleLinuxAssets(TargetPlatform.linux_x64), const ReleaseBundleLinuxAssets(TargetPlatform.linux_x64),
const ReleaseBundleLinuxAssets(TargetPlatform.linux_arm64), const ReleaseBundleLinuxAssets(TargetPlatform.linux_arm64),
// Web targets // Web targets
WebServiceWorker(globals.fs), WebServiceWorker(globals.fs, globals.cache),
const ReleaseAndroidApplication(), const ReleaseAndroidApplication(),
// This is a one-off rule for bundle and aot compat. // This is a one-off rule for bundle and aot compat.
const CopyFlutterBundle(), const CopyFlutterBundle(),

View File

@ -37,7 +37,7 @@ Future<void> buildWeb(
final Status status = globals.logger.startProgress('Compiling $target for the Web...'); final Status status = globals.logger.startProgress('Compiling $target for the Web...');
final Stopwatch sw = Stopwatch()..start(); final Stopwatch sw = Stopwatch()..start();
try { try {
final BuildResult result = await globals.buildSystem.build(WebServiceWorker(globals.fs), Environment( final BuildResult result = await globals.buildSystem.build(WebServiceWorker(globals.fs, globals.cache), Environment(
projectDir: globals.fs.currentDirectory, projectDir: globals.fs.currentDirectory,
outputDir: outputDirectory, outputDir: outputDirectory,
buildDir: flutterProject.directory buildDir: flutterProject.directory

View File

@ -637,7 +637,7 @@ void main() {
environment.outputDir.childDirectory('a').childFile('a.txt') environment.outputDir.childDirectory('a').childFile('a.txt')
..createSync(recursive: true) ..createSync(recursive: true)
..writeAsStringSync('A'); ..writeAsStringSync('A');
await WebServiceWorker(globals.fs).build(environment); await WebServiceWorker(globals.fs, globals.cache).build(environment);
expect(environment.outputDir.childFile('flutter_service_worker.js'), exists); expect(environment.outputDir.childFile('flutter_service_worker.js'), exists);
// Contains file hash. // Contains file hash.
@ -656,7 +656,7 @@ void main() {
environment.outputDir environment.outputDir
.childFile('index.html') .childFile('index.html')
.createSync(recursive: true); .createSync(recursive: true);
await WebServiceWorker(globals.fs).build(environment); await WebServiceWorker(globals.fs, globals.cache).build(environment);
expect(environment.outputDir.childFile('flutter_service_worker.js'), exists); expect(environment.outputDir.childFile('flutter_service_worker.js'), exists);
// Contains file hash for both `/` and index.html. // Contains file hash for both `/` and index.html.
@ -674,7 +674,7 @@ void main() {
environment.outputDir environment.outputDir
.childFile('main.dart.js.map') .childFile('main.dart.js.map')
.createSync(recursive: true); .createSync(recursive: true);
await WebServiceWorker(globals.fs).build(environment); await WebServiceWorker(globals.fs, globals.cache).build(environment);
// No caching of source maps. // No caching of source maps.
expect(environment.outputDir.childFile('flutter_service_worker.js').readAsStringSync(), expect(environment.outputDir.childFile('flutter_service_worker.js').readAsStringSync(),