private function _buildPackageContents($mimeType) { foreach ($this->_providerList->getProviders() as $provider) { $provider->initialize(); } $packageMap = Kwf_SourceMaps_SourceMap::createEmptyMap(''); if ($mimeType == 'text/css' || $mimeType == 'text/css; ie8') { $packageMap->setMimeType('text/css'); } else { $packageMap->setMimeType('text/javascript'); } $trlData = array(); // ***** commonjs $commonJsData = $this->_getCommonJsData($mimeType); if ($commonJsData) { foreach ($commonJsData as &$i) { $data = $i['source']->getMapContentsData(false); if (isset($data->{'_x_org_koala-framework_trlData'})) { $trlData = array_merge($trlData, $data->{'_x_org_koala-framework_trlData'}); } $data->sourcesContent = $data->sources; //browser-pack needs sourcesContent, else it would ignore input source map. This is fake obviously and we'll drop it anyway after browser-pack finished if (isset($i['source']->getMapContentsData(false)->sources[0])) { $i['sourceFile'] = $i['source']->getMapContentsData(false)->sources[0]; } $i['source'] = $i['source']->getFileContentsInlineMap(false); } $contents = 'window.require = ' . Kwf_Assets_CommonJs_BrowserPack::pack(array_values($commonJsData)); $map = Kwf_SourceMaps_SourceMap::createFromInline($contents); $fileContents = $map->getFileContents(); $fileContents .= ";\n"; $map->setFileContents($fileContents); $data = $map->getMapContentsData(false); unset($data->sourcesContent); //drop fake sourcesContent (see comment above) if ($data->sources[0] == 'node_modules/browser-pack/_prelude.js') { $data->sources[0] = '/assets/web/node_modules/browser-pack/_prelude.js'; } $packageMap->concat($map); } // ***** non-commonjs, css $filterMimeType = $mimeType; if ($filterMimeType == 'text/css; ie8') { $filterMimeType = 'text/css'; } foreach ($this->_getFilteredUniqueDependencies($filterMimeType) as $dep) { if ($dep->getIncludeInPackage()) { if (!(($mimeType == 'text/javascript' || $mimeType == 'text/javascript; defer') && $dep->isCommonJsEntry())) { $map = $this->_getFilteredDependencyContents($dep, $mimeType); $data = $map->getMapContentsData(false); if (isset($data->{'_x_org_koala-framework_trlData'})) { $trlData = array_merge($trlData, $data->{'_x_org_koala-framework_trlData'}); } if (strpos($map->getFileContents(), "//@ sourceMappingURL=") !== false && strpos($map->getFileContents(), "//# sourceMappingURL=") !== false) { throw new Kwf_Exception("contents must not contain sourceMappingURL"); } $sourcesCount = 0; $packageData = $packageMap->getMapContentsData(false); if (isset($packageData->sources)) { $sourcesCount = count($packageData->sources); } unset($packageData); // $ret .= "/* *** $dep */\n"; // attention: commenting this in breaks source maps $packageMap->concat($map); if (isset($data->{'_x_org_koala-framework_sourcesContent'})) { $packageMapData = $packageMap->getMapContentsData(false); if (!isset($packageMapData->{'_x_org_koala-framework_sourcesContent'})) { $packageMapData->{'_x_org_koala-framework_sourcesContent'} = array(); } //copy sourcesContent to packageMap with $sourcesCount offset foreach ($data->{'_x_org_koala-framework_sourcesContent'} as $k => $i) { $packageMapData->{'_x_org_koala-framework_sourcesContent'}[$k + $sourcesCount] = $i; } } } } } if ($mimeType == 'text/javascript' || $mimeType == 'text/javascript; defer') { if ($uniquePrefix = Kwf_Config::getValue('application.uniquePrefix')) { $packageMap = Kwf_Assets_Package_Filter_UniquePrefix::filter($packageMap, $uniquePrefix); } } foreach ($this->getProviderList()->getFilters() as $filter) { if ($filter->getExecuteFor() == Kwf_Assets_Filter_Abstract::EXECUTE_FOR_PACKAGE && $filter->getMimeType() == $mimeType) { $packageMap = $filter->filter($packageMap); } } $data = $packageMap->getMapContentsData(false); $data->sourcesContent = array(); foreach ($data->sources as $k => $i) { if (substr($i, 0, 8) != '/assets/') { throw new Kwf_Exception("Source path doesn't start with /assets/: {$i}"); } $i = substr($i, 8); if (isset($data->{'_x_org_koala-framework_sourcesContent'}[$k])) { $data->sourcesContent[$k] = $data->{'_x_org_koala-framework_sourcesContent'}[$k]; } else { $i = new Kwf_Assets_Dependency_File($this->_providerList, $i); $data->sourcesContent[$k] = $i->getContentsSourceString(); } } return array('contents' => $packageMap, 'trlData' => $trlData); }
public function getPackageContents($mimeType, $language, $includeSourceMapComment = true) { if (!Kwf_Assets_BuildCache::getInstance()->building && !Kwf_Config::getValue('assets.lazyBuild')) { if (Kwf_Exception_Abstract::isDebug()) { //proper error message on development server throw new Kwf_Exception("Building assets is disabled (assets.lazyBuild). Please upload build contents."); } else { throw new Kwf_Exception_NotFound(); } } $packageMap = Kwf_SourceMaps_SourceMap::createEmptyMap(''); if ($mimeType == 'text/javascript') { $ext = 'js'; } else { if ($mimeType == 'text/javascript; defer') { $ext = 'defer.js'; } else { if ($mimeType == 'text/css') { $ext = 'css'; } else { if ($mimeType == 'text/css; media=print') { $ext = 'printcss'; } } } } $packageMap->setFile($this->getPackageUrl($ext, $language)); $maxMTime = 0; $commonJsData = array(); $commonJsDeps = array(); foreach ($this->_getFilteredUniqueDependencies($mimeType) as $i) { if ($i->getIncludeInPackage()) { if (($mimeType == 'text/javascript' || $mimeType == 'text/javascript') && $i->isCommonJsEntry()) { $c = $i->getContentsPacked($language)->getFileContentsInlineMap(false); $commonJsDeps = $this->_getCommonJsDeps($i, $language); $commonJsData[$i->__toString()] = array('id' => $i->__toString(), 'source' => $c, 'sourceFile' => $i->__toString(), 'deps' => $commonJsDeps['deps'], 'entry' => true); foreach ($commonJsDeps['data'] as $k => $j) { if (!isset($commonJsData[$k])) { $commonJsData[$k] = $j; } } } else { $map = $i->getContentsPacked($language); if (strpos($map->getFileContents(), "//@ sourceMappingURL=") !== false && strpos($map->getFileContents(), "//# sourceMappingURL=") !== false) { throw new Kwf_Exception("contents must not contain sourceMappingURL"); } foreach ($map->getMapContentsData(false)->sources as &$s) { $s = '/assets/' . $s; } // $ret .= "/* *** $i */\n"; // attention: commenting this in breaks source maps $packageMap->concat($map); } } $mTime = $i->getMTime(); if ($mTime) { $maxMTime = max($maxMTime, $mTime); } } if ($commonJsData) { if ($mimeType == 'text/javascript; defer') { //in defer.js don't include deps that are already loaded in non-defer foreach ($this->_getFilteredUniqueDependencies('text/javascript') as $i) { $commonJsDeps = $this->_getCommonJsDeps($i, $language); foreach (array_keys($commonJsDeps['data']) as $key) { if (isset($commonJsData[$key])) { unset($commonJsData[$key]); } } $c = $i->getContentsPacked($language)->getFileContentsInlineMap(false); $commonJsData[$i->__toString()] = array('id' => $i->__toString(), 'source' => $c, 'sourceFile' => $i->__toString(), 'deps' => $commonJsDeps['deps'], 'entry' => true); } } $contents = 'window.require = ' . Kwf_Assets_CommonJs_BrowserPack::pack(array_values($commonJsData)); $map = Kwf_SourceMaps_SourceMap::createFromInline($contents); $packageMap->concat($map); } if ($mimeType == 'text/javascript' || $mimeType == 'text/javascript; defer') { if ($uniquePrefix = Kwf_Config::getValue('application.uniquePrefix')) { $packageMap = Kwf_Assets_Package_Filter_UniquePrefix::filter($packageMap, $uniquePrefix); } } if ($includeSourceMapComment) { $contents = $packageMap->getFileContents(); if ($mimeType == 'text/javascript') { $ext = 'js'; } else { if ($mimeType == 'text/javascript; defer') { $ext = 'defer.js'; } else { if ($mimeType == 'text/css') { $ext = 'css'; } else { if ($mimeType == 'text/css; media=print') { $ext = 'printcss'; } else { throw new Kwf_Exception_NotYetImplemented(); } } } } if ($ext == 'js' || $ext == 'defer.js') { $contents .= "\n//# sourceMappingURL=" . $this->getPackageUrl($ext . '.map', $language) . "\n"; } else { if ($ext == 'css' || $ext == 'printcss') { $contents .= "\n/*# sourceMappingURL=" . $this->getPackageUrl($ext . '.map', $language) . " */\n"; } } $packageMap->setFileContents($contents); } return $packageMap; }