/** * 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); }
/** * @param bool $configFlag * @param string $appMode * @param bool $result * @dataProvider isEnabledDataProvider * @return void */ public function testIsAssetMinification($configFlag, $appMode, $result) { $contentType = 'content type'; $this->scopeConfigMock->expects($this->any())->method('isSetFlag')->with(sprintf(Minification::XML_PATH_MINIFICATION_ENABLED, $contentType), ScopeInterface::SCOPE_STORE)->willReturn($configFlag); $this->appStateMock->expects($this->any())->method('getMode')->willReturn($appMode); $this->assertEquals($result, $this->minification->isEnabled($contentType)); }
/** * 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); } }
/** * 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; }