Acorn - asset() doesn't work

Hi, i have a problem with asset functions in Acorn.
I use this docs: Acorn 2.x: Assets Management | Roots Documentation
And this is my code:

    public function generateCriticalCSS(): void
    {
        $asset            = \Roots\asset('speed.css');
        $critical_content = $asset->contents();
        $style            = "<style>{$critical_content}</style>";
        echo $style;
    }

But it doesnt work. So i copy one line of this code to setup.php

$asset = \Roots\asset('speed.css');

Fatal error : Uncaught Error: Call to undefined function app()
in /app/wp/wp-content/themes/bibabo/vendor/roots/acorn/src/Roots/helpers.php on line 20

asset() works fine some time ago, but yesterday i upgrade my composer and js libraries and now its doesnt work :frowning:

package.json

    "devDependencies": {
        "@roots/bud": "^6.2.0",
        "@roots/bud-imagemin": "^6.2.0",
        "@roots/bud-postcss": "^6.2.0",
        "@roots/bud-sass": "^6.2.0",
        "@roots/bud-tailwindcss": "^6.2.0",
        "@roots/sage": "^6.2.0",
        "@tailwindcss/typography": "^0.5.2",
        "prettier": "2.7.1",
        "prettier-plugin-tailwindcss": "^0.1.11",
        "tailwindcss-fluid": "^0.2.0",
        "tailwindcss-fluid-spacing": "^0.4.0"
    },
    "volta": {
        "node": "16.15.1",
        "npm": "8.3.1"
    },
    "dependencies": {
        "@alpinejs/intersect": "^3.10.2",
        "alpinejs": "^3.10.2",
        "glightbox": "^3.2.0",
        "moment": "^2.29.3",
        "moment-timezone": "^0.5.34",
        "swiper": "^8.2.4",
        "tailwind-hamburgers": "^1.2.2",
        "tailwindcss-fluid-type": "^1.3.3"
    }

composer.json

    "require": {
        "php": "^7.4|^8.1",
        "advanced-custom-fields/advanced-custom-fields-pro": "~5.12.2",
        "log1x/acf-composer": "^2.0",
        "log1x/sage-directives": "^1.1",
        "log1x/sage-svg": "^1.1",
        "phpunit/phpunit": "^9.5",
        "roots/acorn": "^2.0",
        "wpackagist-plugin/acf-extended": "^0.8.8.7"
    },

Please explain “doesn’t work.”

  • How does it not work?
  • What was the expected outcome?
  • What happened instead?
  • Where is this code called? What calls generateCriticalCss()?

Why did you do this? What did you hope would be the outcome of this change?

What dependencies did you upgrade? If you rollback the upgrades, does everything work as expected?

Hi, sorry, I did not describe it precisely.

I found solution for my problem, but i dont know why this work :wink:

I call generateCriticalCss() in wp_head action.
This is current code status:

    public function generateCriticalCSS(): void
    {
        $asset            = \Roots\asset('speed/entry.css');
        $critical_content = $asset->contents();
        $style            = "<style>{$critical_content}</style>";
        echo Minificator::minifyCode($style);
    }

I must always use speed/entry.css? If i use only \Roots\asset('speed.css'); this function return null.

bud.config.js

        .entry({
            app: ["@scripts/app", "@styles/app"],
            speed: ["@styles/critical"],
            editor: ["@scripts/editor", "@styles/editor"],
            tiny_mce: ["@scripts/tiny_mce"],
        })

The key you need for asset() is going to be the key found in the object in your public/manifest.json file for the asset you want to reference. If it’s not what you expect, I can’t speculate on why that is. Either I don’t understand your setup well enough, of I don’t understand how Bud behaves well enough.