2323use Tracy ;
2424
2525
26+ /**
27+ * @property array<string, mixed> $config
28+ */
2629class 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 ();
0 commit comments