Skip to content

Commit 3e132a3

Browse files
committed
Merge pull request #489 from magento-troll/MAGETWO-33507
[Troll] Refactor CSS and JavaScript minification mechanism
2 parents 870291a + e5301ad commit 3e132a3

File tree

68 files changed

+2570
-2068
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+2570
-2068
lines changed

.php_cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ $finder = Symfony\CS\Finder\DefaultFinder::create()
1717
->exclude('dev/tests/integration/var')
1818
->exclude('lib/internal/Cm')
1919
->exclude('lib/internal/Credis')
20-
->exclude('lib/internal/JSMin')
2120
->exclude('lib/internal/Less')
2221
->exclude('lib/internal/LinLibertineFont')
2322
->exclude('lib/internal/phpseclib')

app/code/Magento/Developer/Model/View/Asset/PreProcessor/DeveloperChain.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,20 @@ class DeveloperChain extends Chain
1414
* @param LocalInterface $asset
1515
* @param string $origContent
1616
* @param string $origContentType
17-
* @param null $origAssetPath
17+
* @param string $origAssetPath
1818
* @codeCoverageIgnore
1919
*/
2020
public function __construct(
2121
LocalInterface $asset,
2222
$origContent,
2323
$origContentType,
24-
$origAssetPath = null
24+
$origAssetPath
2525
) {
2626
parent::__construct(
2727
$asset,
2828
$origContent,
29-
$origContentType
29+
$origContentType,
30+
$origAssetPath
3031
);
3132

3233
$this->targetContentType = $this->origContentType;

app/code/Magento/Developer/Model/View/Page/Config/ClientSideLessCompilation/Renderer.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class Renderer extends Config\Renderer
1919

2020
/**
2121
* @param Config $pageConfig
22-
* @param \Magento\Framework\View\Asset\MinifyService $assetMinifyService
2322
* @param \Magento\Framework\View\Asset\MergeService $assetMergeService
2423
* @param \Magento\Framework\UrlInterface $urlBuilder
2524
* @param \Magento\Framework\Escaper $escaper
@@ -29,7 +28,6 @@ class Renderer extends Config\Renderer
2928
*/
3029
public function __construct(
3130
Config $pageConfig,
32-
\Magento\Framework\View\Asset\MinifyService $assetMinifyService,
3331
\Magento\Framework\View\Asset\MergeService $assetMergeService,
3432
\Magento\Framework\UrlInterface $urlBuilder,
3533
\Magento\Framework\Escaper $escaper,
@@ -41,7 +39,6 @@ public function __construct(
4139

4240
parent::__construct(
4341
$pageConfig,
44-
$assetMinifyService,
4542
$assetMergeService,
4643
$urlBuilder,
4744
$escaper,

app/code/Magento/RequireJs/Block/Html/Head/Config.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\RequireJs\Block\Html\Head;
88

99
use Magento\Framework\RequireJs\Config as RequireJsConfig;
10+
use Magento\Framework\View\Asset\Minification;
1011

1112
/**
1213
* Block responsible for including RequireJs config on the page
@@ -28,12 +29,18 @@ class Config extends \Magento\Framework\View\Element\AbstractBlock
2829
*/
2930
protected $pageConfig;
3031

32+
/**
33+
* @var Minification
34+
*/
35+
protected $minification;
36+
3137
/**
3238
* @param \Magento\Framework\View\Element\Context $context
3339
* @param RequireJsConfig $config
3440
* @param \Magento\RequireJs\Model\FileManager $fileManager
3541
* @param \Magento\Framework\View\Page\Config $pageConfig
3642
* @param \Magento\Framework\View\Asset\ConfigInterface $bundleConfig
43+
* @param Minification $minification
3744
* @param array $data
3845
*/
3946
public function __construct(
@@ -42,13 +49,15 @@ public function __construct(
4249
\Magento\RequireJs\Model\FileManager $fileManager,
4350
\Magento\Framework\View\Page\Config $pageConfig,
4451
\Magento\Framework\View\Asset\ConfigInterface $bundleConfig,
52+
Minification $minification,
4553
array $data = []
4654
) {
4755
parent::__construct($context, $data);
4856
$this->config = $config;
4957
$this->fileManager = $fileManager;
5058
$this->pageConfig = $pageConfig;
5159
$this->bundleConfig = $bundleConfig;
60+
$this->minification = $minification;
5261
}
5362

5463
/**
@@ -58,11 +67,21 @@ public function __construct(
5867
*/
5968
protected function _prepareLayout()
6069
{
61-
$after = RequireJsConfig::REQUIRE_JS_FILE_NAME;
6270
$requireJsConfig = $this->fileManager->createRequireJsConfigAsset();
6371
$requireJsMixinsConfig = $this->fileManager->createRequireJsMixinsAsset();
6472
$assetCollection = $this->pageConfig->getAssetCollection();
6573

74+
$after = RequireJsConfig::REQUIRE_JS_FILE_NAME;
75+
if ($this->minification->isEnabled('js')) {
76+
$minResolver = $this->fileManager->createMinResolverAsset();
77+
$assetCollection->insert(
78+
$minResolver->getFilePath(),
79+
$minResolver,
80+
$after
81+
);
82+
$after = $minResolver->getFilePath();
83+
}
84+
6685
if ($this->bundleConfig->isBundlingJsFiles()) {
6786
$bundleAssets = $this->fileManager->createBundleJsPool();
6887
$staticAsset = $this->fileManager->createStaticJsAsset();
@@ -74,7 +93,7 @@ protected function _prepareLayout()
7493
$assetCollection->insert(
7594
$bundleAsset->getFilePath(),
7695
$bundleAsset,
77-
RequireJsConfig::REQUIRE_JS_FILE_NAME
96+
$after
7897
);
7998
}
8099
$assetCollection->insert(

app/code/Magento/RequireJs/Model/FileManager.php

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
namespace Magento\RequireJs\Model;
77

88
use Magento\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\App\State as AppState;
10+
use Magento\Framework\RequireJs\Config;
911

1012
/**
1113
* A service for handling RequireJS files in the application
1214
*/
1315
class FileManager
1416
{
1517
/**
16-
* @var \Magento\Framework\RequireJs\Config
18+
* @var Config
1719
*/
1820
private $config;
1921

@@ -23,7 +25,7 @@ class FileManager
2325
private $filesystem;
2426

2527
/**
26-
* @var \Magento\Framework\App\State
28+
* @var AppState
2729
*/
2830
private $appState;
2931

@@ -33,15 +35,15 @@ class FileManager
3335
private $assetRepo;
3436

3537
/**
36-
* @param \Magento\Framework\RequireJs\Config $config
38+
* @param Config $config
3739
* @param \Magento\Framework\Filesystem $appFilesystem
38-
* @param \Magento\Framework\App\State $appState
40+
* @param AppState $appState
3941
* @param \Magento\Framework\View\Asset\Repository $assetRepo
4042
*/
4143
public function __construct(
42-
\Magento\Framework\RequireJs\Config $config,
44+
Config $config,
4345
\Magento\Framework\Filesystem $appFilesystem,
44-
\Magento\Framework\App\State $appState,
46+
AppState $appState,
4547
\Magento\Framework\View\Asset\Repository $assetRepo
4648
) {
4749
$this->config = $config;
@@ -62,6 +64,18 @@ public function createRequireJsConfigAsset()
6264
return $this->assetRepo->createArbitrary($relPath, '');
6365
}
6466

67+
/**
68+
* Create '.min' files resolver asset
69+
*
70+
* @return \Magento\Framework\View\Asset\File
71+
*/
72+
public function createMinResolverAsset()
73+
{
74+
$relPath = $this->config->getMinResolverRelativePath();
75+
$this->ensureMinResolverFile($relPath);
76+
return $this->assetRepo->createArbitrary($relPath, '');
77+
}
78+
6579
/**
6680
* Create a view asset representing the aggregated configuration file
6781
*
@@ -95,23 +109,37 @@ public function createRequireJsAsset()
95109
private function ensureSourceFile($relPath)
96110
{
97111
$dir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
98-
if ($this->appState->getMode() == \Magento\Framework\App\State::MODE_DEVELOPER || !$dir->isExist($relPath)) {
112+
if ($this->appState->getMode() == AppState::MODE_DEVELOPER || !$dir->isExist($relPath)) {
99113
$dir->writeFile($relPath, $this->config->getConfig());
100114
}
101115
}
102116

117+
/**
118+
* Make sure the '.min' assets resolver is materialized
119+
*
120+
* @param string $relPath
121+
* @return void
122+
*/
123+
private function ensureMinResolverFile($relPath)
124+
{
125+
$dir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
126+
if ($this->appState->getMode() == AppState::MODE_DEVELOPER || !$dir->isExist($relPath)) {
127+
$dir->writeFile($relPath, $this->config->getMinResolverCode());
128+
}
129+
}
130+
103131
/**
104132
* Create a view asset representing the static js functionality
105133
*
106134
* @return \Magento\Framework\View\Asset\File
107135
*/
108136
public function createStaticJsAsset()
109137
{
110-
if ($this->appState->getMode() != \Magento\Framework\App\State::MODE_PRODUCTION) {
138+
if ($this->appState->getMode() != AppState::MODE_PRODUCTION) {
111139
return false;
112140
}
113141
$libDir = $this->filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW);
114-
$relPath = $libDir->getRelativePath(\Magento\Framework\RequireJs\Config::STATIC_FILE_NAME);
142+
$relPath = $libDir->getRelativePath(Config::STATIC_FILE_NAME);
115143
/** @var $context \Magento\Framework\View\Asset\File\FallbackContext */
116144
$context = $this->assetRepo->getStaticViewFileContext();
117145

@@ -126,12 +154,12 @@ public function createStaticJsAsset()
126154
public function createBundleJsPool()
127155
{
128156
$bundles = [];
129-
if ($this->appState->getMode() == \Magento\Framework\App\State::MODE_PRODUCTION) {
157+
if ($this->appState->getMode() == AppState::MODE_PRODUCTION) {
130158
$libDir = $this->filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW);
131159
/** @var $context \Magento\Framework\View\Asset\File\FallbackContext */
132160
$context = $this->assetRepo->getStaticViewFileContext();
133161

134-
$bundleDir = $context->getPath() . '/' .\Magento\Framework\RequireJs\Config::BUNDLE_JS_DIR;
162+
$bundleDir = $context->getPath() . '/' . Config::BUNDLE_JS_DIR;
135163

136164
if (!$libDir->isExist($bundleDir)) {
137165
return [];

app/code/Magento/RequireJs/Test/Unit/Block/Html/Head/ConfigTest.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,15 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
3636
protected $blockConfig;
3737

3838
/**
39-
* @var \Magento\Framework\View\Page\Config|\Magento\Framework\View\Asset\ConfigInterface
39+
* @var \Magento\Framework\View\Asset\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
4040
*/
4141
protected $bundleConfig;
4242

43+
/**
44+
* @var \Magento\Framework\View\Asset\Minification|\PHPUnit_Framework_MockObject_MockObject
45+
*/
46+
private $minificationMock;
47+
4348
protected function setUp()
4449
{
4550
$this->context = $this->getMock('\Magento\Framework\View\Element\Context', [], [], '', false);
@@ -65,6 +70,11 @@ public function testSetLayout()
6570
->expects($this->atLeastOnce())
6671
->method('getFilePath')
6772
->willReturn('/path/to/require/require.js');
73+
$minResolverAsset = $this->getMockForAbstractClass('\Magento\Framework\View\Asset\LocalInterface');
74+
$minResolverAsset
75+
->expects($this->atLeastOnce())
76+
->method('getFilePath')
77+
->willReturn('/path/to/require/require-min-resolver.js');
6878

6979
$this->fileManager
7080
->expects($this->once())
@@ -82,6 +92,10 @@ public function testSetLayout()
8292
->expects($this->once())
8393
->method('createBundleJsPool')
8494
->will($this->returnValue([$asset]));
95+
$this->fileManager
96+
->expects($this->once())
97+
->method('createMinResolverAsset')
98+
->will($this->returnValue($minResolverAsset));
8599

86100
$layout = $this->getMock('Magento\Framework\View\LayoutInterface');
87101

@@ -97,7 +111,24 @@ public function testSetLayout()
97111
->method('insert')
98112
->willReturn(true);
99113

100-
$object = new Config($this->context, $this->config, $this->fileManager, $this->pageConfig, $this->bundleConfig);
114+
$this->minificationMock = $this->getMockBuilder('Magento\Framework\View\Asset\Minification')
115+
->disableOriginalConstructor()
116+
->getMock();
117+
$this->minificationMock
118+
->expects($this->any())
119+
->method('isEnabled')
120+
->with('js')
121+
->willReturn(true);
122+
123+
124+
$object = new Config(
125+
$this->context,
126+
$this->config,
127+
$this->fileManager,
128+
$this->pageConfig,
129+
$this->bundleConfig,
130+
$this->minificationMock
131+
);
101132
$object->setLayout($layout);
102133
}
103134

@@ -112,7 +143,18 @@ public function testToHtml()
112143
$this->getMockForAbstractClass('\Magento\Framework\App\Config\ScopeConfigInterface')
113144
));
114145
$this->config->expects($this->once())->method('getBaseConfig')->will($this->returnValue('the config data'));
115-
$object = new Config($this->context, $this->config, $this->fileManager, $this->pageConfig, $this->bundleConfig);
146+
$this->minificationMock = $this->getMockBuilder('Magento\Framework\View\Asset\Minification')
147+
->disableOriginalConstructor()
148+
->getMock();
149+
150+
$object = new Config(
151+
$this->context,
152+
$this->config,
153+
$this->fileManager,
154+
$this->pageConfig,
155+
$this->bundleConfig,
156+
$this->minificationMock
157+
);
116158
$html = $object->toHtml();
117159
$expectedFormat = <<<expected
118160
<script type="text/javascript">

0 commit comments

Comments
 (0)