Skip to content

Commit

Permalink
Allow specifying SWC version (#54)
Browse files Browse the repository at this point in the history
Feat: Allow specifying the SWC version (#53)
  • Loading branch information
pan93412 authored Sep 17, 2024
1 parent 62f8224 commit c6a6940
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 3 deletions.
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/sensiolabs_typescript.yaml
sensiolabs_typescript:
swc_version: v1.7.27-nightly-20240911.1
Note that you should remove the existing SWC binary in the download directory (``var`` by default) after switching the ``swc_version``; the download is only triggered if no binary is found in the download directory. Otherwise, the existing binary will still be used.

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
13 changes: 11 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 All @@ -126,6 +126,15 @@ private function downloadAndExtract(string $binaryName): void
$progressBar->setProgress($dlNow);
},
]);

if (200 !== $statusCode = $response->getStatusCode()) {
$exceptionMessage = \sprintf('Could not download SWC binary from "%s" (request responded with %d).', $url, $statusCode);
if (404 === $statusCode) {
$exceptionMessage .= PHP_EOL.\sprintf('Check that the version "%s" defined in "sensiolabs_typescript.swc_version" exists.', $this->swcVersion);
}
throw new \Exception($exceptionMessage);
}

$fileHandler = fopen($targetPath, 'w');
if (false === $fileHandler) {
throw new \Exception(\sprintf('Could not open file "%s" for writing.', $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

0 comments on commit c6a6940

Please sign in to comment.