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

Enqueue styles and scripts #67

Open
djmtype opened this issue Oct 25, 2021 · 6 comments
Open

Enqueue styles and scripts #67

djmtype opened this issue Oct 25, 2021 · 6 comments

Comments

@djmtype
Copy link

djmtype commented Oct 25, 2021

How do we properly enqueue styes and scripts with this plugin?

If I reference these files from the dist directory that Sage ends up building, they are appended with random characters, so that's not going to work unless this plugin understands Sage's asset handling.

Or do I need to process these files in advance?

@yannickninot
Copy link

@djmtype Did you find a solution?
I have the same problem and am looking for a solution

@yannickninot
Copy link

Here is a temporary solution.
In setup.php if (has_block('acf/blockName')) { bundle('blockName')->enqueue(); }

@djmtype
Copy link
Author

djmtype commented Jul 4, 2022

@yannickninot Thanks!
And here's how this looks in its entirety for anyone else in need.

Create the template:
/resources/views/blocks/testimonial.blade.php

Added the s/css file:
resources/styles/blocks/testimonial.scss

testimonial.blade.php

{{--
EnqueueStyle: 
--}}

Leave EnqueueStyle empty as this will be declared within setup.php.

Otherwise, we don't want to load the same CSS file twice – once with the hash, as will be defined in setup.php, and another time via EnqueueStyle.

bud.config.js

app.entry({
      app: ['@scripts/app', '@styles/app'],
      editor: ['@scripts/editor', '@styles/editor'],
      testimonial: ['@styles/blocks/testimonial']
    })

setup.php
Find the add_action function for enqueuing scripts.

add_action('wp_enqueue_scripts', function () {
    bundle('app')->enqueue();
    if (has_block('acf/testimonial')) { 
        bundle('testimonial')->enqueue(); 
    }
}, 100);

@djmtype
Copy link
Author

djmtype commented Jul 4, 2022

If you want to expose those styles to the editor, you'll have to duplicate your efforts within setup.php under enqueue_block_editor_assets.

add_action('enqueue_block_editor_assets', function () {
    bundle('editor')->enqueue();
    if (has_block('acf/testimonial')) { 
        bundle('testimonial')->enqueue(); 
    }
}, 100);

@Striffly
Copy link

Striffly commented Sep 1, 2022

I found a trick to include the front style in Gutenberg :

  1. Copy the style from your app.css file to the editor.css file (with a simple configuration, an @import should be enough).
  2. Add the postcss-prefixwrap plugin. Use either postcss.config.js (tested) or bud.config.js (not tested). Set .editor-styles-wrapper as prefix.
  3. Make sure that the plugin is only executed on the editor.css file : from postcss.config.js, just check if the name of the processed file contains editor.css.
    Ex:
module.exports = (api) => {
  return {
    ident: 'postcss',
    plugins: getPlugins(api.file.includes('/editor.css')),
  };
};
  1. Disable the editor's styles
add_action('after_setup_theme', function () {
  remove_editor_styles();
}, 999);

And it's done !

@robmeijerink
Copy link
Contributor

@djmtype Thank you for figuring it out for us. Saved us some research time and I used some of your code to enable Bud support. I made a PR #75 so we can enqueue our styles and scripts again like before, but this time by using the Bud bundle name instead of the path to file like before.

Sorry for the late response. I haven't had much spare time to look into it.

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

No branches or pull requests

4 participants