Skip to content

Commit 85cc31c

Browse files
authored
Merge pull request #14 from o2ps/update
2 parents 31750ab + eba1bc7 commit 85cc31c

File tree

14 files changed

+169
-49
lines changed

14 files changed

+169
-49
lines changed

.travis.yml

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ dist: xenial
33
language: php
44

55
php:
6-
- 7.1
76
- 7.2
87
- 7.3
8+
- 7.4
99

1010
env:
1111
- NETTE_VERSION="^2.4"
@@ -17,36 +17,25 @@ before_install:
1717

1818
install:
1919
- composer require "nette/di:${NETTE_VERSION}" --no-update
20-
- composer require "latte/latte:${NETTE_VERSION}" "nette/application:${NETTE_VERSION}" "nette/bootstrap:${NETTE_VERSION}" --dev --no-update
20+
- composer require "nette/application:${NETTE_VERSION}" "nette/bootstrap:${NETTE_VERSION}" --dev --no-update
2121
- travis_retry composer update --prefer-source --no-interaction
2222

2323
script:
2424
- vendor/bin/tester -c tests/php.unix.ini tests/
2525

2626
jobs:
2727
include:
28-
- stage: QA
29-
name: Lint
30-
php: 7.3
31-
env: NETTE_VERSION="^2.4"
32-
before_script:
33-
- travis_retry composer global require "jakub-onderka/php-parallel-lint:^1.0.0"
34-
script:
35-
- $HOME/.composer/vendor/bin/parallel-lint -e php,phpt src/ tests/
36-
3728
- stage: QA
3829
name: Static Analysis
39-
php: 7.3
40-
env: NETTE_VERSION="^2.4"
41-
before_script:
42-
- travis_retry composer global require "phpstan/phpstan-shim:^0.11.0"
30+
php: 7.4
31+
env: NETTE_VERSION="^3.0"
4332
script:
44-
- $HOME/.composer/vendor/bin/phpstan.phar analyze --no-progress --no-interaction -l max src/
33+
- vendor/bin/phpstan.phar analyze --no-progress --no-interaction -l max src/
4534

4635
- stage: QA
4736
name: Code Coverage
48-
php: 7.3
49-
env: NETTE_VERSION="^2.4"
37+
php: 7.4
38+
env: NETTE_VERSION="^3.0"
5039
script:
5140
- vendor/bin/tester -p phpdbg -c tests/php.unix.ini -s --coverage coverage.xml --coverage-src src/ tests/
5241
after_success:

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ WebpackNetteAdapter is a tool that helps integrate your Nette Framework applicat
1414
$ composer require oops/webpack-nette-adapter
1515
```
1616

17-
Oops/WebpackNetteAdapter requires PHP >= 7.1.
17+
Oops/WebpackNetteAdapter requires PHP >= 7.2.
1818

1919

2020
## Usage
@@ -23,7 +23,7 @@ Register the extension in your config file, and configure it. The two `build` op
2323

2424
```yaml
2525
extensions:
26-
webpack: Oops\WebpackNetteAdapter\DI\WebpackExtension(%debugMode%)
26+
webpack: Oops\WebpackNetteAdapter\DI\WebpackExtension(%debugMode%, %consoleMode%)
2727

