Skip to content

Commit

Permalink
Merge pull request #14 from o2ps/update
Browse files Browse the repository at this point in the history
  • Loading branch information
jiripudil authored Dec 14, 2019
2 parents 31750ab + eba1bc7 commit 85cc31c
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 49 deletions.
25 changes: 7 additions & 18 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ dist: xenial
language: php

php:
- 7.1
- 7.2
- 7.3
- 7.4

env:
- NETTE_VERSION="^2.4"
Expand All @@ -17,36 +17,25 @@ before_install:

install:
- composer require "nette/di:${NETTE_VERSION}" --no-update
- composer require "latte/latte:${NETTE_VERSION}" "nette/application:${NETTE_VERSION}" "nette/bootstrap:${NETTE_VERSION}" --dev --no-update
- composer require "nette/application:${NETTE_VERSION}" "nette/bootstrap:${NETTE_VERSION}" --dev --no-update
- travis_retry composer update --prefer-source --no-interaction

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

jobs:
include:
- stage: QA
name: Lint
php: 7.3
env: NETTE_VERSION="^2.4"
before_script:
- travis_retry composer global require "jakub-onderka/php-parallel-lint:^1.0.0"
script:
- $HOME/.composer/vendor/bin/parallel-lint -e php,phpt src/ tests/

- stage: QA
name: Static Analysis
php: 7.3
env: NETTE_VERSION="^2.4"
before_script:
- travis_retry composer global require "phpstan/phpstan-shim:^0.11.0"
php: 7.4
env: NETTE_VERSION="^3.0"
script:
- $HOME/.composer/vendor/bin/phpstan.phar analyze --no-progress --no-interaction -l max src/
- vendor/bin/phpstan.phar analyze --no-progress --no-interaction -l max src/

- stage: QA
name: Code Coverage
php: 7.3
env: NETTE_VERSION="^2.4"
php: 7.4
env: NETTE_VERSION="^3.0"
script:
- vendor/bin/tester -p phpdbg -c tests/php.unix.ini -s --coverage coverage.xml --coverage-src src/ tests/
after_success:
Expand Down
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ WebpackNetteAdapter is a tool that helps integrate your Nette Framework applicat
$ composer require oops/webpack-nette-adapter
```

Oops/WebpackNetteAdapter requires PHP >= 7.1.
Oops/WebpackNetteAdapter requires PHP >= 7.2.


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

```yaml
extensions:
webpack: Oops\WebpackNetteAdapter\DI\WebpackExtension(%debugMode%)
webpack: Oops\WebpackNetteAdapter\DI\WebpackExtension(%debugMode%, %consoleMode%)

