@@ -49,11 +49,12 @@ public function __construct(
49
49
protected function configure (): void
50
50
{
51
51
$ this
52
+ ->addOption ('force ' , 'f ' , null , 'Force compiling the assets even in debug mode ' )
52
53
->setHelp (<<<'EOT'
53
54
The <info>%command.name%</info> command compiles and dumps all the assets in
54
55
the asset mapper into the final public directory (usually <comment>public/assets</comment>).
55
56
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 .
57
58
EOT
58
59
);
59
60
}
@@ -62,8 +63,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
62
63
{
63
64
$ io = new SymfonyStyle ($ input , $ output );
64
65
65
- $ this ->eventDispatcher ?->dispatch(new PreAssetsCompileEvent ($ io ));
66
-
67
66
// remove existing config files
68
67
$ this ->compiledConfigReader ->removeConfig (AssetMapper::MANIFEST_FILE_NAME );
69
68
$ this ->compiledConfigReader ->removeConfig (ImportMapGenerator::IMPORT_MAP_CACHE_FILENAME );
@@ -74,6 +73,26 @@ protected function execute(InputInterface $input, OutputInterface $output): int
74
73
$ entrypointFiles [$ entrypointName ] = $ path ;
75
74
}
76
75
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
+
77
96
$ manifest = $ this ->createManifestAndWriteFiles ($ io );
78
97
$ manifestPath = $ this ->compiledConfigReader ->saveConfig (AssetMapper::MANIFEST_FILE_NAME , $ manifest );
79
98
$ io ->comment (\sprintf ('Manifest written to <info>%s</info> ' , $ this ->shortenPath ($ manifestPath )));
@@ -87,13 +106,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
87
106
$ styledEntrypointNames = array_map (fn (string $ entrypointName ) => \sprintf ('<info>%s</> ' , $ entrypointName ), array_keys ($ entrypointFiles ));
88
107
$ io ->comment (\sprintf ('Entrypoint metadata written for <comment>%d</> entrypoints (%s). ' , \count ($ entrypointFiles ), implode (', ' , $ styledEntrypointNames )));
89
108
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
-
97
109
return 0 ;
98
110
}
99
111
0 commit comments