Skip to content

[AssetMapper] Make asset-map:compile clean assets in debug mode, require --force to generate them #59073

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions src/Symfony/Component/AssetMapper/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ CHANGELOG
---

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would not have been easier to get / use with an option "--purge" + keeping the current behaviour ?

A compile command that clear files feels very weird .. using different arguments to do the same thing also 🤷

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My reasoning is that in dev mode you don't want the assets to block dynamic generation, yet this is the default right now, and this serves no DX purpose.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current DX can be improved for users after they compile assets locally, yes.

But this PR can also make things worse for all the others.. Two opposite behaviour between debug and not debug.. i'm not sure this will be a DX win. 🤷‍♂️

Two commands: asset-map:compile and asset-map:purge (or asset-map:clear) feels easier to explain, document, use.

Just a personal feeling.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain what you mean by "worse".
The behavior I'm proposing here is not opposite, it's consistent with what each mode (debug/non-debug) needs to work. Adding a myriad of commands for fine grained operations will just make things more complex, aka worse, when we know what the behavior should be to make things Just Work (TM)


7.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ public function __construct(
protected function configure(): void
{
$this
->addOption('force', 'f', null, 'Force compiling the assets even in debug mode')
->setHelp(<<<'EOT'
The <info>%command.name%</info> command compiles and dumps all the assets in
the asset mapper into the final public directory (usually <comment>public/assets</comment>).

This command is meant to be run during deployment.
When run in debug mode, this command actually removes those assets, unless <info>--force</info> is passed.
EOT
);
}
Expand All @@ -62,8 +63,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

$this->eventDispatcher?->dispatch(new PreAssetsCompileEvent($io));

// remove existing config files
$this->compiledConfigReader->removeConfig(AssetMapper::MANIFEST_FILE_NAME);
$this->compiledConfigReader->removeConfig(ImportMapGenerator::IMPORT_MAP_CACHE_FILENAME);
Expand All @@ -74,6 +73,26 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$entrypointFiles[$entrypointName] = $path;
}

if ($this->isDebug && !$input->getOption('force')) {
$didUnlink = false;
$publicDir = $this->assetsFilesystem->getDestinationPath();

foreach ($this->assetMapper->allAssets() as $asset) {
if (is_file($publicDir.'/'.$asset->publicPath)) {
unlink($publicDir.'/'.$asset->publicPath);
Copy link
Contributor

@chadyred chadyred Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, the FileSystem component could be used here ? (With exists and remove functions) ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not without reasons. What would be yours?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, it handles issues in case of errors like "impossible" to remove a directory and raise an IOException well formatted and exists handle cases like PHP_MAXPATHLEN, I think the lib is made to ease developer life, isn't it ? :)

$didUnlink = true;
}
}

if ($didUnlink) {
$io->warning('Running in debug mode: removed compiled assets to let them be served dynamically. Use --force to generate them instead.');
}

return 0;
}

$this->eventDispatcher?->dispatch(new PreAssetsCompileEvent($io));

$manifest = $this->createManifestAndWriteFiles($io);
$manifestPath = $this->compiledConfigReader->saveConfig(AssetMapper::MANIFEST_FILE_NAME, $manifest);
$io->comment(\sprintf('Manifest written to <info>%s</info>', $this->shortenPath($manifestPath)));
Expand All @@ -87,13 +106,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$styledEntrypointNames = array_map(fn (string $entrypointName) => \sprintf('<info>%s</>', $entrypointName), array_keys($entrypointFiles));
$io->comment(\sprintf('Entrypoint metadata written for <comment>%d</> entrypoints (%s).', \count($entrypointFiles), implode(', ', $styledEntrypointNames)));

if ($this->isDebug) {
$io->warning(\sprintf(
'Debug mode is enabled in your project: Symfony will not serve any changed assets until you delete the files in the "%s" directory again.',
$this->shortenPath(\dirname($manifestPath))
));
}

return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function testAssetsAreCompiled()

$command = $application->find('asset-map:compile');
$tester = new CommandTester($command);
$exitCode = $tester->execute([]);
$exitCode = $tester->execute(['--force' => true]);
$this->assertSame(0, $exitCode);
// match Compiling \d+ assets
$this->assertMatchesRegularExpression('/Compiled \d+ assets/', $tester->getDisplay());
Expand Down Expand Up @@ -106,6 +106,12 @@ public function testAssetsAreCompiled()
'/assets/subdir/file5.js',
'/assets/file4.js',
], $entrypointData);

$tester = new CommandTester($command);
$exitCode = $tester->execute(['--force' => false]);
$this->assertSame(0, $exitCode);

$this->assertStringStartsWith('[WARNING] Running in debug mode: removed compiled assets', ltrim($tester->getDisplay()));
}

public function testEventIsDispatched()
Expand All @@ -124,7 +130,7 @@ public function testEventIsDispatched()

$command = $application->find('asset-map:compile');
$tester = new CommandTester($command);
$tester->execute([]);
$tester->execute(['--force' => true]);
$this->assertTrue($listenerCalled);
}
}