├── .gitignore ├── README.md ├── index.js ├── package-lock.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | vendors 2 | node_modules 3 | /.project 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # php loader for webpack 2 | 3 | This package will load any php content and use php to turn it into a html page. 4 | 5 | Ex: 6 | 7 | ## Installation 8 | 9 | `npm install webpack-php-loader` 10 | 11 | ## Usage 12 | 13 | ``` javascript 14 | var fileContent = require("php!./file.php"); 15 | // => run file.php with php and return it as some content (html for example) 16 | ``` 17 | 18 | It can also be used inside the webpack configuration file (webpack.js): 19 | 20 | 21 | ``` javascript 22 | module.exports = { 23 | ... 24 | module: { 25 | loaders: [ 26 | ... 27 | { 28 | test: /\.php$/, 29 | loaders: [ 30 | 'html-minify', 31 | 'php-loader' 32 | ] 33 | }, 34 | ... 35 | ] 36 | }, 37 | ... 38 | } 39 | ``` 40 | 41 | [Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html) 42 | 43 | ## Configuration 44 | 45 | Here is a list of the available configuration options: 46 | 47 | - proxy: set the name of a proxy script to be used as a loader for the resource 48 | - args: list of string to be added as arguments to the proxy (or php) script 49 | - debug: add depandancies as html comment in the output (this will modify the output of the php script, and can lead to invalid results) 50 | 51 | example usage: 52 | 53 | ```javascript 54 | loaders: [ 55 | 'php-loader?' + JSON.stringify({ 56 | proxy: 'router.php', 57 | args: [ '--arg1=no' ], 58 | debug: true 59 | }) 60 | ] 61 | 62 | ``` 63 | 64 | In this case, the command to be executed by php-loader to get the file 'resource.php' will be: 65 | ```bash 66 | php router.php --arg1=no resource.php 67 | 68 | ``` 69 | 70 | ## License 71 | 72 | MIT (http://www.opensource.org/licenses/mit-license.php) 73 | 74 | based on https://github.com/jehon/php-loader 75 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* 2 | MIT License http://www.opensource.org/licenses/mit-license.php 3 | Author Jo https://github.com/surikat/ 4 | inspired by https://www.npmjs.com/package/php-loader by Tobias Koppers @sokra 5 | */ 6 | 7 | const glob = require('glob'); 8 | const util = require('util'); 9 | const loaderUtils = require('loader-utils'); 10 | const exec = util.promisify(require('child_process').exec); 11 | const shellescape = require('shell-escape'); 12 | 13 | module.exports = function(content){ 14 | 15 | let self = this; 16 | 17 | // Hold the name of the file to be executed (a resource in webpack terminology) 18 | let resource = this.resource; 19 | 20 | // The directory where the original file was run. This is necessary 21 | // if the php script use the __DIR__ variable, wich refer to the original file. 22 | // So if the file is runned "inline", we need to change path into that path so that 23 | // __DIR__ point to the correct location. 24 | let cwd = this.context; 25 | 26 | let query = ( typeof this.query === 'string' ? loaderUtils.parseQuery(this.query || '?') : this.query ) || {}; 27 | 28 | let options = Object.assign({ 29 | proxyScript: null 30 | }, query); 31 | 32 | 33 | let args = [ ]; 34 | if (options.proxy) { 35 | this.addDependency(options.proxy); 36 | args.push(options.proxy); 37 | } 38 | 39 | this.addDependency(resource); 40 | args.push(resource); 41 | 42 | if (options.args) { 43 | args = args.concat(options.args); 44 | } 45 | 46 | let callback = this.async(); 47 | let debug = options.debug; 48 | let cmd = `php -r 'require $argv[1]; echo PHP_EOL, json_encode(get_included_files());' ${shellescape(args)}`; 49 | async function runPhp() { 50 | if(debug){ 51 | console.log(cmd); 52 | } 53 | try{ 54 | let {stdout, stderr} = await exec(cmd); 55 | if(debug){ 56 | console.log('stdout:', stdout); 57 | console.log('stderr:', stderr); 58 | } 59 | 60 | if(stderr){ 61 | self.emitError(stderr); 62 | callback(stderr); 63 | }else{ 64 | // Split out last line which contains list of included files and add them as Webpack dependencies 65 | const includedSeparator = stdout.lastIndexOf('\n'); 66 | const dependencies = stdout.slice(includedSeparator); 67 | stdout = stdout.slice(0, includedSeparator); 68 | 69 | JSON.parse(dependencies).forEach(function(dependency) { 70 | self.addDependency(dependency) 71 | }); 72 | 73 | callback(null, stdout); 74 | } 75 | }catch(e){ 76 | callback(e); 77 | } 78 | } 79 | runPhp(); 80 | 81 | }; 82 | 83 | module.exports.raw = true; 84 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "webpack-php-loader", 3 | "version": "0.5.0", 4 | "lockfileVersion": 1, 5 | "dependencies": { 6 | "shell-escape": { 7 | "version": "0.2.0", 8 | "resolved": "https://registry.npmjs.org/shell-escape/-/shell-escape-0.2.0.tgz", 9 | "integrity": "sha1-aP0CXrBJC09WegJ/C/IkgLX4QTM=" 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "webpack-php-loader", 3 | "version": "0.5.0", 4 | "author": "Jo ", 5 | "description": "php loader module for webpack", 6 | "keywords": [ 7 | "webpack", 8 | "php", 9 | "loader" 10 | ], 11 | "repository": { 12 | "type": "git", 13 | "url": "git@github.com:redcatjs/webpack-php-loader.git" 14 | }, 15 | "license": "MIT", 16 | "dependencies": { 17 | "glob": "^7.0.5", 18 | "loader-utils": "latest", 19 | "shell-escape": "^0.2.0" 20 | } 21 | } 22 | --------------------------------------------------------------------------------