Skip to content

Commit 304cf4f

Browse files
[AssetMapper] Make "asset-map:compile" clean assets in debug mode, require --force to generate them
1 parent 0a9eb28 commit 304cf4f

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

src/Symfony/Component/AssetMapper/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ CHANGELOG
55
---
66

77
* Add support for pre-compressing assets with Brotli, Zstandard, Zopfli, and gzip
8+
* Make `asset-map:compile` remove assets in debug mode
9+
* Add `--force` to the `asset-map:compile` command to make it generate assets in debug mode
810

911
7.2
1012
---

src/Symfony/Component/AssetMapper/Command/AssetMapperCompileCommand.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ public function __construct(
4949
protected function configure(): void
5050
{
5151
$this
52+
->addOption('force', 'f', null, 'Force compiling the assets even in debug mode')
5253
->setHelp(<<<'EOT'
5354
The <info>%command.name%</info> command compiles and dumps all the assets in
5455
the asset mapper into the final public directory (usually <comment>public/assets</comment>).
5556
56-
This command is meant to be run during deployment.
57+
When run in debug mode, this command actually removes those assets, unless <info>--force</info> is passed.
5758
EOT
5859
);
5960
}
@@ -62,8 +63,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6263
{
6364
$io = new SymfonyStyle($input, $output);
6465

65-
$this->eventDispatcher?->dispatch(new PreAssetsCompileEvent($io));
66-
6766
// remove existing config files
6867
$this->compiledConfigReader->removeConfig(AssetMapper::MANIFEST_FILE_NAME);
6968
$this->compiledConfigReader->removeConfig(ImportMapGenerator::IMPORT_MAP_CACHE_FILENAME);
@@ -74,6 +73,26 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7473
$entrypointFiles[$entrypointName] = $path;
7574
}
7675

76+
if ($this->isDebug && !$input->getOption('force')) {
77+
$didUnlink = false;
78+
$publicDir = $this->assetsFilesystem->getDestinationPath();
79+
80+
foreach ($this->assetMapper->allAssets() as $asset) {
81+
if (is_file($publicDir.'/'.$asset->publicPath)) {
82+
unlink($publicDir.'/'.$asset->publicPath);
83+
$didUnlink = true;
84+
}
85+
}
86+
87+
if ($didUnlink) {
88+
$io->warning('Running in debug mode: removed compiled assets to let them be served dynamically. Use --force to generate them instead.');
89+
}
90+
91+
return 0;
92+
}
93+
94+
$this->eventDispatcher?->dispatch(new PreAssetsCompileEvent($io));
95+
7796
$manifest = $this->createManifestAndWriteFiles($io);
7897
$manifestPath = $this->compiledConfigReader->saveConfig(AssetMapper::MANIFEST_FILE_NAME, $manifest);
7998
$io->comment(\sprintf('Manifest written to <info>%s</info>', $this->shortenPath($manifestPath)));
@@ -87,13 +106,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
87106
$styledEntrypointNames = array_map(fn (string $entrypointName) => \sprintf('<info>%s</>', $entrypointName), array_keys($entrypointFiles));
88107
$io->comment(\sprintf('Entrypoint metadata written for <comment>%d</> entrypoints (%s).', \count($entrypointFiles), implode(', ', $styledEntrypointNames)));
89108

90-
if ($this->isDebug) {
91-
$io->warning(\sprintf(
92-
'Debug mode is enabled in your project: Symfony will not serve any changed assets until you delete the files in the "%s" directory again.',
93-
$this->shortenPath(\dirname($manifestPath))
94-
));
95-
}
96-
97109
return 0;
98110
}
99111

src/Symfony/Component/AssetMapper/Tests/Command/AssetMapperCompileCommandTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function testAssetsAreCompiled()
5454

5555
$command = $application->find('asset-map:compile');
5656
$tester = new CommandTester($command);
57-
$exitCode = $tester->execute([]);
57+
$exitCode = $tester->execute(['--force' => true]);
5858
$this->assertSame(0, $exitCode);
5959
// match Compiling \d+ assets
6060
$this->assertMatchesRegularExpression('/Compiled \d+ assets/', $tester->getDisplay());
@@ -106,6 +106,12 @@ public function testAssetsAreCompiled()
106106
'/assets/subdir/file5.js',
107107
'/assets/file4.js',
108108
], $entrypointData);
109+
110+
$tester = new CommandTester($command);
111+
$exitCode = $tester->execute(['--force' => false]);
112+
$this->assertSame(0, $exitCode);
113+
114+
$this->assertStringStartsWith('[WARNING] Running in debug mode: removed compiled assets', ltrim($tester->getDisplay()));
109115
}
110116

111117
public function testEventIsDispatched()
@@ -124,7 +130,7 @@ public function testEventIsDispatched()
124130

125131
$command = $application->find('asset-map:compile');
126132
$tester = new CommandTester($command);
127-
$tester->execute([]);
133+
$tester->execute(['--force' => true]);
128134
$this->assertTrue($listenerCalled);
129135
}
130136
}

0 commit comments

Comments
 (0)