infotechnohelp/package-loader

PHP helper to load front-end packages

1.0.1 2019-07-28 10:20 UTC

This package is auto-updated.

Last update: 2024-10-29 00:36:54 UTC


README

`composer require infotechnohelp/package-loader'

Basic usage

Assume that you have webroot/node_modules with jquery installed

use PackageLoader\PackageLoader;

$npm = new PackageLoader(\Cake\Routing\Router::url('/') . 'node_modules/');

echo $npm->script('jquery/dist/jquery.min');

Basic usage with map

use PackageLoader\PackageLoader;

$npm = new PackageLoader(\Cake\Routing\Router::url('/') . 'node_modules/', CONFIG . 'npm-map');

echo $npm->map()->scripts();

Map structure

config/npm-map.json

{
  "js": {
    "jquery": "jquery/dist/jquery.min",
    "api-client": "@infotechnohelp/api-client/src/index"
  },
  "css": {
    ...
  }
}

Add all scripts

echo $npm->map()->scripts();

Add provided scripts only

echo $npm->map()->scripts(['jquery']);

Add all except provided scripts

echo $npm->map()->scripts(['jquery'], true);

Sub-packages

Map structure

{
  "js": {
    "jquery": "jquery/dist/jquery.min",
    "auth-api": {
      "login": "cakephp-auth-api-client/src/login",
      "logout" : "cakephp-auth-api-client/src/logout"
    }
  }
}

Add all scripts

echo $npm->map()->scripts();

Add provided scripts only

echo $npm->map()->scripts(['jquery', 'auth-api:login']);

Add all except provided scripts

echo $npm->map()->scripts(['auth-api:logout'], true);

(Results will be same, only jquery and login scripts loaded)

Load stylesheets

Map structure

{
  "css": {
    ...
  }
}

Add a certain stylesheet

echo $npm->stylesheet('path-inside-node_modules');

Add all stylesheets

echo $npm->map()->stylesheets();

Everything works same as with scripts (only & except conditions)