Skip to content
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ js_dist
/assets/vendor/
drivers
chromedriver.log
/htdocs/dist
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ phpstan:
### Compiler les assets pour la production
assets:
./node_modules/.bin/webpack -p
php bin/console importmap:install
Comment thread
Mopolo marked this conversation as resolved.

### Lancer le watcher pour les assets
watch:
Expand Down
2 changes: 2 additions & 0 deletions app/config/packages/asset_mapper.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ framework:
- '%kernel.project_dir%/../assets/'
importmap_path: '%kernel.project_dir%/../importmap.php'
missing_import_mode: strict
vendor_dir: '%kernel.project_dir%/../assets/vendor'
public_prefix: /dist/

when@prod:
framework:
Expand Down
9 changes: 9 additions & 0 deletions app/config/services.yml
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Surcharge du répertoire final des assets après compilation (cf. https://symfony.com/doc/current/frontend/asset_mapper.html#serving-assets-in-dev-vs-prod).

Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,12 @@ services:
autowire: true
arguments:
$transports: !tagged_iterator app.social_network.transport

asset_mapper.local_public_assets_filesystem:
class: AppBundle\AssetMapper\PostCompilationCopyHandler
autowire: true

asset_mapper.compiled_asset_mapper_config_reader:
class: Symfony\Component\AssetMapper\CompiledAssetMapperConfigReader
arguments:
$directory: '%kernel.project_dir%/../htdocs/dist'
2 changes: 2 additions & 0 deletions htdocs/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@ RewriteRule ^ask/forum/? https://event.afup.org [R=301,L]
RewriteRule ^ask/? / [R=301,L]

## This url are handled by symfony
RewriteCond %{ENV:APP_ENV} ^(dev|test)$
RewriteRule (dist)/? /index.php [L] # Asset in local development
RewriteRule (event|connect|_|association|talks|meetups|blog|admin|p|home|news|techno_watch|member|login|logout|password-reset|rss.xml)/? /index.php [END]
7 changes: 7 additions & 0 deletions importmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@
'path' => './assets/app.js',
'entrypoint' => true,
],
'semantic-ui/dist/semantic.min.css' => [
'version' => '2.5.0',
Comment thread
stakovicz marked this conversation as resolved.
'type' => 'css'
],
'semantic-ui/dist/semantic.min.js' => [
'version' => '2.5.0'
],
];
64 changes: 64 additions & 0 deletions sources/AppBundle/AssetMapper/PostCompilationCopyHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace AppBundle\AssetMapper;

use Symfony\Component\AssetMapper\Compressor\CompressorInterface;
use Symfony\Component\AssetMapper\Path\PublicAssetsFilesystemInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Filesystem\Filesystem;

/**
* cf. https://symfony.com/doc/current/frontend/asset_mapper.html#serving-assets-in-dev-vs-prod
*/
class PostCompilationCopyHandler implements PublicAssetsFilesystemInterface
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

C'est à cause du htdocs ça ou autre chose ?

Si un jour on migre vers un public est-ce que cette classe sera encore nécessaire ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Oui c'est en partie à cause de ça mais surtout parce que la commande asset-map:compile se base sur le paramètre %kernel.project_dir% qui dans notre cas est le répertoire app à la racine.
Ainsi, sans surcharge, les fichiers compilés seraient écrits dans app/public.

J'aurai pu surcharger la valeur de %kernel.project_dir% dans le composer.json config.extra.symfony-app-dir mais j'ai préféré éviter pour limiter les effets de bord.

{
private Filesystem $filesystem;

/**
* @param string[] $extensionsToCompress
*/
public function __construct(
#[Autowire('%kernel.project_dir%/../htdocs')]
private readonly string $publicDir,
private readonly ?CompressorInterface $compressor = null,
private readonly array $extensionsToCompress = [],
) {
$this->filesystem = new Filesystem();
}

public function write(string $path, string $contents): void
{
$targetPath = $this->publicDir . '/' . ltrim($path, '/');

$this->filesystem->dumpFile($targetPath, $contents);
$this->compress($targetPath);
}

public function copy(string $originPath, string $path): void
{
$targetPath = $this->publicDir . '/' . ltrim($path, '/');

$this->filesystem->copy($originPath, $targetPath, true);
$this->compress($targetPath);
}

public function getDestinationPath(): string
{
return $this->publicDir;
}

private function compress(string $targetPath): void
{
foreach ($this->extensionsToCompress as $ext) {
if (!str_ends_with($targetPath, ".$ext")) {
continue;
}

$this->compressor?->compress($targetPath);

return;
}
}
}
4 changes: 2 additions & 2 deletions templates/admin/base_with_header.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Administration AFUP</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
<link rel="stylesheet" href="{{ asset('vendor/semantic-ui/dist/semantic.min.css') }}">
<link rel="icon" type="image/png" href="/templates/administration/images/favicon-96x96.png" sizes="96x96" />
<link rel="icon" type="image/svg+xml" href="/templates/administration/images/favicon.svg" />
<link rel="shortcut icon" href="/templates/administration/images/favicon.ico" />
Expand Down Expand Up @@ -133,7 +133,7 @@

{% block javascript %}
<script src="https://code.jquery.com/jquery-3.1.1.min.js" crossorigin="anonymous"></script>
Comment thread
stakovicz marked this conversation as resolved.
<script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.js"></script>
<script src="{{ asset('vendor/semantic-ui/dist/semantic.min.js') }}"></script>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css">
<script src="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion templates/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@

{% block footer '' %}
{% block javascripts '' %}
{# {% block importmap %}{{ importmap('app') }}{% endblock %}#}
{% block importmap %}{{ importmap('app') }}{% endblock %}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

L'importmap il faut la mettre dans la balise <head> plutôt.

Mais bon ce template c'est pas pour l'admin donc faudra juste penser à le bouger quand on activera l'asset mapper pour le front.

</body>
</html>
Loading