public function getContentsPacked($language) { $paths = self::_getAllPaths(); $pathType = $this->getType(); $f = $paths[$pathType] . substr($this->_builtFile, strpos($this->_builtFile, '/')); $contents = file_get_contents($f); $contents = rtrim($contents); $contents = explode("\n", $contents); if (substr($contents[count($contents) - 1], 0, 21) == '//# sourceMappingURL=') { //remove sourceMappingURL comment unset($contents[count($contents) - 1]); } $contents = implode("\n", $contents); $paths = self::_getAllPaths(); $pathType = $this->getType(); $f = $paths[$pathType] . substr($this->_sourceMapFile, strpos($this->_sourceMapFile, '/')); $mapContents = file_get_contents($f); $cacheFile = sys_get_temp_dir() . '/kwf-uglifyjs/' . $this->getFileNameWithType() . '.map.' . md5($mapContents); if (!file_exists($cacheFile)) { $map = new Kwf_SourceMaps_SourceMap($mapContents, $contents); if (!is_dir(dirname($cacheFile))) { mkdir(dirname($cacheFile), 0777, true); } $data = $map->getMapContentsData(); if (count($data->sources) != 1) { throw new Kwf_Exception('map must consist only of a single source'); } $data->sources = array($this->getFileNameWithType()); $map->save($cacheFile); } else { $map = new Kwf_SourceMaps_SourceMap(file_get_contents($cacheFile), $contents); } return $map; }
public static function build($buildFile, $sourceFileUrl) { $uglifyjs = getcwd() . "/" . VENDOR_PATH . "/bin/node " . getcwd() . '/node_modules/uglify-js/bin/uglifyjs'; $cmd = "{$uglifyjs} "; $cmd .= "--source-map " . escapeshellarg("{$buildFile}.min.js.map.json") . ' '; $cmd .= "--prefix 2 "; $cmd .= "--output " . escapeshellarg("{$buildFile}.min.js") . ' '; $cmd .= escapeshellarg($buildFile); $cmd .= " 2>&1"; $out = array(); exec($cmd, $out, $retVal); if ($retVal) { throw new Kwf_Exception("uglifyjs failed: " . implode("\n", $out)); } $contents = file_get_contents("{$buildFile}.min.js"); $contents = str_replace("\n//# sourceMappingURL={$buildFile}.min.js.map.json", '', $contents); $mapData = json_decode(file_get_contents("{$buildFile}.min.js.map.json"), true); if (count($mapData['sources']) > 1) { throw new Kwf_Exception("uglifyjs must not return multiple sources, " . count($mapData['sources']) . " returned for '{$buildFile}'"); } unset($mapData['file']); $mapData['sources'][0] = $sourceFileUrl; file_put_contents("{$buildFile}.min.js.map.json", json_encode($mapData)); $map = new Kwf_SourceMaps_SourceMap(file_get_contents("{$buildFile}.min.js.map.json"), $contents); $mappings = $map->getMappings(); foreach ($mappings as $k => $m) { $mappings[$k]['originalName'] = null; } $map->setMappings($mappings); $map->save("{$buildFile}.min.js.map.json", "{$buildFile}.min.js"); //adds last extension return $map; }
public function getContentsPacked() { $paths = $this->_providerList->getPathTypes(); $pathType = $this->getType(); $f = $paths[$pathType] . substr($this->_builtFile, strpos($this->_builtFile, '/')); $contents = file_get_contents($f); $contents = rtrim($contents); $contents = explode("\n", $contents); if (substr($contents[count($contents) - 1], 0, 21) == '//# sourceMappingURL=') { //remove sourceMappingURL comment unset($contents[count($contents) - 1]); } $contents = implode("\n", $contents); $pathType = $this->getType(); $f = $paths[$pathType] . substr($this->_sourceMapFile, strpos($this->_sourceMapFile, '/')); $mapContents = file_get_contents($f); $cacheFile = sys_get_temp_dir() . '/kwf-uglifyjs/' . $this->getFileNameWithType() . '.map.v3.' . md5($mapContents); if (!file_exists($cacheFile)) { $map = new Kwf_SourceMaps_SourceMap($mapContents, $contents); $mappings = $map->getMappings(); foreach ($mappings as $k => $m) { $mappings[$k]['originalName'] = null; } $map->setMappings($mappings); if (!is_dir(dirname($cacheFile))) { mkdir(dirname($cacheFile), 0777, true); } $data = $map->getMapContentsData(); if (count($data->sources) != 1) { throw new Kwf_Exception('map must consist only of a single source'); } $data->sources = array('/assets/' . $this->getFileNameWithType()); $data->{'_x_org_koala-framework_masterFiles'} = array($this->getAbsoluteFileName()); $map->save($cacheFile); } else { $map = new Kwf_SourceMaps_SourceMap(file_get_contents($cacheFile), $contents); } return $map; }
public function warmupCaches() { $cacheFile = $this->_getCacheFileName(); $useCache = false; if (file_exists("{$cacheFile}.sourcetimes")) { $useCache = true; $sourceTimes = unserialize(file_get_contents("{$cacheFile}.sourcetimes")); foreach ($sourceTimes as $i) { if ($i['mtime']) { if (!file_exists($i['file']) || filemtime($i['file']) != $i['mtime']) { //file was modified or deleted $useCache = false; break; } } else { if (file_exists($i['file'])) { //file didn't exist, was created $useCache = false; break; } } } } if (!$useCache) { $fileName = $this->getAbsoluteFileName(); static $loadPath; if (!isset($loadPath)) { $loadPath = array(); foreach (glob(VENDOR_PATH . '/bower_components/*') as $p) { $bowerMain = null; $mainExt = null; if (file_exists($p . '/bower.json')) { $bower = json_decode(file_get_contents($p . '/bower.json')); if (isset($bower->main) && is_string($bower->main)) { $bowerMain = $bower->main; $mainExt = substr($bowerMain, -5); } } if ($mainExt == '.scss' || $mainExt == '.sass') { $mainDir = substr($bowerMain, 0, strrpos($bowerMain, '/')); $loadPath[] = $p . '/' . $mainDir; } else { if (file_exists($p . '/scss')) { $loadPath[] = $p . '/scss'; } } } $loadPath[] = './scss'; if (KWF_PATH == '..') { $loadPath[] = substr(getcwd(), 0, strrpos(getcwd(), '/')) . '/sass/Kwf/stylesheets'; } else { $loadPath[] = KWF_PATH . '/sass/Kwf/stylesheets'; } $loadPath = escapeshellarg(implode(PATH_SEPARATOR, $loadPath)); } if (substr($fileName, 0, 2) == './') { $fileName = getcwd() . substr($fileName, 1); } $bin = Kwf_Config::getValue('server.nodeSassBinary'); if (!$bin) { $bin = getcwd() . "/" . VENDOR_PATH . "/bin/node " . dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/node_modules/node-sass/bin/node-sass'; } $cmd = "{$bin} --include-path {$loadPath} --output-style compressed "; $cmd .= " --source-map " . escapeshellarg($cacheFile . '.map'); $cmd .= " " . escapeshellarg($fileName) . " " . escapeshellarg($cacheFile); $cmd .= " 2>&1"; $out = array(); exec($cmd, $out, $retVal); if ($retVal) { throw new Kwf_Exception("compiling sass failed: " . implode("\n", $out)); } $map = json_decode(file_get_contents("{$cacheFile}.map")); $sourceFiles = array(); foreach ($map->sources as $k => $i) { //sources are relative to cache/sass, strip that if (substr($i, 0, 6) != '../../') { throw new Kwf_Exception('source doesn\'t start with ../../'); } $i = substr($i, 6); $f = self::getPathWithTypeByFileName(getcwd() . '/' . $i); if (!$f) { throw new Kwf_Exception("Can't find path for '" . getcwd() . '/' . $i . "'"); } $map->sources[$k] = $f; $sourceFiles[] = $f; if (substr($f, 0, 16) == 'web/scss/config/') { $sourceFiles[] = 'kwf/sass/Kwf/stylesheets/config/' . substr($f, 16); } else { if (substr($f, 0, 32) == 'kwf/sass/Kwf/stylesheets/config/') { $sourceFiles[] = 'web/scss/config/' . substr($f, 32); } } } $map->file = $cacheFile; file_put_contents("{$cacheFile}.map", json_encode($map)); $ret = file_get_contents($cacheFile); $ret = str_replace("@charset \"UTF-8\";\n", '', $ret); //remove charset, no need to adjust sourcemap as sourcemap doesn't include that (bug in libsass) $ret = preg_replace("#/\\*\\# sourceMappingURL=.* \\*/#", '', $ret); $map = new Kwf_SourceMaps_SourceMap(file_get_contents("{$cacheFile}.map"), $ret); if (strpos($ret, 'cssClass') !== false && (strpos($ret, '$cssClass') !== false || strpos($ret, '.cssClass') !== false)) { $cssClass = $this->_getComponentCssClass(); if ($cssClass) { if (strpos($ret, '.cssClass') !== false) { $map->stringReplace('.cssClass', ".{$cssClass}"); } if (strpos($ret, '$cssClass') !== false) { $map->stringReplace('$cssClass', ".{$cssClass}"); } } } if (strpos($ret, 'kwfup-') !== false) { if (Kwf_Config::getValue('application.uniquePrefix')) { $map->stringReplace('kwfup-', Kwf_Config::getValue('application.uniquePrefix') . '-'); } else { $map->stringReplace('kwfup-', ''); } } $usesVars = false; $assetVars = self::getAssetVariables(); foreach ($assetVars as $k => $i) { $search = 'var(' . $k . ')'; if (strpos($ret, $search) !== false) { $map->stringReplace($search, $i); $usesVars = true; } } $map->save("{$cacheFile}.map", $cacheFile); unset($map); $sourceTimes = array(); foreach ($sourceFiles as $f) { $f = new Kwf_Assets_Dependency_File($f); $f = $f->getAbsoluteFileName(); $sourceTimes[] = array('file' => $f, 'mtime' => file_exists($f) ? filemtime($f) : null); } if ($usesVars) { $files = array('assetVariables.ini', 'config.ini', KWF_PATH . '/config.ini'); if (Kwf_Config::getValue('kwc.theme')) { $files[] = Kwf_Config_Web::findThemeConfigIni(Kwf_Config::getValue('kwc.theme')); } foreach ($files as $f) { $sourceTimes[] = array('file' => $f, 'mtime' => file_exists($f) ? filemtime($f) : null); } } file_put_contents("{$cacheFile}.sourcetimes", serialize($sourceTimes)); } }