Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/swashata/wpackio-enqueue

WordPress & PHP API for @wpackio/scripts
https://github.com/swashata/wpackio-enqueue

bundler javascript wordpress wordpress-development

Last synced: 9 days ago
JSON representation

WordPress & PHP API for @wpackio/scripts

Awesome Lists containing this project

README

        

# [WPACK.IO](http://wpack.io) Enqueue API

[![Build Status](https://travis-ci.com/swashata/wpackio-enqueue.svg?branch=master)](https://travis-ci.com/swashata/wpackio-enqueue) [![codecov](https://codecov.io/gh/swashata/wpackio-enqueue/branch/master/graph/badge.svg)](https://codecov.io/gh/swashata/wpackio-enqueue) [![Latest Stable Version](https://poser.pugx.org/wpackio/enqueue/v/stable)](https://packagist.org/packages/wpackio/enqueue)

This is the PHP companion of [`@wpackio/scripts`](https://github.com/swashata/wp-webpack-script).

It gives you all the APIs you will need to properly consume assets generated from
`@wpackio/scripts` from your WordPress plugins or themes.

## Detailed Documentation

This README only covers the very basics and a quick start guide, without explaining
the overall usage.

Please visit our [official documentation](https://wpack.io) site for detailed
instruction.

## Installation

### Using Composer

We recommend using [composer](https://getcomposer.org/) for using this [library](https://packagist.org/packages/wpackio/enqueue).

```bash
composer require wpackio/enqueue
```

Then in your plugin main file or `functions.php` file of your theme, load
composer auto-loader.

```php
enqueue = new \WPackio\Enqueue( 'wpackplugin', 'dist', '1.0.0', 'plugin', __FILE__ );
// Enqueue a few of our entry points
add_action( 'wp_enqueue_scripts', [ $this, 'plugin_enqueue' ] );
}

public function plugin_enqueue() {
$this->enqueue->enqueue( 'app', 'main', [] );
$this->enqueue->enqueue( 'app', 'mobile', [] );
$this->enqueue->enqueue( 'foo', 'main', [] );
}
}

// Init
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,
'runtime_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.

`runtime_js_handle` is added in 3.4 and can predictably set the handle of the
common runtime JavaScript. This is useful to localize/translate dependent script
handles in the same files entry. By calling `wp_set_script_translations` on the
runtime you can collectively enqueue translate json for all the dependencies on
the entries.

For information on usage and API, please visit official documentation site
[wpack.io](https://wpack.io).

## Avoid conflict in multiple WordPress Plugins

Always require the latest version of `Wpackio\Enqueue`. The autoloader is set
to load only one instance and will not conflict with existing class.

However, if you want to load conflict free, kindly use [Strauss](https://github.com/BrianHenryIE/strauss).

## Actions and Filters

### Filter `wpackio_print_public_path`

Accepts 3 parameters:

- `$publichPathUrl` The URL that is used for the publicPath
- `$appName` Application Name
- `$outputPath` Output path relative to the root of this plugin/theme.

Using this you can dynamically change the public path that is used for code splitting.
This can be used to change the public path to a CDN.

#### Example Code to replace all wpack.io public path with a cdn url

```php
add_filter( 'wpackio_print_public_path', 'set_public_path_to_cdn' );

function set_public_path_to_cdn( $publichPathUrl ) {
$home_url = get_home_url(); // WordPress home url
$cdn_url = 'https://cdn.example.com'; // CDN url

// replace wordpress home url with cdn url
return str_replace($home_url, $cdn_url, $publichPathUrl);
}
```
#### Example Code to the change the public path url only for a specific instance of wpack.io

```php
add_filter( 'wpackio_print_public_path', 'set_public_path_to_cdn', 10, 2 );

function set_public_path_to_cdn( $publichPathUrl, $appName ) {

// check for our plugin
if( 'myPlugin' !== $appName ) return $publichPathUrl;

$home_url = get_home_url(); // WordPress home url
$cdn_url = 'https://cdn.example.com'; // CDN url

// replace WordPress home url with cdn url
return str_replace($home_url, $cdn_url, $publichPathUrl);
}
```