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

Update examples to recover from HMR error #763

Merged
merged 1 commit into from
Sep 27, 2018
Merged
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
8 changes: 7 additions & 1 deletion examples/basic/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import express from 'express';
import app from './server';

let app = require('./server').default;

if (module.hot) {
module.hot.accept('./server', function() {
console.log('🔁 HMR Reloading `./server`...');
try {
app = require('./server').default;
} catch (error) {
console.error(error);
}
});
console.info('✅ Server-side HMR Enabled!');
}
Expand Down
41 changes: 21 additions & 20 deletions examples/with-afterjs/src/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import app from './server';
import http from 'http';
import express from 'express';

const server = http.createServer(app);

let currentApp = app;

server.listen(process.env.PORT || 3000, error => {
if (error) {
console.log(error);
}

console.log('🚀 started');
});
let app = require('./server').default;

if (module.hot) {
console.log('✅ Server-side HMR Enabled!');

module.hot.accept('./server', () => {
module.hot.accept('./server', function() {
console.log('🔁 HMR Reloading `./server`...');
server.removeListener('request', currentApp);
const newApp = require('./server').default;
server.on('request', newApp);
currentApp = newApp;
try {
app = require('./server').default;
} catch (error) {
console.error(error);
}
});
console.info('✅ Server-side HMR Enabled!');
}

const port = process.env.PORT || 3000;

export default express()
.use((req, res) => app.handle(req, res))
.listen(port, function(err) {
if (err) {
console.error(err);
return;
}
console.log(`> Started on port ${port}`);
});
8 changes: 7 additions & 1 deletion examples/with-custom-babel-config/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import express from 'express';
import app from './server';

let app = require('./server').default;

if (module.hot) {
module.hot.accept('./server', function() {
console.log('🔁 HMR Reloading `./server`...');
try {
app = require('./server').default;
} catch (error) {
console.error(error);
}
});
console.info('✅ Server-side HMR Enabled!');
}
Expand Down
8 changes: 7 additions & 1 deletion examples/with-custom-environment-variables/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import express from 'express';
import app from './server';

let app = require('./server').default;

if (module.hot) {
module.hot.accept('./server', function() {
console.log('🔁 HMR Reloading `./server`...');
try {
app = require('./server').default;
} catch (error) {
console.error(error);
}
});
console.info('✅ Server-side HMR Enabled!');
}
Expand Down
8 changes: 7 additions & 1 deletion examples/with-custom-webpack-config/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import express from 'express';
import app from './server';

let app = require('./server').default;

if (module.hot) {
module.hot.accept('./server', function() {
console.log('🔁 HMR Reloading `./server`...');
try {
app = require('./server').default;
} catch (error) {
console.error(error);
}
});
console.info('✅ Server-side HMR Enabled!');
}
Expand Down
35 changes: 21 additions & 14 deletions examples/with-elm/src/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import app from './server';
import http from 'http';
import express from 'express';

const server = http.createServer(app);

let currentApp = app;

server.listen(process.env.PORT || 3000);
let app = require('./server').default;

if (module.hot) {
console.log('✅ Server-side HMR Enabled!');

module.hot.accept('./server', () => {
module.hot.accept('./server', function() {
console.log('🔁 HMR Reloading `./server`...');
server.removeListener('request', currentApp);
const newApp = require('./server').default;
server.on('request', newApp);
currentApp = newApp;
try {
app = require('./server').default;
} catch (error) {
console.error(error);
}
});
console.info('✅ Server-side HMR Enabled!');
}

const port = process.env.PORT || 3000;

export default express()
.use((req, res) => app.handle(req, res))
.listen(port, function(err) {
if (err) {
console.error(err);
return;
}
console.log(`> Started on port ${port}`);
});
8 changes: 7 additions & 1 deletion examples/with-firebase-functions/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import express from 'express';
import app from './server';

let app = require('./server').default;

if (module.hot) {
module.hot.accept('./server', function() {
console.log('🔁 HMR Reloading `./server`...');
try {
app = require('./server').default;
} catch (error) {
console.error(error);
}
});
console.info('✅ Server-side HMR Enabled!');
}
Expand Down
8 changes: 7 additions & 1 deletion examples/with-heroku/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import express from 'express';
import app from './server';

let app = require('./server').default;

if (module.hot) {
module.hot.accept('./server', function() {
console.log('🔁 HMR Reloading `./server`...');
try {
app = require('./server').default;
} catch (error) {
console.error(error);
}
});
console.info('✅ Server-side HMR Enabled!');
}
Expand Down
8 changes: 7 additions & 1 deletion examples/with-hyperapp/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import express from 'express';
import app from './server';

let app = require('./server').default;

if (module.hot) {
module.hot.accept('./server', function() {
console.log('🔁 HMR Reloading `./server`...');
try {
app = require('./server').default;
} catch (error) {
console.error(error);
}
});
console.info('✅ Server-side HMR Enabled!');
}
Expand Down
8 changes: 7 additions & 1 deletion examples/with-inferno/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import express from 'express';
import app from './server';

let app = require('./server').default;

if (module.hot) {
module.hot.accept('./server', function() {
console.log('🔁 HMR Reloading `./server`...');
try {
app = require('./server').default;
} catch (error) {
console.error(error);
}
});
console.info('✅ Server-side HMR Enabled!');
}
Expand Down
8 changes: 7 additions & 1 deletion examples/with-jest-snapshots/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import express from 'express';
import app from './server';

let app = require('./server').default;

if (module.hot) {
module.hot.accept('./server', function() {
console.log('🔁 HMR Reloading `./server`...');
try {
app = require('./server').default;
} catch (error) {
console.error(error);
}
});
console.info('✅ Server-side HMR Enabled!');
}
Expand Down
8 changes: 7 additions & 1 deletion examples/with-jsxstyle/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import express from 'express';
import app from './server';

let app = require('./server').default;

if (module.hot) {
module.hot.accept('./server', function() {
console.log('🔁 HMR Reloading `./server`...');
try {
app = require('./server').default;
} catch (error) {
console.error(error);
}
});
console.info('✅ Server-side HMR Enabled!');
}
Expand Down
22 changes: 14 additions & 8 deletions examples/with-koa/src/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
import app from './server';
import http from 'http';

let app = require('./server').default;

// Use `app#callback()` method here instead of directly
// passing `app` as an argument to `createServer` (or use `app#listen()` instead)
// @see https://github.com/koajs/koa/blob/master/docs/api/index.md#appcallback
let currentHandler = app.callback();
const server = http.createServer(currentHandler);

server.listen(process.env.PORT || 3000, (error) => {
server.listen(process.env.PORT || 3000, error => {
if (error) {
console.log(error)
console.log(error);
}

console.log('🚀 started')
console.log('🚀 started');
});

if (module.hot) {
console.log('✅ Server-side HMR Enabled!');

module.hot.accept('./server', () => {
console.log('🔁 HMR Reloading `./server`...');
server.removeListener('request', currentHandler);
const newHandler = require('./server').default.callback();
server.on('request', newHandler);
currentHandler = newHandler;

try {
const newHandler = require('./server').default.callback();
server.removeListener('request', currentHandler);
server.on('request', newHandler);
currentHandler = newHandler;
} catch (error) {
console.error(error);
}
});
}
8 changes: 7 additions & 1 deletion examples/with-loadable-components/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import express from 'express';
import app from './server';

let app = require('./server').default;

if (module.hot) {
module.hot.accept('./server', function() {
console.log('🔁 HMR Reloading `./server`...');
try {
app = require('./server').default;
} catch (error) {
console.error(error);
}
});
console.info('✅ Server-side HMR Enabled!');
}
Expand Down
8 changes: 7 additions & 1 deletion examples/with-material-ui/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import express from 'express';
import app from './server';

let app = require('./server').default;

if (module.hot) {
module.hot.accept('./server', function() {
console.log('🔁 HMR Reloading `./server`...');
try {
app = require('./server').default;
} catch (error) {
console.error(error);
}
});
console.info('✅ Server-side HMR Enabled!');
}
Expand Down
8 changes: 7 additions & 1 deletion examples/with-mdx/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import express from 'express';
import app from './server';

let app = require('./server').default;

if (module.hot) {
module.hot.accept('./server', function() {
console.log('🔁 HMR Reloading `./server`...');
try {
app = require('./server').default;
} catch (error) {
console.error(error);
}
});
console.info('✅ Server-side HMR Enabled!');
}
Expand Down
8 changes: 7 additions & 1 deletion examples/with-now/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import express from 'express';
import app from './server';

let app = require('./server').default;

if (module.hot) {
module.hot.accept('./server', function() {
console.log('🔁 HMR Reloading `./server`...');
try {
app = require('./server').default;
} catch (error) {
console.error(error);
}
});
console.info('✅ Server-side HMR Enabled!');
}
Expand Down
16 changes: 11 additions & 5 deletions examples/with-polka/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import app from './server';
import http from 'http';

let app = require('./server').default;

const server = http.createServer(app);

let currentApp = app;
Expand All @@ -18,9 +19,14 @@ if (module.hot) {

module.hot.accept('./server', () => {
console.log('🔁 HMR Reloading `./server`...');
server.removeListener('request', currentApp);
const newApp = require('./server').default;
server.on('request', newApp);
currentApp = newApp;

try {
app = require('./server').default;
server.removeListener('request', currentApp);
server.on('request', app);
currentApp = app;
} catch (error) {
console.error(error);
}
});
}
Loading