Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion lib/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,45 @@ public function outputByPackage($glue = "\n\n"){
}

protected function _output($modules, $glue = "\n\n"){
$modules = array_values($modules);
$code = array();
foreach ($modules as $module){
$urls = array();
$deps = array();
$mods = array();

foreach ($modules as $module) {
$urls[$module['id']] = $module['url'];
$mods[$module['url']] = $module;
$deps[$module['url']] = array();
}

foreach ($mods as $module) {
foreach ($module['dependencies'] as $dependency) {
if (isset($urls[$dependency])) {
$deps[$module['url']][] = $urls[$dependency];
}
}
}

function calc_deps(&$results, $deps, $url = null) {
$is_root = empty($url);
$list = $is_root ? $deps : $deps[$url];

foreach ($list as $key => $val) {
$target = $is_root ? $key : $val;
calc_deps($results, $deps, $target);

if (!in_array($target, $results)) {
$results[] = $target;
}
}
};

$orders = array();
calc_deps($orders, $deps);

foreach ($orders as $url){
$module = $mods[$url];
if (empty($module['content']) && !empty($module['url'])){
$module['content'] = file_get_contents($module['url']);
}
Expand Down
30 changes: 18 additions & 12 deletions lib/Packager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public function __construct(){
}

/**
* Sets the base path where ->req() and ->addAlias methos are relative to
* Sets the base path where ->req() and ->addAlias methods are relative to
* If the baseurl is not set, it will default to the directory of this
* Packager.php file
*
* @param string $url
* @return Packager
*/
public function setBaseUrl($url){
$this->_baseurl = $url;
$this->_baseurl = str_replace(DIRECTORY_SEPARATOR, '/', $url);
return $this;
}

Expand Down Expand Up @@ -90,24 +90,30 @@ protected function _req($id){
if (strpos($id, '!') !== false) return;

$filename = $id;
$package = '';
$extension = Path::extname($filename);
$amd = !in_array($extension, array('.js', '.css'/* more? */));
if ($amd) $filename .= '.js';

$package = '';
foreach ($this->_alias as $alias => $url){
if ($id == $alias){
$filename = $url;
} else {
$len = strlen($alias);
if (substr($filename, 0, $len) == $alias){
$filename = Path::resolve($url, substr($filename, $len + 1));
$package = $alias;
if (!preg_match('/^(\/|\.\/|\.\.\/)/', $filename)) {
$syms = explode('/', $filename);
for ($i = count($syms); $i > 0; $i--) {
$path = join('/', array_slice($syms, 0, $i));
if (isset($this->_alias[$path])) {
$package = $path;
if ($path = $this->_alias[$path]) {
if ($path == 'empty:') return; // exclude this module
array_splice($syms, 0, $i, $path); // replace matched
} else {
array_splice($syms, 0, $i); // delete matched
}
break;
}
}
$filename = implode('/', $syms);
}

if ($amd) $filename .= '.js';

$filename = Path::resolve($this->_baseurl, $filename);
if (isset($this->_files[$filename])) return;

Expand Down