diff --git a/README.md b/README.md index 7dbf89f..4de32a4 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,17 @@ webpack: - main.css ``` +#### Public URL (e.g. Docker usage) + +Dev-server might have different URLs for different access points. For example, when running in Docker Compose setup, the Nette application accesses it via the internal Docker network, while you access it in the browser via the exposed port. For this, you can set up a different `publicUrl`. + +```yaml +webpack: + devServer: + url: http://webpack-dev-server:3000 # URL over internal Docker network + publicUrl: http://localhost:3030 # exposed port from the dev-server container +``` + ### Asset resolvers and manifest file diff --git a/src/DI/WebpackExtension.php b/src/DI/WebpackExtension.php index 1aea59d..23fe79d 100644 --- a/src/DI/WebpackExtension.php +++ b/src/DI/WebpackExtension.php @@ -38,7 +38,8 @@ class WebpackExtension extends CompilerExtension 'devServer' => [ 'enabled' => NULL, 'url' => NULL, - 'timeout' => 0.1, + 'publicUrl' => NULL, + 'timeout' => 0.1, 'ignoredAssets' => [], ], 'build' => [ @@ -96,6 +97,7 @@ public function loadConfiguration(): void ->setFactory(DevServer::class, [ $config['devServer']['enabled'], $config['devServer']['url'] ?? '', + $config['devServer']['publicUrl'], $config['devServer']['timeout'], new Statement(Client::class), ]); @@ -173,6 +175,7 @@ private function setupAssetResolver(array $config): ServiceDefinition $devServerInstance = new DevServer( $config['devServer']['enabled'], $config['devServer']['url'] ?? '', + $config['devServer']['publicUrl'] ?? '', $config['devServer']['timeout'] ?? 0.1, new Client() ); diff --git a/src/DevServer.php b/src/DevServer.php index 1b95770..493dac4 100644 --- a/src/DevServer.php +++ b/src/DevServer.php @@ -26,6 +26,11 @@ class DevServer */ private $url; + /** + * @var ?string + */ + private $publicUrl; + /** * @var float */ @@ -37,10 +42,11 @@ class DevServer private $httpClient; - public function __construct(bool $enabled, string $url, float $timeout, ClientInterface $httpClient) + public function __construct(bool $enabled, string $url, ?string $publicUrl, float $timeout, ClientInterface $httpClient) { $this->enabled = $enabled; $this->url = $url; + $this->publicUrl = $publicUrl; $this->timeout = $timeout; $this->httpClient = $httpClient; } @@ -48,7 +54,7 @@ public function __construct(bool $enabled, string $url, float $timeout, ClientIn public function getUrl(): string { - return $this->url; + return $this->publicUrl ?? $this->url; } diff --git a/tests/WebpackNetteAdapter/DevServerTest.phpt b/tests/WebpackNetteAdapter/DevServerTest.phpt index e01a943..a0bb059 100644 --- a/tests/WebpackNetteAdapter/DevServerTest.phpt +++ b/tests/WebpackNetteAdapter/DevServerTest.phpt @@ -38,8 +38,9 @@ class DevServerTest extends TestCase public function testDevServer(): void { - $devServer = new DevServer(TRUE, 'http://localhost:3000', 0.1, $this->httpClient); + $devServer = new DevServer(TRUE, 'http://localhost:3000', NULL, 0.1, $this->httpClient); Assert::true($devServer->isEnabled()); + Assert::same($devServer->getUrl(), 'http://localhost:3000'); $this->httpClient->shouldReceive('request') ->with('GET', 'http://localhost:3000', ['http_errors' => FALSE, 'verify' => FALSE, 'timeout' => 0.1]) @@ -48,9 +49,23 @@ class DevServerTest extends TestCase } + public function testPublicUrl(): void + { + $devServer = new DevServer(TRUE, 'http://localhost:3000', 'http://localhost:3030', 0.1, $this->httpClient); + Assert::true($devServer->isEnabled()); + Assert::same($devServer->getUrl(), 'http://localhost:3030'); + + $this->httpClient->shouldReceive('request') + ->with('GET', 'http://localhost:3000', ['http_errors' => FALSE, 'verify' => FALSE, 'timeout' => 0.1]) + ->andReturn(new Response(404)); + + Assert::true($devServer->isAvailable()); + } + + public function testUnavailable(): void { - $devServer = new DevServer(TRUE, 'http://localhost:3000', 0.5, $this->httpClient); + $devServer = new DevServer(TRUE, 'http://localhost:3000', NULL, 0.5, $this->httpClient); Assert::true($devServer->isEnabled()); $this->httpClient->shouldReceive('request') @@ -62,7 +77,7 @@ class DevServerTest extends TestCase public function testDisabled(): void { - $devServer = new DevServer(FALSE, 'http://localhost:3000', 0.1, $this->httpClient); + $devServer = new DevServer(FALSE, 'http://localhost:3000', NULL, 0.1, $this->httpClient); Assert::false($devServer->isEnabled()); Assert::false($devServer->isAvailable()); }