From 426b10e25655e7c829971eb877325c7c5fd21a2a Mon Sep 17 00:00:00 2001 From: Martin Jinda Date: Tue, 26 Nov 2019 10:42:17 +0100 Subject: [PATCH 1/6] added proxy setting --- src/DI/WebpackExtension.php | 5 ++++- src/DevServer.php | 10 ++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/DI/WebpackExtension.php b/src/DI/WebpackExtension.php index 1aea59d..e18d526 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, + 'proxy' => 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']['proxy'] ?? '', $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']['proxy'] ?? '', $config['devServer']['timeout'] ?? 0.1, new Client() ); diff --git a/src/DevServer.php b/src/DevServer.php index 1b95770..a70fa5c 100644 --- a/src/DevServer.php +++ b/src/DevServer.php @@ -26,6 +26,11 @@ class DevServer */ private $url; + /** + * @var string + */ + private $proxy; + /** * @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 $proxy, float $timeout, ClientInterface $httpClient) { $this->enabled = $enabled; $this->url = $url; + $this->proxy = $proxy; $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->proxy ? $this->proxy : $this->url; } From 8f654beaec13d8b143d74d0c5ba8546485c60c39 Mon Sep 17 00:00:00 2001 From: Martin Jinda Date: Wed, 27 Nov 2019 15:27:16 +0100 Subject: [PATCH 2/6] add proxy to tests --- tests/WebpackNetteAdapter/DevServerTest.phpt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/WebpackNetteAdapter/DevServerTest.phpt b/tests/WebpackNetteAdapter/DevServerTest.phpt index e01a943..6f76ed7 100644 --- a/tests/WebpackNetteAdapter/DevServerTest.phpt +++ b/tests/WebpackNetteAdapter/DevServerTest.phpt @@ -38,7 +38,7 @@ 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', '', 0.1, $this->httpClient); Assert::true($devServer->isEnabled()); $this->httpClient->shouldReceive('request') @@ -50,7 +50,7 @@ class DevServerTest extends TestCase public function testUnavailable(): void { - $devServer = new DevServer(TRUE, 'http://localhost:3000', 0.5, $this->httpClient); + $devServer = new DevServer(TRUE, 'http://localhost:3000', '', 0.5, $this->httpClient); Assert::true($devServer->isEnabled()); $this->httpClient->shouldReceive('request') @@ -62,7 +62,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', '', 0.1, $this->httpClient); Assert::false($devServer->isEnabled()); Assert::false($devServer->isAvailable()); } From 34d6de1b0a2833a1616572635cf4e8ec6c209148 Mon Sep 17 00:00:00 2001 From: Martin Jinda Date: Mon, 16 Dec 2019 14:27:39 +0100 Subject: [PATCH 3/6] test() if proxy is set should be return in getUrl --- tests/WebpackNetteAdapter/DevServerTest.phpt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/WebpackNetteAdapter/DevServerTest.phpt b/tests/WebpackNetteAdapter/DevServerTest.phpt index 6f76ed7..1c77f90 100644 --- a/tests/WebpackNetteAdapter/DevServerTest.phpt +++ b/tests/WebpackNetteAdapter/DevServerTest.phpt @@ -48,6 +48,20 @@ 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); From e2b5b5f0a2c72d1c7cd560fd7ffe25a880b9c5b1 Mon Sep 17 00:00:00 2001 From: Martin Jinda Date: Mon, 16 Dec 2019 14:29:25 +0100 Subject: [PATCH 4/6] fix() rename proxy to publicUrl + replace if set with null-coalescing operator --- src/DI/WebpackExtension.php | 6 +++--- src/DevServer.php | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/DI/WebpackExtension.php b/src/DI/WebpackExtension.php index e18d526..d25e0fd 100644 --- a/src/DI/WebpackExtension.php +++ b/src/DI/WebpackExtension.php @@ -38,7 +38,7 @@ class WebpackExtension extends CompilerExtension 'devServer' => [ 'enabled' => NULL, 'url' => NULL, - 'proxy' => NULL, + 'publicUrl' => NULL, 'timeout' => 0.1, 'ignoredAssets' => [], ], @@ -97,7 +97,7 @@ public function loadConfiguration(): void ->setFactory(DevServer::class, [ $config['devServer']['enabled'], $config['devServer']['url'] ?? '', - $config['devServer']['proxy'] ?? '', + $config['devServer']['publicUrl'] ?? '', $config['devServer']['timeout'], new Statement(Client::class), ]); @@ -175,7 +175,7 @@ private function setupAssetResolver(array $config): ServiceDefinition $devServerInstance = new DevServer( $config['devServer']['enabled'], $config['devServer']['url'] ?? '', - $config['devServer']['proxy'] ?? '', + $config['devServer']['publicUrl'] ?? '', $config['devServer']['timeout'] ?? 0.1, new Client() ); diff --git a/src/DevServer.php b/src/DevServer.php index a70fa5c..c1281aa 100644 --- a/src/DevServer.php +++ b/src/DevServer.php @@ -29,7 +29,7 @@ class DevServer /** * @var string */ - private $proxy; + private $publicUrl; /** * @var float @@ -42,11 +42,11 @@ class DevServer private $httpClient; - public function __construct(bool $enabled, string $url, string $proxy, float $timeout, ClientInterface $httpClient) + public function __construct(bool $enabled, string $url, string $publicUrl, float $timeout, ClientInterface $httpClient) { $this->enabled = $enabled; $this->url = $url; - $this->proxy = $proxy; + $this->publicUrl = $publicUrl; $this->timeout = $timeout; $this->httpClient = $httpClient; } @@ -54,7 +54,7 @@ public function __construct(bool $enabled, string $url, string $proxy, float $ti public function getUrl(): string { - return $this->proxy ? $this->proxy : $this->url; + return $this->publicUrl ?? $this->url; } From 9c7e777e386aae70fd6fde5e4d065f493a37c0e5 Mon Sep 17 00:00:00 2001 From: Martin Jinda Date: Mon, 16 Dec 2019 14:48:19 +0100 Subject: [PATCH 5/6] updated readme --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) 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 From f292925455e2a44a78a328e53d6a241c933b5bc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Pudil?= Date: Tue, 7 Jan 2020 16:03:52 +0100 Subject: [PATCH 6/6] fix publicUrl nullability --- src/DI/WebpackExtension.php | 2 +- src/DevServer.php | 4 ++-- tests/WebpackNetteAdapter/DevServerTest.phpt | 7 ++++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/DI/WebpackExtension.php b/src/DI/WebpackExtension.php index d25e0fd..23fe79d 100644 --- a/src/DI/WebpackExtension.php +++ b/src/DI/WebpackExtension.php @@ -97,7 +97,7 @@ public function loadConfiguration(): void ->setFactory(DevServer::class, [ $config['devServer']['enabled'], $config['devServer']['url'] ?? '', - $config['devServer']['publicUrl'] ?? '', + $config['devServer']['publicUrl'], $config['devServer']['timeout'], new Statement(Client::class), ]); diff --git a/src/DevServer.php b/src/DevServer.php index c1281aa..493dac4 100644 --- a/src/DevServer.php +++ b/src/DevServer.php @@ -27,7 +27,7 @@ class DevServer private $url; /** - * @var string + * @var ?string */ private $publicUrl; @@ -42,7 +42,7 @@ class DevServer private $httpClient; - public function __construct(bool $enabled, string $url, string $publicUrl, float $timeout, ClientInterface $httpClient) + public function __construct(bool $enabled, string $url, ?string $publicUrl, float $timeout, ClientInterface $httpClient) { $this->enabled = $enabled; $this->url = $url; diff --git a/tests/WebpackNetteAdapter/DevServerTest.phpt b/tests/WebpackNetteAdapter/DevServerTest.phpt index 1c77f90..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]) @@ -64,7 +65,7 @@ class DevServerTest extends TestCase 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') @@ -76,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()); }