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 6.4] Question about bundle paths for import #1201

Closed
tacman opened this issue Oct 18, 2023 · 5 comments
Closed

[AssetMapper 6.4] Question about bundle paths for import #1201

tacman opened this issue Oct 18, 2023 · 5 comments

Comments

@tacman
Copy link
Contributor

tacman commented Oct 18, 2023

My controller.json file references a css file, but I get an error

"name": "@survos/simple-datatables",
"description": "integrate php and javascript helpers for Simple Datatables",
"license": "MIT",
"version": "1.0.4",
"symfony": {
  "controllers": {
    "table": {
      "main": "src/controllers/table_controller.js",
      "webpackMode": "eager",
      "fetch": "lazy",
      "enabled": true,
      "autoimport": {
        "@survos/simple-datatables/style.css": true
      }
    }
  },

The error:

An exception has been thrown during the rendering of a template ("An "autoimport" in "/assets/controllers.json" refers to "@survos/simple-datatables/style.css". This path could not be found in the asset mapper and no "@survos/simple-datatables" entry was found in importmap.php. And so, the file cannot be loaded.").

Indeed, the css is not in the importmap.php, how is that added? I do have it in my bundle

ls simple-datatables-bundle/assets/
package.json  
src  
style.css

indeed, though, the mapping is missing:

bin/console debug:asset-map --full

Asset Mapper Paths
------------------

 ------------------------------------------------------------------------------------ --------------------------- 
  Path                                                                                 Namespace prefix           
 ------------------------------------------------------------------------------------ --------------------------- 
  /home/tac/g/survos/survos/packages/simple-datatables-bundle/assets/src/controllers   @survos/simple-datatables  
  vendor/symfony/stimulus-bundle/assets/dist                                           @symfony/stimulus-bundle   
  assets                                                                                                          
 ------------------------------------------------------------------------------------ --------------------------- 

Mapped Assets
-------------

 ----------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------- 
  Logical Path                                                Filesystem Path                                                                                                     
 ----------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------- 
  @survos/simple-datatables/simple_datatables_controller.js   /home/tac/g/survos/survos/packages/simple-datatables-bundle/assets/src/controllers/simple_datatables_controller.js  
  @survos/simple-datatables/table_controller.js               /home/tac/g/survos/survos/packages/simple-datatables-bundle/assets/src/controllers/table_controller.js              
  @symfony/stimulus-bundle/controllers.js                     vendor/symfony/stimulus-bundle/assets/dist/controllers.js                                                           
  @symfony/stimulus-bundle/loader.js                          vendor/symfony/stimulus-bundle/assets/dist/loader.js                                                                
  styles/app.css                                              assets/styles/app.css                                                                                               
  bootstrap.js                                                assets/bootstrap.js                                                                                                 
  app.js                                                      assets/app.js                                                                                                       
  controllers/hello_controller.js                             assets/controllers/hello_controller.js                                                                              
  vendor/simple-datatables/dist/column_filter.min.css         assets/vendor/simple-datatables/dist/column_filter.min.css                                                          
  vendor/simple-datatables/simple-datatables.index.js         assets/vendor/simple-datatables/simple-datatables.index.js                                                          
  vendor/@hotwired/stimulus/stimulus.index.js                 assets/vendor/@hotwired/stimulus/stimulus.index.js                                                                  

bin/console debug:asset-map 

Asset Mapper Paths
------------------

 --------------------------------------------------- --------------------------- 
  Path                                                Namespace prefix           
 --------------------------------------------------- --------------------------- 
  /home/tac/g/survos/surv.../assets/src/controllers   @survos/simple-datatables  
  vendor/symfony/stimulus-bundle/assets/dist          @symfony/stimulus-bundle   
  assets                                                                         
 --------------------------------------------------- --------------------------- 

Mapped Assets
-------------

 --------------------------------------------------- ---------------------------------------------------- 
  Logical Path                                        Filesystem Path                                     
 --------------------------------------------------- ---------------------------------------------------- 
  @survos/simple-datatabl...atatables_controller.js   /home/tac/g/survos/surv...atatables_controller.js   
  @survos/simple-datatables/table_controller.js       /home/tac/g/survos/surv...ers/table_controller.js   
  @symfony/stimulus-bundle/controllers.js             vendor/symfony/stimulus...ets/dist/controllers.js   
  @symfony/stimulus-bundle/loader.js                  vendor/symfony/stimulus...e/assets/dist/loader.js   
  styles/app.css                                      assets/styles/app.css                               
  bootstrap.js                                        assets/bootstrap.js                                 
  app.js                                              assets/app.js                                       
  controllers/hello_controller.js                     assets/controllers/hello_controller.js              
  vendor/simple-datatable...t/column_filter.min.css   assets/vendor/simple-da...t/column_filter.min.css   
  vendor/simple-datatable...ple-datatables.index.js   assets/vendor/simple-da...ple-datatables.index.js   
  vendor/@hotwired/stimulus/stimulus.index.js         assets/vendor/@hotwired/stimulus/stimulus.index.js

Since this is 6.4, and css assets are so new, perhaps it's a bug. But equally likely is that I'm not naming my file correctly in the autoimports section of the bundle.

@tacman
Copy link
Contributor Author

tacman commented Oct 18, 2023

Perhaps I need not a namespaced path, but a relative path, like ../syle.css? If so, would that get expanded during the autoimport to vendor/...?

@weaverryan
Copy link
Member

Indeed, the css is not in the importmap.php, how is that added? I do have it in my bundle

We add a namspaced path via prepending configuration - good example here - https://github.com/symfony/ux/blob/2.x/src/Autocomplete/src/DependencyInjection/AutocompleteExtension.php#L52-L61

@tacman
Copy link
Contributor Author

tacman commented Oct 18, 2023

So I did have that logic in my bundle, but it was pointing to the src/Controllers directory. I've fixed it now so that it points to assets and the controllers and css are available.

bin/console debug:asset-map  | grep survos/simple  
  /home/tac/g/survos/surv...atatables-bundle/assets   @survos/simple-datatables  
  @survos/simple-datatabl...atatables_controller.js   /home/tac/g/survos/surv...atatables_controller.js   
  @survos/simple-datatabl...ers/table_controller.js   /home/tac/g/survos/surv...ers/table_controller.js   
  @survos/simple-datatables/package.json              /home/tac/g/survos/surv...dle/assets/package.json   
  @survos/simple-datatables/style.css                 /home/tac/g/survos/surv...bundle/assets/style.css   

What is the format for importing the css from a bundle into the script?

    <script type="module">
        import '@survos/simple-datatables/style.css'

Isn't working, nor my variations on that, do I need some sort of import first?

@weaverryan
Copy link
Member

If you're putting the CSS in autoimport, you shouldn't need to do anything else. It should output inside the ux_controller_link_tags() function.

But, more generally, if you WERE to import a CSS file from JS, you'd need to use either the relative path to the file or, if your CSS file is in importmap.php (it probably isn't), then you could use the import name there. But inside of JS, asset mapper paths are meaningless. These are really just for using with the asset() Twig function.

