public function enqueue() {
bundle('example')->enqueue();
}
This is working fine in the frontend but not in editor. You can check by adding a simple console.log(‘test’) to editor.js or blocks/example.js file, and it doesn’t show in the editor.
Alternatively, the following seems to be working for simple js:
ACF block enqueue function:
public function enqueue() {
wp_enqueue_script('example_js', \Roots\asset('example.js')->uri(), [], '', true);
}
Then console.log(‘test’) is showing both in the frontend and in the editor.
But then I tried a more complex script for the block, importing Swiper. Using wp_enqueue_script() doesn’t work anymore as swiper is not initialized. Using the bundle() function makes it work in the frontend but still breaks js in the editor.
Am I doing something wrong ? I can’t get why this is happening
BTW thank you for all your amazing work, I’ve been using Sage for years and I love it !
I don’t personally use the enqueue method so I may not be much help but, Sage uses enqueue_block_editor_assets (docs) to enqueue assets in the block editor.
I’ve seen it as it’s how the editor.js file is added. But I’ve been using the enqueue() function in ACF blocks to load scripts only when the block is used.
I could of course add all my blocks’ scripts in the editor, but I would still need the enqueue() function in ACF blocks for the frontend. And then I would still have the issues mentioned.
First, in your block enqueue method, the bundle function needs a parameter.
Second, you need to list the scripts and styles in the bud config file. I believe it’s a bud.config.json file located at your theme’s root.
Look how I set it in my first post. For a block Example, I added an entry named ‘example’ in the bud config file, which is an array containing the needed scripts and/or styles for the block. Then in the bundle function I set the parameter to be this entry ‘example’.
Even if you have only one script or style file, it still needs to be an array.