A loader for webpack that executes Hacklang/PHP scripts, returning their output as a String.
To begin, you'll need to install webpack-hhvm-php-loader
:
npm install webpack-hhvm-php-loader --save-dev
# Or
yarn add -D webpack-hhvm-php-loader
Then add the loader to your webpack
config. For example:
webpack.config.js
// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.php.?$/i,
use: 'webpack-hhvm-php-loader',
},
],
},
};
users.php
<?hh
# Example PHP/Hacklang structure to export:
$user = array(
'username' => 'justsml',
'first_name' => 'Dan',
'date_created' => '2020-12-01',
);
# Write JSON (cross-platform native object interface)
echo json_encode($user, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
file.js
import jsonResults from './users.php';
console.log('username:', jsonResults.username);
And run webpack
via your preferred method.
Name | Type | Default | Description |
---|---|---|---|
esModule |
{Boolean} |
true |
Uses ES modules syntax |
timeout |
{Number} |
5000 |
Number of milliseconds to wait before timing out |
parser |
{String} |
json |
Either json or string mode |
engine |
{String} |
hhvm |
The executable to run your script with. Defaults to hhvm |
Type: Boolean
Default: true
By default, webpack-hhvm-php-loader
generates JS modules that use the ES modules syntax.
There are some cases in which using ES modules is beneficial, like in the case of module concatenation and tree shaking.
You can enable CommonJS module syntax using:
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.php.?$/i,
use: [
{
loader: 'webpack-hhvm-php-loader',
options: {
esModule: false,
},
},
],
},
],
},
};
Type: Number
Default: 5000
The script execution will timeout after timeout
milliseconds elapses.
import jsonResults from './users.php';
console.log('username:', jsonResults.username);
import jsonResults from 'webpack-hhvm-php-loader!./users.php';
Beware, if you already define loader(s) for extension(s) in webpack.config.js
you should use:
import jsonResults from '!!webpack-hhvm-php-loader!./users.php'; // Adding `!!` to a request will disable all loaders specified in the configuration