@@ -23,15 +23,115 @@ It includes the following extensisons:
2323npm install @roots/bud-preset-wordpress --save-dev
2424```
2525
26- ## Excluding predefined externals
26+ ## Managing WordPress enqueues
2727
28- If WordPress provides a package in your bundle any references you make to it will be replaced by a reference to the WP provided global.
28+ If you are using [ roots/sage] ( https://roots.io/sage ) these details are handled for you by Acorn.
29+
30+ Otherwise you will want to do something like this:
31+
32+ ``` php
33+ add_action('enqueue_block_editor_assets', function () {
34+ // couple helper functions
35+ $get_path = fn ($endpoint) => join([get_template_directory(), 'dist', $endpoint], '/');
36+ $read = fn ($endpoint) => file_get_contents($dist_path($endpoint));
37+
38+ // entrypoints.json as an object
39+ $entrypoints = json_decode($read('entrypoints.json'));
40+
41+ wp_enqueue_script(
42+ 'my-theme/editor/js',
43+ $get_path($entrypoints->editor->js[1]),
44+ [],
45+ null,
46+ true
47+ );
48+
49+ wp_enqueue_inline_script(
50+ 'my-theme/editor/js',
51+ $read($entrypoints->editor->js[0]),
52+ 'before',
53+ );
54+
55+ wp_enqueue_style(
56+ 'my-theme/editor/css',
57+ $get_path($entrypoints->editor->css[0]),
58+ [],
59+ null
60+ );
61+ });
62+ ```
63+
64+ ## Managing WordPress runtime dependencies
65+
66+ If WordPress provides a package any references you make to it will be replaced by a reference to the WP provided global.
67+
68+ For example, if you import jquery in your application like so:
69+
70+ ```
71+ import $ from 'jquery'
72+ ```
73+
74+ It will be bundled as something like this:
75+
76+ ``` js
77+ const e = window .jQuery
78+ ```
79+
80+ If you check out ` entrypoints.json ` you'll see the WordPress dependencies listed per entrypoint under the ` dependencies ` key:
81+
82+ ``` json
83+ {
84+ "editor" : {
85+ "js" : [
86+ " js/runtime.6390bb.js" ,
87+ " js/editor.b7e1d1.js"
88+ ],
89+ "css" : [
90+ " css/editor.8cd6ea.css"
91+ ],
92+ "dependencies" : [
93+ " jquery" ,
94+ " wp-blocks" ,
95+ " wp-dom-ready"
96+ ]
97+ }
98+ }
99+ ```
100+
101+ The intent is for you to read this file in your WordPress theme or plugin and enqueue the dependencies dynamically.
102+
103+ For example, building off the previous example code, this is how one might handle enqueues in a WordPress theme:
104+
105+ ``` php
106+ add_action('enqueue_block_editor_assets', function () {
107+ // couple helper functions
108+ $get_path = fn ($endpoint) => join([get_template_directory(), 'dist', $endpoint], '/');
109+ $read = fn ($endpoint) => file_get_contents($dist_path($endpoint));
110+
111+ // entrypoints.json as an object
112+ $entrypoints = json_decode($read('entrypoints.json'));
113+
114+ wp_enqueue_script(
115+ 'my-theme/editor/js',
116+ $get_path($entrypoints->editor->js[1]),
117+ $entrypoints->editor->dependencies,
118+ null,
119+ true
120+ );
121+
122+ // ...
123+ });
124+ ```
125+
126+ ### Excluding modules
29127
30128There may be situations where you want to exclude a package from this behavior. For example, you may wish to use a different version of jQuery than the one provided by WordPress.
31129
32- To that end you can pass an array of packages to exclude:
130+ To address this, you can pass an array of packages to exclude:
33131
34132``` ts title=bud.config.ts
133+ import type {Bud } from ' @roots/bud'
134+
35135export default async (bud : Bud ) => {
36136 bud .wp .externals .exclude ([' jquery' ])
37137}
0 commit comments