@tacman
Copy link
Contributor Author

tacman commented Oct 18, 2023

OK, I think I understand now! Thanks!

FWIW, here it is in action. No build step!

symfony new simple-datatables-demo --webapp --version=next --php=8.2 && cd simple-datatables-demo
composer config minimum-stability dev
composer config extra.symfony.allow-contrib true
composer req symfony/asset-mapper:^6.4
composer req symfony/stimulus-bundle:2.x-dev
composer req survos/simple-datatables-bundle
bin/console make:controller Simple -i
cat > templates/simple.html.twig <<END
{% extends 'base.html.twig' %}

{% block body %}
     <table class="table" {{ stimulus_controller('@survos/simple-datatables-bundle/table', {perPage: 5, sortable: true}) }}>
        <thead>
        <tr>
            <th>abbr</th>
            <th>name</th>
            <th>number</th>
        </thead>
        <tbody>
        {% for j in 1..12 %}
            <tr>
                <td>{{ j |date('2023-' ~ j ~ '-01') |date('M') }}</td>
                <td>{{ j |date('2023-' ~ j ~ '-01') |date('F') }}</td>
                <td>{{ j }}</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
{% endblock %}
END
symfony server:start -d
symfony open:local --path=/simple

@tacman tacman closed this as completed Oct 18, 2023
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

No branches or pull requests

2 participants