webpack:
build:
Expand Down Expand Up @@ -53,6 +53,17 @@ webpack:
timeout: 0.1 # (seconds) default
```

#### Ignored assets

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.

```yaml
webpack:
devServer:
ignoredAssets:
- main.css
```


### Asset resolvers and manifest file

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
"issues": "https://github.com/o2ps/WebpackNetteAdapter/issues"
},
"require": {
"php": "^7.1",
"php": "^7.2",
"guzzlehttp/guzzle": "^6.3",
"nette/di": "^2.4.10 || ^3.0"
},
"require-dev": {
"latte/latte": "^2.4 || ^3.0",
"latte/latte": "^2.4",
"mockery/mockery": "^1.0",
"nette/application": "^2.4 || ^3.0",
"nette/bootstrap": "^2.4 || ^3.0",
"nette/tester": "^2.0",
"phpstan/phpstan": "^0.12",
"tracy/tracy": "^2.4 || ^3.0"
},
"suggest": {
Expand All @@ -42,6 +43,5 @@
"OopsTests\\WebpackNetteAdapter\\": "tests/WebpackNetteAdapter"
}
},
"minimum-stability": "beta",
"prefer-stable": true
}
5 changes: 0 additions & 5 deletions phpstan.neon

This file was deleted.

31 changes: 28 additions & 3 deletions src/AssetLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,53 @@ class AssetLocator
*/
private $assetResolver;

/**
* @var DevServer
*/
private $devServer;

/**
* @var string[]
*/
private $ignoredAssetNames;


/**
* @param string[] $ignoredAssetNames
*/
public function __construct(
BuildDirectoryProvider $directoryProvider,
PublicPathProvider $publicPathProvider,
AssetNameResolverInterface $assetResolver
AssetNameResolverInterface $assetResolver,
DevServer $devServer,
array $ignoredAssetNames
)
{
$this->directoryProvider = $directoryProvider;
$this->publicPathProvider = $publicPathProvider;
$this->assetResolver = $assetResolver;
$this->devServer = $devServer;
$this->ignoredAssetNames = $ignoredAssetNames;
}


public function locateInPublicPath(string $asset): string
{
return $this->publicPathProvider->getPublicPath() . '/' . $this->assetResolver->resolveAssetName($asset);
if ($this->devServer->isAvailable() && \in_array($asset, $this->ignoredAssetNames, TRUE)) {
return 'data:,';
}

return \rtrim($this->publicPathProvider->getPublicPath(), '/') . '/' . \ltrim($this->assetResolver->resolveAssetName($asset), '/');
}


public function locateInBuildDirectory(string $asset): string
{
return $this->directoryProvider->getBuildDirectory() . '/' . $this->assetResolver->resolveAssetName($asset);
if ($this->devServer->isAvailable() && \in_array($asset, $this->ignoredAssetNames, TRUE)) {
return 'data:,';
}

return \rtrim($this->directoryProvider->getBuildDirectory(), '/') . '/' . \ltrim($this->assetResolver->resolveAssetName($asset), '/');
}

}
5 changes: 4 additions & 1 deletion src/AssetNameResolver/DebuggerAwareAssetNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class DebuggerAwareAssetNameResolver implements AssetNameResolverInterface
private $inner;

/**
* @var array[]
* @var array<array{string, string}>
*/
private $resolvedAssets = [];

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


/**
* @return array<array{string, string}>
*/
public function getResolvedAssets(): array
{
return $this->resolvedAssets;
Expand Down
2 changes: 1 addition & 1 deletion src/AssetNameResolver/ManifestAssetNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class ManifestAssetNameResolver implements AssetNameResolverInterface
private $loader;

/**
* @var string[]|NULL
* @var array<string, string>|NULL
*/
private $manifestCache;

Expand Down
5 changes: 4 additions & 1 deletion src/AssetNameResolver/StaticAssetNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ final class StaticAssetNameResolver implements AssetNameResolverInterface
{

/**
* @var string[]
* @var array<string, string>
*/
private $resolutions;


/**
* @param array<string, string> $resolutions
*/
public function __construct(array $resolutions)
{
$this->resolutions = $resolutions;
Expand Down
39 changes: 28 additions & 11 deletions src/DI/WebpackExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@
use Tracy;


/**
* @property array<string, mixed> $config
*/
class WebpackExtension extends CompilerExtension
{

/**
* @var array<string, mixed>
*/
private $defaults = [
'debugger' => NULL,
'macros' => NULL,
'devServer' => [
'enabled' => NULL,
'url' => NULL,
'timeout' => 0.1,
'timeout' => 0.1,
'ignoredAssets' => [],
],
'build' => [
'directory' => NULL,
Expand All @@ -45,12 +52,14 @@ class WebpackExtension extends CompilerExtension
];


public function __construct(bool $debugMode)
public function __construct(bool $debugMode, ?bool $consoleMode = NULL)
{
$consoleMode = $consoleMode ?? \PHP_SAPI === 'cli';

$this->defaults['debugger'] = $debugMode;
$this->defaults['macros'] = \interface_exists(ILatteFactory::class);
$this->defaults['devServer']['enabled'] = $debugMode;
$this->defaults['manifest']['optimize'] = ! $debugMode;
$this->defaults['manifest']['optimize'] = ! $debugMode && ( ! $consoleMode || (bool) \getenv('OOPS_WEBPACK_OPTIMIZE_MANIFEST'));
}


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

$assetLocator = $builder->addDefinition($this->prefix('assetLocator'))
->setFactory(AssetLocator::class);

$builder->addDefinition($this->prefix('devServer'))
->setFactory(DevServer::class, [
$config['devServer']['enabled'],
$config['devServer']['url'] ?? '',
$config['devServer']['timeout'],
new Statement(Client::class)
new Statement(Client::class),
]);

$assetLocator = $builder->addDefinition($this->prefix('assetLocator'))
->setFactory(AssetLocator::class, [
'ignoredAssetNames' => $config['devServer']['ignoredAssets'],
]);

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

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

if ($this->config['debugger'] && \interface_exists(Tracy\IBarPanel::class)) {
$builder->getDefinition($this->prefix('pathProvider'))
->addSetup('@Tracy\Bar::addPanel', [
new Statement(WebpackPanel::class)
]);
$definition = $builder->getDefinition($this->prefix('pathProvider'));
\assert($definition instanceof ServiceDefinition);

$definition->addSetup('@Tracy\Bar::addPanel', [
new Statement(WebpackPanel::class)
]);
}
}


/**
* @param array<string, mixed> $config
*/
private function setupAssetResolver(array $config): ServiceDefinition
{
$builder = $this->getContainerBuilder();
Expand Down
4 changes: 2 additions & 2 deletions src/Latte/WebpackMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
/**
* - {webpack 'asset.name.js'}
*/
class WebpackMacros extends MacroSet
final class WebpackMacros extends MacroSet
{

public static function install(Compiler $compiler): void
{
$me = new static($compiler);
$me = new self($compiler);
$me->addMacro('webpack', [$me, 'macroWebpackAsset']);
}

Expand Down
1 change: 1 addition & 0 deletions src/Manifest/ManifestLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function __construct(BuildDirectoryProvider $directoryProvider)

/**
* @throws CannotLoadManifestException
* @return array<string, string>
*/
public function loadManifest(string $fileName): array
{
Expand Down
Loading

0 comments on commit 85cc31c

Please sign in to comment.