Skip to content

Commit

Permalink
support javascript module imports (#18)
Browse files Browse the repository at this point in the history
* support javascript module imports

* remove unused method

* fix issue with replacing entire levelUp path
  • Loading branch information
TomK committed Jan 16, 2019
1 parent d4e1b63 commit 9b86166
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 19 deletions.
40 changes: 22 additions & 18 deletions src/Resources/AbstractDispatchableResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,26 @@ protected function _dispatch() { }
*/
protected function _makeFullPath($relativePath, $workingDirectory)
{
if($relativePath == '.')
// levelUps
$newParts = [];
$parts = explode('/', Path::url($workingDirectory, $relativePath));
while($part = array_shift($parts))
{
return $workingDirectory;
}
$levelUps = substr_count($relativePath, '../');
if($levelUps > 0)
{
$relativePath = str_replace('../', '', $relativePath);
$workingDirectoryParts = explode('/', $workingDirectory);
$moves = count($workingDirectoryParts) - $levelUps;
if($moves < 0)
if($part !== '..' && $parts && $parts[0] === '..')
{
array_shift($parts);
}
else
{
//Relative to this directory is not allowed
return null;
$newParts[] = $part;
}
return Path::custom('/', array_merge(array_slice($workingDirectoryParts, 0, $moves), [$relativePath]));
}
return $workingDirectory[0] !== '.' ? Path::url($workingDirectory, $relativePath) : $relativePath;
$relativePath = Path::url(...$newParts);

// currentDir
$relativePath = preg_replace('~(?<=\/|^).\/~', '', $relativePath);

return $relativePath;
}

/**
Expand All @@ -108,13 +110,15 @@ protected function _makeFullPath($relativePath, $workingDirectory)
*/
protected function _getDispatchUrl($path): string
{
list($newPath, $append) = Strings::explode('?', $path, [$path, null], 2);

if(!$this->_manager->isExternalUrl($newPath))
if($this->_manager->isExternalUrl($path))
{
$newPath = Path::system($this->_makeFullPath(dirname($newPath), dirname($this->_path)), basename($newPath));
return $path;
}

list($newPath, $append) = Strings::explode('?', $path, [$path, null], 2);

$newPath = $this->_makeFullPath($newPath, dirname($this->_path));

$url = $this->_manager->getResourceUri($newPath);
if(empty($url))
{
Expand Down
40 changes: 39 additions & 1 deletion src/Resources/JavascriptResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
namespace Packaged\Dispatch\Resources;

use JShrink\Minifier;
use Packaged\Helpers\Strings;

class JavascriptResource extends AbstractDispatchableResource
{
protected $_options = [
'minify' => 'true',
'dispatch' => true,
'minify' => true,
];

public function getExtension()
Expand All @@ -19,6 +21,42 @@ public function getContentType()
return "text/javascript";
}

protected function _dispatch()
{
$this->_content = preg_replace_callback(
'~(import\s+.+?\s+from\s+)(["\']?)(.+?)\2(.*?)~',
[$this, "_dispatchImportUrls"],
$this->_content
);
}

/**
* Dispatch a nested URL
*
* @param $uri
*
* @return string
* @throws \Exception
*/
protected function _dispatchImportUrls($uri)
{
return $uri[1] . Strings::wrap($this->_getDispatchUrl($uri[3]), $uri[2], true) . $uri[4];
}

protected function _makeFullPath($relativePath, $workingDirectory)
{
$path = parent::_makeFullPath($relativePath, $workingDirectory);
if(!file_exists($this->_manager->getFilePath($path)))
{
$ext = pathinfo($this->_path, PATHINFO_EXTENSION);
if(file_exists($this->_manager->getFilePath($path . '.' . $ext)))
{
return $path . '.' . $ext;
}
}
return $path;
}

protected function _minify()
{
try
Expand Down
3 changes: 3 additions & 0 deletions tests/Resources/AbstractDispatchableResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public function testJsContent()
$resource->setFilePath($manager->getFilePath('js/url.min.js'));
$resource->setContent(file_get_contents(Path::system($root, Dispatch::RESOURCES_DIR, 'js', 'url.min.js')));
$content = $resource->getContent();
$this->assertContains('import test from \'./test\';', $content);
$this->assertContains('import {default as alert} from \'r/ef6402a7/js/alert.js\';', $content);
$this->assertContains('import misc from \'r/d023f9c3/js/misc.js\';', $content);
$this->assertContains('"url(" + test(p) + ")"', $content);
}
}
4 changes: 4 additions & 0 deletions tests/_root/resources/js/url.min.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import test from './test';
import {default as alert} from './alert';
import misc from './misc.js';

function test(p) {return p + '.png';}

var p = 'test';
Expand Down

0 comments on commit 9b86166

Please sign in to comment.