Skip to content

Commit

Permalink
Merge pull request #489 from magento-troll/MAGETWO-33507
Browse files Browse the repository at this point in the history
[Troll] Refactor CSS and JavaScript minification mechanism
  • Loading branch information
slopukhov committed Jul 24, 2015
2 parents 870291a + e5301ad commit 3e132a3
Show file tree
Hide file tree
Showing 68 changed files with 2,570 additions and 2,068 deletions.
1 change: 0 additions & 1 deletion .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ $finder = Symfony\CS\Finder\DefaultFinder::create()
->exclude('dev/tests/integration/var')
->exclude('lib/internal/Cm')
->exclude('lib/internal/Credis')
->exclude('lib/internal/JSMin')
->exclude('lib/internal/Less')
->exclude('lib/internal/LinLibertineFont')
->exclude('lib/internal/phpseclib')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@ class DeveloperChain extends Chain
* @param LocalInterface $asset
* @param string $origContent
* @param string $origContentType
* @param null $origAssetPath
* @param string $origAssetPath
* @codeCoverageIgnore
*/
public function __construct(
LocalInterface $asset,
$origContent,
$origContentType,
$origAssetPath = null
$origAssetPath
) {
parent::__construct(
$asset,
$origContent,
$origContentType
$origContentType,
$origAssetPath
);

$this->targetContentType = $this->origContentType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class Renderer extends Config\Renderer

/**
* @param Config $pageConfig
* @param \Magento\Framework\View\Asset\MinifyService $assetMinifyService
* @param \Magento\Framework\View\Asset\MergeService $assetMergeService
* @param \Magento\Framework\UrlInterface $urlBuilder
* @param \Magento\Framework\Escaper $escaper
Expand All @@ -29,7 +28,6 @@ class Renderer extends Config\Renderer
*/
public function __construct(
Config $pageConfig,
\Magento\Framework\View\Asset\MinifyService $assetMinifyService,
\Magento\Framework\View\Asset\MergeService $assetMergeService,
\Magento\Framework\UrlInterface $urlBuilder,
\Magento\Framework\Escaper $escaper,
Expand All @@ -41,7 +39,6 @@ public function __construct(

parent::__construct(
$pageConfig,
$assetMinifyService,
$assetMergeService,
$urlBuilder,
$escaper,
Expand Down
23 changes: 21 additions & 2 deletions app/code/Magento/RequireJs/Block/Html/Head/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Magento\RequireJs\Block\Html\Head;

use Magento\Framework\RequireJs\Config as RequireJsConfig;
use Magento\Framework\View\Asset\Minification;

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

/**
* @var Minification
*/
protected $minification;

/**
* @param \Magento\Framework\View\Element\Context $context
* @param RequireJsConfig $config
* @param \Magento\RequireJs\Model\FileManager $fileManager
* @param \Magento\Framework\View\Page\Config $pageConfig
* @param \Magento\Framework\View\Asset\ConfigInterface $bundleConfig
* @param Minification $minification
* @param array $data
*/
public function __construct(
Expand All @@ -42,13 +49,15 @@ public function __construct(
\Magento\RequireJs\Model\FileManager $fileManager,
\Magento\Framework\View\Page\Config $pageConfig,
\Magento\Framework\View\Asset\ConfigInterface $bundleConfig,
Minification $minification,
array $data = []
) {
parent::__construct($context, $data);
$this->config = $config;
$this->fileManager = $fileManager;
$this->pageConfig = $pageConfig;
$this->bundleConfig = $bundleConfig;
$this->minification = $minification;
}

/**
Expand All @@ -58,11 +67,21 @@ public function __construct(
*/
protected function _prepareLayout()
{
$after = RequireJsConfig::REQUIRE_JS_FILE_NAME;
$requireJsConfig = $this->fileManager->createRequireJsConfigAsset();
$requireJsMixinsConfig = $this->fileManager->createRequireJsMixinsAsset();
$assetCollection = $this->pageConfig->getAssetCollection();

$after = RequireJsConfig::REQUIRE_JS_FILE_NAME;
if ($this->minification->isEnabled('js')) {
$minResolver = $this->fileManager->createMinResolverAsset();
$assetCollection->insert(
$minResolver->getFilePath(),
$minResolver,
$after
);
$after = $minResolver->getFilePath();
}

if ($this->bundleConfig->isBundlingJsFiles()) {
$bundleAssets = $this->fileManager->createBundleJsPool();
$staticAsset = $this->fileManager->createStaticJsAsset();
Expand All @@ -74,7 +93,7 @@ protected function _prepareLayout()
$assetCollection->insert(
$bundleAsset->getFilePath(),
$bundleAsset,
RequireJsConfig::REQUIRE_JS_FILE_NAME
$after
);
}
$assetCollection->insert(
Expand Down
50 changes: 39 additions & 11 deletions app/code/Magento/RequireJs/Model/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
namespace Magento\RequireJs\Model;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\State as AppState;
use Magento\Framework\RequireJs\Config;

/**
* A service for handling RequireJS files in the application
*/
class FileManager
{
/**
* @var \Magento\Framework\RequireJs\Config
* @var Config
*/
private $config;

Expand All @@ -23,7 +25,7 @@ class FileManager
private $filesystem;

/**
* @var \Magento\Framework\App\State
* @var AppState
*/
private $appState;

Expand All @@ -33,15 +35,15 @@ class FileManager
private $assetRepo;

/**
* @param \Magento\Framework\RequireJs\Config $config
* @param Config $config
* @param \Magento\Framework\Filesystem $appFilesystem
* @param \Magento\Framework\App\State $appState
* @param AppState $appState
* @param \Magento\Framework\View\Asset\Repository $assetRepo
*/
public function __construct(
\Magento\Framework\RequireJs\Config $config,
Config $config,
\Magento\Framework\Filesystem $appFilesystem,
\Magento\Framework\App\State $appState,
AppState $appState,
\Magento\Framework\View\Asset\Repository $assetRepo
) {
$this->config = $config;
Expand All @@ -62,6 +64,18 @@ public function createRequireJsConfigAsset()
return $this->assetRepo->createArbitrary($relPath, '');
}

/**
* Create '.min' files resolver asset
*
* @return \Magento\Framework\View\Asset\File
*/
public function createMinResolverAsset()
{
$relPath = $this->config->getMinResolverRelativePath();
$this->ensureMinResolverFile($relPath);
return $this->assetRepo->createArbitrary($relPath, '');
}

/**
* Create a view asset representing the aggregated configuration file
*
Expand Down Expand Up @@ -95,23 +109,37 @@ public function createRequireJsAsset()
private function ensureSourceFile($relPath)
{
$dir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
if ($this->appState->getMode() == \Magento\Framework\App\State::MODE_DEVELOPER || !$dir->isExist($relPath)) {
if ($this->appState->getMode() == AppState::MODE_DEVELOPER || !$dir->isExist($relPath)) {
$dir->writeFile($relPath, $this->config->getConfig());
}
}

/**
* Make sure the '.min' assets resolver is materialized
*
* @param string $relPath
* @return void
*/
private function ensureMinResolverFile($relPath)
{
$dir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
if ($this->appState->getMode() == AppState::MODE_DEVELOPER || !$dir->isExist($relPath)) {
$dir->writeFile($relPath, $this->config->getMinResolverCode());
}
}

/**
* Create a view asset representing the static js functionality
*
* @return \Magento\Framework\View\Asset\File
*/
public function createStaticJsAsset()
{
if ($this->appState->getMode() != \Magento\Framework\App\State::MODE_PRODUCTION) {
if ($this->appState->getMode() != AppState::MODE_PRODUCTION) {
return false;
}
$libDir = $this->filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW);
$relPath = $libDir->getRelativePath(\Magento\Framework\RequireJs\Config::STATIC_FILE_NAME);
$relPath = $libDir->getRelativePath(Config::STATIC_FILE_NAME);
/** @var $context \Magento\Framework\View\Asset\File\FallbackContext */
$context = $this->assetRepo->getStaticViewFileContext();

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

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

if (!$libDir->isExist($bundleDir)) {
return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
protected $blockConfig;

/**
* @var \Magento\Framework\View\Page\Config|\Magento\Framework\View\Asset\ConfigInterface
* @var \Magento\Framework\View\Asset\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $bundleConfig;

/**
* @var \Magento\Framework\View\Asset\Minification|\PHPUnit_Framework_MockObject_MockObject
*/
private $minificationMock;

protected function setUp()
{
$this->context = $this->getMock('\Magento\Framework\View\Element\Context', [], [], '', false);
Expand All @@ -65,6 +70,11 @@ public function testSetLayout()
->expects($this->atLeastOnce())
->method('getFilePath')
->willReturn('/path/to/require/require.js');
$minResolverAsset = $this->getMockForAbstractClass('\Magento\Framework\View\Asset\LocalInterface');
$minResolverAsset
->expects($this->atLeastOnce())
->method('getFilePath')
->willReturn('/path/to/require/require-min-resolver.js');

$this->fileManager
->expects($this->once())
Expand All @@ -82,6 +92,10 @@ public function testSetLayout()
->expects($this->once())
->method('createBundleJsPool')
->will($this->returnValue([$asset]));
$this->fileManager
->expects($this->once())
->method('createMinResolverAsset')
->will($this->returnValue($minResolverAsset));

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

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

$object = new Config($this->context, $this->config, $this->fileManager, $this->pageConfig, $this->bundleConfig);
$this->minificationMock = $this->getMockBuilder('Magento\Framework\View\Asset\Minification')
->disableOriginalConstructor()
->getMock();
$this->minificationMock
->expects($this->any())
->method('isEnabled')
->with('js')
->willReturn(true);


$object = new Config(
$this->context,
$this->config,
$this->fileManager,
$this->pageConfig,
$this->bundleConfig,
$this->minificationMock
);
$object->setLayout($layout);
}

Expand All @@ -112,7 +143,18 @@ public function testToHtml()
$this->getMockForAbstractClass('\Magento\Framework\App\Config\ScopeConfigInterface')
));
$this->config->expects($this->once())->method('getBaseConfig')->will($this->returnValue('the config data'));
$object = new Config($this->context, $this->config, $this->fileManager, $this->pageConfig, $this->bundleConfig);
$this->minificationMock = $this->getMockBuilder('Magento\Framework\View\Asset\Minification')
->disableOriginalConstructor()
->getMock();

$object = new Config(
$this->context,
$this->config,
$this->fileManager,
$this->pageConfig,
$this->bundleConfig,
$this->minificationMock
);
$html = $object->toHtml();
$expectedFormat = <<<expected
<script type="text/javascript">
Expand Down
Loading

0 comments on commit 3e132a3

Please sign in to comment.