Skip to content

Commit aaebfaf

Browse files
authored
Merge pull request #71 from rkostrzewski/feature/webpack-ssr
feature: Prerender using webpack
2 parents 9c9edc1 + 5ffc9fc commit aaebfaf

25 files changed

+420
-292
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"devDependencies": {
7474
"babel-cli": "^6.24.0",
7575
"babel-eslint": "^7.2.1",
76+
"babel-plugin-transform-runtime": "^6.23.0",
7677
"chrome-launcher": "^0.1.1",
7778
"chrome-remote-interface": "^0.23.2",
7879
"eslint": "^3.19.0",
@@ -81,6 +82,8 @@
8182
"html-webpack-exclude-assets-plugin": "0.0.5",
8283
"lodash": "^4.17.4",
8384
"ncp": "^2.0.0",
85+
"node-sass": "^4.5.3",
86+
"sass-loader": "^6.0.6",
8487
"tap-diff": "^0.1.1",
8588
"tape": "^4.6.3",
8689
"uuid": "^3.0.1"
@@ -134,7 +137,7 @@
134137
"rimraf": "^2.6.1",
135138
"script-ext-html-webpack-plugin": "^1.8.0",
136139
"simplehttp2server": "^2.0.0",
137-
"sw-precache-webpack-plugin": "^0.11.0",
140+
"sw-precache-webpack-plugin": "^0.11.2",
138141
"tmp": "0.0.31",
139142
"unfetch": "^3.0.0",
140143
"url-loader": "^0.5.8",

src/commands/build.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import { resolve } from 'path';
22
import promisify from 'es6-promisify';
33
import rimraf from 'rimraf';
44
import asyncCommand from '../lib/async-command';
5-
import webpackConfig from '../lib/webpack-config';
6-
import transformConfig from '../lib/transform-config';
7-
import runWebpack, { showStats, writeJsonStats } from '../lib/run-webpack';
5+
import runWebpack, { showStats, writeJsonStats } from '../lib/webpack/run-webpack';
86

97
export default asyncCommand({
108
command: 'build [src] [dest]',
@@ -47,15 +45,12 @@ export default asyncCommand({
4745
},
4846

4947
async handler(argv) {
50-
let config = webpackConfig(argv);
51-
await transformConfig(argv, config);
52-
5348
if (argv.clean) {
5449
let dest = resolve(argv.cwd || process.cwd(), argv.dest || 'build');
5550
await promisify(rimraf)(dest);
5651
}
5752

58-
let stats = await runWebpack(false, config);
53+
let stats = await runWebpack(false, argv);
5954
showStats(stats);
6055

6156
if (argv.json) {

src/commands/watch.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import asyncCommand from '../lib/async-command';
2-
import webpackConfig from '../lib/webpack-config';
3-
import transformConfig from '../lib/transform-config';
42
import getSslCert from '../lib/ssl-cert';
5-
import runWebpack, { showStats } from '../lib/run-webpack';
3+
import runWebpack, { showStats } from '../lib/webpack/run-webpack';
64

75
export default asyncCommand({
86
command: 'watch [src]',
@@ -54,10 +52,7 @@ export default asyncCommand({
5452
argv.https = ssl;
5553
}
5654

57-
let config = webpackConfig(argv);
58-
await transformConfig(argv, config);
59-
60-
let stats = await runWebpack(true, config, showStats);
55+
let stats = await runWebpack(true, argv, showStats);
6156
showStats(stats);
6257
}
6358
});

src/lib/prerender.js

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports.pitch = function(remainingRequest) {
1616
}
1717

1818
return `
19-
import async from ${JSON.stringify(path.resolve(__dirname, '../components/async'))};
19+
import async from ${JSON.stringify(path.resolve(__dirname, '../../components/async'))};
2020
2121
function load(cb) {
2222
require.ensure([], function(require) {

src/lib/webpack/dummy-loader.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function(source, map) {
2+
this.callback(null, source, map);
3+
};

src/lib/webpack/prerender.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { resolve } from 'path';
2+
3+
export default function prerender(outputDir, params) {
4+
params = params || {};
5+
6+
let entry = resolve(outputDir, './ssr-build/ssr-bundle.js'),
7+
url = params.url || '/';
8+
9+
global.location = { href:url, pathname:url };
10+
global.history = {};
11+
12+
let m = require(entry),
13+
app = m && m.default || m;
14+
15+
if (typeof app!=='function') {
16+
// eslint-disable-next-line no-console
17+
console.warn('Entry does not export a Component function/class, aborting prerendering.');
18+
return '';
19+
}
20+
21+
let preact = require('preact'),
22+
renderToString = require('preact-render-to-string');
23+
24+
let html = renderToString(preact.h(app, { url }));
25+
26+
return html;
27+
}

0 commit comments

Comments
 (0)