Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript use absolute path #796

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
29 changes: 28 additions & 1 deletion src/Assetic/Filter/TypeScriptFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ class TypeScriptFilter extends BaseNodeFilter
{
private $tscBin;
private $nodeBin;
private $useAbsolutePath = false;

public function __construct($tscBin = '/usr/bin/tsc', $nodeBin = null)
{
$this->tscBin = $tscBin;
$this->nodeBin = $nodeBin;
}

public function setUseAbsolutePath($useAbsolutePath)
{
$this->useAbsolutePath = $useAbsolutePath;
}

public function filterLoad(AssetInterface $asset)
{
Expand All @@ -48,7 +54,7 @@ public function filterLoad(AssetInterface $asset)
$inputPath = $inputDirPath.DIRECTORY_SEPARATOR.$templateName.'.ts';
$outputPath = FilesystemUtils::createTemporaryFile('typescript_out');

file_put_contents($inputPath, $asset->getContent());
file_put_contents($inputPath, $this->getAssetContent($asset));

$pb->add($inputPath)->add('--out')->add($outputPath);

Expand Down Expand Up @@ -77,4 +83,25 @@ public function filterLoad(AssetInterface $asset)
public function filterDump(AssetInterface $asset)
{
}

private function getAssetContent(AssetInterface $asset)
{
if ($this->useAbsolutePath && $asset->getSourcePath() && $asset->getSourceRoot()) {
$sourceDirName = pathinfo($asset->getSourcePath(), PATHINFO_DIRNAME);
$dir = $asset->getSourceRoot() . DIRECTORY_SEPARATOR . $sourceDirName;

$func = function ($matches) use ($dir) {
$path = realpath($dir . DIRECTORY_SEPARATOR . $matches[2]);
if ($path === false) {
$path = $matches[2];
}

return $matches[1] . $path;
};

return preg_replace_callback('|(\s*/{3}\s*<reference\s+path=")([^"]+)|', $func, $asset->getContent());
}

return $asset->getContent();
}
}
25 changes: 25 additions & 0 deletions tests/Assetic/Test/Filter/TypeScriptFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Assetic\Test\Filter;

use Assetic\Asset\FileAsset;
use Assetic\Asset\StringAsset;
use Assetic\Filter\TypeScriptFilter;

Expand Down Expand Up @@ -67,4 +68,28 @@ function greeter(person : Person) {
$this->assertContains('function greeter(person)', $asset->getContent());
$this->assertNotContains('interface Person', $asset->getContent());
}

/**
* @dataProvider relativeToAbsolutePathsDataProvider
*/
public function testRelativeToAbsolutePaths($sourceFile)
{
$this->filter->setUseAbsolutePath(true);
$asset = new FileAsset($sourceFile);
$asset->load($this->filter);

$this->assertStringStartsWith("var a = 'test';", $asset->getContent());
}

public function relativeToAbsolutePathsDataProvider()
{
return array(
array(__DIR__ . '/fixtures/typescript/relative_path/test1.ts'),
array(__DIR__ . '/fixtures/typescript/relative_path/test2.ts'),
array(__DIR__ . '/fixtures/typescript/relative_path/test3.ts'),
array(__DIR__ . '/fixtures/typescript/relative_path/test4.ts'),
array(__DIR__ . '/fixtures/typescript/relative_path/test5.ts'),
array(__DIR__ . '/fixtures/typescript/relative_path/test6.ts'),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var a = 'test';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference path="../referenced.ts" />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference path="../referenced.ts" />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
///<reference path="../referenced.ts" />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
///<reference path="../referenced.ts"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
///<reference path="../referenced.ts" />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference path="../referenced.ts" />