예제 #1
0
 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;
 }