diff --git a/config/services.php b/config/services.php index 123e89c..1a8f6da 100644 --- a/config/services.php +++ b/config/services.php @@ -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') + ]); ; }; diff --git a/src/AssetMapper/TypeScriptCompiler.php b/src/AssetMapper/TypeScriptCompiler.php index 531cb72..30930d1 100644 --- a/src/AssetMapper/TypeScriptCompiler.php +++ b/src/AssetMapper/TypeScriptCompiler.php @@ -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(); @@ -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; diff --git a/src/Command/TypeScriptCompileCommand.php b/src/Command/TypeScriptCompileCommand.php index a718274..6dfc375 100644 --- a/src/Command/TypeScriptCompileCommand.php +++ b/src/Command/TypeScriptCompileCommand.php @@ -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 ) { @@ -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; } } diff --git a/src/DependencyInjection/SensiolabsTypescriptExtension.php b/src/DependencyInjection/SensiolabsTypescriptExtension.php index 8441338..83bb7e5 100644 --- a/src/DependencyInjection/SensiolabsTypescriptExtension.php +++ b/src/DependencyInjection/SensiolabsTypescriptExtension.php @@ -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 @@ -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; diff --git a/src/TypeScriptBuilder.php b/src/TypeScriptBuilder.php index 0c02033..39922f3 100644 --- a/src/TypeScriptBuilder.php +++ b/src/TypeScriptBuilder.php @@ -15,6 +15,7 @@ public function __construct( private readonly string $projectRootDir, private readonly ?string $binaryPath, private readonly bool $embedSourcemap, + private readonly array $ignoredPaths = [], ) { } @@ -22,13 +23,21 @@ public function __construct( 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, '..')) {