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 static function pack($data) { $cmd = getcwd() . "/" . VENDOR_PATH . "/bin/node " . __DIR__ . "/BrowserPack.js"; $process = new Symfony\Component\Process\Process($cmd); $process->setInput(json_encode($data)); $process->mustRun(); return $process->getOutput(); }
public static function count($contents) { $cmd = getcwd() . "/" . VENDOR_PATH . "/bin/node " . KWF_PATH . "/Kwf/Assets/Util/CssSelectorCount.js"; $cmd .= " 2>&1"; $process = new Symfony\Component\Process\Process($cmd); $process->setEnv(array('NODE_PATH' => getcwd() . '/node_modules')); $process->setInput($contents); $process->mustRun(); $out = json_decode($process->getOutput(), true); return $out['selectors']; }
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; }