Skip to content

Commit 9b86166

Browse files
authored
support javascript module imports (#18)
* support javascript module imports * remove unused method * fix issue with replacing entire levelUp path
1 parent d4e1b63 commit 9b86166

File tree

4 files changed

+68
-19
lines changed

4 files changed

+68
-19
lines changed

src/Resources/AbstractDispatchableResource.php

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,26 @@ protected function _dispatch() { }
8080
*/
8181
protected function _makeFullPath($relativePath, $workingDirectory)
8282
{
83-
if($relativePath == '.')
83+
// levelUps
84+
$newParts = [];
85+
$parts = explode('/', Path::url($workingDirectory, $relativePath));
86+
while($part = array_shift($parts))
8487
{
85-
return $workingDirectory;
86-
}
87-
$levelUps = substr_count($relativePath, '../');
88-
if($levelUps > 0)
89-
{
90-
$relativePath = str_replace('../', '', $relativePath);
91-
$workingDirectoryParts = explode('/', $workingDirectory);
92-
$moves = count($workingDirectoryParts) - $levelUps;
93-
if($moves < 0)
88+
if($part !== '..' && $parts && $parts[0] === '..')
89+
{
90+
array_shift($parts);
91+
}
92+
else
9493
{
95-
//Relative to this directory is not allowed
96-
return null;
94+
$newParts[] = $part;
9795
}
98-
return Path::custom('/', array_merge(array_slice($workingDirectoryParts, 0, $moves), [$relativePath]));
9996
}
100-
return $workingDirectory[0] !== '.' ? Path::url($workingDirectory, $relativePath) : $relativePath;
97+
$relativePath = Path::url(...$newParts);
98+
99+
// currentDir
100+
$relativePath = preg_replace('~(?<=\/|^).\/~', '', $relativePath);
101+
102+
return $relativePath;
101103
}
102104

103105
/**
@@ -108,13 +110,15 @@ protected function _makeFullPath($relativePath, $workingDirectory)
108110
*/
109111
protected function _getDispatchUrl($path): string
110112
{
111-
list($newPath, $append) = Strings::explode('?', $path, [$path, null], 2);
112-
113-
if(!$this->_manager->isExternalUrl($newPath))
113+
if($this->_manager->isExternalUrl($path))
114114
{
115-
$newPath = Path::system($this->_makeFullPath(dirname($newPath), dirname($this->_path)), basename($newPath));
115+
return $path;
116116
}
117117

118+
list($newPath, $append) = Strings::explode('?', $path, [$path, null], 2);
119+
120+
$newPath = $this->_makeFullPath($newPath, dirname($this->_path));
121+
118122
$url = $this->_manager->getResourceUri($newPath);
119123
if(empty($url))
120124
{

src/Resources/JavascriptResource.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
namespace Packaged\Dispatch\Resources;
33

44
use JShrink\Minifier;
5+
use Packaged\Helpers\Strings;
56

67
class JavascriptResource extends AbstractDispatchableResource
78
{
89
protected $_options = [
9-
'minify' => 'true',
10+
'dispatch' => true,
11+
'minify' => true,
1012
];
1113

1214
public function getExtension()
@@ -19,6 +21,42 @@ public function getContentType()
1921
return "text/javascript";
2022
}
2123

24+
protected function _dispatch()
25+
{
26+
$this->_content = preg_replace_callback(
27+
'~(import\s+.+?\s+from\s+)(["\']?)(.+?)\2(.*?)~',
28+
[$this, "_dispatchImportUrls"],
29+
$this->_content
30+
);
31+
}
32+
33+
/**
34+
* Dispatch a nested URL
35+
*
36+
* @param $uri
37+
*
38+
* @return string
39+
* @throws \Exception
40+
*/
41+
protected function _dispatchImportUrls($uri)
42+
{
43+
return $uri[1] . Strings::wrap($this->_getDispatchUrl($uri[3]), $uri[2], true) . $uri[4];
44+
}
45+
46+
protected function _makeFullPath($relativePath, $workingDirectory)
47+
{
48+
$path = parent::_makeFullPath($relativePath, $workingDirectory);
49+
if(!file_exists($this->_manager->getFilePath($path)))
50+
{
51+
$ext = pathinfo($this->_path, PATHINFO_EXTENSION);
52+
if(file_exists($this->_manager->getFilePath($path . '.' . $ext)))
53+
{
54+
return $path . '.' . $ext;
55+
}
56+
}
57+
return $path;
58+
}
59+
2260
protected function _minify()
2361
{
2462
try

tests/Resources/AbstractDispatchableResourceTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public function testJsContent()
4646
$resource->setFilePath($manager->getFilePath('js/url.min.js'));
4747
$resource->setContent(file_get_contents(Path::system($root, Dispatch::RESOURCES_DIR, 'js', 'url.min.js')));
4848
$content = $resource->getContent();
49+
$this->assertContains('import test from \'./test\';', $content);
50+
$this->assertContains('import {default as alert} from \'r/ef6402a7/js/alert.js\';', $content);
51+
$this->assertContains('import misc from \'r/d023f9c3/js/misc.js\';', $content);
4952
$this->assertContains('"url(" + test(p) + ")"', $content);
5053
}
5154
}

tests/_root/resources/js/url.min.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import test from './test';
2+
import {default as alert} from './alert';
3+
import misc from './misc.js';
4+
15
function test(p) {return p + '.png';}
26

37
var p = 'test';

0 commit comments

Comments
 (0)