Skip to content

Commit 1110498

Browse files
author
Pavlo Cherniavskyi
committed
MAGETWO-33622: Junior+ Developer Workflow
1 parent 97f39b2 commit 1110498

File tree

5 files changed

+50
-36
lines changed

5 files changed

+50
-36
lines changed

app/etc/di.xml

+7-2
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,14 @@
181181
<argument name="mode" xsi:type="init_parameter">Magento\Framework\App\State::PARAM_MODE</argument>
182182
</arguments>
183183
</type>
184-
<type name="\Magento\Framework\View\Asset\Source">
184+
<type name="Magento\Framework\View\Asset\Source">
185185
<arguments>
186-
<argument name="appmode" xsi:type="init_parameter">Magento\Framework\App\State::PARAM_MODE</argument>
186+
<argument name="appMode" xsi:type="init_parameter">Magento\Framework\App\State::PARAM_MODE</argument>
187+
</arguments>
188+
</type>
189+
<type name="Magento\Framework\View\Page\Config\Renderer">
190+
<arguments>
191+
<argument name="appMode" xsi:type="init_parameter">Magento\Framework\App\State::PARAM_MODE</argument>
187192
</arguments>
188193
</type>
189194
<type name="Magento\Framework\App\Arguments\ValidationState">

lib/internal/Magento/Framework/View/Asset/File.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function getPath()
101101
public function getRelativeSourceFilePath()
102102
{
103103
$path = $this->filePath;
104-
if (strpos($this->source->findRelativeSourceFile($this), 'less')) {
104+
if (strpos($this->source->findRelativeSourceFilePath($this), 'less')) {
105105
$path = str_replace('.css', '.less', $this->filePath);
106106
}
107107
$result = '';
@@ -130,9 +130,7 @@ private function join($path, $item)
130130
public function getSourceFile()
131131
{
132132
if (null === $this->resolvedFile) {
133-
$result = $this->source->getFile($this);
134-
$this->resolvedFile = $result['abs'];
135-
$this->filePath = $result['relativePath'];
133+
$this->resolvedFile = $this->source->getFile($this);
136134
if (false === $this->resolvedFile) {
137135
throw new File\NotFoundException("Unable to resolve the source file for '{$this->getPath()}'");
138136
}

lib/internal/Magento/Framework/View/Asset/Source.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,7 @@ public function getFile(LocalInterface $asset)
9494
return false;
9595
}
9696
list($dirCode, $path) = $result;
97-
return [
98-
'abs' => $this->filesystem->getDirectoryRead($dirCode)->getAbsolutePath($path),
99-
'relativePath' => $path
100-
];
97+
return $this->filesystem->getDirectoryRead($dirCode)->getAbsolutePath($path);
10198
}
10299

103100
/**
@@ -146,7 +143,7 @@ private function preProcess(LocalInterface $asset)
146143
$this->rootDir->readFile($path),
147144
$this->getContentType($path),
148145
$path,
149-
'developer'
146+
$this->appMode
150147
);
151148
$preProcessors = $this->preProcessorPool
152149
->getPreProcessors($chain->getOrigContentType(), $chain->getTargetContentType());
@@ -232,7 +229,7 @@ private function findFile(LocalInterface $asset, \Magento\Framework\View\Asset\F
232229
return $dir->getAbsolutePath($asset->getPath());
233230
}
234231

235-
public function findRelativeSourceFile(LocalInterface $asset)
232+
public function findRelativeSourceFilePath(LocalInterface $asset)
236233
{
237234
$sourceFile = $this->findSourceFile($asset);
238235
if (!$sourceFile) {

lib/internal/Magento/Framework/View/Page/Config/Renderer.php

+37-20
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ class Renderer
5454
*/
5555
protected $urlBuilder;
5656

57+
/**
58+
* @var string
59+
*/
60+
private $appMode;
61+
62+
/**
63+
* @var \Magento\Framework\View\Asset\Repository
64+
*/
65+
private $assetRepo;
66+
5767
/**
5868
* @param \Magento\Framework\View\Page\Config $pageConfig
5969
* @param \Magento\Framework\View\Asset\MinifyService $assetMinifyService
@@ -70,7 +80,9 @@ public function __construct(
7080
\Magento\Framework\UrlInterface $urlBuilder,
7181
\Magento\Framework\Escaper $escaper,
7282
\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
7486
) {
7587
$this->pageConfig = $pageConfig;
7688
$this->assetMinifyService = $assetMinifyService;
@@ -79,6 +91,8 @@ public function __construct(
7991
$this->escaper = $escaper;
8092
$this->string = $string;
8193
$this->logger = $logger;
94+
$this->assetRepo = $assetRepo;
95+
$this->appMode = $appMode;
8296
}
8397

8498
/**
@@ -219,12 +233,11 @@ public function prepareFavicon()
219233
public function renderAssets()
220234
{
221235
$resultGroups = array_fill_keys($this->assetTypeOrder, '');
236+
// less js have to be injected before any *.js in developer mode
237+
$resultGroups = $this->renderLessJsScripts($resultGroups);
222238
/** @var $group \Magento\Framework\View\Asset\PropertyGroup */
223239
foreach ($this->pageConfig->getAssetCollection()->getGroups() as $group) {
224240
$type = $group->getProperty(GroupedCollection::PROPERTY_CONTENT_TYPE);
225-
if ($type == 'js') {
226-
$resultGroups[$type] .= $this->renderLessJsInclude();
227-
}
228241
if (!isset($resultGroups[$type])) {
229242
$resultGroups[$type] = '';
230243
}
@@ -365,18 +378,16 @@ protected function renderAssetHtml($template, $assets)
365378
{
366379
$result = '';
367380
try {
368-
/** @var $asset \Magento\Framework\View\Asset\File */
369381
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) {
372384
if ($asset->getSourceUrl() != $asset->getUrl()) {
373385
$attributes = $this->addDefaultAttributes('less', []);
374386
$groupTemplate = $this->getAssetTemplate('less', $attributes);
375387
$result .= sprintf($groupTemplate, $asset->getSourceUrl());
376388
} else {
377-
$result .= sprintf($template, $asset->getSourceUrl());
389+
$result .= sprintf($template, $asset->getUrl());
378390
}
379-
380391
} else {
381392
$result .= sprintf($template, $asset->getUrl());
382393
}
@@ -388,17 +399,23 @@ protected function renderAssetHtml($template, $assets)
388399
return $result;
389400
}
390401

391-
private function renderLessJsInclude()
402+
/**
403+
* Injecting less.js compiler
404+
*
405+
* @param $resultGroups
406+
*
407+
* @return mixed
408+
*/
409+
private function renderLessJsScripts($resultGroups)
392410
{
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;
403420
}
404421
}

lib/web/less/config.less.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11

2-
32
less = {
4-
env: "developer",
5-
async: false,
6-
fileAsync: false
3+
env: "development"
74
};

0 commit comments

Comments
 (0)