Skip to content

Commit c6a6940

Browse files
authored
Allow specifying SWC version (#54)
Feat: Allow specifying the SWC version (#53)
1 parent 62f8224 commit c6a6940

File tree

6 files changed

+31
-3
lines changed

6 files changed

+31
-3
lines changed

config/services.php

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
abstract_arg('path to the binaries download directory'),
2323
abstract_arg('path to the swc binary'),
2424
abstract_arg('swc configuration file'),
25+
abstract_arg('swc version'),
2526
])
2627
->set('sensiolabs_typescript.command.build', TypeScriptBuildCommand::class)
2728
->args([

doc/index.rst

+11
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@ that binary with the ``swc_binary`` option:
110110
sensiolabs_typescript:
111111
swc_binary: 'node_modules/.bin/swc'
112112
113+
By default, the bundle uses SWC v1.3.92. However, you can specify a different
114+
SWC version to compile your codebase if you need a newer feature or bug fix:
115+
116+
.. code-block:: yaml
117+
118+
# config/packages/sensiolabs_typescript.yaml
119+
sensiolabs_typescript:
120+
swc_version: v1.7.27-nightly-20240911.1
121+
122+
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.
123+
113124
Configuring the compiler
114125
------------------------
115126

src/DependencyInjection/SensiolabsTypeScriptExtension.php

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function load(array $configs, ContainerBuilder $container): void
3131
->replaceArgument(3, $config['binary_download_dir'])
3232
->replaceArgument(4, $config['swc_binary'])
3333
->replaceArgument(5, $config['swc_config_file'])
34+
->replaceArgument(6, $config['swc_version'])
3435
;
3536
$container->findDefinition('sensiolabs_typescript.js_asset_compiler')
3637
->replaceArgument(0, $config['source_dir'])
@@ -75,6 +76,10 @@ public function getConfigTreeBuilder(): TreeBuilder
7576
->info('Path to .swcrc configuration file to use')
7677
->defaultValue('%kernel.project_dir%/.swcrc')
7778
->end()
79+
->scalarNode('swc_version')
80+
->info('The SWC version to use')
81+
->defaultValue('v1.3.92')
82+
->end()
7883
->end()
7984
;
8085

src/Tools/TypeScriptBinaryFactory.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

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

1615
public function __construct(
1716
private readonly string $binaryDownloadDir,
17+
private readonly string $swcVersion,
1818
?HttpClientInterface $httpClient = null,
1919
) {
2020
$this->httpClient = $httpClient ?? HttpClient::create();
@@ -105,7 +105,7 @@ private function downloadAndExtract(string $binaryName): void
105105
if (file_exists($targetPath)) {
106106
return;
107107
}
108-
$url = \sprintf(self::SWC_RELEASE_URL_PATTERN, self::VERSION, $binaryName);
108+
$url = \sprintf(self::SWC_RELEASE_URL_PATTERN, $this->swcVersion, $binaryName);
109109

110110
if ($this->output->isVerbose()) {
111111
$this->output->note(\sprintf('Downloading SWC binary from "%s" to "%s"...', $url, $targetPath));
@@ -126,6 +126,15 @@ private function downloadAndExtract(string $binaryName): void
126126
$progressBar->setProgress($dlNow);
127127
},
128128
]);
129+
130+
if (200 !== $statusCode = $response->getStatusCode()) {
131+
$exceptionMessage = \sprintf('Could not download SWC binary from "%s" (request responded with %d).', $url, $statusCode);
132+
if (404 === $statusCode) {
133+
$exceptionMessage .= PHP_EOL.\sprintf('Check that the version "%s" defined in "sensiolabs_typescript.swc_version" exists.', $this->swcVersion);
134+
}
135+
throw new \Exception($exceptionMessage);
136+
}
137+
129138
$fileHandler = fopen($targetPath, 'w');
130139
if (false === $fileHandler) {
131140
throw new \Exception(\sprintf('Could not open file "%s" for writing.', $targetPath));

src/TypeScriptBuilder.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function __construct(
2626
private readonly string $binaryDownloadDir,
2727
private readonly ?string $buildBinaryPath,
2828
private readonly ?string $configFile,
29+
private readonly string $swcVersion,
2930
) {
3031
}
3132

@@ -76,7 +77,7 @@ private function getBuildBinary(): TypeScriptBinary
7677
if ($this->buildBinary) {
7778
return $this->buildBinary;
7879
}
79-
$typescriptBinaryFactory = new TypeScriptBinaryFactory($this->binaryDownloadDir);
80+
$typescriptBinaryFactory = new TypeScriptBinaryFactory($this->binaryDownloadDir, $this->swcVersion);
8081
$typescriptBinaryFactory->setOutput($this->output);
8182

8283
return $this->buildBinary = $this->buildBinaryPath ?

tests/Tools/TypeScriptBinaryFactoryTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ private function getBinaryFactory(): TypeScriptBinaryFactory
1313
{
1414
return new TypeScriptBinaryFactory(
1515
$this->binaryDownloadDir,
16+
'v1.3.92'
1617
);
1718
}
1819

0 commit comments

Comments
 (0)