@@ -54,6 +54,16 @@ class Renderer
54
54
*/
55
55
protected $ urlBuilder ;
56
56
57
+ /**
58
+ * @var string
59
+ */
60
+ private $ appMode ;
61
+
62
+ /**
63
+ * @var \Magento\Framework\View\Asset\Repository
64
+ */
65
+ private $ assetRepo ;
66
+
57
67
/**
58
68
* @param \Magento\Framework\View\Page\Config $pageConfig
59
69
* @param \Magento\Framework\View\Asset\MinifyService $assetMinifyService
@@ -70,7 +80,9 @@ public function __construct(
70
80
\Magento \Framework \UrlInterface $ urlBuilder ,
71
81
\Magento \Framework \Escaper $ escaper ,
72
82
\Magento \Framework \Stdlib \String $ string ,
73
- \Psr \Log \LoggerInterface $ logger
83
+ \Psr \Log \LoggerInterface $ logger ,
84
+ \Magento \Framework \View \Asset \Repository $ assetRepo ,
85
+ $ appMode = \Magento \Framework \App \State::MODE_DEFAULT
74
86
) {
75
87
$ this ->pageConfig = $ pageConfig ;
76
88
$ this ->assetMinifyService = $ assetMinifyService ;
@@ -79,6 +91,8 @@ public function __construct(
79
91
$ this ->escaper = $ escaper ;
80
92
$ this ->string = $ string ;
81
93
$ this ->logger = $ logger ;
94
+ $ this ->assetRepo = $ assetRepo ;
95
+ $ this ->appMode = $ appMode ;
82
96
}
83
97
84
98
/**
@@ -219,12 +233,11 @@ public function prepareFavicon()
219
233
public function renderAssets ()
220
234
{
221
235
$ resultGroups = array_fill_keys ($ this ->assetTypeOrder , '' );
236
+ // less js have to be injected before any *.js in developer mode
237
+ $ resultGroups = $ this ->renderLessJsScripts ($ resultGroups );
222
238
/** @var $group \Magento\Framework\View\Asset\PropertyGroup */
223
239
foreach ($ this ->pageConfig ->getAssetCollection ()->getGroups () as $ group ) {
224
240
$ type = $ group ->getProperty (GroupedCollection::PROPERTY_CONTENT_TYPE );
225
- if ($ type == 'js ' ) {
226
- $ resultGroups [$ type ] .= $ this ->renderLessJsInclude ();
227
- }
228
241
if (!isset ($ resultGroups [$ type ])) {
229
242
$ resultGroups [$ type ] = '' ;
230
243
}
@@ -365,18 +378,16 @@ protected function renderAssetHtml($template, $assets)
365
378
{
366
379
$ result = '' ;
367
380
try {
368
- /** @var $asset \Magento\Framework\View\Asset\File */
369
381
foreach ($ assets as $ asset ) {
370
-
371
- if (true ) {
382
+ /** @var $asset \Magento\Framework\View\Asset\File */
383
+ if ($ this -> appMode == \ Magento \ Framework \ App \State:: MODE_DEVELOPER ) {
372
384
if ($ asset ->getSourceUrl () != $ asset ->getUrl ()) {
373
385
$ attributes = $ this ->addDefaultAttributes ('less ' , []);
374
386
$ groupTemplate = $ this ->getAssetTemplate ('less ' , $ attributes );
375
387
$ result .= sprintf ($ groupTemplate , $ asset ->getSourceUrl ());
376
388
} else {
377
- $ result .= sprintf ($ template , $ asset ->getSourceUrl ());
389
+ $ result .= sprintf ($ template , $ asset ->getUrl ());
378
390
}
379
-
380
391
} else {
381
392
$ result .= sprintf ($ template , $ asset ->getUrl ());
382
393
}
@@ -388,17 +399,23 @@ protected function renderAssetHtml($template, $assets)
388
399
return $ result ;
389
400
}
390
401
391
- private function renderLessJsInclude ()
402
+ /**
403
+ * Injecting less.js compiler
404
+ *
405
+ * @param $resultGroups
406
+ *
407
+ * @return mixed
408
+ */
409
+ private function renderLessJsScripts ($ resultGroups )
392
410
{
393
- $ result = '' ;
394
- $ result .= '<script>
395
- less = {
396
- env: "production",
397
- async: false,
398
- fileAsync: false
399
- };
400
- </script> ' ;
401
- $ result .= sprintf ('<script src="%s"></script> ' . "\n" , '//cdnjs.cloudflare.com/ajax/libs/less.js/2.3.1/less.min.js ' ) ;
402
- return $ result ;
411
+ if (\Magento \Framework \App \State::MODE_DEVELOPER == $ this ->appMode ) {
412
+ // less js have to be injected before any *.js in developer mode
413
+ $ lessJsConfigAsset = $ this ->assetRepo ->createAsset ('less/config.less.js ' );
414
+ $ resultGroups ['js ' ] .= sprintf ('<script src="%s"></script> ' . "\n" , $ lessJsConfigAsset ->getUrl ()) ;
415
+ $ lessJsAsset = $ this ->assetRepo ->createAsset ('less/less.min.js ' );
416
+ $ resultGroups ['js ' ] .= sprintf ('<script src="%s"></script> ' . "\n" , $ lessJsAsset ->getUrl ()) ;
417
+ }
418
+
419
+ return $ resultGroups ;
403
420
}
404
421
}
0 commit comments