Skip to content

Commit

Permalink
Base off of prerenderer 0.6.0, update documentation & package.json.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshTheDerf committed Jan 23, 2018
1 parent 8134995 commit 1efe73d
Show file tree
Hide file tree
Showing 43 changed files with 6,779 additions and 1,961 deletions.
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,3 @@ $RECYCLE.BIN/

# Windows shortcuts
*.lnk

# End of https://www.gitignore.io/api/node,macos,linux,windows,visualstudiocode

dist/
2 changes: 0 additions & 2 deletions .npmignore

This file was deleted.

263 changes: 185 additions & 78 deletions README.md

Large diffs are not rendered by default.

89 changes: 55 additions & 34 deletions es5-autogenerated/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
'use strict';

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp-promise');
var Prerenderer = require('prerenderer');
var Prerenderer = require('@prerenderer/prerenderer');
var PuppeteerRenderer = require('@prerenderer/renderer-puppeteer');

function PrerendererWebpackPlugin() {
function PrerenderSPAPlugin() {
var _this = this;

var rendererOptions = {}; // Primarily for backwards-compatibility.

// Normal args object.

for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}

// Normal args object.
if (args.length === 1) {
this._options = args[0] || {};

// Backwards-compatibility with prerender-spa-plugin
// Backwards-compatibility with v2
} else {
var staticDir = void 0,
routes = void 0;
Expand All @@ -29,34 +35,55 @@ function PrerendererWebpackPlugin() {
routes ? this._options.routes = routes : null;
}

if (!this.options) this.options = {};
if (!this._options) this._options = {};

// Backwards compatiblity with v2.
if (this._options.captureAfterDocumentEvent) {
console.warn('[Prerender-SPA-Plugin] captureAfterDocumentEvent has been renamed to renderAfterDocumentEvent and should be moved to the renderer options.');
rendererOptions.renderAfterDocumentEvent = this._options.captureAfterDocumentEvent;
}

if (this._options.captureAfterDocumentEvent) {
console.warn('[Prerender-SPA-Plugin] captureAfterElementExists has been renamed to renderAfterElementExists and should be moved to the renderer options.');
rendererOptions.renderAfterElementExists = this._options.captureAfterElementExists;
}

if (this._options.captureAfterTime) {
console.warn('[Prerender-SPA-Plugin] captureAfterTime has been renamed to renderAfterTime and should be moved to the renderer options.');
rendererOptions.renderAfterTime = this._options.captureAfterTime;
}

this._options.server = this._options.server || {};
this._options.renderer = this._options.renderer || new PuppeteerRenderer(_extends({
headless: true
}, rendererOptions));

if (this._options.postProcessHtml) {
console.warn('[Prerender-SPA-Plugin] postProcessHtml should be migrated to postProcess! Consult the documentation for more information.');
}
}

PrerendererWebpackPlugin.prototype.apply = function (compiler) {
PrerenderSPAPlugin.prototype.apply = function (compiler) {
var _this2 = this;

compiler.plugin('after-emit', function (compilation, done) {
// For backwards-compatibility with prerender-spa-plugin
if (!_this2.routes && _this2.paths) _this2.routes = _this2.paths;

var PrerendererInstance = new Prerenderer(_this2._options);

PrerendererInstance.initialize().then(function () {
return PrerendererInstance.renderRoutes(_this2._options.routes || []);
})
// Backwards-compatibility with v2 (postprocessHTML should be migrated to postProcess)
.then(function (renderedRoutes) {
return _this2._options.postProcessHtml ? renderedRoutes.map(function (renderedRoute) {
var processed = _this2._options.postProcessHtml(renderedRoute);
if (typeof processed === 'string') renderedRoute.html = processed;else renderedRoute = processed;

return renderedRoute;
}) : renderedRoutes;
}).then(function (renderedRoutes) {
var route = renderedRoutes.route,
html = renderedRoutes.html;


if (_this2._options.postProcessHtml) {
renderedRoutes.html = _this2._options.postProcessHtml({
html,
route
});
}

return renderedRoutes;
return _this2._options.postProcess ? renderedRoutes.map(function (renderedRoute) {
return _this2._options.postProcess(renderedRoute);
}) : renderedRoutes;
}).then(function (processedRoutes) {
var promises = Promise.all(processedRoutes.map(function (processedRoute) {
var outputDir = path.join(_this2._options.outputDir || _this2._options.staticDir, processedRoute.route);
Expand All @@ -65,19 +92,17 @@ PrerendererWebpackPlugin.prototype.apply = function (compiler) {
return mkdirp(outputDir).then(function () {
return new Promise(function (resolve, reject) {
fs.writeFile(outputFile, processedRoute.html.trim(), function (err) {
if (err) reject(`[PrerendererWebpackPlugin] Unable to write rendered route to file "${outputFile}" \n ${err}`);
if (err) reject(`[Prerender-SPA-Plugin] Unable to write rendered route to file "${outputFile}" \n ${err}.`);
});

resolve();
});
}).catch(function (err) {
if (typeof err === 'string') {
err = `[PrerendererWebpackPlugin] Unable to create directory ${outputDir} for route ${processedRoute.route}. \n ${err}`;
err = `[Prerender-SPA-Plugin] Unable to create directory ${outputDir} for route ${processedRoute.route}. \n ${err}`;
}

setTimeout(function () {
throw err;
});
throw err;
});
}));

Expand All @@ -87,16 +112,12 @@ PrerendererWebpackPlugin.prototype.apply = function (compiler) {
done();
}).catch(function (err) {
PrerendererInstance.destroy();
console.error('[PrerendererWebpackPlugin] Unable to prerender all routes!');
setTimeout(function () {
throw err;
});
console.error('[Prerender-SPA-Plugin] Unable to prerender all routes!');
throw err;
});
});
};

PrerendererWebpackPlugin.BrowserRenderer = Prerenderer.BrowserRenderer;
PrerendererWebpackPlugin.ChromeRenderer = Prerenderer.ChromeRenderer;
PrerendererWebpackPlugin.JSDOMRenderer = Prerenderer.JSDOMRenderer;
PrerenderSPAPlugin.PuppeteerRenderer = PuppeteerRenderer;

module.exports = PrerendererWebpackPlugin;
module.exports = PrerenderSPAPlugin;
78 changes: 50 additions & 28 deletions es6/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
const fs = require('fs')
const path = require('path')
const mkdirp = require('mkdirp-promise')
const Prerenderer = require('prerenderer')
const PuppeteerRenderer = require('./puppeteer-renderer')
const Prerenderer = require('@prerenderer/prerenderer')
const PuppeteerRenderer = require('@prerenderer/renderer-puppeteer')

function PrerenderSPAPlugin (...args) {
const rendererOptions = {} // Primarily for backwards-compatibility.

// Normal args object.
if (args.length === 1) {
this._options = args[0] || {}

// Backwards-compatibility with prerender-spa-plugin
// Backwards-compatibility with v2
} else {
console.warn("[prerender-spa-plugin] You appear to be using the v2 argument-based configuration options. It's recommended that you migrate to the clearer object-based configuration system.\nCheck the documentation for more information.")
let staticDir, routes

args.forEach(arg => {
Expand All @@ -23,36 +26,58 @@ function PrerenderSPAPlugin (...args) {
routes ? this._options.routes = routes : null
}

if (!this.options) this.options = {}
if (!this._options) this._options = {}

// Backwards compatiblity with v2.
if (this._options.captureAfterDocumentEvent) {
console.warn('[prerender-spa-plugin] captureAfterDocumentEvent has been renamed to renderAfterDocumentEvent and should be moved to the renderer options.')
rendererOptions.renderAfterDocumentEvent = this._options.captureAfterDocumentEvent
}

if (this._options.captureAfterDocumentEvent) {
console.warn('[prerender-spa-plugin] captureAfterElementExists has been renamed to renderAfterElementExists and should be moved to the renderer options.')
rendererOptions.renderAfterElementExists = this._options.captureAfterElementExists
}

if (this._options.captureAfterTime) {
console.warn('[prerender-spa-plugin] captureAfterTime has been renamed to renderAfterTime and should be moved to the renderer options.')
rendererOptions.renderAfterTime = this._options.captureAfterTime
}

this._options.server = this._options.server || {}
this._options.renderer = this._options.renderer || new PuppeteerRenderer({
headless: true
headless: true,
...rendererOptions
})

if (this._options.postProcessHtml) {
console.warn('[prerender-spa-plugin] postProcessHtml should be migrated to postProcess! Consult the documentation for more information.')
}
}

PrerenderSPAPlugin.prototype.apply = function (compiler) {
compiler.plugin('after-emit', (compilation, done) => {
// For backwards-compatibility with prerender-spa-plugin
if (!this.routes && this.paths) this.routes = this.paths

const PrerendererInstance = new Prerenderer(this._options)

PrerendererInstance.initialize()
.then(() => {
return PrerendererInstance.renderRoutes(this._options.routes || [])
})
.then(renderedRoutes => {
const {route, html} = renderedRoutes

if (this._options.postProcessHtml) {
renderedRoutes.html = this._options.postProcessHtml({
html,
route
})
}

return renderedRoutes
})
// Backwards-compatibility with v2 (postprocessHTML should be migrated to postProcess)
.then(renderedRoutes => this._options.postProcessHtml
? renderedRoutes.map(renderedRoute => {
const processed = this._options.postProcessHtml(renderedRoute)
if (typeof processed === 'string') renderedRoute.html = processed
else renderedRoute = processed

return renderedRoute
})
: renderedRoutes
)
.then(renderedRoutes => this._options.postProcess
? renderedRoutes.map(renderedRoute => this._options.postProcess(renderedRoute))
: renderedRoutes
)
.then(processedRoutes => {
const promises = Promise.all(processedRoutes.map(processedRoute => {
const outputDir = path.join(this._options.outputDir || this._options.staticDir, processedRoute.route)
Expand All @@ -62,18 +87,18 @@ PrerenderSPAPlugin.prototype.apply = function (compiler) {
.then(() => {
return new Promise((resolve, reject) => {
fs.writeFile(outputFile, processedRoute.html.trim(), err => {
if (err) reject(`[Prerender-SPA-Plugin] Unable to write rendered route to file "${outputFile}" \n ${err}`)
if (err) reject(`[prerender-spa-plugin] Unable to write rendered route to file "${outputFile}" \n ${err}.`)
})

resolve()
})
})
.catch(err => {
if (typeof err === 'string') {
err = `[Prerender-SPA-Plugin] Unable to create directory ${outputDir} for route ${processedRoute.route}. \n ${err}`
err = `[prerender-spa-plugin] Unable to create directory ${outputDir} for route ${processedRoute.route}. \n ${err}`
}

setTimeout(function () { throw err })
throw err
})
}))

Expand All @@ -85,15 +110,12 @@ PrerenderSPAPlugin.prototype.apply = function (compiler) {
})
.catch(err => {
PrerendererInstance.destroy()
console.error('[Prerender-SPA-Plugin] Unable to prerender all routes!')
setTimeout(function () { throw err })
console.error('[prerender-spa-plugin] Unable to prerender all routes!')
console.error(err)
})
})
}

PrerenderSPAPlugin.BrowserRenderer = Prerenderer.BrowserRenderer
PrerenderSPAPlugin.ChromeRenderer = Prerenderer.ChromeRenderer
PrerenderSPAPlugin.JSDOMRenderer = Prerenderer.JSDOMRenderer
PrerenderSPAPlugin.PuppeteerRenderer = PuppeteerRenderer

module.exports = PrerenderSPAPlugin
Loading

0 comments on commit 1efe73d

Please sign in to comment.