webpack-php-proxy-server
v1.0.1
Published
Extends Webpack Dev Server to use PHP as a middleware, proxying /api requests to a PHP server
Downloads
91
Maintainers
Readme
Webpack DevServer with PHP
Overview
This project sets up a development server using Webpack and PHP. It ensures that the PHP server runs concurrently with the Webpack development server, allowing you to develop and test PHP applications seamlessly.
Table of Contents
Installation
Clone the repository:
git clone [email protected]:amenophis1er/webpack-php-proxy-server.git cd webpack-php-proxy-server
Install dependencies:
npm install
Usage
To set up the development server, you need to modify your Webpack configuration to include the setupDevServer
function.
Webpack Configuration
Create or modify your Webpack configuration file:
// webpack.config.js const { setupDevServer } = require('webpack-php-proxy-server'); module.exports = async function (env, argv) { const phpServerDir = 'examples'; // Change to your PHP server directory const apiPrefix = 'api'; // Change to your desired API prefix const config = { // Your existing Webpack configuration }; await setupDevServer(config, { port: 8080, // Optional, specify a port for Webpack dev server phpServerDir, apiPrefix, }); return config; };
Run the development server:
npm start
This will start both the Webpack development server and the PHP server.
Testing
This project includes tests for the server setup functions using Jest. To run the tests, follow these steps:
Install testing dependencies:
npm install --save-dev jest
Run the tests:
npm test
Example Tests
The following test cases are included:
Testing the
getAvailablePort
function:const { getAvailablePort } = require('webpack-php-proxy-server'); test('finds an available port', async () => { const port = await getAvailablePort(8000, 8005); expect(port).toBeGreaterThanOrEqual(8000); expect(port).toBeLessThanOrEqual(8005); });
Configuration
The main configuration options for setupDevServer
are:
port
: (Optional) Port number for the Webpack development server. If not provided, an available port will be used.phpServerDir
: Directory to serve PHP files from.apiPrefix
: (Optional) Prefix for API routes. Defaults toapi
.
License
This project is licensed under the MIT License. See the LICENSE file for details.