Skip to content
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

[AssetMapper] Re-configure "missing_import_mode" in prod/non-prod envs #1347

Merged
merged 1 commit into from
Nov 14, 2024

Conversation

Kocal
Copy link
Member

@Kocal Kocal commented Oct 11, 2024

Q A
License MIT
Doc issue/PR symfony/symfony-docs#...

When working on my project with the AssetMapper, it became frustrating to found that my relative import didn't work because .js was missing at the end of the import path.

I wanted to implement something which tells the user to specify .js if we found that <import path>.js exists, but... the feature already exist, through missing_import_mode configuration.

The thing is, defaulting to warn (which warns import errors in the profiler) by default is not very useful.
When you work locally on your project, you want errors to be displayed clearly so you can quickly fix them.

This PR updates the missing_import_mode to strict (so exceptions are thrown) on all environment, but keep it to warn in prod environment.

@symfony-recipes-bot symfony-recipes-bot enabled auto-merge (squash) October 11, 2024 21:26
Copy link

github-actions bot commented Oct 11, 2024

Thanks for the PR 😍

How to test these changes in your application

  1. Define the SYMFONY_ENDPOINT environment variable:

    # On Unix-like (BSD, Linux and macOS)
    export SYMFONY_ENDPOINT=https://raw.githubusercontent.com/symfony/recipes/flex/pull-1347/index.json
    # On Windows
    SET SYMFONY_ENDPOINT=https://raw.githubusercontent.com/symfony/recipes/flex/pull-1347/index.json
  2. Install the package(s) related to this recipe:

    composer req 'symfony/flex:^1.16'
    composer req 'symfony/asset-mapper:^6.4'
  3. Don't forget to unset the SYMFONY_ENDPOINT environment variable when done:

    # On Unix-like (BSD, Linux and macOS)
    unset SYMFONY_ENDPOINT
    # On Windows
    SET SYMFONY_ENDPOINT=

Diff between recipe versions

In order to help with the review stage, I'm in charge of computing the diff between the various versions of patched recipes.
I'm going keep this comment up to date with any updates of the attached patch.

symfony/asset-mapper

6.3 vs 6.4
diff --git a/symfony/asset-mapper/6.3/assets/app.js b/symfony/asset-mapper/6.4/assets/app.js
index cb0082a..6174cc6 100644
--- a/symfony/asset-mapper/6.3/assets/app.js
+++ b/symfony/asset-mapper/6.4/assets/app.js
@@ -4,4 +4,6 @@
  * This file will be included onto the page via the importmap() Twig function,
  * which should already be in your base.html.twig.
  */
-console.log('This log comes from assets/app.js - welcome to AssetMapper! 🎉')
+import './styles/app.css';
+
+console.log('This log comes from assets/app.js - welcome to AssetMapper! 🎉');
diff --git a/symfony/asset-mapper/6.3/config/packages/asset_mapper.yaml b/symfony/asset-mapper/6.4/config/packages/asset_mapper.yaml
index d1ac653..f7653e9 100644
--- a/symfony/asset-mapper/6.3/config/packages/asset_mapper.yaml
+++ b/symfony/asset-mapper/6.4/config/packages/asset_mapper.yaml
@@ -3,3 +3,9 @@ framework:
         # The paths to make available to the asset mapper.
         paths:
             - assets/
+        missing_import_mode: strict
+
+when@prod:
+    framework:
+        asset_mapper:
+            missing_import_mode: warn
diff --git a/symfony/asset-mapper/6.3/importmap.php b/symfony/asset-mapper/6.4/importmap.php
index 5c2c21d..70ebf14 100644
--- a/symfony/asset-mapper/6.3/importmap.php
+++ b/symfony/asset-mapper/6.4/importmap.php
@@ -1,21 +1,19 @@
 <?php
 
 /**
- * Returns the import map for this application.
+ * Returns the importmap for this application.
  *
  * - "path" is a path inside the asset mapper system. Use the
  *     "debug:asset-map" command to see the full list of paths.
  *
- * - "preload" set to true for any modules that are loaded on the initial
- *     page load to help the browser download them earlier.
+ * - "entrypoint" (JavaScript only) set to true for any module that will
+ *     be used as an "entrypoint" (and passed to the importmap() Twig function).
  *
  * The "importmap:require" command can be used to add new entries to this file.
- *
- * This file has been auto-generated by the importmap commands.
  */
 return [
     'app' => [
-        'path' => 'app.js',
-        'preload' => true,
+        'path' => './assets/app.js',
+        'entrypoint' => true,
     ],
 ];
diff --git a/symfony/asset-mapper/6.3/manifest.json b/symfony/asset-mapper/6.4/manifest.json
index c6fb477..7454a3d 100644
--- a/symfony/asset-mapper/6.3/manifest.json
+++ b/symfony/asset-mapper/6.4/manifest.json
@@ -6,22 +6,19 @@
     },
     "aliases": ["asset-mapper", "importmap"],
     "gitignore": [
-        "/%PUBLIC_DIR%/assets/"
+        "/%PUBLIC_DIR%/assets/",
+        "/assets/vendor/"
     ],
+    "composer-scripts": {
+        "importmap:install": "symfony-cmd"
+    },
     "add-lines": [
         {
             "file": "templates/base.html.twig",
-            "content": "            {{ importmap() }}",
+            "content": "            {% block importmap %}{{ importmap('app') }}{% endblock %}",
             "position": "after_target",
             "target": "{% block javascripts %}",
             "warn_if_missing": true
-        },
-        {
-            "file": "templates/base.html.twig",
-            "content": "            <link rel=\"stylesheet\" href=\"{{ asset('styles/app.css') }}\">",
-            "position": "after_target",
-            "target": "{% block stylesheets %}",
-            "warn_if_missing": true
         }
     ],
     "conflict": {

auto-merge was automatically disabled October 11, 2024 21:31

Head branch was pushed to by a user without write access

@Kocal Kocal force-pushed the asset-mapper-strict-mode-dev branch from 3916cd2 to 083d835 Compare October 11, 2024 21:31
@symfony-recipes-bot symfony-recipes-bot enabled auto-merge (squash) October 11, 2024 21:31
Copy link
Member

@fabpot fabpot left a comment

Choose a reason for hiding this comment

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

That works for me

@Kocal
Copy link
Member Author

Kocal commented Oct 24, 2024

(checks are failing because sf/asset-mapper requires php >=8.1, while it use php 7.4)

auto-merge was automatically disabled November 1, 2024 13:02

Head branch was pushed to by a user without write access

@Kocal Kocal force-pushed the asset-mapper-strict-mode-dev branch from 083d835 to ab0209c Compare November 1, 2024 13:02
@Kocal
Copy link
Member Author

Kocal commented Nov 1, 2024

PR rebased

@symfony-recipes-bot symfony-recipes-bot enabled auto-merge (squash) November 1, 2024 13:02
@fabpot fabpot disabled auto-merge November 14, 2024 22:23
@fabpot fabpot merged commit 6c43cb8 into symfony:main Nov 14, 2024
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants