From be183c024a59f384dd4ae70412cfa217aceced90 Mon Sep 17 00:00:00 2001 From: tim neutkens Date: Sat, 4 Mar 2017 12:00:37 +0100 Subject: [PATCH 1/3] Use module-alias to alias preact server side --- examples/using-preact/next.config.js | 10 +++++++++ examples/using-preact/package.json | 5 +++-- examples/using-preact/server.js | 32 ++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 examples/using-preact/server.js diff --git a/examples/using-preact/next.config.js b/examples/using-preact/next.config.js index 9c6e10d567996..603aa345ffd3c 100644 --- a/examples/using-preact/next.config.js +++ b/examples/using-preact/next.config.js @@ -4,6 +4,16 @@ module.exports = { 'react': 'preact-compat/dist/preact-compat', 'react-dom': 'preact-compat/dist/preact-compat' } + + // Disable uglify. This has been fixed in https://github.com/developit/preact-compat/issues/155. + // Can be removed once there is a new preact-compat release. + config.plugins = config.plugins.filter((plugin) => { + if (plugin.constructor.name === 'UglifyJsPlugin') { + return false + } else { + return true + } + }) return config } } diff --git a/examples/using-preact/package.json b/examples/using-preact/package.json index 42201a4bc2872..34d52adff4eca 100644 --- a/examples/using-preact/package.json +++ b/examples/using-preact/package.json @@ -2,11 +2,12 @@ "name": "hello-world", "version": "1.0.0", "scripts": { - "dev": "next", + "dev": "node server.js", "build": "next build", - "start": "next start" + "start": "NODE_ENV=production node server.js" }, "dependencies": { + "module-alias": "^2.0.0", "next": "^2.0.0-beta", "preact": "^7.1.0", "preact-compat": "^3.9.4", diff --git a/examples/using-preact/server.js b/examples/using-preact/server.js new file mode 100644 index 0000000000000..4a227b834fcd3 --- /dev/null +++ b/examples/using-preact/server.js @@ -0,0 +1,32 @@ +const moduleAlias = require('module-alias') + +moduleAlias.addAlias('react', 'preact-compat') +moduleAlias.addAlias('react-dom', 'preact-compat') + +const { createServer } = require('http') +const { parse } = require('url') +const next = require('next') + +const dev = process.env.NODE_ENV !== 'production' +const app = next({ dev }) +const handle = app.getRequestHandler() + +app.prepare() +.then(() => { + createServer((req, res) => { + const parsedUrl = parse(req.url, true) + const { pathname, query } = parsedUrl + + if (pathname === '/a') { + app.render(req, res, '/b', query) + } else if (pathname === '/b') { + app.render(req, res, '/a', query) + } else { + handle(req, res, parsedUrl) + } + }) + .listen(3000, (err) => { + if (err) throw err + console.log('> Ready on http://localhost:3000') + }) +}) From ea4614b4d01e4fbab58f57145e80b56b18ee86a1 Mon Sep 17 00:00:00 2001 From: tim neutkens Date: Sat, 4 Mar 2017 12:56:18 +0100 Subject: [PATCH 2/3] Use module-alias to alias inferno server side --- examples/using-inferno/next.config.js | 8 +++++- examples/using-inferno/package.json | 6 +++-- examples/using-inferno/server.js | 37 +++++++++++++++++++++++++++ examples/using-preact/next.config.js | 9 ++++++- examples/using-preact/server.js | 10 +++++--- 5 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 examples/using-inferno/server.js diff --git a/examples/using-inferno/next.config.js b/examples/using-inferno/next.config.js index 7137bc28a210c..b5fe966555b6e 100644 --- a/examples/using-inferno/next.config.js +++ b/examples/using-inferno/next.config.js @@ -1,5 +1,11 @@ module.exports = { - webpack: function (config) { + webpack: function (config, { dev }) { + // For the development version, we'll use React. + // Because, it support react hot loading and so on. + if (dev) { + return config + } + config.resolve.alias = { 'react': 'inferno-compat', 'react-dom': 'inferno-compat' diff --git a/examples/using-inferno/package.json b/examples/using-inferno/package.json index 33cb11b9a6816..dcb96596500ec 100644 --- a/examples/using-inferno/package.json +++ b/examples/using-inferno/package.json @@ -2,13 +2,15 @@ "name": "using-inferno", "version": "1.0.0", "scripts": { - "dev": "next", + "dev": "node server.js", "build": "next build", - "start": "next start" + "start": "NODE_ENV=production node server.js" }, "dependencies": { "inferno": "^1.0.7", "inferno-compat": "^1.0.7", + "inferno-server": "^1.3.0-rc.9", + "module-alias": "^2.0.0", "next": "^2.0.0-beta", "react": "^15.4.2", "react-dom": "^15.4.2" diff --git a/examples/using-inferno/server.js b/examples/using-inferno/server.js new file mode 100644 index 0000000000000..7c2651e8f48d5 --- /dev/null +++ b/examples/using-inferno/server.js @@ -0,0 +1,37 @@ +const dev = process.env.NODE_ENV !== 'production' +const moduleAlias = require('module-alias') + +// For the development version, we'll use React. +// Because, it support react hot loading and so on. +if (!dev) { + moduleAlias.addAlias('react', 'inferno-compat') + moduleAlias.addAlias('react-dom/server', 'inferno-server') + moduleAlias.addAlias('react-dom', 'inferno-compat') +} + +const { createServer } = require('http') +const { parse } = require('url') +const next = require('next') + +const app = next({ dev }) +const handle = app.getRequestHandler() + +app.prepare() +.then(() => { + createServer((req, res) => { + const parsedUrl = parse(req.url, true) + const { pathname, query } = parsedUrl + + if (pathname === '/a') { + app.render(req, res, '/b', query) + } else if (pathname === '/b') { + app.render(req, res, '/a', query) + } else { + handle(req, res, parsedUrl) + } + }) + .listen(3000, (err) => { + if (err) throw err + console.log('> Ready on http://localhost:3000') + }) +}) diff --git a/examples/using-preact/next.config.js b/examples/using-preact/next.config.js index 603aa345ffd3c..02e8a3299f11a 100644 --- a/examples/using-preact/next.config.js +++ b/examples/using-preact/next.config.js @@ -1,5 +1,11 @@ module.exports = { - webpack: function (config) { + webpack: function (config, { dev }) { + // For the development version, we'll use React. + // Because, it support react hot loading and so on. + if (dev) { + return config + } + config.resolve.alias = { 'react': 'preact-compat/dist/preact-compat', 'react-dom': 'preact-compat/dist/preact-compat' @@ -14,6 +20,7 @@ module.exports = { return true } }) + return config } } diff --git a/examples/using-preact/server.js b/examples/using-preact/server.js index 4a227b834fcd3..16db82b75b576 100644 --- a/examples/using-preact/server.js +++ b/examples/using-preact/server.js @@ -1,13 +1,17 @@ +const dev = process.env.NODE_ENV !== 'production' const moduleAlias = require('module-alias') -moduleAlias.addAlias('react', 'preact-compat') -moduleAlias.addAlias('react-dom', 'preact-compat') +// For the development version, we'll use React. +// Because, it support react hot loading and so on. +if (dev) { + moduleAlias.addAlias('react', 'preact-compat') + moduleAlias.addAlias('react-dom', 'preact-compat') +} const { createServer } = require('http') const { parse } = require('url') const next = require('next') -const dev = process.env.NODE_ENV !== 'production' const app = next({ dev }) const handle = app.getRequestHandler() From d6c059f5a0df2112eb455a7e7707e892a9170b33 Mon Sep 17 00:00:00 2001 From: tim neutkens Date: Sat, 4 Mar 2017 13:05:30 +0100 Subject: [PATCH 3/3] Remove unneeded routes example --- examples/using-inferno/server.js | 10 +--------- examples/using-preact/server.js | 10 +--------- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/examples/using-inferno/server.js b/examples/using-inferno/server.js index 7c2651e8f48d5..e35e2286c1edf 100644 --- a/examples/using-inferno/server.js +++ b/examples/using-inferno/server.js @@ -20,15 +20,7 @@ app.prepare() .then(() => { createServer((req, res) => { const parsedUrl = parse(req.url, true) - const { pathname, query } = parsedUrl - - if (pathname === '/a') { - app.render(req, res, '/b', query) - } else if (pathname === '/b') { - app.render(req, res, '/a', query) - } else { - handle(req, res, parsedUrl) - } + handle(req, res, parsedUrl) }) .listen(3000, (err) => { if (err) throw err diff --git a/examples/using-preact/server.js b/examples/using-preact/server.js index 16db82b75b576..7ecf74fd0f93f 100644 --- a/examples/using-preact/server.js +++ b/examples/using-preact/server.js @@ -19,15 +19,7 @@ app.prepare() .then(() => { createServer((req, res) => { const parsedUrl = parse(req.url, true) - const { pathname, query } = parsedUrl - - if (pathname === '/a') { - app.render(req, res, '/b', query) - } else if (pathname === '/b') { - app.render(req, res, '/a', query) - } else { - handle(req, res, parsedUrl) - } + handle(req, res, parsedUrl) }) .listen(3000, (err) => { if (err) throw err