[flutter_tool] Make the registrant import relative to the entrypoint (#50302)
This commit is contained in:
@@ -351,13 +351,14 @@ class FlutterWebShellBuilder implements Builder {
|
||||
return;
|
||||
}
|
||||
final AssetId outputId = buildStep.inputId.changeExtension('_web_entrypoint.dart');
|
||||
final String pluginRegistrantPath = _getPluginRegistrantPath(dartEntrypointId.path);
|
||||
if (hasPlugins) {
|
||||
await buildStep.writeAsString(outputId, '''
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
|
||||
|
||||
import 'generated_plugin_registrant.dart';
|
||||
import '$pluginRegistrantPath';
|
||||
import "${path.url.basename(buildStep.inputId.path)}" as entrypoint;
|
||||
|
||||
Future<void> main() async {
|
||||
@@ -384,6 +385,13 @@ Future<void> main() async {
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the relative path to the generated plugin registrant from the app
|
||||
/// app entrypoint.
|
||||
String _getPluginRegistrantPath(String entrypoint) {
|
||||
return path.url.relative('lib/generated_plugin_registrant.dart',
|
||||
from: path.url.dirname(entrypoint));
|
||||
}
|
||||
|
||||
@override
|
||||
Map<String, List<String>> get buildExtensions => const <String, List<String>>{
|
||||
'.dart': <String>['_web_entrypoint.dart'],
|
||||
|
||||
@@ -69,6 +69,21 @@ void main() {
|
||||
verify(mockBuildStep.writeAsString(any,
|
||||
argThat(isNot(contains('registerPlugins(webPluginRegistry)'))))).called(1);
|
||||
});
|
||||
|
||||
test('FlutterWebShellBuilder correctly imports registrant', () async {
|
||||
final AssetId nestedEntrypoint = AssetId('hello_world', 'lib/src/nested_main.dart');
|
||||
when(mockBuildStep.inputId).thenReturn(nestedEntrypoint);
|
||||
|
||||
const FlutterWebShellBuilder builder = FlutterWebShellBuilder(
|
||||
hasPlugins: true,
|
||||
initializePlatform: true,
|
||||
);
|
||||
|
||||
await builder.build(mockBuildStep);
|
||||
|
||||
verify(mockBuildStep.writeAsString(any,
|
||||
argThat(contains("import '../generated_plugin_registrant.dart';")))).called(1);
|
||||
});
|
||||
}
|
||||
|
||||
class MockBuildStep extends Mock implements BuildStep {}
|
||||
|
||||
Reference in New Issue
Block a user