Skip to content

Webpack plugin for requiring PHP files from JavaScript with Uniter

Notifications You must be signed in to change notification settings

uniter/webpack-uniter-plugin

Repository files navigation

webpack-uniter-plugin

Build Status

Webpack plugin for requiring PHP files from JavaScript using Uniter via uniter-loader.

Usage

npm install --save-dev webpack webpack-uniter-plugin

Simple usage (requiring a single PHP module)

Add to webpack.config.js:

const UniterPlugin = require('webpack-uniter-plugin');

module.exports = {
    context: __dirname,
    entry: './js/src/index',
    output: {
        path: __dirname + '/dist/',
        filename: 'browser.js'
    },
    plugins: [
        new UniterPlugin()
    ]
};

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>Webpack-Uniter-Plugin demo</title>
    </head>
    <body>
        <h1>Webpack-Uniter-Plugin demo</h1>

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

and open demo.html in a browser.

About

Webpack plugin for requiring PHP files from JavaScript with Uniter

Resources

Stars

Watchers

Forks

Packages

No packages published