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

Support add importmap script into head #4168

Open
4 tasks done
eshengsky opened this issue Aug 29, 2024 · 5 comments
Open
4 tasks done

Support add importmap script into head #4168

eshengsky opened this issue Aug 29, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@eshengsky
Copy link

Is your feature request related to a problem? Please describe.

I want to add a script which type is 'importmap' into top of head, but all solution is failed. The official custom head config, will add script into end of head, this causes 'importmap' to fail to work.

Describe the solution you'd like

  • add 'importmap' config in .vitepress/config
  • or, let head config to support position top/end

Describe alternatives you've considered

No response

Additional context

No response

Validations

@brc-dd brc-dd added the enhancement New feature or request label Aug 29, 2024
@brc-dd
Copy link
Member

brc-dd commented Aug 29, 2024

We can move that above other scripts by default maybe 👀 But having an order attribute might be more customizable.

@mistic100
Copy link

I think importmap should have a special handling and be directly added to the HTML at compile time (even in dev mode) and not added at runtime by useUpdateHead.

When declaring a head element with ['script', { type: 'importmap' }, ``....``] I get the following error (french):

Les mappages d’importation ne sont pas autorisés après le chargement d’un module ou le démarrage du préchargement.

Which roughly transletes to

Import mapping are not autorised after a module has been loaded or after the preloading has begun.

Has I understand it, importmap scripts cannot be added dynamically to the page.

@mistic100
Copy link

mistic100 commented Aug 31, 2024

Relevant vite issue vitejs/vite#15192

A working solution is privided here : use a Vite plugin to inject the importmap inside the HTML.

Example:

export default defineConfig({
    vite: {
        plugins: [
            {
                name: 'html-transform',
                transformIndexHtml(html) {
                    return html.replace('</head>', `
<script type="importmap">
{
    "imports": {
        .....
    }
}
</script>
</head>`,
                    );
                },
            },
        ],
    },
});

@eshengsky
Copy link
Author

Relevant vite issue vitejs/vite#15192

A working solution is privided here : use a Vite plugin to inject the importmap inside the HTML.

Example:

export default defineConfig({
    vite: {
        plugins: [
            {
                name: 'html-transform',
                transformIndexHtml(html) {
                    return html.replace('</head>', `
<script type="importmap">
{
    "imports": {
        .....
    }
}
</script>
</head>`,
                    );
                },
            },
        ],
    },
});

This is not working when vitepress build docs, just work in dev mode.

@mistic100
Copy link

mistic100 commented Sep 27, 2024

Vitepress transformHtml option have to be used to achieve the same effect at build time

transformHtml(html) {
        return html.replace('<head>', `<head>${importmap}`);
    },

See here for complete implementation (bottom of the file)
https://github.com/mistic100/Photo-Sphere-Viewer/blob/main/docs/.vitepress/config.mts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants