From 0fc1c6f6d7ece693be365877ca53c1d11736b790 Mon Sep 17 00:00:00 2001 From: Harry Terkelsen Date: Thu, 6 Feb 2020 17:08:02 -0800 Subject: [PATCH] [flutter_tool] Make the registrant import relative to the entrypoint (#50302) --- .../lib/src/build_runner/build_script.dart | 10 +++++++++- .../build_runner/build_script_test.dart | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/flutter_tools/lib/src/build_runner/build_script.dart b/packages/flutter_tools/lib/src/build_runner/build_script.dart index 30dfb37302..8ce12fcff1 100644 --- a/packages/flutter_tools/lib/src/build_runner/build_script.dart +++ b/packages/flutter_tools/lib/src/build_runner/build_script.dart @@ -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 main() async { @@ -384,6 +385,13 @@ Future 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> get buildExtensions => const >{ '.dart': ['_web_entrypoint.dart'], diff --git a/packages/flutter_tools/test/general.shard/build_runner/build_script_test.dart b/packages/flutter_tools/test/general.shard/build_runner/build_script_test.dart index 739d55f7dc..3fbd94d0f7 100644 --- a/packages/flutter_tools/test/general.shard/build_runner/build_script_test.dart +++ b/packages/flutter_tools/test/general.shard/build_runner/build_script_test.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 {}