Skip to content

Commit 168f281

Browse files
committed
feat: use unique handle for runtime.js
By doing this, we are preventing multiple enqueue of the same file in the same page and executing modules multiple time. Fixes swashata/wp-webpack-script#13
1 parent 1df013e commit 168f281

8 files changed

+92
-46
lines changed

.vscode/settings.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
{
2-
"phpcs.enable": true
2+
"phpcs.enable": true,
3+
"spellright.language": [
4+
"en"
5+
],
6+
"spellright.documentTypes": [
7+
"markdown",
8+
"latex",
9+
"plaintext"
10+
]
311
}

inc/Enqueue.php

+6
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ public function getAssets( $dir, $entryPoint, $config ) {
194194
if ( $config['js'] && isset( $enqueue['js'] ) && count( (array) $enqueue['js'] ) ) {
195195
foreach ( $enqueue['js'] as $index => $js ) {
196196
$handle = $identifier . '_' . $index;
197+
// If the js is runtime, then use an unique handle
198+
if ( $js === $dir . '/runtime.js' ) {
199+
$handle = 'wpackio_' . $this->appName . $dir . '_runtime';
200+
// By making it unique, we rely on WordPress to only
201+
// enqueue it once.
202+
}
197203
$js_handles[] = [
198204
'handle' => $handle,
199205
'url' => $this->getUrl( $js ),

tests/data/dist/app/manifest.json

+13-9
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,32 @@
66
"main.js.map": "app/main.js.map",
77
"mobile.js": "app/mobile.js",
88
"mobile.js.map": "app/mobile.js.map",
9+
"runtime.js": "app/runtime.js",
10+
"runtime.js.map": "app/runtime.js.map",
911
"wpackioEp": {
1012
"main": {
11-
"css": [
12-
"app/main.css",
13-
"app/foo.css"
14-
],
1513
"js": [
16-
"app/main.js",
17-
"app/foo.js"
18-
],
19-
"css.map": [
20-
"app/main.css.map"
14+
"app/runtime.js",
15+
"app/main.js"
2116
],
2217
"js.map": [
18+
"app/runtime.js.map",
2319
"app/main.js.map"
20+
],
21+
"css": [
22+
"app/main.css"
23+
],
24+
"css.map": [
25+
"app/main.css.map"
2426
]
2527
},
2628
"mobile": {
2729
"js": [
30+
"app/runtime.js",
2831
"app/mobile.js"
2932
],
3033
"js.map": [
34+
"app/runtime.js.map",
3135
"app/mobile.js.map"
3236
]
3337
}

tests/testcases/EnqueueTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,45 @@ public function test_getAssets_for_plugin() {
172172
$this->assertMatchesSnapshot( $assets );
173173
}
174174

175+
public function test_getAssets_has_same_handle_for_every_runtime() {
176+
$runtime_handles = [];
177+
$enqueue = new \WPackio\Enqueue( 'foo', 'dist', '1.0.0', 'plugin', $this->pp );
178+
$ass_main = $enqueue->getAssets( 'app', 'main', [
179+
'js' => true,
180+
'css' => true,
181+
'js_dep' => [],
182+
'css_dep' => [],
183+
'identifier' => false,
184+
'in_footer' => true,
185+
'media' => 'all',
186+
] );
187+
$ass_mobile = $enqueue->getAssets( 'app', 'mobile', [
188+
'js' => true,
189+
'css' => true,
190+
'js_dep' => [],
191+
'css_dep' => [],
192+
'identifier' => false,
193+
'in_footer' => true,
194+
'media' => 'all',
195+
] );
196+
197+
foreach ( $ass_main['js'] as $js ) {
198+
if ( strpos( $js['url'], 'runtime.js' ) !== false ) {
199+
$runtime_handles[] = $js['handle'];
200+
}
201+
}
202+
foreach ( $ass_mobile['js'] as $js ) {
203+
if ( strpos( $js['url'], 'runtime.js' ) !== false ) {
204+
$runtime_handles[] = $js['handle'];
205+
}
206+
}
207+
$this->assertTrue(
208+
count( array_unique( $runtime_handles ) ) === 1
209+
&& end( $runtime_handles ) === 'wpackio_fooapp_runtime'
210+
);
211+
}
212+
213+
175214
public function test_enqueue() {
176215
// Get the manifest beforehand for assertion
177216
$path_to_manifest = dirname( $this->pp ) . '/dist/app/manifest.json';

tests/testcases/__snapshots__/EnqueueTest__test_enqueue__1.php

+3-8
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,18 @@
66
'handle' => 'wpackIoAppMain_0_css',
77
'url' => 'http://example.com/path/to/plugin/dist/app/main.css',
88
),
9-
1 =>
10-
array (
11-
'handle' => 'wpackIoAppMain_1_css',
12-
'url' => 'http://example.com/path/to/plugin/dist/app/foo.css',
13-
),
149
),
1510
'js' =>
1611
array (
1712
0 =>
1813
array (
19-
'handle' => 'wpackIoAppMain_0',
20-
'url' => 'http://example.com/path/to/plugin/dist/app/main.js',
14+
'handle' => 'wpackio_fooapp_runtime',
15+
'url' => 'http://example.com/path/to/plugin/dist/app/runtime.js',
2116
),
2217
1 =>
2318
array (
2419
'handle' => 'wpackIoAppMain_1',
25-
'url' => 'http://example.com/path/to/plugin/dist/app/foo.js',
20+
'url' => 'http://example.com/path/to/plugin/dist/app/main.js',
2621
),
2722
),
2823
);

tests/testcases/__snapshots__/EnqueueTest__test_getAssets_for_plugin__1.php

+3-8
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,18 @@
66
'handle' => 'wpackIoAppMain_0_css',
77
'url' => 'http://example.com/path/to/plugin/dist/app/main.css',
88
),
9-
1 =>
10-
array (
11-
'handle' => 'wpackIoAppMain_1_css',
12-
'url' => 'http://example.com/path/to/plugin/dist/app/foo.css',
13-
),
149
),
1510
'js' =>
1611
array (
1712
0 =>
1813
array (
19-
'handle' => 'wpackIoAppMain_0',
20-
'url' => 'http://example.com/path/to/plugin/dist/app/main.js',
14+
'handle' => 'wpackio_fooapp_runtime',
15+
'url' => 'http://example.com/path/to/plugin/dist/app/runtime.js',
2116
),
2217
1 =>
2318
array (
2419
'handle' => 'wpackIoAppMain_1',
25-
'url' => 'http://example.com/path/to/plugin/dist/app/foo.js',
20+
'url' => 'http://example.com/path/to/plugin/dist/app/main.js',
2621
),
2722
),
2823
);

