예제 #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;
 }
예제 #2
0
 public function testWithNoMapping2()
 {
     $map = new Kwf_SourceMaps_SourceMap(Kwf_SourceMaps_TestData::$testSmallMap1, Kwf_SourceMaps_TestData::$testSmallGeneratedCode1);
     $map1 = Kwf_SourceMaps_SourceMap::createEmptyMap("aaa;\nbbb;");
     $map2 = new Kwf_SourceMaps_SourceMap(Kwf_SourceMaps_TestData::$testSmallMap2, Kwf_SourceMaps_TestData::$testSmallGeneratedCode2);
     $map->concat($map1);
     $map->concat($map2);
     $mappings = $map->getMappings();
     $mappingsOffs = 2;
     $genLineOffs = 1 + 2;
     $this->assertEquals($mappings[$mappingsOffs + 0], array('generatedLine' => $genLineOffs + 1, 'generatedColumn' => 1, 'originalSource' => '/the/root/one2.js', 'originalLine' => 1, 'originalColumn' => 1, 'originalName' => null));
     $this->assertEquals($mappings[$mappingsOffs + 1], array('generatedLine' => $genLineOffs + 1, 'generatedColumn' => 5, 'originalSource' => '/the/root/one2.js', 'originalLine' => 1, 'originalColumn' => 5, 'originalName' => null));
 }
예제 #3
0
 public function testAdd()
 {
     $map = new Kwf_SourceMaps_SourceMap(Kwf_SourceMaps_TestData::$testMap, Kwf_SourceMaps_TestData::$testGeneratedCode);
     $mappings = $map->getMappings();
     $map->addSource('three.js');
     $sources = $map->getSources();
     $this->assertEquals(array('one.js', 'two.js', 'three.js'), $sources);
     $c = $map->getFileContentsInlineMap(false);
     $map = Kwf_SourceMaps_SourceMap::createFromInline($c);
     $this->assertEquals(array('one.js', 'two.js', 'three.js'), $sources);
     $map->addMapping(1, 1, 1, 1, 'one.js');
     $c = $map->getFileContentsInlineMap(false);
     $this->assertEquals(array('one.js', 'two.js', 'three.js'), $sources);
 }
 public function testStringReplaceMultipleInOneLine()
 {
     $map = new Kwf_SourceMaps_SourceMap(Kwf_SourceMaps_TestData::$testMap, Kwf_SourceMaps_TestData::$testGeneratedCode);
     $map->stringReplace('a', 'xbbbbxxxxxxxx');
     //0        1         2         3         4
     //1234567890123456789012345678901234567890123
     //   ONE.foo=function(a){return baz(a);};
     $s = " ONE.foo=function(xbbbbxxxxxxxx){return bxbbbbxxxxxxxxz(xbbbbxxxxxxxx);};\n" . " TWO.inc=function(xbbbbxxxxxxxx){return xbbbbxxxxxxxx+1;};";
     $this->assertEquals($map->getFileContents(), $s);
     $mappings = $map->getMappings();
     $this->assertEquals($mappings[3], array('generatedLine' => 1, 'generatedColumn' => 18, 'originalSource' => '/the/root/one.js', 'originalLine' => 1, 'originalColumn' => 21, 'originalName' => 'bar'));
     $this->assertEquals($mappings[4], array('generatedLine' => 1, 'generatedColumn' => 21 + 12, 'originalSource' => '/the/root/one.js', 'originalLine' => 2, 'originalColumn' => 3, 'originalName' => null));
     $this->assertEquals($mappings[5], array('generatedLine' => 1, 'generatedColumn' => 28 + 12, 'originalSource' => '/the/root/one.js', 'originalLine' => 2, 'originalColumn' => 10, 'originalName' => 'baz'));
     $this->assertEquals($mappings[6], array('generatedLine' => 1, 'generatedColumn' => 32 + 12 + 12, 'originalSource' => '/the/root/one.js', 'originalLine' => 2, 'originalColumn' => 14, 'originalName' => 'bar'));
     $this->assertEquals($mappings[10], array('generatedLine' => 2, 'generatedColumn' => 18, 'originalSource' => '/the/root/two.js', 'originalLine' => 1, 'originalColumn' => 21, 'originalName' => 'n'));
     $this->assertEquals($mappings[11], array('generatedLine' => 2, 'generatedColumn' => 21 + 12, 'originalSource' => '/the/root/two.js', 'originalLine' => 2, 'originalColumn' => 3, 'originalName' => null));
     $this->assertEquals($mappings[12], array('generatedLine' => 2, 'generatedColumn' => 28 + 12, 'originalSource' => '/the/root/two.js', 'originalLine' => 2, 'originalColumn' => 10, 'originalName' => 'n'));
 }
