/** * Include RequireJs configuration as an asset on the page * * @return $this */ protected function _prepareLayout() { $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(); /** @var \Magento\Framework\View\Asset\File $bundleAsset */ if (!empty($bundleAssets) && $staticAsset !== false) { $bundleAssets = array_reverse($bundleAssets); foreach ($bundleAssets as $bundleAsset) { $assetCollection->insert($bundleAsset->getFilePath(), $bundleAsset, $after); } $assetCollection->insert($staticAsset->getFilePath(), $staticAsset, reset($bundleAssets)->getFilePath()); $after = $staticAsset->getFilePath(); } } $assetCollection->insert($requireJsConfig->getFilePath(), $requireJsConfig, $after); $assetCollection->insert($requireJsMixinsConfig->getFilePath(), $requireJsMixinsConfig, $after); return parent::_prepareLayout(); }
/** * Resolve file name * * @param string $path * @return string */ public function resolve($path) { if (!$this->minification->isEnabled(pathinfo($path, PATHINFO_EXTENSION))) { return $path; } return str_replace(self::FILE_PART, '.', $path); }
/** * Transform content and/or content type for the specified preprocessing chain object * * @param PreProcessor\Chain $chain * @return void */ public function process(PreProcessor\Chain $chain) { if ($this->minification->isEnabled(pathinfo($chain->getTargetAssetPath(), PATHINFO_EXTENSION)) && $this->minification->isMinifiedFilename($chain->getTargetAssetPath()) && !$this->minification->isMinifiedFilename($chain->getOrigAssetPath())) { $content = $this->adapter->minify($chain->getContent()); $chain->setContent($content); } }
/** * @param bool $isEnabled * @param string $requested * @param string $alternative * @param string $expected * @param string $resolvedOriginal * @param string $resolvedAlternative * @return void * @dataProvider resolveDataProvider */ public function testResolve($isEnabled, $requested, $alternative, $expected, $resolvedOriginal, $resolvedAlternative) { $this->assetMinificationMock->expects($this->any())->method('isEnabled')->willReturn($isEnabled); $this->assetMinificationMock->expects($this->any())->method('addMinifiedSign')->with($requested)->willReturn($alternative); $this->resolverMock->expects($this->any())->method('resolve')->willReturnMap([['', $requested, null, null, null, null, $resolvedOriginal], ['', $alternative, null, null, null, null, $resolvedAlternative]]); $this->assertEquals($expected, $this->minification->resolve('', $requested)); }
/** * Get path of file after using fallback rules * * @param string $type * @param string $file * @param string|null $area * @param ThemeInterface|null $theme * @param string|null $locale * @param string|null $module * @return string|false */ public function resolve($type, $file, $area = null, ThemeInterface $theme = null, $locale = null, $module = null) { $path = $this->fallback->resolve($type, $file, $area, $theme, $locale, $module); if (!$path && $file != ($newFile = $this->minification->removeMinifiedSign($file))) { $path = $this->fallback->resolve($type, $newFile, $area, $theme, $locale, $module); } return $path; }
public function setUp() { $this->source = $this->getMock('Magento\\Framework\\View\\Asset\\Source', [], [], '', false); $this->context = $this->getMockForAbstractClass('\\Magento\\Framework\\View\\Asset\\ContextInterface'); $this->minificationMock = $this->getMockBuilder('Magento\\Framework\\View\\Asset\\Minification')->disableOriginalConstructor()->getMock(); $this->minificationMock->expects($this->any())->method('addMinifiedSign')->willReturnArgument(0); $this->object = new File($this->source, $this->context, 'dir/file.css', 'Magento_Module', 'css', $this->minificationMock); }
/** * @param string $targetPath * @param string $originalPath * @param int $minifyCalls * @param int $setContentCalls * @param bool $isEnabled * @return void * @dataProvider processDataProvider */ public function testProcess($targetPath, $originalPath, $minifyCalls, $setContentCalls, $isEnabled) { $chainMock = $this->getMockBuilder('Magento\\Framework\\View\\Asset\\PreProcessor\\Chain')->disableOriginalConstructor()->getMock(); $chainMock->expects($this->any())->method('getTargetAssetPath')->willReturn($targetPath); $chainMock->expects($this->exactly($setContentCalls))->method('setContent')->with('minified content'); $chainMock->expects($this->any())->method('getContent')->willReturn('original content'); $chainMock->expects($this->any())->method('getOrigAssetPath')->willReturn($originalPath); $this->adapterMock->expects($this->exactly($minifyCalls))->method('minify')->with('original content')->willReturn('minified content'); $this->minificationMock->expects($this->any())->method('isEnabled')->willReturnMap([['css', $isEnabled]]); $this->minificationMock->expects($this->any())->method('isMinifiedFilename')->willReturnMap([['test.min.css', true], ['test.jpeg', false], ['test.css', false]]); $this->minify->process($chainMock); }
public function testResolveJsToNotFindMinifiedVersion() { $this->resolverMock ->expects($this->exactly(2)) ->method('resolve') ->willReturnMap( [ ['', 'test.js', null, null, null, null, '/var/test.js'], ['', 'test.min.js', null, null, null, null, false] ] ); $this->assetMinificationMock->expects($this->once()) ->method('isMinifiedFilename') ->willReturn(false); $this->assetMinificationMock->expects($this->once()) ->method('isEnabled') ->with('js') ->willReturn(true); $this->assetMinificationMock->expects($this->once()) ->method('addMinifiedSign') ->with('test.js') ->willReturn('test.min.js'); $this->assertEquals('/var/test.js', $this->minification->resolve('', 'test.js')); }
public function testGetMinResolverRelativePath() { $this->minificationMock->expects($this->any())->method('addMinifiedSign')->willReturnArgument(0); $this->context->expects($this->once())->method('getConfigPath')->will($this->returnValue('path')); $actual = $this->object->getMinResolverRelativePath(); $this->assertSame('path/requirejs-min-resolver.js', $actual); }
/** * @return void * @covers \Magento\Framework\View\Asset\Bundle::getAssetKey * @covers \Magento\Framework\View\Asset\Bundle::save */ public function testMinSuffix() { $this->minificationMock->expects($this->any())->method('addMinifiedSign')->withConsecutive(['onefile.js'], ['onefile.js'], ['/js/bundle/bundle0.js'])->willReturnOnConsecutiveCalls('onefile.min.js', 'onefile.min.js', '/js/bundle/bundle0.min.js'); $contextMock = $this->getMockBuilder('Magento\\Framework\\View\\Asset\\File\\FallbackContext')->disableOriginalConstructor()->getMock(); $contextMock->expects($this->any())->method('getAreaCode')->willReturn('area'); $contextMock->expects($this->any())->method('getThemePath')->willReturn('theme-path'); $contextMock->expects($this->any())->method('getLocale')->willReturn('locale'); $assetMock = $this->getMockBuilder('Magento\\Framework\\View\\Asset\\LocalInterface')->setMethods(['getContentType', 'getContext'])->getMockForAbstractClass(); $assetMock->expects($this->any())->method('getContext')->willReturn($contextMock); $assetMock->expects($this->any())->method('getContentType')->willReturn('js'); $assetMock->expects($this->any())->method('getFilePath')->willReturn('onefile.js'); $writeMock = $this->getMockBuilder('Magento\\Framework\\Filesystem\\Directory\\WriteInterface')->getMockForAbstractClass(); $writeMock->expects($this->once())->method('writeFile')->with('/js/bundle/bundle0.min.js', $this->stringContains('onefile.min.js')); $this->filesystemMock->expects($this->any())->method('getDirectoryWrite')->willReturn($writeMock); $this->bundle->addAsset($assetMock); $this->bundle->flush(); }
/** * @return void */ public function testGetExcludes() { $this->scopeConfigMock->expects($this->once())->method('getValue')->with('dev/js/minify_exclude')->willReturn(" /tiny_mce/ \n" . " /tiny_mce2/ "); $expected = ['/tiny_mce/', '/tiny_mce2/']; $this->assertEquals($expected, $this->minification->getExcludes('js')); /** check cache: */ $this->assertEquals($expected, $this->minification->getExcludes('js')); }
public function testSetLayout() { $this->bundleConfig->expects($this->once())->method('isBundlingJsFiles')->willReturn(true); $filePath = 'require_js_fie_path'; $asset = $this->getMockForAbstractClass('\\Magento\\Framework\\View\\Asset\\LocalInterface'); $asset->expects($this->atLeastOnce())->method('getFilePath')->willReturn($filePath); $requireJsAsset = $this->getMockForAbstractClass('\\Magento\\Framework\\View\\Asset\\LocalInterface'); $requireJsAsset->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())->method('createRequireJsConfigAsset')->will($this->returnValue($requireJsAsset)); $this->fileManager->expects($this->once())->method('createRequireJsMixinsAsset')->will($this->returnValue($requireJsAsset)); $this->fileManager->expects($this->once())->method('createStaticJsAsset')->will($this->returnValue($requireJsAsset)); $this->fileManager->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'); $assetCollection = $this->getMockBuilder('Magento\\Framework\\View\\Asset\\GroupedCollection')->disableOriginalConstructor()->getMock(); $this->pageConfig->expects($this->atLeastOnce())->method('getAssetCollection')->willReturn($assetCollection); $assetCollection->expects($this->atLeastOnce())->method('insert')->willReturn(true); $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); }
/** * @param array $types * @return void */ protected function save($types) { $dir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW); $bundlePath = ''; foreach ($types as $parts) { /** @var FallbackContext $context */ $context = reset(reset($parts)['assets'])->getContext(); $bundlePath = empty($bundlePath) ? $context->getPath() . Manager::BUNDLE_PATH : $bundlePath; $this->fillContent($parts, $context); } $this->content[max(0, count($this->content) - 1)] .= $this->getInitJs(); foreach ($this->content as $partIndex => $content) { $dir->writeFile($this->minification->addMinifiedSign($bundlePath . $partIndex . '.js'), $content); } }
/** * Get path of file after using fallback rules * * @param string $type * @param string $file * @param string|null $area * @param ThemeInterface|null $theme * @param string|null $locale * @param string|null $module * @return string|false */ private function resolveJsMinification( $type, $file, $area = null, ThemeInterface $theme = null, $locale = null, $module = null ) { $path = $this->fallback->resolve($type, $file, $area, $theme, $locale, $module); /** * Minified version as priority one */ if ($path && $this->minification->isMinifiedFilename($path)) { return $path; } /** * If minification is disabled - return already found path */ if (!$this->minification->isEnabled('js')) { return $path; } /** * Try to find minified version of file, * or return already found path */ return $this->fallback->resolve( $type, $this->minification->addMinifiedSign($file), $area, $theme, $locale, $module ) ?: $path; }
/** * @return string */ public function getMinResolverCode() { $excludes = []; foreach ($this->minification->getExcludes('js') as $expression) { $excludes[] = '!url.match(/' . str_replace('/', '\\/', $expression) . '/)'; } $excludesCode = empty($excludes) ? 'true' : implode('&&', $excludes); $result = <<<code if (!require.s.contexts._.__load) { require.s.contexts._.__load = require.s.contexts._.load; require.s.contexts._.load = function(id, url) { if ({$excludesCode}) { url = url.replace(/(\\.min)?\\.js\$/, '.min.js'); } return require.s.contexts._.__load.apply(require.s.contexts._, [id, url]); } } code; if ($this->minification->isEnabled('js')) { $result = $this->minifyAdapter->minify($result); } return $result; }