2828
webpack:
2929
build:
@@ -53,6 +53,17 @@ webpack:
5353
timeout: 0.1 # (seconds) default
5454
```
5555

56+
#### Ignored assets
57+
58+
You can also configure a set of asset names that should be ignored (i.e. resolved to an empty data URI) if the dev-server is available. This can be helpful e.g. if you use [`style-loader`](https://www.npmjs.com/package/style-loader) in development which does not emit any CSS files.
59+
60+
```yaml
61+
webpack:
62+
devServer:
63+
ignoredAssets:
64+
- main.css
65+
```
66+
5667

5768
### Asset resolvers and manifest file
5869

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@
1616
"issues": "https://github.com/o2ps/WebpackNetteAdapter/issues"
1717
},
1818
"require": {
19-
"php": "^7.1",
19+
"php": "^7.2",
2020
"guzzlehttp/guzzle": "^6.3",
2121
"nette/di": "^2.4.10 || ^3.0"
2222
},
2323
"require-dev": {
24-
"latte/latte": "^2.4 || ^3.0",
24+
"latte/latte": "^2.4",
2525
"mockery/mockery": "^1.0",
2626
"nette/application": "^2.4 || ^3.0",
2727
"nette/bootstrap": "^2.4 || ^3.0",
2828
"nette/tester": "^2.0",
29+
"phpstan/phpstan": "^0.12",
2930
"tracy/tracy": "^2.4 || ^3.0"
3031
},
3132
"suggest": {
@@ -42,6 +43,5 @@
4243
"OopsTests\\WebpackNetteAdapter\\": "tests/WebpackNetteAdapter"
4344
}
4445
},
45-
"minimum-stability": "beta",
4646
"prefer-stable": true
4747
}

phpstan.neon

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/AssetLocator.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,53 @@ class AssetLocator
2525
*/
2626
private $assetResolver;
2727

28+
/**
29+
* @var DevServer
30+
*/
31+
private $devServer;
32+
33+
/**
34+
* @var string[]
35+
*/
36+
private $ignoredAssetNames;
2837

38+
39+
/**
40+
* @param string[] $ignoredAssetNames
41+
*/
2942
public function __construct(
3043
BuildDirectoryProvider $directoryProvider,
3144
PublicPathProvider $publicPathProvider,
32-
AssetNameResolverInterface $assetResolver
45+
AssetNameResolverInterface $assetResolver,
46+
DevServer $devServer,
47+
array $ignoredAssetNames
3348
)
3449
{
3550
$this->directoryProvider = $directoryProvider;
3651
$this->publicPathProvider = $publicPathProvider;
3752
$this->assetResolver = $assetResolver;
53+
$this->devServer = $devServer;
54+
$this->ignoredAssetNames = $ignoredAssetNames;
3855
}
3956

4057

4158
public function locateInPublicPath(string $asset): string
4259
{
43-
return $this->publicPathProvider->getPublicPath() . '/' . $this->assetResolver->resolveAssetName($asset);
60+
if ($this->devServer->isAvailable() && \in_array($asset, $this->ignoredAssetNames, TRUE)) {
61+
return 'data:,';
62+
}
63+
64+
return \rtrim($this->publicPathProvider->getPublicPath(), '/') . '/' . \ltrim($this->assetResolver->resolveAssetName($asset), '/');
4465
}
4566

4667

4768
public function locateInBuildDirectory(string $asset): string
4869
{
49-
return $this->directoryProvider->getBuildDirectory() . '/' . $this->assetResolver->resolveAssetName($asset);
70+
if ($this->devServer->isAvailable() && \in_array($asset, $this->ignoredAssetNames, TRUE)) {
71+
return 'data:,';
72+
}
73+
74+
return \rtrim($this->directoryProvider->getBuildDirectory(), '/') . '/' . \ltrim($this->assetResolver->resolveAssetName($asset), '/');
5075
}
5176

5277
}

src/AssetNameResolver/DebuggerAwareAssetNameResolver.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class DebuggerAwareAssetNameResolver implements AssetNameResolverInterface
1414
private $inner;
1515

1616
/**
17-
* @var array[]
17+
* @var array<array{string, string}>
1818
*/
1919
private $resolvedAssets = [];
2020

@@ -33,6 +33,9 @@ public function resolveAssetName(string $asset): string
3333
}
3434

3535

36+
/**
37+
* @return array<array{string, string}>
38+
*/
3639
public function getResolvedAssets(): array
3740
{
3841
return $this->resolvedAssets;

src/AssetNameResolver/ManifestAssetNameResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ final class ManifestAssetNameResolver implements AssetNameResolverInterface
2222
private $loader;
2323

2424
/**
25-
* @var string[]|NULL
25+
* @var array<string, string>|NULL
2626
*/
2727
private $manifestCache;
2828

src/AssetNameResolver/StaticAssetNameResolver.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ final class StaticAssetNameResolver implements AssetNameResolverInterface
99
{
1010

1111
/**
12-
* @var string[]
12+
* @var array<string, string>
1313
*/
1414
private $resolutions;
1515

1616

17+
/**
18+
* @param array<string, string> $resolutions
19+
*/
1720
public function __construct(array $resolutions)
1821
{
1922
$this->resolutions = $resolutions;

src/DI/WebpackExtension.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,23 @@
2323
use Tracy;
2424

2525

26+
/**
27+
* @property array<string, mixed> $config
28+
*/
2629
class WebpackExtension extends CompilerExtension
2730
{
2831

32+
/**
33+
* @var array<string, mixed>
34+
*/
2935
private $defaults = [
3036
'debugger' => NULL,
3137
'macros' => NULL,
3238
'devServer' => [
3339
'enabled' => NULL,
3440
'url' => NULL,
35-
'timeout' => 0.1,
41+
'timeout' => 0.1,
42+
'ignoredAssets' => [],
3643
],
3744
'build' => [
3845
'directory' => NULL,
@@ -45,12 +52,14 @@ class WebpackExtension extends CompilerExtension
4552
];
4653

4754

48-
public function __construct(bool $debugMode)
55+
public function __construct(bool $debugMode, ?bool $consoleMode = NULL)
4956
{
57+
$consoleMode = $consoleMode ?? \PHP_SAPI === 'cli';
58+
5059
$this->defaults['debugger'] = $debugMode;
5160
$this->defaults['macros'] = \interface_exists(ILatteFactory::class);
5261
$this->defaults['devServer']['enabled'] = $debugMode;
53-
$this->defaults['manifest']['optimize'] = ! $debugMode;
62+
$this->defaults['manifest']['optimize'] = ! $debugMode && ( ! $consoleMode || (bool) \getenv('OOPS_WEBPACK_OPTIMIZE_MANIFEST'));
5463
}
5564

5665

@@ -83,15 +92,17 @@ public function loadConfiguration(): void
8392
$builder->addDefinition($this->prefix('buildDirProvider'))
8493
->setFactory(BuildDirectoryProvider::class, [$config['build']['directory']]);
8594

86-
$assetLocator = $builder->addDefinition($this->prefix('assetLocator'))
87-
->setFactory(AssetLocator::class);
88-
8995
$builder->addDefinition($this->prefix('devServer'))
9096
->setFactory(DevServer::class, [
9197
$config['devServer']['enabled'],
9298
$config['devServer']['url'] ?? '',
9399
$config['devServer']['timeout'],
94-
new Statement(Client::class)
100+
new Statement(Client::class),
101+
]);
102+
103+
$assetLocator = $builder->addDefinition($this->prefix('assetLocator'))
104+
->setFactory(AssetLocator::class, [
105+
'ignoredAssetNames' => $config['devServer']['ignoredAssets'],
95106
]);
96107

97108
$assetResolver = $this->setupAssetResolver($config);
@@ -110,6 +121,7 @@ public function loadConfiguration(): void
110121
? $latteFactory->getResultDefinition()
111122
: $latteFactory;
112123

124+
\assert($definition instanceof ServiceDefinition);
113125
$definition
114126
->addSetup('?->addProvider(?, ?)', ['@self', 'webpackAssetLocator', $assetLocator])
115127
->addSetup('?->onCompile[] = function ($engine) { Oops\WebpackNetteAdapter\Latte\WebpackMacros::install($engine->getCompiler()); }', ['@self']);
@@ -126,14 +138,19 @@ public function beforeCompile(): void
126138
$builder = $this->getContainerBuilder();
127139

128140
if ($this->config['debugger'] && \interface_exists(Tracy\IBarPanel::class)) {
129-
$builder->getDefinition($this->prefix('pathProvider'))
130-
->addSetup('@Tracy\Bar::addPanel', [
131-
new Statement(WebpackPanel::class)
132-
]);
141+
$definition = $builder->getDefinition($this->prefix('pathProvider'));
142+
\assert($definition instanceof ServiceDefinition);
143+
144+
$definition->addSetup('@Tracy\Bar::addPanel', [
145+
new Statement(WebpackPanel::class)
146+
]);
133147
}
134148
}
135149

136150

151+
/**
152+
* @param array<string, mixed> $config
153+
*/
137154
private function setupAssetResolver(array $config): ServiceDefinition
138155
{
139156
$builder = $this->getContainerBuilder();

src/Latte/WebpackMacros.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
/**
1414
* - {webpack 'asset.name.js'}
1515
*/
16-
class WebpackMacros extends MacroSet
16+
final class WebpackMacros extends MacroSet
1717
{
1818

1919
public static function install(Compiler $compiler): void
2020
{
21-
$me = new static($compiler);
21+
$me = new self($compiler);
2222
$me->addMacro('webpack', [$me, 'macroWebpackAsset']);
2323
}
2424

0 commit comments

Comments
 (0)