예제 #5
0
 public function testGetMappings()
 {
     $map = new Kwf_SourceMaps_SourceMap(Kwf_SourceMaps_TestData::$testMap, Kwf_SourceMaps_TestData::$testGeneratedCode);
     $mappings = $map->getMappings();
     $this->assertEquals(count($mappings), 13);
     $this->assertEquals($mappings[0], array('generatedLine' => 1, 'generatedColumn' => 1, 'originalSource' => '/the/root/one.js', 'originalLine' => 1, 'originalColumn' => 1, 'originalName' => null));
     $this->assertEquals($mappings[1], array('generatedLine' => 1, 'generatedColumn' => 5, 'originalSource' => '/the/root/one.js', 'originalLine' => 1, 'originalColumn' => 5, 'originalName' => null));
     $this->assertEquals($mappings[2], array('generatedLine' => 1, 'generatedColumn' => 9, 'originalSource' => '/the/root/one.js', 'originalLine' => 1, 'originalColumn' => 11, 'originalName' => null));
     $this->assertEquals($mappings[3], array('generatedLine' => 1, 'generatedColumn' => 18, 'originalSource' => '/the/root/one.js', 'originalLine' => 1, 'originalColumn' => 21, 'originalName' => 'bar'));
     $this->assertEquals($mappings[4], array('generatedLine' => 1, 'generatedColumn' => 21, 'originalSource' => '/the/root/one.js', 'originalLine' => 2, 'originalColumn' => 3, 'originalName' => null));
     $this->assertEquals($mappings[5], array('generatedLine' => 1, 'generatedColumn' => 28, 'originalSource' => '/the/root/one.js', 'originalLine' => 2, 'originalColumn' => 10, 'originalName' => 'baz'));
     $this->assertEquals($mappings[6], array('generatedLine' => 1, 'generatedColumn' => 32, 'originalSource' => '/the/root/one.js', 'originalLine' => 2, 'originalColumn' => 14, 'originalName' => 'bar'));
     $this->assertEquals($mappings[7], array('generatedLine' => 2, 'generatedColumn' => 1, 'originalSource' => '/the/root/two.js', 'originalLine' => 1, 'originalColumn' => 1, 'originalName' => null));
     $this->assertEquals($mappings[8], array('generatedLine' => 2, 'generatedColumn' => 5, 'originalSource' => '/the/root/two.js', 'originalLine' => 1, 'originalColumn' => 5, 'originalName' => null));
     $this->assertEquals($mappings[9], array('generatedLine' => 2, 'generatedColumn' => 9, 'originalSource' => '/the/root/two.js', 'originalLine' => 1, 'originalColumn' => 11, 'originalName' => null));
     $this->assertEquals($mappings[10], array('generatedLine' => 2, 'generatedColumn' => 18, 'originalSource' => '/the/root/two.js', 'originalLine' => 1, 'originalColumn' => 21, 'originalName' => 'n'));
     $this->assertEquals($mappings[11], array('generatedLine' => 2, 'generatedColumn' => 21, 'originalSource' => '/the/root/two.js', 'originalLine' => 2, 'originalColumn' => 3, 'originalName' => null));
     $this->assertEquals($mappings[12], array('generatedLine' => 2, 'generatedColumn' => 28, 'originalSource' => '/the/root/two.js', 'originalLine' => 2, 'originalColumn' => 10, 'originalName' => 'n'));
 }
 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;
 }