{"id":15292284,"url":"https://github.com/swashata/wpackio-enqueue","last_synced_at":"2025-04-13T09:32:44.004Z","repository":{"id":54151770,"uuid":"152847322","full_name":"swashata/wpackio-enqueue","owner":"swashata","description":"WordPress \u0026 PHP API for @wpackio/scripts","archived":false,"fork":false,"pushed_at":"2023-12-05T01:17:39.000Z","size":128,"stargazers_count":12,"open_issues_count":4,"forks_count":7,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-27T01:35:50.768Z","etag":null,"topics":["bundler","javascript","wordpress","wordpress-development"],"latest_commit_sha":null,"homepage":"https://wpack.io","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/swashata.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-13T07:33:51.000Z","updated_at":"2024-05-06T16:42:38.000Z","dependencies_parsed_at":"2024-06-18T19:51:19.228Z","dependency_job_id":"74501e50-847b-48fd-a944-71d4e19c33ee","html_url":"https://github.com/swashata/wpackio-enqueue","commit_stats":{"total_commits":63,"total_committers":8,"mean_commits":7.875,"dds":"0.23809523809523814","last_synced_commit":"944dd17df10e2e534e62ca3f5f20e550c2058a63"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swashata%2Fwpackio-enqueue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swashata%2Fwpackio-enqueue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swashata%2Fwpackio-enqueue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swashata%2Fwpackio-enqueue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/swashata","download_url":"https://codeload.github.com/swashata/wpackio-enqueue/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248537136,"owners_count":21120709,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bundler","javascript","wordpress","wordpress-development"],"created_at":"2024-09-30T16:17:15.830Z","updated_at":"2025-04-13T09:32:43.724Z","avatar_url":"https://github.com/swashata.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# [WPACK.IO](http://wpack.io) Enqueue API\n\n[![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)\n\nThis is the PHP companion of [`@wpackio/scripts`](https://github.com/swashata/wp-webpack-script).\n\nIt gives you all the APIs you will need to properly consume assets generated from\n`@wpackio/scripts` from your WordPress plugins or themes.\n\n## Detailed Documentation\n\nThis README only covers the very basics and a quick start guide, without explaining\nthe overall usage.\n\nPlease visit our [official documentation](https://wpack.io) site for detailed\ninstruction.\n\n## Installation\n\n### Using Composer\n\nWe recommend using [composer](https://getcomposer.org/) for using this [library](https://packagist.org/packages/wpackio/enqueue).\n\n```bash\ncomposer require wpackio/enqueue\n```\n\nThen in your plugin main file or `functions.php` file of your theme, load\ncomposer auto-loader.\n\n```php\n\u003c?php\n\n// Require the composer autoload for getting conflict-free access to enqueue\nrequire_once __DIR__ . '/vendor/autoload.php';\n\n// Instantiate\n$enqueue = new \\WPackio\\Enqueue( 'appName', 'outputPath', '1.0.0', 'plugin', __FILE__ );\n```\n\n### Manual\n\nIf you do not wish to use composer, then download the file [`Enqueue.php`](inc/Enqueue.php).\n\nRemove the namespace line `namespace WPackio;` and rename the classname from\n`Enqueue` to something less generic, like `MyPluginEnqueue`. This ensures\nconflict-free loading.\n\nThen require the file in your plugin entry-point or `functions.php` file of your theme.\n\n```php\n\u003c?php\n\n// Require the file yourself\nrequire_once __DIR__ . '/inc/MyPluginEnqueue.php';\n\n// Instantiate\n$enqueue = new MyPluginEnqueue( 'appName', 'outputPath', '1.0.0', 'plugin', __FILE__ );\n```\n\n## Getting Started\n\nWhich ever way, you choose to install, you have to make sure to instantiate the\nclass early during the entry-point of your plugin or theme.\n\nThis ensures that we hava necessary javascript in our website frontend and admin end\nto make webpack code-splitting and dynamic import work.\n\nA common pattern may look like this.\n\n```php\n\u003c?php\n// Assuming this is the main plugin file.\n\n// Require the composer autoload for getting conflict-free access to enqueue\nrequire_once __DIR__ . '/vendor/autoload.php';\n\n// Do stuff through this plugin\nclass MyPluginInit {\n\t/**\n\t * @var \\WPackio\\Enqueue\n\t */\n\tpublic $enqueue;\n\n\tpublic function __construct() {\n\t\t// It is important that we init the Enqueue class right at the plugin/theme load time\n\t\t$this-\u003eenqueue = new \\WPackio\\Enqueue( 'wpackplugin', 'dist', '1.0.0', 'plugin', __FILE__ );\n\t\t// Enqueue a few of our entry points\n\t\tadd_action( 'wp_enqueue_scripts', [ $this, 'plugin_enqueue' ] );\n\t}\n\n\n\tpublic function plugin_enqueue() {\n\t\t$this-\u003eenqueue-\u003eenqueue( 'app', 'main', [] );\n\t\t$this-\u003eenqueue-\u003eenqueue( 'app', 'mobile', [] );\n\t\t$this-\u003eenqueue-\u003eenqueue( 'foo', 'main', [] );\n\t}\n}\n\n\n// Init\nnew MyPluginInit();\n```\n\n## Default configuration when calling `enqueue`\n\n```php\n[\n\t'js' =\u003e true,\n\t'css' =\u003e true,\n\t'js_dep' =\u003e [],\n\t'css_dep' =\u003e [],\n\t'in_footer' =\u003e true,\n\t'media' =\u003e 'all',\n\t'main_js_handle' =\u003e null,\n\t'runtime_js_handle' =\u003e null,\n];\n```\n\n`main_js_handle` is added in 3.3 and can predictably set the handle of primary\nJavaScript file. Useful for translations etc.\n\n`runtime_js_handle` is added in 3.4 and can predictably set the handle of the\ncommon runtime JavaScript. This is useful to localize/translate dependent script\nhandles in the same files entry. By calling `wp_set_script_translations` on the\nruntime you can collectively enqueue translate json for all the dependencies on\nthe entries.\n\nFor information on usage and API, please visit official documentation site\n[wpack.io](https://wpack.io).\n\n## Avoid conflict in multiple WordPress Plugins\n\nAlways require the latest version of `Wpackio\\Enqueue`. The autoloader is set\nto load only one instance and will not conflict with existing class.\n\nHowever, if you want to load conflict free, kindly use [Strauss](https://github.com/BrianHenryIE/strauss).\n\n## Actions and Filters\n\n### Filter `wpackio_print_public_path`\n\nAccepts 3 parameters:\n\n- `$publichPathUrl` The URL that is used for the publicPath\n- `$appName` Application Name\n- `$outputPath` Output path relative to the root of this plugin/theme.\n\n\nUsing this you can dynamically change the public path that is used for code splitting.\nThis can be used to change the public path to a CDN.\n\n#### Example Code to replace all wpack.io public path with a cdn url\n\n```php\nadd_filter( 'wpackio_print_public_path', 'set_public_path_to_cdn' );\n\nfunction set_public_path_to_cdn( $publichPathUrl ) {\n\t$home_url = get_home_url(); // WordPress home url\n\t$cdn_url = 'https://cdn.example.com'; // CDN url\n\n\t// replace wordpress home url with cdn url\n\treturn str_replace($home_url, $cdn_url, $publichPathUrl);\n}\n```\n#### Example Code to the change the public path url only for a specific instance of wpack.io\n\n```php\nadd_filter( 'wpackio_print_public_path', 'set_public_path_to_cdn', 10, 2 );\n\nfunction set_public_path_to_cdn( $publichPathUrl, $appName ) {\n\t\n\t// check for our plugin\n\tif( 'myPlugin' !== $appName ) return $publichPathUrl;\n\n\t$home_url = get_home_url(); // WordPress home url\n\t$cdn_url = 'https://cdn.example.com'; // CDN url\n\n\t// replace WordPress home url with cdn url\n\treturn str_replace($home_url, $cdn_url, $publichPathUrl);\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswashata%2Fwpackio-enqueue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fswashata%2Fwpackio-enqueue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswashata%2Fwpackio-enqueue/lists"}