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 registerAsset() function so register functionality can work directly with css #63

Closed
acalvino4 opened this issue Apr 6, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@acalvino4
Copy link

acalvino4 commented Apr 6, 2023

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

I need to add css to my control panel, and want it to work whether I've built my production css or not, whether I'm running my dev server or not, and want it to work with hash suffixes that may change. Hence, I need it to work with my vite setup.

I can easily do this with js/ts using a custom module event like

        Event::on(
            View::class,
            View::EVENT_BEFORE_RENDER_TEMPLATE,
            function(TemplateEvent $event) {
                Vite::getInstance()?->vite->register('src/scripts/cp.ts');
            }
        );

However, if I try to load a css file that way, I get this console error in the browser

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/css". Strict MIME type checking is enforced for module scripts per HTML spec.

since vite->register outputs a script tag.

Describe the solution you would like

The ideal solution would be modifying the register function to detect what type of resource is being registered and output the appropriate tag. An alternative function such as registerAsset() is completely fine by me as well.

Describe alternatives you have considered

  • There of course is the cp css plugin, but that on it's own doesn't deal well with the cache-busting hashes, and doesn't allow access to css via the dev server / without building.
  • I could just use the register function that this plugin already provides to register js, then load my css from there. That's obviously unideal since it introduces extraneous intermediate files, and is the kind of annoyance that led to this issue.
  • I could probably figure out a way to combine Craft::$app->getView()->registerCssFile() with Vite::getInstance()?->vite->asset(), and that is my backup plan, but it's an undesirable increase in complexity.
@acalvino4 acalvino4 added the enhancement New feature or request label Apr 6, 2023
@khalwat
Copy link
Contributor

khalwat commented Feb 16, 2024

You should be able to do this by leveraging the https://github.com/nystudio107/craft-plugin-vite package. It's what I use for my plugins, you can see example usage in the Retour plugin:

https://github.com/nystudio107/craft-retour

@khalwat khalwat closed this as completed Feb 16, 2024
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

2 participants