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 import map support in thymleaf #314

Open
benzen opened this issue Jul 14, 2023 · 0 comments
Open

add import map support in thymleaf #314

benzen opened this issue Jul 14, 2023 · 0 comments

Comments

@benzen
Copy link

benzen commented Jul 14, 2023

Given a spring boot /spring web app which contains some js and css assets.

When importing css files, spring boot does the right thing by mapping the used path to a path that contains a file hash. Great.
When my css files import other files, say font, spring boot does the right thing and update the path used ton use a version of thr same path which include file hash. Wonderfull.
When My html page import js script, spring boot, once again does the right thing Cooool.

But if my js files are js modules which require one another the the import path is not updated.
I've raised this issue previously and I remember being said that parsing js import is way more complexe than css import.

Which I understand and accept.

But I've found a way that could help to find a better way.
In html now we can define an import map, which will declare a mapping between a used path and a real one for our js assets.
This can be used to support fully js modules files with cross import and file name hash.

We can do it by hand, but I think that spring would be able to auto generate and inject that import map for us in the html pages.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap

Here is what I use

<script type="importmap" th:inline="javascript"> { "imports": { "turbo": "[(@{/webjars/hotwired__turbo/dist/turbo.es2017-esm.js})]", "stimulus": "[(@{/webjars/hotwired__stimulus/dist/stimulus.js})]", "chart-js": "[(@{/webjars/chartjs/dist/chart.esm.js})]", "luxon": "[(@{/webjars/luxon/build/es6/luxon.js})]" } } </script>

Since we can inject link with thymleaf, we can generate them completly with the hash.
Maybe a new form would need to be created to produce this import map and not always produce it.

Hope this will help advance the conversation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant