Пример #1
0
 /**
  * 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);
     }
 }
Пример #2
0
 /**
  * Get path to minified file for specified original file
  *
  * @param string $originalFile path to original file relative to pub/view_cache
  * @param string $targetFile path relative to pub/view_cache
  * @return void
  */
 public function minifyFile($originalFile, $targetFile)
 {
     if ($this->_isUpdateNeeded($targetFile)) {
         $content = $this->rootDirectory->readFile($originalFile);
         $content = $this->adapter->minify($content);
         $this->pubViewCacheDir->writeFile($targetFile, $content);
     }
 }
Пример #3
0
 /**
  * @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);
 }
Пример #4
0
 /**
  * @param int $mtimeOrig
  * @param int $mtimeMinified
  * @param bool $isMinifyExpected
  * @dataProvider minifyMtimeDataProvider
  */
 public function testMinifyMtime($mtimeOrig, $mtimeMinified, $isMinifyExpected)
 {
     $this->prepareAttemptToMinifyMock(true, false);
     $model = new Minified($this->_asset, $this->_logger, $this->_filesystem, $this->_baseUrl, $this->_adapter, Minified::MTIME);
     $this->_rootDir->expects($this->any())->method('getRelativePath')->will($this->returnValueMap([['/foo/bar/test/library.min.js', 'test/library.min.js'], ['/foo/bar/test/library.js', 'test/library.js']]));
     $this->_rootDir->expects($this->once())->method('isExist')->with('test/library.min.js')->will($this->returnValue(false));
     $this->_rootDir->expects($this->once())->method('stat')->with('test/library.js')->will($this->returnValue(['mtime' => $mtimeOrig]));
     $this->_staticViewDir->expects($this->once())->method('stat')->with($this->anything())->will($this->returnValue(['mtime' => $mtimeMinified]));
     if ($isMinifyExpected) {
         $this->_asset->expects($this->once())->method('getContent')->will($this->returnValue('content'));
         $this->_adapter->expects($this->once())->method('minify')->with('content')->will($this->returnValue('mini'));
         $this->_staticViewDir->expects($this->once())->method('writeFile')->with($this->anything(), 'mini');
     } else {
         $this->_adapter->expects($this->never())->method('minify');
     }
     $this->assertStringMatchesFormat('%s_library.min.js', $model->getFilePath());
 }
Пример #5
0
 /**
  * Perform actual minification
  *
  * @return void
  */
 protected function minify()
 {
     $isExists = $this->staticViewDir->isExist($this->path);
     if (!$isExists) {
         $shouldMinify = true;
     } elseif ($this->strategy == self::FILE_EXISTS) {
         $shouldMinify = false;
     } else {
         $origlFile = $this->rootDir->getRelativePath($this->originalAsset->getSourceFile());
         $origMtime = $this->rootDir->stat($origlFile)['mtime'];
         $minMtime = $this->staticViewDir->stat($this->path)['mtime'];
         $shouldMinify = $origMtime != $minMtime;
     }
     if ($shouldMinify) {
         $content = $this->adapter->minify($this->originalAsset->getContent());
         $this->staticViewDir->writeFile($this->path, $content);
     }
 }
Пример #6
0
    public function testGetMinResolverCode()
    {
        $this->minificationMock->expects($this->once())->method('getExcludes')->with('js')->willReturn(['\\.min\\.']);
        $this->minificationMock->expects($this->once())->method('isEnabled')->with('js')->willReturn(true);
        $this->minifyAdapterMock->expects($this->once())->method('minify')->willReturnArgument(0);
        $expected = <<<code
    if (!require.s.contexts._.__load) {
        require.s.contexts._.__load = require.s.contexts._.load;
        require.s.contexts._.load = function(id, url) {
            if (!url.match(/\\.min\\./)) {
                url = url.replace(/(\\.min)?\\.js\$/, '.min.js');
            }
            return require.s.contexts._.__load.apply(require.s.contexts._, [id, url]);
        }
    }

code;
        $this->assertEquals($expected, $this->object->getMinResolverCode());
    }
Пример #7
0
    /**
     * @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;
    }