[flutter_tools] update compilation to use package config (#54467)
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:package_config/package_config.dart';
|
||||
|
||||
import '../../artifacts.dart';
|
||||
import '../../base/build.dart';
|
||||
import '../../base/file_system.dart';
|
||||
@@ -196,7 +198,7 @@ class KernelSnapshot extends Target {
|
||||
}
|
||||
final BuildMode buildMode = getBuildModeForName(environment.defines[kBuildMode]);
|
||||
final String targetFile = environment.defines[kTargetFile] ?? globals.fs.path.join('lib', 'main.dart');
|
||||
final String packagesPath = environment.projectDir.childFile('.packages').path;
|
||||
final File packagesFile = environment.projectDir.childFile('.packages');
|
||||
final String targetFileAbsolute = globals.fs.file(targetFile).absolute.path;
|
||||
// everything besides 'false' is considered to be enabled.
|
||||
final bool trackWidgetCreation = environment.defines[kTrackWidgetCreation] != 'false';
|
||||
@@ -229,6 +231,17 @@ class KernelSnapshot extends Target {
|
||||
forceLinkPlatform = false;
|
||||
}
|
||||
|
||||
final PackageConfig packageConfig = await loadPackageConfigUri(
|
||||
packagesFile.absolute.uri,
|
||||
loader: (Uri uri) {
|
||||
final File file = globals.fs.file(uri);
|
||||
if (!file.existsSync()) {
|
||||
return null;
|
||||
}
|
||||
return file.readAsBytes();
|
||||
}
|
||||
);
|
||||
|
||||
final CompilerOutput output = await compiler.compile(
|
||||
sdkRoot: globals.artifacts.getArtifactPath(
|
||||
Artifact.flutterPatchedSdkPath,
|
||||
@@ -240,7 +253,7 @@ class KernelSnapshot extends Target {
|
||||
trackWidgetCreation: trackWidgetCreation && buildMode == BuildMode.debug,
|
||||
targetModel: targetModel,
|
||||
outputFilePath: environment.buildDir.childFile('app.dill').path,
|
||||
packagesPath: packagesPath,
|
||||
packagesPath: packagesFile.path,
|
||||
linkPlatformKernelIn: forceLinkPlatform || buildMode.isPrecompiled,
|
||||
mainPath: targetFileAbsolute,
|
||||
depFilePath: environment.buildDir.childFile('kernel_snapshot.d').path,
|
||||
@@ -248,6 +261,7 @@ class KernelSnapshot extends Target {
|
||||
fileSystemRoots: fileSystemRoots,
|
||||
fileSystemScheme: fileSystemScheme,
|
||||
dartDefines: parseDartDefines(environment),
|
||||
packageConfig: packageConfig,
|
||||
);
|
||||
if (output == null || output.errorCount != 0) {
|
||||
throw Exception('Errors during snapshot creation: $output');
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:crypto/crypto.dart';
|
||||
import 'package:package_config/package_config.dart';
|
||||
|
||||
import '../../artifacts.dart';
|
||||
import '../../base/file_system.dart';
|
||||
import '../../base/io.dart';
|
||||
import '../../build_info.dart';
|
||||
import '../../compile.dart';
|
||||
import '../../dart/package_map.dart';
|
||||
import '../../globals.dart' as globals;
|
||||
import '../build_system.dart';
|
||||
@@ -56,35 +56,39 @@ class WebEntrypointTarget extends Target {
|
||||
final String targetFile = environment.defines[kTargetFile];
|
||||
final bool shouldInitializePlatform = environment.defines[kInitializePlatform] == 'true';
|
||||
final bool hasPlugins = environment.defines[kHasWebPlugins] == 'true';
|
||||
final String importPath = globals.fs.path.absolute(targetFile);
|
||||
final Uri importUri = environment.fileSystem.file(targetFile).absolute.uri;
|
||||
final PackageConfig packageConfig = await loadPackageConfigUri(
|
||||
environment.projectDir.childFile('.packages').absolute.uri,
|
||||
loader: (Uri uri) {
|
||||
final File file = environment.fileSystem.file(uri);
|
||||
if (!file.existsSync()) {
|
||||
return null;
|
||||
}
|
||||
return file.readAsBytes();
|
||||
}
|
||||
);
|
||||
|
||||
// Use the package uri mapper to find the correct package-scheme import path
|
||||
// Use the PackageConfig to find the correct package-scheme import path
|
||||
// for the user application. If the application has a mix of package-scheme
|
||||
// and relative imports for a library, then importing the entrypoint as a
|
||||
// file-scheme will cause said library to be recognized as two distinct
|
||||
// libraries. This can cause surprising behavior as types from that library
|
||||
// will be considered distinct from each other.
|
||||
final PackageUriMapper packageUriMapper = PackageUriMapper(
|
||||
importPath,
|
||||
PackageMap.globalPackagesPath,
|
||||
null,
|
||||
null,
|
||||
);
|
||||
|
||||
// By construction, this will only be null if the .packages file does not
|
||||
// have an entry for the user's application or if the main file is
|
||||
// outside of the lib/ directory.
|
||||
final String mainImport = packageUriMapper.map(importPath)?.toString()
|
||||
?? globals.fs.file(importPath).absolute.uri.toString();
|
||||
final String mainImport = packageConfig.toPackageUri(importUri)?.toString()
|
||||
?? importUri.toString();
|
||||
|
||||
String contents;
|
||||
if (hasPlugins) {
|
||||
final String generatedPath = environment.projectDir
|
||||
final Uri generatedUri = environment.projectDir
|
||||
.childDirectory('lib')
|
||||
.childFile('generated_plugin_registrant.dart')
|
||||
.absolute.path;
|
||||
final String generatedImport = packageUriMapper.map(generatedPath)?.toString()
|
||||
?? globals.fs.file(generatedPath).absolute.uri.toString();
|
||||
.absolute
|
||||
.uri;
|
||||
final String generatedImport = packageConfig.toPackageUri(generatedUri)?.toString()
|
||||
?? generatedUri.toString();
|
||||
contents = '''
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user