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

Allow specifying SWC version #54

Merged
merged 7 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
abstract_arg('path to the binaries download directory'),
abstract_arg('path to the swc binary'),
abstract_arg('swc configuration file'),
abstract_arg('swc version'),
])
->set('sensiolabs_typescript.command.build', TypeScriptBuildCommand::class)
->args([
Expand Down
11 changes: 11 additions & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ that binary with the ``swc_binary`` option:
sensiolabs_typescript:
swc_binary: 'node_modules/.bin/swc'

By default, the bundle uses SWC v1.3.92. However, you can specify a different
SWC version to compile your codebase if you need a newer feature or bug fix:

.. code-block:: yaml

# config/packages/asset_mapper.yaml
pan93412 marked this conversation as resolved.
Show resolved Hide resolved
sensiolabs_typescript:
swc_version: v1.7.27-nightly-20240911.1

Note that you should remove the existing SWC binary in the `var` directory after switching the `swc_version`; otherwise, it will use the old binary.
pan93412 marked this conversation as resolved.
Show resolved Hide resolved

Configuring the compiler
------------------------

Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/SensiolabsTypeScriptExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function load(array $configs, ContainerBuilder $container): void
->replaceArgument(3, $config['binary_download_dir'])
->replaceArgument(4, $config['swc_binary'])
->replaceArgument(5, $config['swc_config_file'])
->replaceArgument(6, $config['swc_version'])
;
$container->findDefinition('sensiolabs_typescript.js_asset_compiler')
->replaceArgument(0, $config['source_dir'])
Expand Down Expand Up @@ -75,6 +76,10 @@ public function getConfigTreeBuilder(): TreeBuilder
->info('Path to .swcrc configuration file to use')
->defaultValue('%kernel.project_dir%/.swcrc')
->end()
->scalarNode('swc_version')
->info('The SWC version to use')
->defaultValue('v1.3.92')
->end()
->end()
;

Expand Down
4 changes: 2 additions & 2 deletions src/Tools/TypeScriptBinaryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

class TypeScriptBinaryFactory
{
private const VERSION = 'v1.3.92';
private const SWC_RELEASE_URL_PATTERN = 'https://github.com/swc-project/swc/releases/download/%s/%s';
private HttpClientInterface $httpClient;
private SymfonyStyle $output;

public function __construct(
private readonly string $binaryDownloadDir,
private readonly string $swcVersion,
?HttpClientInterface $httpClient = null,
) {
$this->httpClient = $httpClient ?? HttpClient::create();
Expand Down Expand Up @@ -105,7 +105,7 @@ private function downloadAndExtract(string $binaryName): void
if (file_exists($targetPath)) {
return;
}
$url = \sprintf(self::SWC_RELEASE_URL_PATTERN, self::VERSION, $binaryName);
$url = \sprintf(self::SWC_RELEASE_URL_PATTERN, $this->swcVersion, $binaryName);

if ($this->output->isVerbose()) {
$this->output->note(\sprintf('Downloading SWC binary from "%s" to "%s"...', $url, $targetPath));
Expand Down
3 changes: 2 additions & 1 deletion src/TypeScriptBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function __construct(
private readonly string $binaryDownloadDir,
private readonly ?string $buildBinaryPath,
private readonly ?string $configFile,
private readonly string $swcVersion,
) {
}

Expand Down Expand Up @@ -76,7 +77,7 @@ private function getBuildBinary(): TypeScriptBinary
if ($this->buildBinary) {
return $this->buildBinary;
}
$typescriptBinaryFactory = new TypeScriptBinaryFactory($this->binaryDownloadDir);
$typescriptBinaryFactory = new TypeScriptBinaryFactory($this->binaryDownloadDir, $this->swcVersion);
$typescriptBinaryFactory->setOutput($this->output);

return $this->buildBinary = $this->buildBinaryPath ?
Expand Down
1 change: 1 addition & 0 deletions tests/Tools/TypeScriptBinaryFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ private function getBinaryFactory(): TypeScriptBinaryFactory
{
return new TypeScriptBinaryFactory(
$this->binaryDownloadDir,
'v1.3.92'
);
}

Expand Down