Skip to content

Commit 7f4fa1d

Browse files
committed
Merge branch 'proxy-setting' (#13)
2 parents 85cc31c + f292925 commit 7f4fa1d

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ webpack:
6464
- main.css
6565
```
6666

67+
#### Public URL (e.g. Docker usage)
68+
69+
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`.
70+
71+
```yaml
72+
webpack:
73+
devServer:
74+
url: http://webpack-dev-server:3000 # URL over internal Docker network
75+
publicUrl: http://localhost:3030 # exposed port from the dev-server container
76+
```
77+
6778

6879
### Asset resolvers and manifest file
6980

src/DI/WebpackExtension.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ class WebpackExtension extends CompilerExtension
3838
'devServer' => [
3939
'enabled' => NULL,
4040
'url' => NULL,
41-
'timeout' => 0.1,
41+
'publicUrl' => NULL,
42+
'timeout' => 0.1,
4243
'ignoredAssets' => [],
4344
],
4445
'build' => [
@@ -96,6 +97,7 @@ public function loadConfiguration(): void
9697
->setFactory(DevServer::class, [
9798
$config['devServer']['enabled'],
9899
$config['devServer']['url'] ?? '',
100+
$config['devServer']['publicUrl'],
99101
$config['devServer']['timeout'],
100102
new Statement(Client::class),
101103
]);
@@ -173,6 +175,7 @@ private function setupAssetResolver(array $config): ServiceDefinition
173175
$devServerInstance = new DevServer(
174176
$config['devServer']['enabled'],
175177
$config['devServer']['url'] ?? '',
178+
$config['devServer']['publicUrl'] ?? '',
176179
$config['devServer']['timeout'] ?? 0.1,
177180
new Client()
178181
);

src/DevServer.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class DevServer
2626
*/
2727
private $url;
2828

29+
/**
30+
* @var ?string
31+
*/
32+
private $publicUrl;
33+
2934
/**
3035
* @var float
3136
*/
@@ -37,18 +42,19 @@ class DevServer
3742
private $httpClient;
3843

3944

40-
public function __construct(bool $enabled, string $url, float $timeout, ClientInterface $httpClient)
45+
public function __construct(bool $enabled, string $url, ?string $publicUrl, float $timeout, ClientInterface $httpClient)
4146
{
4247
$this->enabled = $enabled;
4348
$this->url = $url;
49+
$this->publicUrl = $publicUrl;
4450
$this->timeout = $timeout;
4551
$this->httpClient = $httpClient;
4652
}
4753

4854

4955
public function getUrl(): string
5056
{
51-
return $this->url;
57+
return $this->publicUrl ?? $this->url;
5258
}
5359

5460

tests/WebpackNetteAdapter/DevServerTest.phpt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ class DevServerTest extends TestCase
3838

3939
public function testDevServer(): void
4040
{
41-
$devServer = new DevServer(TRUE, 'http://localhost:3000', 0.1, $this->httpClient);
41+
$devServer = new DevServer(TRUE, 'http://localhost:3000', NULL, 0.1, $this->httpClient);
4242
Assert::true($devServer->isEnabled());
43+
Assert::same($devServer->getUrl(), 'http://localhost:3000');
4344

4445
$this->httpClient->shouldReceive('request')
4546
->with('GET', 'http://localhost:3000', ['http_errors' => FALSE, 'verify' => FALSE, 'timeout' => 0.1])
@@ -48,9 +49,23 @@ class DevServerTest extends TestCase
4849
}
4950

5051

52+
public function testPublicUrl(): void
53+
{
54+
$devServer = new DevServer(TRUE, 'http://localhost:3000', 'http://localhost:3030', 0.1, $this->httpClient);
55+
Assert::true($devServer->isEnabled());
56+
Assert::same($devServer->getUrl(), 'http://localhost:3030');
57+
58+
$this->httpClient->shouldReceive('request')
59+
->with('GET', 'http://localhost:3000', ['http_errors' => FALSE, 'verify' => FALSE, 'timeout' => 0.1])
60+
->andReturn(new Response(404));
61+
62+
Assert::true($devServer->isAvailable());
63+
}
64+
65+
5166
public function testUnavailable(): void
5267
{
53-
$devServer = new DevServer(TRUE, 'http://localhost:3000', 0.5, $this->httpClient);
68+
$devServer = new DevServer(TRUE, 'http://localhost:3000', NULL, 0.5, $this->httpClient);
5469
Assert::true($devServer->isEnabled());
5570

5671
$this->httpClient->shouldReceive('request')
@@ -62,7 +77,7 @@ class DevServerTest extends TestCase
6277

6378
public function testDisabled(): void
6479
{
65-
$devServer = new DevServer(FALSE, 'http://localhost:3000', 0.1, $this->httpClient);
80+
$devServer = new DevServer(FALSE, 'http://localhost:3000', NULL, 0.1, $this->httpClient);
6681
Assert::false($devServer->isEnabled());
6782
Assert::false($devServer->isAvailable());
6883
}

0 commit comments

Comments
 (0)