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

Added an 'ignore' option in bundle config #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
34 changes: 18 additions & 16 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,27 @@
param('kernel.project_dir'),
abstract_arg('path to binary'),
abstract_arg('embed sourcemap'),
abstract_arg('ignore list'),
])
->set('typescript.command.build', TypeScriptCompileCommand::class)
->args([
service('typescript.builder')
])
->tag('console.command')
->args([
service('typescript.builder')
])
->tag('console.command')
->set('typescript.js_asset_compiler', TypeScriptCompiler::class)
->tag('asset_mapper.compiler', [
'priority' => 9
])
->args([
abstract_arg('path to typescript source dir'),
abstract_arg('path to typescript output directory'),
service('typescript.builder'),
])
->tag('asset_mapper.compiler', [
'priority' => 9
])
->args([
abstract_arg('path to typescript source dir'),
abstract_arg('path to typescript output directory'),
service('typescript.builder'),
abstract_arg('ignore list'),
])
->set('typescript.public_asset_path_resolver', TypeScriptPublicPathAssetPathResolver::class)
->decorate('asset_mapper.public_assets_path_resolver')
->args([
service('.inner')
]);
->decorate('asset_mapper.public_assets_path_resolver')
->args([
service('.inner')
]);
;
};
4 changes: 4 additions & 0 deletions src/AssetMapper/TypeScriptCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function __construct(
private readonly array $typeScriptFilesPaths,
private readonly string $jsPathDirectory,
private readonly string $projectRootDir,
private readonly array $ignoredPaths = [],
)
{
$this->fileSystem = new Filesystem();
Expand All @@ -32,6 +33,9 @@ public function supports(MappedAsset $asset): bool
if (!str_ends_with($asset->sourcePath, '.ts')) {
return false;
}
if (in_array($asset->sourcePath, $this->ignoredPaths, true)) {
return false;
}
foreach ($this->typeScriptFilesPaths as $path) {
if (is_dir($path) && !str_starts_with($this->fileSystem->makePathRelative(realpath($asset->sourcePath), realpath($path)), '../')) {
return true;
Expand Down
12 changes: 8 additions & 4 deletions src/Command/TypeScriptCompileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
)]
class TypeScriptCompileCommand extends Command
{
private const EXCLUDED_FILE_ERROR_MESSAGE = 'Error: cannot process file because it\'s ignored by .swcrc';

public function __construct(
private TypeScriptBuilder $typeScriptCompiler
) {
Expand All @@ -34,12 +36,14 @@ public function execute(InputInterface $input, OutputInterface $output): int
$this->typeScriptCompiler->setOutput($io);

foreach ($this->typeScriptCompiler->runBuild() as $process) {
$process->wait(function ($type, $buffer) use ($io) {
$io->write($buffer);
});
$process->wait();

if (!$process->isSuccessful()) {
$io->error('Sass build failed');
if (str_contains($process->getErrorOutput(), self::EXCLUDED_FILE_ERROR_MESSAGE)) {
$io->note('One or more files have been skipped file because they are ignored');
continue;
}
$io->error('Typescript build failed');
$error = true;
}
}
Expand Down
24 changes: 16 additions & 8 deletions src/DependencyInjection/SensiolabsTypescriptExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ public function load(array $configs, ContainerBuilder $container): void
->replaceArgument(0, $config['source_dir'])
->replaceArgument(1, '%kernel.project_dir%/var/compiled_js')
->replaceArgument(3, $config['binary'])
->replaceArgument(4, $config['embed_sourcemap']);
->replaceArgument(4, $config['embed_sourcemap'])
->replaceArgument(5, $config['ignore']);

$container->findDefinition('typescript.js_asset_compiler')
->replaceArgument(0, $config['source_dir'])
->replaceArgument(1, '%kernel.project_dir%/var/compiled_js')
->replaceArgument(2, '%kernel.project_dir%');
->replaceArgument(2, '%kernel.project_dir%')
->replaceArgument(3, $config['ignore']);
}

public function getConfiguration(array $config, ContainerBuilder $container): ?ConfigurationInterface
Expand All @@ -52,20 +54,26 @@ public function getConfigTreeBuilder(): TreeBuilder
$rootNode
->children()
->arrayNode('source_dir')
->info('Path to your TypeScript directories')
->info('List of paths (files or directories) to your TypeScript directories')
->cannotBeEmpty()
->scalarPrototype()
->end()
->defaultValue(['%kernel.project_dir%/assets/typescript'])
->end()
->scalarNode('binary')
->end()
->scalarNode('binary')
->info('The TypeScript compiler binary to use')
->defaultNull()
->end()
->scalarNode('embed_sourcemap')
->end()
->scalarNode('embed_sourcemap')
->info('Whether to embed the sourcemap in the compiled CSS. By default, enabled only when debug mode is on.')
->defaultValue($this->isDebug)
->end()
->end()
->arrayNode('ignore')
->info('List of paths (files or directories) to exclude from compilation and asset mapping.')
->scalarPrototype()
->end()
->defaultValue([])
->end()
->end();

return $treeBuilder;
Expand Down
13 changes: 11 additions & 2 deletions src/TypeScriptBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,29 @@ public function __construct(
private readonly string $projectRootDir,
private readonly ?string $binaryPath,
private readonly bool $embedSourcemap,
private readonly array $ignoredPaths = [],
)
{
}

public function runBuild(): \Generator
{
$binary = $this->createBinary();
$fs = new Filesystem();

$args = ['--out-dir', $this->compiledFilesPaths];

$additionalConf = [];
if ($this->embedSourcemap) {
$args = array_merge($args, ['--source-maps', 'true']);
}
$fs = new Filesystem();
if ($this->ignoredPaths) {
$additionalConf = ["exclude" => array_map(fn ($path) => trim($fs->makePathRelative($path, $this->projectRootDir), '/'), $this->ignoredPaths)];
// $args = array_merge($args, ['--ignore', ...array_map(fn ($path) => trim($fs->makePathRelative($path, $this->projectRootDir), '/'), $this->ignoredPaths)]);
}

if ($additionalConf) {
$args = array_merge($args, ['--config-json', json_encode($additionalConf)]);
}
foreach ($this->typeScriptFilesPaths as $typeScriptFilePath) {
$relativePath = $fs->makePathRelative($typeScriptFilePath, $this->projectRootDir);
if (str_starts_with($relativePath, '..')) {
Expand Down