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

Add baseUrl to a importmap() #173

Closed
tacman opened this issue Oct 4, 2023 · 8 comments
Closed

Add baseUrl to a importmap() #173

tacman opened this issue Oct 4, 2023 · 8 comments

Comments

@tacman
Copy link

tacman commented Oct 4, 2023

Symfony's importmap() function generates

<script type="importmap">
{
    "imports": {
        "app": "/assets/app-e4599d4ad24d45913e4d05569a4414b4.js",
        "/assets/bootstrap.js": "/assets/bootstrap-c423b8bbc1f9cae218c105ca8ca9f767.js",
        "@symfony/stimulus-bundle": "/assets/@symfony/stimulus-bundle/loader-e1ee9ace0562f2e6a52301e4ccc8627d.js",
        "/assets/@symfony/stimulus-bundle/controllers.js": "/assets/@symfony/stimulus-bundle/controllers-abf606f3085d4a46150023eb97dbf33b.js",
        "/assets/controllers/hello_controller.js": "/assets/controllers/hello_controller-c19dd529e97e8ef33363253ca96d13fe.js",
        "/assets/controllers/api_controller.js": "/assets/controllers/api_controller-192ad26be8b939ca8ca28b3dad1fa53f.js",
        "@hotwired/stimulus": "https://cdn.jsdelivr.net/npm/@hotwired/[email protected]/+esm",
        "@fullcalendar/daygrid": "https://cdn.jsdelivr.net/npm/@fullcalendar/[email protected]/+esm",
        "@fullcalendar/interaction": "https://cdn.jsdelivr.net/npm/@fullcalendar/[email protected]/+esm",
        "@fullcalendar/core": "https://cdn.jsdelivr.net/npm/@fullcalendar/[email protected]/+esm"
    }
}

when there's a base url, though, those routes need to be prefixed, like the path() commands.

I use this to publish to github pages:

bin/console -e prod stenope:build ./docs --base-url /micro-calendar/

https://tacman.github.io/micro-calendar/fullcalendar/

I've hacked around this problem by embedding my app.js in the HTML itself, but obviously it'd be better if the generator tweaked the paths.

Thanks.

@ogizanagi
Copy link
Member

Is there a way to provide a base URL when using importmap? I suspect this to be more a Symfony feature request rather than a Stenope one

@tacman
Copy link
Author

tacman commented Oct 9, 2023

Not that I know of. Can you point to where in your code the links are created?

Your code IS working as expected for the web page links, just not for the assets. I think the issue is best fixed in Stenope, since --base-url is being passed into your build command. The asset links are probably generated differently than the embedded HTML page links.

For production, the assetmapper files exist on disk, so it's just a simple path (in dev, there's some magic that maps the asset routes). That's why I think the baseUrl needs to be added in stenope.

I can provide a very simple demo repo if that's helpful.

@ogizanagi
Copy link
Member

ogizanagi commented Oct 9, 2023

I'm not used to the AssetMapper yet. That's why I'm wondering how could Stenope communicate the baseURL to generate the proper links when using importmap? Where should we hook?

For routing (path), this is handled by setting the request context:

Stenope/src/Builder.php

Lines 186 to 189 in 4ff235c

public function setBaseUrl(string $baseUrl): void
{
$this->router->getContext()->setBaseUrl('/' . ltrim($baseUrl, '/'));
}

What would be the equivalent for generating the proper importmap?
Or is there a config entry that would allow to configure the base URL in the userland code, allowing to use an env var?

@tacman
Copy link
Author

tacman commented Oct 9, 2023

Thanks. AssetMapper 6.4 is marked as experimental, but will be stable in 6.4. I'll add this as an issue, as it does seem like baseUrl should be honored there, too. Can we keep this open until it's functional?

I've been playing around with the steno-skeleton, trying to remove webpack and replace it with AssetMapper. It'll be very cool when it's done!

@ogizanagi
Copy link
Member

ogizanagi commented Nov 15, 2023

It seems to me the default asset package is used to render the importmap. Did you try to set the base_path here (using an env var to provide when building for your production environment) ?

@ogizanagi
Copy link
Member

ogizanagi commented Nov 15, 2023

Should work using 6.4 at least : symfony/symfony#52561

With:

    assets:
        base_urls: https://stenopephp.github.io/skeleton/

image

and with base_path :

    assets:
        base_path: /skeleton

image

Please note this : symfony/symfony#52561 (comment)

@ogizanagi
Copy link
Member

See StenopePHP/skeleton#2 for skeleton upgrade to the asset mapper

ogizanagi added a commit to StenopePHP/skeleton that referenced this issue Dec 8, 2023
To experiment with asset mapper & stenope, according to
StenopePHP/Stenope#173 issue.

Also, this will be the future of assets handling with Symfony, no more
bundler implied :
symfony/demo#1449 (comment)
@ogizanagi
Copy link
Member

Closing as resolved, using Symfony 6.4/7.0

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