public function filter(Kwf_SourceMaps_SourceMap $sourcemap) { $temp = tempnam('temp/', 'commonjs'); file_put_contents($temp, $sourcemap->getFileContents()); $parsedFile = Kwf_Assets_CommonJs_ModuleDepsParser::parse($temp); unlink($temp); $newSourceMap = Kwf_SourceMaps_SourceMap::createEmptyMap($parsedFile['source']); if (isset($sourcemap->getMapContentsData(false)->{'_x_org_koala-framework_trlData'})) { $newSourceMap->getMapContentsData(false)->{'_x_org_koala-framework_trlData'} = $sourcemap->getMapContentsData(false)->{'_x_org_koala-framework_trlData'}; } if (isset($sourcemap->getMapContentsData(false)->{'_x_org_koala-framework_masterFiles'})) { $newSourceMap->getMapContentsData(false)->{'_x_org_koala-framework_masterFiles'} = $sourcemap->getMapContentsData(false)->{'_x_org_koala-framework_masterFiles'}; } return $newSourceMap; }
public function getContentsPacked() { $mtime = filemtime(__DIR__ . "/build.js"); $it = new RecursiveDirectoryIterator(getcwd() . '/' . VENDOR_PATH . '/bower_components/tinymce/js/tinymce'); $it = new RecursiveIteratorIterator($it); foreach ($it as $i) { $mtime = max($mtime, $i->getMTime()); } if (!file_exists('temp/tinymce-build-out.js.mtime') || file_get_contents('temp/tinymce-build-out.js.mtime') != $mtime) { $cmd = getcwd() . "/" . VENDOR_PATH . "/bin/node " . __DIR__ . "/build.js"; putenv("NODE_PATH=" . getcwd() . "/node_modules"); exec($cmd, $out, $ret); putenv("NODE_PATH="); if ($ret) { throw new Kwf_Exception("tinymce build failed: " . implode("\n", $out)); } if (!file_exists('temp/tinymce-build-out.js')) { throw new Kwf_Exception("TinyMce build not found"); } file_put_contents('temp/tinymce-build-out.js.mtime', $mtime); } $buildFile = sys_get_temp_dir() . '/kwf-uglifyjs/tinymce/v2-' . md5(file_get_contents('temp/tinymce-build-out.js')); if (!file_exists("{$buildFile}.min.js")) { $dir = dirname($buildFile); if (!file_exists($dir)) { mkdir($dir, 0777, true); } copy('temp/tinymce-build-out.js', $buildFile); Kwf_Assets_Dependency_Filter_UglifyJs::build($buildFile, '/assets/web/temp/tinymce-build-out.js'); } $ret = new Kwf_SourceMaps_SourceMap(file_get_contents("{$buildFile}.min.js.map.json"), file_get_contents("{$buildFile}.min.js")); $data = $ret->getMapContentsData(false); $data->{'_x_org_koala-framework_sourcesContent'}[0] = file_get_contents('temp/tinymce-build-out.js'); return $ret; }
public static function run($pluginsInitCode, Kwf_SourceMaps_SourceMap $sourcemap) { $js = "\n require('es6-promise').polyfill(); //required for older nodejs\n var postcss = require('postcss');\n var plugins = [];\n " . $pluginsInitCode . "\n var instance = postcss( plugins );\n var css = '';\n process.stdin.setEncoding('utf-8')\n process.stdin.on('data', function(buf) { css += buf.toString(); });\n process.stdin.on('end', function() {\n instance.process(css).then(function (result) {\n process.stdout.write(result.css);\n }).catch(function(e) {\n console.log(e);\n process.exit(1);\n });\n });\n process.stdin.resume();\n "; $runfile = tempnam("temp/", 'postcss'); file_put_contents($runfile, $js); putenv("NODE_PATH=" . getcwd() . "/node_modules" . PATH_SEPARATOR . getcwd() . "/" . KWF_PATH . PATH_SEPARATOR . getcwd() . "/"); $cmd = getcwd() . "/" . VENDOR_PATH . "/bin/node " . $runfile; $cmd .= " 2>&1"; $process = new Symfony\Component\Process\Process($cmd); $mapData = $sourcemap->getMapContentsData(false); $hasSourcemap = !!$mapData->mappings; if ($hasSourcemap) { $process->setInput($sourcemap->getFileContentsInlineMap(false)); } else { $process->setInput($sourcemap->getFileContents()); } if ($process->run() !== 0) { throw new Kwf_Exception("Process '{$cmd}' failed with " . $process->getExitCode() . "\n" . $process->getOutput()); } putenv("NODE_PATH="); unlink($runfile); $out = $process->getOutput(); if (Kwf_SourceMaps_SourceMap::hasInline($out)) { $ret = Kwf_SourceMaps_SourceMap::createFromInline($out); } else { $ret = Kwf_SourceMaps_SourceMap::createEmptyMap($out); $ret->setMimeType('text/css'); } $ret->setSources($sourcemap->getSources()); return $ret; }
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 function save(Kwf_SourceMaps_SourceMap $map, $cacheId) { $this->_cache[$cacheId] = $map; $cacheFile = 'cache/assetdeps/' . md5($cacheId) . 'v2'; $data = $map->getMapContentsData(true); //this will trigger _generateMappings file_put_contents($cacheFile, serialize($map)); $masterFiles = array(); if (isset($data->{'_x_org_koala-framework_masterFiles'})) { foreach ($data->{'_x_org_koala-framework_masterFiles'} as $f) { $masterFiles[] = array('file' => $f, 'hash' => $this->_hashFile($f)); } } file_put_contents($cacheFile . '.masterFiles', serialize($masterFiles)); }
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; }
/** * Concat sourcemaps and keep mappings intact * * This is implemented very efficent by avoiding to parse the whole mappings string. */ public function concat(Kwf_SourceMaps_SourceMap $other) { if ($this->_mappingsChanged) { $this->_generateMappings(); } $missingLines = substr_count($this->_fileContents, "\n") - substr_count($this->_map->mappings, ";"); if ($missingLines > 0) { $this->_map->mappings .= str_repeat(';', $missingLines); } if (!isset($this->_map->{'_x_org_koala-framework_last'})) { $this->_addLastExtension(); } if (strlen($this->_fileContents) > 0) { if (substr($this->_fileContents, -1) != "\n") { $this->_fileContents .= "\n"; $this->_map->mappings .= ';'; } } $this->_fileContents .= $other->_fileContents; $data = $other->getMapContentsData(); if ($this->_map->mappings) { $previousFileLast = $this->_map->{'_x_org_koala-framework_last'}; } else { $previousFileLast = (object) array('source' => 0, 'originalLine' => 0, 'originalColumn' => 0, 'name' => 0); } if (!$data->mappings) { $data->mappings = str_repeat(';', substr_count($other->_fileContents, "\n")); $data->{'_x_org_koala-framework_last'} = (object) array('source' => -1, 'originalLine' => $previousFileLast->originalLine, 'originalColumn' => $previousFileLast->originalColumn, 'name' => -1); } $previousFileSourcesCount = count($this->_map->sources); $previousFileNamesCount = count($this->_map->names); if ($previousFileLast->source > $previousFileSourcesCount) { if ($previousFileSourcesCount != 0 && $previousFileLast->source != 0) { throw new Exception("Invalid last source, must not be higher than sources"); } } if ($previousFileLast->name > $previousFileNamesCount) { if ($previousFileNamesCount != 0 && $previousFileLast->name != 0) { throw new Exception("Invalid last name, must not be higher than names"); } } if ($data->sources) { foreach ($data->sources as $s) { $this->_map->sources[] = $s; } } if ($data->names) { foreach ($data->names as $n) { $this->_map->names[] = $n; } } $otherMappings = $data->mappings; $str = ''; $otherMappingsEnd = strlen($otherMappings); $otherMappingsPos = 0; while ($otherMappingsPos < $otherMappingsEnd && $otherMappings[$otherMappingsPos] === ';') { $str .= $otherMappings[$otherMappingsPos]; $otherMappingsPos++; } if ($otherMappingsPos < $otherMappingsEnd) { // Generated column. $str .= Kwf_SourceMaps_Base64VLQ::encode(Kwf_SourceMaps_Base64VLQ::decodePos($otherMappings, $otherMappingsPos)); if ($otherMappingsPos < $otherMappingsEnd && !($otherMappings[$otherMappingsPos] == ',' || $otherMappings[$otherMappingsPos] == ';')) { // Original source. $value = Kwf_SourceMaps_Base64VLQ::decodePos($otherMappings, $otherMappingsPos); if ($previousFileSourcesCount) { $absoluteValue = $value + $previousFileSourcesCount; $value = $absoluteValue - $previousFileLast->source; } $str .= Kwf_SourceMaps_Base64VLQ::encode($value); // Original line. $str .= Kwf_SourceMaps_Base64VLQ::encode(Kwf_SourceMaps_Base64VLQ::decodePos($otherMappings, $otherMappingsPos) - $previousFileLast->originalLine); // Original column. $str .= Kwf_SourceMaps_Base64VLQ::encode(Kwf_SourceMaps_Base64VLQ::decodePos($otherMappings, $otherMappingsPos) - $previousFileLast->originalColumn); // Original name. if ($otherMappingsPos < $otherMappingsEnd && !($otherMappings[$otherMappingsPos] == ',' || $otherMappings[$otherMappingsPos] == ';')) { $value = Kwf_SourceMaps_Base64VLQ::decodePos($otherMappings, $otherMappingsPos); if ($previousFileNamesCount) { $absoluteValue = $value + $previousFileNamesCount; $value = $absoluteValue - $previousFileLast->name; } $str .= Kwf_SourceMaps_Base64VLQ::encode($value); } else { if (!count($data->names)) { //file doesn't have names at all, we don't have to adjust that offset } else { //loop thru mappings until we find a block with name while ($otherMappingsPos < $otherMappingsEnd) { if ($otherMappings[$otherMappingsPos] === ';') { $str .= $otherMappings[$otherMappingsPos]; $otherMappingsPos++; } else { if ($otherMappings[$otherMappingsPos] === ',') { $str .= $otherMappings[$otherMappingsPos]; $otherMappingsPos++; } else { // Generated column. $str .= Kwf_SourceMaps_Base64VLQ::encode(Kwf_SourceMaps_Base64VLQ::decodePos($otherMappings, $otherMappingsPos)); if ($otherMappingsPos < $otherMappingsEnd && !($otherMappings[$otherMappingsPos] == ',' || $otherMappings[$otherMappingsPos] == ';')) { // Original source. $str .= Kwf_SourceMaps_Base64VLQ::encode(Kwf_SourceMaps_Base64VLQ::decodePos($otherMappings, $otherMappingsPos)); // Original line. $str .= Kwf_SourceMaps_Base64VLQ::encode(Kwf_SourceMaps_Base64VLQ::decodePos($otherMappings, $otherMappingsPos)); // Original column. $str .= Kwf_SourceMaps_Base64VLQ::encode(Kwf_SourceMaps_Base64VLQ::decodePos($otherMappings, $otherMappingsPos)); if ($otherMappingsPos < $otherMappingsEnd && !($otherMappings[$otherMappingsPos] == ',' || $otherMappings[$otherMappingsPos] == ';')) { // Original name. $value = Kwf_SourceMaps_Base64VLQ::decodePos($otherMappings, $otherMappingsPos); if ($previousFileNamesCount) { $absoluteValue = $value + $previousFileNamesCount; $value = $absoluteValue - $previousFileLast->name; } $str .= Kwf_SourceMaps_Base64VLQ::encode($value); break; } } } } } } } } } $this->_map->mappings .= $str . substr($otherMappings, $otherMappingsPos); $this->_map->{'_x_org_koala-framework_last'} = (object) array('source' => $previousFileSourcesCount + $data->{'_x_org_koala-framework_last'}->source, 'name' => $previousFileNamesCount + $data->{'_x_org_koala-framework_last'}->name, 'originalLine' => $data->{'_x_org_koala-framework_last'}->originalLine, 'originalColumn' => $data->{'_x_org_koala-framework_last'}->originalColumn); }