tests/testcases/__snapshots__/EnqueueTest__test_getAssets_for_theme__1.php

+3-8
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,18 @@
66
'handle' => 'wpackIoAppMain_0_css',
77
'url' => 'http://example.com/path/to/template/directory/dist/app/main.css',
88
),
9-
1 =>
10-
array (
11-
'handle' => 'wpackIoAppMain_1_css',
12-
'url' => 'http://example.com/path/to/template/directory/dist/app/foo.css',
13-
),
149
),
1510
'js' =>
1611
array (
1712
0 =>
1813
array (
19-
'handle' => 'wpackIoAppMain_0',
20-
'url' => 'http://example.com/path/to/template/directory/dist/app/main.js',
14+
'handle' => 'wpackio_fooapp_runtime',
15+
'url' => 'http://example.com/path/to/template/directory/dist/app/runtime.js',
2116
),
2217
1 =>
2318
array (
2419
'handle' => 'wpackIoAppMain_1',
25-
'url' => 'http://example.com/path/to/template/directory/dist/app/foo.js',
20+
'url' => 'http://example.com/path/to/template/directory/dist/app/main.js',
2621
),
2722
),
2823
);

tests/testcases/__snapshots__/EnqueueTest__test_getManifest__1.php

+16-12
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,42 @@
66
'main.js.map' => 'app/main.js.map',
77
'mobile.js' => 'app/mobile.js',
88
'mobile.js.map' => 'app/mobile.js.map',
9+
'runtime.js' => 'app/runtime.js',
10+
'runtime.js.map' => 'app/runtime.js.map',
911
'wpackioEp' =>
1012
array (
1113
'main' =>
1214
array (
13-
'css' =>
15+
'js' =>
1416
array (
15-
0 => 'app/main.css',
16-
1 => 'app/foo.css',
17+
0 => 'app/runtime.js',
18+
1 => 'app/main.js',
1719
),
18-
'js' =>
20+
'js.map' =>
1921
array (
20-
0 => 'app/main.js',
21-
1 => 'app/foo.js',
22+
0 => 'app/runtime.js.map',
23+
1 => 'app/main.js.map',
2224
),
23-
'css.map' =>
25+
'css' =>
2426
array (
25-
0 => 'app/main.css.map',
27+
0 => 'app/main.css',
2628
),
27-
'js.map' =>
29+
'css.map' =>
2830
array (
29-
0 => 'app/main.js.map',
31+
0 => 'app/main.css.map',
3032
),
3133
),
3234
'mobile' =>
3335
array (
3436
'js' =>
3537
array (
36-
0 => 'app/mobile.js',
38+
0 => 'app/runtime.js',
39+
1 => 'app/mobile.js',
3740
),
3841
'js.map' =>
3942
array (
40-
0 => 'app/mobile.js.map',
43+
0 => 'app/runtime.js.map',
44+
1 => 'app/mobile.js.map',
4145
),
4246
),
4347
),

0 commit comments

Comments
 (0)