Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Virtual Host Support #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hapi-webpack-plugin",
"version": "2.0.0",
"version": "2.0.1",
"description": "Webpack middleware for Hapi. Supports HMR.",
"homepage": "https://github.com/SimonDegraeve/hapi-webpack-plugin",
"main": "./lib/index.js",
Expand Down
100 changes: 98 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

Please download the appropriate version for you.

* For **webpack 1.x** use version < 1.3.0 of this package.
* For **webpack 1.x** use version <= 1.3.0 of this package.
* For **webpack 2.x** use version >= 2.0.0 of this package.

## Installation
Expand Down Expand Up @@ -133,11 +133,107 @@ export default {
};
```

**Virtual Hosts Binding**

Hapi allows plugins to be bound to virtual hosts via the `routes.vhost`
option of [`server.register()`](https://hapijs.com/api#serverregisterplugins-options-callback).
As of version 2.0.1 this plugin supports binding a unqiue webpack configuration
to a specific virtual host. Further, multiple instances of the plugin can be
registered simultaneously allowing for multiple unique webpack configurations to be running
for different virtual hosts on a single server.

```js
/**
* file: index-vhost.js
*/

/**
* Import dependencies
*/
import {Server} from 'hapi';
import WebpackPlugin from 'hapi-webpack-plugin';


/**
* Create server
*/
const server = new Server();
server.connection({port: 3000});

/**
* Register muliple plugin instances, one for each webpack/vhost combination, and start server
*/
server.register([
{
register: WebpackPlugin,
options: './webpack.client.config.js',
routes:
{
vhosts: 'client.mydomain.com'
}
},
{
register: WebpackPlugin,
options: './webpack.admin.config.js',
routes:
{
vhosts: 'admin.mydomain.com'
}
}],
error => {
if (error) {
return console.error(error);
}
server.start(() => console.log('Server running at:', server.info.uri));
});
```
```js
/**
* file: webpack.client.config.js
*/

/**
* Export webpack configuration for client
*/
export default {
entry: 'client_app.js',

// webpack-dev-middleware options
// See https://github.com/webpack/webpack-dev-middleware
assets: {},

// webpack-hot-middleware options
// See https://github.com/glenjamin/webpack-hot-middleware
hot: {}
};

/**
* file: webpack.admin.config.js
*/

/**
* Export webpack configuration for admin
*/
export default {
entry: 'admin_app.js',

// webpack-dev-middleware options
// See https://github.com/webpack/webpack-dev-middleware
assets: {},

// webpack-hot-middleware options
// See https://github.com/glenjamin/webpack-hot-middleware
hot: {}
};
```



## Licence

The MIT License (MIT)

Copyright (c) 2015 Simon Degraeve
Copyright (c) 2017 Simon Degraeve

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
32 changes: 17 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,27 @@ function register(server, options, next) {
const webpackDevMiddleware = WebpackDevMiddleware(compiler, config.assets);
const webpackHotMiddleware = WebpackHotMiddleware(compiler, config.hot);

// Handle webpackDevMiddleware
server.ext('onRequest', (request, reply) => {
// Handle webpack middlware
var webpackMiddlewareHandler = (request, reply) => {
const {req, res} = request.raw;

// Handle webpackDevMiddleware
webpackDevMiddleware(req, res, error => {
if (error) {
return reply(error);
}
reply.continue();
// Handle webpackHotMiddleware
webpackHotMiddleware(req, res, error => {
if (error) {
return reply(error);
}
reply.continue();
});
});
});
};

// Handle webpackHotMiddleware
server.ext('onRequest', (request, reply) => {
const {req, res} = request.raw;
webpackHotMiddleware(req, res, error => {
if (error) {
return reply(error);
}
reply.continue();
});
});
// catch all route handler (which respects vhost passed into plugin)
server.route({ method: '*', path: '/{p*}', handler: webpackMiddlewareHandler });

// Expose compiler
server.expose({compiler});
Expand All @@ -66,7 +66,9 @@ function register(server, options, next) {
*/
register.attributes = {
name: 'webpack',
version
multiple: true,
version,

};


Expand Down