nystudio107 / craft-vite

Allows the use of the Vite.js next generation frontend tooling with Craft CMS
MIT License
52 stars 16 forks source link

Use inside general.php #43

Closed webrgp closed 2 years ago

webrgp commented 2 years ago

Question

I am trying to load a built css file in my general.php file. What's the PHP version of {{ craft.vite.entry("app.css") }}?

khalwat commented 2 years ago

I think you probably should not be loading CSS inside of your config/general.php file but:

Vite::$plugin->viteService->entry($path)

https://github.com/nystudio107/craft-plugin-vite/blob/develop/src/variables/ViteVariableTrait.php#L83

webrgp commented 2 years ago

Here is some context: I am using doublesecretagency/craft-cpcss plugin to load some custom styling in the control panel, which I generate using Vite. In it's settings I am referencing a custom alias @controlPanelCss.

In the general.php file I have this:


use craft\helpers\App;
use nystudio107\vite\Vite;

return [
    ...
    'aliases' => [
        ...
        '@controlPanelCss' => App::env('SITE_URL') . Vite::$plugin->vite->entry('src/js/website-styles.css'),
        ...
    ],
    ...
];

But now I get this error:

Fatal error: Uncaught Error: Typed static property nystudio107\vite\Vite::$plugin must not be accessed before initialization in /var/www/html/cms/config/general.php:15 Stack trace: #0 /var/www/html/cms/vendor/craftcms/cms/src/services/Config.php(277): include() #1 /var/www/html/cms/vendor/craftcms/cms/src/services/Config.php(269): craft\services\Config->_configFromFileInternal('/var/www/html/c...') #2 /var/www/html/cms/vendor/craftcms/cms/bootstrap/bootstrap.php(166): craft\services\Config->getConfigFromFile('general') #3 /var/www/html/cms/vendor/craftcms/cms/bootstrap/web.php(40): require('/var/www/html/c...') #4 /var/www/html/cms/web/index.php(11): require('/var/www/html/c...') #5 {main} thrown in /var/www/html/cms/config/general.php on line 15

Any thoughts?

khalwat commented 2 years ago

Plugins are not loaded yet when general.php is executed.

I'd suggest just writing a little custom module to load your CSS, ala: https://github.com/nystudio107/craft/blob/craft-vite/cms/modules/sitemodule/src/SiteModule.php#L111

webrgp commented 2 years ago

Thanks! I will pursue this path.