/** * 处理media资源,地址替换 */ private function handleMediaResource() { static $reg = null; if (is_null($reg)) { $processor = parent::getInstance('media'); $mediaTypes = $processor->getOptions(); $mediaTypes = comma_str_to_array($mediaTypes['type']); if (!empty($mediaTypes)) { $reg = '/(?<=[\'\\"])(?:' . str_replace('/', '\\/', C('STATIC_VIRTUAL_PREFIX')) . '[^\'\\"\\\\]{1,300})\\.(?:' . implode('|', $mediaTypes) . ')(?=[\'\\"\\\\])/'; } else { $reg = false; } } if ($reg) { $this->contents = preg_replace_callback($reg, array($this, 'replaceMediaPath'), $this->contents); } }
/** * 编译入口 */ public function run() { // 派发预处理开始事件 trigger('process_start', $this); $this->rmOldBuildFile(); foreach ($this->options['process'] as $item) { trigger('one_process_start', $this, $item); if (empty($item['name'])) { // 如果不存在name,则用预处理器类型名,作为name // name的作用用于生成map的key,及文件名,使统一处理器,可以处理多种类型文件分别生成不同的map $item['name'] = $item['processor']; } // 初始化map if (!isset($this->map[$item['name']])) { $this->map[$item['name']] = array(); } // 初始化预处理器 $processor = new stdClass(); $processor->return = null; trigger('processor_init', $this, $item, $processor); $processor = is_null($processor->return) ? Preprocess::getInstance($item['processor'], $this->map, $item) : $processor->return; // 如果处理图片,则需要先处理合图 if ($processor instanceof MediaPreprocess && strpos($item['type'], 'png') !== false && C('IS_MERGE_IMAGE')) { $this->processSprite($processor, $item); } // 根据扫描目录和类型,扫描到所有待编译文件 $fileList = $this->getFileList($item['from'], $item['type'], C('SRC.SRC_PATH')); foreach ($fileList as $file) { mark('处理文件:' . $file); $processor->setFile($file); $processor->process(); $processor->compress(); $path = $processor->getRelativePath(); $buildPath = $this->writeBuildFile($processor, $item, $path); // 更新map $this->updateMap($item['name'], $path, $buildPath); } trigger('one_process_end', $this, $item); $processor->end(); // 一种类型的文件处理完后,导出map到相应文件中 $this->exportMapByType($item['name']); } $this->copyToTestEnv(); // 派发预处理结束事件 trigger('process_end', $this); }
public function run($params) { $item = $params[2]; if ($this->options['requirejs.path'] && $item['processor'] === 'js') { mark('requireJs插件开始处理' . $this->options['requirejs.path'], 'emphasize'); $this->options['requirejs.path'] = C('SRC.SRC_PATH') . $this->options['requirejs.path']; if (!file_exists($this->options['requirejs.path'])) { mark('"' . $this->options['requirejs.path'] . '"不存在,请检查m3d.php中requireJs配置', 'error'); return; } $tool = $params[1]; $map = $this->getMap($tool); $script = $this->genScript($map); $processor = Preprocess::getInstance('js'); $mapFiles = $this->options['requirejs.path']; if (is_string($mapFiles)) { $mapFiles = array($mapFiles); } foreach ($mapFiles as $file) { $processor->setFile($file); $processor->setContents($script); $processor->process(); $processor->compress(); $path = $processor->getRelativePath(); $buildPath = $tool->writeBuildFile($processor, $item, $path); $oldBuildPath = $tool->getMap('js', $path); if ($buildPath !== $oldBuildPath) { $tool->updateMap('js', $path, $buildPath); // 清除文件 $file = C('SRC.BUILD_PATH') . $oldBuildPath; if (file_exists($file)) { unlink($file); } $file = C('SRC.BUILD_CACHE_PATH') . $oldBuildPath; if (file_exists($file)) { unlink($file); } trigger('change_file', $path); } } } }
/** * 处理js地址 * @param simple_html_dom $html */ private function handleJs(simple_html_dom $html) { $lDelimiter = C('SRC.SMARTY_LEFT_DELIMITER'); $rDelimiter = C('SRC.SMARTY_RIGHT_DELIMITER'); foreach ($html->find('script') as $value) { if (!empty($value->src)) { $this->replacePath('js', $value, 'src'); } else { // 处理内联js if (isset($value->_xcompress) && $value->_xcompress === 'true') { $processor = Preprocess::getInstance('js'); $processor->setFile($this->path); $processor->setContents($value->innertext); $processor->process(); $processor->compress(); $text = $lDelimiter . 'literal' . $rDelimiter; $text .= $processor->getContents(); $text .= $lDelimiter . '/literal' . $rDelimiter; $value->innertext = $text; $value->_xcompress = null; } } } }