Skip to content

Commit

Permalink
Merge branch 'proxy-setting' (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiripudil committed Jan 11, 2020
2 parents 85cc31c + f292925 commit 7f4fa1d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion src/DI/WebpackExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class WebpackExtension extends CompilerExtension
'devServer' => [
'enabled' => NULL,
'url' => NULL,
'timeout' => 0.1,
'publicUrl' => NULL,
'timeout' => 0.1,
'ignoredAssets' => [],
],
'build' => [
Expand Down Expand Up @@ -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),
]);
Expand Down Expand Up @@ -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()
);
Expand Down
10 changes: 8 additions & 2 deletions src/DevServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class DevServer
*/
private $url;

/**
* @var ?string
*/
private $publicUrl;

/**
* @var float
*/
Expand All @@ -37,18 +42,19 @@ 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;
}


public function getUrl(): string
{
return $this->url;
return $this->publicUrl ?? $this->url;
}


Expand Down
21 changes: 18 additions & 3 deletions tests/WebpackNetteAdapter/DevServerTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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')
Expand All @@ -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());
}
Expand Down

0 comments on commit 7f4fa1d

Please sign in to comment.