Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
many fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pieter Jordaan committed Nov 1, 2017
1 parent 06516e6 commit 2f2501f
Show file tree
Hide file tree
Showing 32 changed files with 225 additions and 77 deletions.
8 changes: 8 additions & 0 deletions spec/require.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ describe("Require.js module register method test", function() {
expect(lib.require('foo')).toEqual('FOO');
});

it("with AMD define object", function() {
lib.register("foo-static", function (define, require, module, exports) {
define([], {foo: 1});
});

expect(lib.require('foo-static')).toEqual({foo: 1});
});

it("with AMD define and reserved requirements", function() {
lib.register("amd_define/foo", function (define, require, module, exports) {
define([], function () {
Expand Down
2 changes: 1 addition & 1 deletion src/Bundler/Pipeline/ContentPipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function push(array $dependencies, ReaderInterface $file_reader, File $ta
$module_name = $file->getName();

if (!empty($this->config->getSourceRoot())
&& false !== strpos($module_name, $this->config->getSourceRoot())
&& 0 === strpos($module_name, $this->config->getSourceRoot())
) {
$chopped = substr($file->dir, strlen($this->config->getSourceRoot()));
$base_dir = $chopped ? trim($chopped, '/') : '';
Expand Down
4 changes: 3 additions & 1 deletion src/Bundler/Processor/ModuleProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public function peek(string $cwd, ContentState $state): void

public function transpile(string $cwd, ContentItem $item): void
{
$js = "register('" . $item->module_name . "', function (define, require, module, exports) {\n";
$js = "register("
. json_encode($item->module_name, JSON_UNESCAPED_SLASHES)
. ", function (define, require, module, exports) {\n";
$js .= $item->getContent();
$js .= "\n});\n";

Expand Down
4 changes: 1 addition & 3 deletions src/Bundler/Processor/TsContentProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ public function transpile(string $cwd, ContentItem $item): void
{
$module_name = $item->module_name;

if (false !== ($i = strrpos($module_name, '.'))) {
$module_name = substr($module_name, 0, $i);
}
$module_name = preg_replace('/\.ts$/i', '', $module_name);

$item->transition(
ContentState::PROCESSED,
Expand Down
2 changes: 2 additions & 0 deletions src/Bundler/Runner/js/tsc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ function compile(source) {
compilerOptions: {
inlineSourceMap: false,
skipLibCheck: true,
importHelpers: true,
target: ts.ScriptTarget.ES5,
module: ts.ModuleKind.CommonJS,
moduleResolution: ts.ModuleResolutionKind.NodeJs,
emitDecoratorMetadata: true,
experimentalDecorators: true
}
});
Expand Down
10 changes: 7 additions & 3 deletions src/Config/FileConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,21 @@ final class FileConfig implements ConfigInterface
* @param bool $dev
* @param EventDispatcherInterface $dispatcher
* @param LoggerInterface $logger
* @param mixed[] $non_static_options
*/
public function __construct(
string $config_file,
array $plugins = [],
bool $dev = false,
EventDispatcherInterface $dispatcher = null,
LoggerInterface $logger = null
LoggerInterface $logger = null,
array $non_static_options = []
) {
$this->config_file = $config_file;
$this->config_file_contents = json_decode(file_get_contents($this->config_file), true);
$this->config_file_contents = array_merge(
json_decode(file_get_contents($this->config_file), true),
$non_static_options
);
$this->dispatcher = $dispatcher ?? new EventDispatcher();
$this->logger = $logger ?? new NullLogger();
$this->plugins = $plugins;
Expand Down Expand Up @@ -123,7 +128,6 @@ public function getOutputFolder(bool $include_public_folder = true): string
if (! $include_public_folder) {
return $output_folder;
}

$web_root = $this->config_file_contents['web-root'];
return rtrim($web_root, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $output_folder;
}
Expand Down
6 changes: 6 additions & 0 deletions src/Event/AssetEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ final class AssetEvents
* @var string
*/
public const READY = 'asset.ready';

/**
* Used to find the source of an asset. Can be used for example with the AngularPlugin to find stylesheets of an
* Angular component and instructing to use a less file instead.
*/
public const FIND_SOURCE = 'asset.find_source';
}
29 changes: 29 additions & 0 deletions src/Event/FindSourceEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* @copyright 2017 Hostnet B.V.
*/
declare(strict_types=1);

namespace Hostnet\Component\Resolver\Event;

use Symfony\Component\EventDispatcher\Event;

class FindSourceEvent extends Event
{
private $file_path;

public function __construct(string $file_path)
{
$this->file_path = $file_path;
}

public function getFilepath(): string
{
return $this->file_path;
}

public function setFilepath(string $file_path): void
{
$this->file_path = $file_path;
}
}
14 changes: 10 additions & 4 deletions src/EventListener/AngularHtmlListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

use Hostnet\Component\Resolver\Bundler\Asset;
use Hostnet\Component\Resolver\Event\AssetEvent;
use Hostnet\Component\Resolver\Event\AssetEvents;
use Hostnet\Component\Resolver\Event\FindSourceEvent;
use Hostnet\Component\Resolver\File;
use Hostnet\Component\Resolver\FileSystem\FileReader;
use Hostnet\Component\Resolver\FileSystem\ReaderInterface;
Expand Down Expand Up @@ -79,12 +81,16 @@ function ($match) use ($file, $reader) {
*/
private function getCompiledAssetFor(string $linked_file, File $owning_file, ReaderInterface $reader): string
{
$file_path = $linked_file;

if ($file_path[0] === '.' && $owning_file->dir !== '.' && !empty($owning_file->dir)) {
$file_path = $owning_file->dir . '/' . $file_path;
if (substr($linked_file, 1, 0) === '/') {
$file_path = $linked_file;
} else {
$file_path = $owning_file->dir . '/' . $linked_file;
}

$event = new FindSourceEvent($file_path);
$this->plugin_api->getConfig()->getEventDispatcher()->dispatch(AssetEvents::FIND_SOURCE, $event);
$file_path = $event->getFilepath();

$target_file = new File(File::clean($file_path));
$pipeline = $this->plugin_api->getPipeline();
$asset = new Asset($this->plugin_api->getFinder()->all($target_file), $pipeline->peek($target_file));
Expand Down
30 changes: 17 additions & 13 deletions src/Import/BuiltIn/AngularImportCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,25 @@

namespace Hostnet\Component\Resolver\Import\BuiltIn;

use Hostnet\Component\Resolver\Event\AssetEvents;
use Hostnet\Component\Resolver\Event\FindSourceEvent;
use Hostnet\Component\Resolver\File;
use Hostnet\Component\Resolver\Import\Import;
use Hostnet\Component\Resolver\Import\ImportCollection;
use Hostnet\Component\Resolver\Import\ImportCollectorInterface;
use Hostnet\Component\Resolver\Plugin\PluginApi;

/**
* Angular asset resolver.
*/
final class AngularImportCollector implements ImportCollectorInterface
{
private $plugin_api;

public function __construct(PluginApi $plugin_api)
{
$this->plugin_api = $plugin_api;
}
/**
* {@inheritdoc}
*/
Expand All @@ -33,25 +42,20 @@ public function collect(string $cwd, File $file, ImportCollection $imports): voi

if (preg_match_all('/templateUrl\s*:(\s*[\'"`](.*?)[\'"`]\s*)/m', $content, $matches) > 0) {
foreach ($matches[2] as $match) {
$file_path = $match;

if ($file_path[0] === '.') {
$file_path = $file->dir . substr($file_path, 1);
}

$file_path = File::clean($file->dir . '/' . $match);
$imports->addImport(new Import($file_path, new File($file_path), true));
}
}
if (preg_match_all('/styleUrls *:(\s*\[[^\]]*?\])/', $content, $matches) > 0) {
if (preg_match_all('/styleUrls\s*:(\s*\[[^\]]*?\])/', $content, $matches) > 0) {
foreach ($matches[1] as $match) {
if (preg_match_all('/([\'`"])((?:[^\\\\]\\\\\1|.)*?)\1/', $match, $inner_matches) > 0) {
foreach ($inner_matches[2] as $inner_match) {
$file_path = $inner_match;

if ($file_path[0] === '.') {
$file_path = $file->dir . substr($file_path, 1);
}

$event = new FindSourceEvent($file->dir . '/./' . $inner_match);
$this->plugin_api->getConfig()->getEventDispatcher()->dispatch(
AssetEvents::FIND_SOURCE,
$event
);
$file_path = File::clean($event->getFilepath());
$imports->addImport(new Import($file_path, new File($file_path), true));
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/Import/BuiltIn/JsImportCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public function supports(File $file): bool
public function collect(string $cwd, File $file, ImportCollection $imports): void
{
$content = file_get_contents(File::makeAbsolutePath($file->path, $cwd));
// remove contents if applicable so we do not find require statements inside commented code.
// that way the only accidental require statements we find are the one we find in if statements
// or between try { .. } catch.
$content = preg_replace('#\\/\\*([^*]|[\\r\\n]|(\\*+([^*/]|[\\r\\n])))*\\*+\\/#', '', $content) ? : $content;
$n = preg_match_all('/(.?)require\(([\']([^\']+)[\']|["]([^"]+)["])\)/', $content, $matches);

for ($i = 0; $i < $n; $i++) {
Expand Down
11 changes: 1 addition & 10 deletions src/Import/BuiltIn/TsImportCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,13 @@ public function collect(string $cwd, File $file, ImportCollection $imports): voi
{
$content = file_get_contents(File::makeAbsolutePath($file->path, $cwd));
$n = preg_match_all('/import([^;\'"]*from)?\s+["\'](.*?)["\'];/', $content, $matches);

$this->js_import_collector->collect($cwd, $file, $imports);

for ($i = 0; $i < $n; $i++) {
$path = $matches[2][$i];

try {
$import = $this->nodejs_resolver->asRequire($path, $file);
$base_name = basename($import->getImportedFile()->path);

$ext = substr($base_name, strpos($base_name, '.'));

if ($ext === '.d.ts') {
continue;
}

$import = $this->nodejs_resolver->asRequire($path, $file);
$imports->addImport($import);
} catch (\RuntimeException $e) {
continue;
Expand Down
Loading

0 comments on commit 2f2501f

Please sign in to comment.