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;
 }
Ejemplo n.º 2
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);
 }
Ejemplo n.º 3
0
 public function filter(Kwf_SourceMaps_SourceMap $sourcemap)
 {
     putenv("NODE_PATH=" . getcwd() . "/node_modules");
     $cmd = getcwd() . "/" . VENDOR_PATH . "/bin/node " . __DIR__ . "/CssChunks.js";
     $cmd .= " 2>&1";
     $process = new Symfony\Component\Process\Process($cmd);
     $process->setInput($sourcemap->getFileContentsInlineMap(false));
     $process->mustRun();
     $out = $process->getOutput();
     $out = explode("\n/* ***** NEXT CHUNK ***** */\n", $out);
     $ret = array();
     foreach ($out as $chunk) {
         if (Kwf_SourceMaps_SourceMap::hasInline($chunk)) {
             $mapChunk = Kwf_SourceMaps_SourceMap::createFromInline($chunk);
         } else {
             $mapChunk = Kwf_SourceMaps_SourceMap::createEmptyMap($chunk);
             $mapChunk->setMimeType('text/css');
         }
         $ret[] = $mapChunk;
     }
     return $ret;
 }