npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

uniter-loader

v1.1.0

Published

Uniter-Loader - Webpack loader for requiring PHP files from JavaScript with Uniter

Downloads

18

Readme

uniter-loader

Build Status

Webpack loader for requiring PHP files from JavaScript using Uniter via PHPify.

Usage

npm install --save-dev webpack uniter-loader

Simple usage (requiring a single PHP module)

Add to webpack.config.js:

module.exports = {
    context: __dirname,
    entry: './js/src/index',
    module: {
        rules: [
            {
                test: /\.php$/,
                use: 'uniter-loader'
            }
        ]
    },
    output: {
        path: __dirname + '/dist/',
        filename: 'browser.js'
    }
};

Define an empty uniter.config.js:

module.exports = {};

Create a PHP module php/src/MyApp/doubleIt.php:

<?php

namespace MyApp;

$doubleIt = function ($num) {
    return $num * 2;
};

return $doubleIt; 

Call from JS module js/src/index.js:

var doubleItModule = require('./php/src/MyApp/doubleIt.php')();

doubleItModule.execute().then(function (doubleIt) {
    console.log('Double 4 is ' + doubleIt(4));
});

Run Webpack:

mkdir dist
node_modules/.bin/webpack --devtool=source-map --mode=development --progress

Load the bundle on a webpage, demo.html:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">

        <title>Uniter-Loader demo</title>
    </head>
    <body>
        <h1>Uniter-Loader demo</h1>

        <script src="dist/browser.js"></script>
    </body>
</html>

and open demo.html in a browser.

Complex usage (compiling a Composer app with the Symfony EventDispatcher component)

To avoid lots of typing, you can check out the source for this section here: https://github.com/uniter/event-dispatcher-demo

Install the Symfony EventDispatcher component

composer require symfony/event-dispatcher

Add to webpack.config.js:

module.exports = {
    context: __dirname,
    entry: './js/src/index',
    module: {
        rules: [
            {
                test: /\.php$/,
                use: 'uniter-loader'
            }
        ]
    },
    output: {
        path: __dirname + '/dist/',
        filename: 'browser.js'
    }
};

Add to uniter.config.js:

Note that unlike the example above, we have specified which PHP files to additionally transpile along with those that were require()'d from JS-land, and include in the compiled bundle for the browser.

module.exports = {
    phpify: {
        include: [
            "php/**/*.php",
            "vendor/autoload.php",
            "vendor/composer/**/*.php",
            "vendor/symfony/event-dispatcher/**/*.php"
        ]
    }
};

Create a PHP module php/src/MyApp/dispatchIt.php:

<?php

namespace MyApp;

use Symfony\Component\EventDispatcher\EventDispatcher;

// Load Composer's autoloader
require_once __DIR__ . '/../../../vendor/autoload.php';

$eventDispatcher = new EventDispatcher();
$eventDispatcher->addListener('my.event', function () {
    print 'Listener called!';
});

$eventDispatcher->dispatch('my.event');
print 'and...';
$eventDispatcher->dispatch('my.event');

Call from JS module js/src/index.js:

var dispatchItModule = require('./php/src/MyApp/dispatchIt.php')();

// Hook stdout and stderr up to the DOM
dispatchItModule.getStdout().on('data', function (data) {
    document.body.insertAdjacentHTML('beforeEnd', data + '<br>');
});
dispatchItModule.getStderr().on('data', function (data) {
    document.body.insertAdjacentHTML('beforeEnd', data + '<br>');
});

dispatchItModule.execute();

Run Webpack:

mkdir dist
node_modules/.bin/webpack --devtool=source-map --mode=development --progress

Load the bundle on a webpage, demo.html:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">

        <title>Uniter-Loader demo</title>
    </head>
    <body>
        <h1>Uniter-Loader demo</h1>

        <script src="dist/browser.js"></script>
    </body>
</html>

and open demo.html in a browser.

You should then see the output on the page from running the PHP code browser-side:

Listener called!
and...
Listener called!

Keeping up to date