Skip to content

Commit

Permalink
feat: add new config main_js_handle
Browse files Browse the repository at this point in the history
Using it one can predictably set the handle of primary
JavaScript file. Useful for translations.
  • Loading branch information
swashata committed Jul 30, 2021
1 parent 1d405e7 commit 7ab363c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,22 @@ class MyPluginInit {
new MyPluginInit();
```

## Default configuration when calling `enqueue`

```php
[
'js' => true,
'css' => true,
'js_dep' => [],
'css_dep' => [],
'in_footer' => true,
'media' => 'all',
'main_js_handle' => null,
];
```

`main_js_handle` is added in 3.3 and can predictably set the handle of primary
JavaScript file. Useful for translations etc.

For information on usage and API, please visit official documentation site
[wpack.io](https://wpack.io).
12 changes: 11 additions & 1 deletion inc/Enqueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,18 @@ public function getAssets( $name, $entryPoint, $config ) {
$css_handles = [];

// Figure out all javascript assets
if ( $config['js'] && isset( $enqueue['js'] ) && count( (array) $enqueue['js'] ) ) {
$total_js_assets = count( (array) $enqueue['js'] );
if ( $config['js'] && isset( $enqueue['js'] ) && $total_js_assets ) {
foreach ( $enqueue['js'] as $index => $js ) {
$handle = $this->getHandle( $name, $js, 'script' );
// override the last one's handle if needed
if (
$index === $total_js_assets - 1
&& isset( $config['main_js_handle'] )
&& ! empty( $config['main_js_handle'] )
) {
$handle = $config['main_js_handle'];
}
// If the js is runtime, then use an unique handle
// if ( $js === $dir . '/runtime.js' ) {
// $handle = 'wpackio_' . $this->appName . $dir . '_runtime';
Expand Down Expand Up @@ -368,6 +377,7 @@ public function normalizeAssetConfig( $config ) {
'css_dep' => [],
'in_footer' => true,
'media' => 'all',
'main_js_handle' => null,
]
);
}
Expand Down
14 changes: 13 additions & 1 deletion tests/testcases/EnqueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ public function test_getHandle_throws_exception() {
*
* @return (Enqueue|array)[] Touple of Enqueue and assets.
*/
protected function prepare_enqueue_assets() {
protected function prepare_enqueue_assets( $main_handle = null ) {
// Prepare
$enqueue = new \WPackio\Enqueue( 'foo', 'dist', '1.0.0', 'plugin', $this->pp );
\Brain\Monkey\Functions\expect( 'wp_register_script' )
Expand All @@ -496,6 +496,7 @@ protected function prepare_enqueue_assets() {
'css_dep' => [],
'in_footer' => true,
'media' => 'all',
'main_js_handle' => $main_handle,
] );
return [ $enqueue, $assets ];
}
Expand Down Expand Up @@ -581,4 +582,15 @@ public function test_getJsHandles_works_for_proper_assets() {
$this->assertCount( 0, $enqueue->getJsHandles( [ 'css' => [] ] ) );
$this->assertCount( 0, $enqueue->getJsHandles( [ 'css' => [ 'bla' ] ] ) );
}

/**
* @testdox can set 'main_js_handle'
*/
public function test_can_set_main_js_handle_manually() {
[ $enqueue, $assets ] = $this->prepare_enqueue_assets( 'wpackio-main-js' );
$this->assertEquals(
'wpackio-main-js',
$enqueue->getPrimaryJsHandle( $assets )
);
}
}

0 comments on commit 7ab363c

Please sign in to comment.