Skip to content

Commit

Permalink
add integration test for templateProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Aug 6, 2018
1 parent 1e6b771 commit bc2272f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 52 deletions.
17 changes: 14 additions & 3 deletions packages/electrode-react-webapp/lib/async-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const Renderer = require("./renderer");
const { resolvePath } = require("./utils");
const Token = require("./token");
const stringArray = require("string-array");
const _ = require("lodash");
const Path = require("path");

const { TEMPLATE_DIR } = require("./symbols");
Expand All @@ -28,14 +29,24 @@ const tokenOpenTagRegex = new RegExp(
class AsyncTemplate {
constructor(options) {
this._options = options;
this._tokenHandlers = [];
this._tokenHandlers = [].concat(this._options.tokenHandlers).filter(x => x);
this._handlersMap = {};
// the same context that gets passed to each token handler's setup function
this._handlerContext = _.merge(
{
user: {
// set routeOptions in user also for consistency
routeOptions: options.routeOptions
}
},
options
);
this._initializeTemplate(options.htmlFile);
this._initializeTokenHandlers([].concat(this._options.tokenHandlers).filter(x => x));
}

initializeRenderer(reset) {
if (reset || !this._renderer) {
this._initializeTokenHandlers(this._tokenHandlers);
this._applyTokenLoad();
this._renderer = new Renderer({
htmlTokens: this._tokens,
Expand Down Expand Up @@ -328,7 +339,7 @@ class AsyncTemplate {

_loadTokenHandler(path) {
const mod = loadHandler(path);
return mod(this._options, this);
return mod(this._handlerContext, this);
}

_applyTokenLoad() {
Expand Down
6 changes: 5 additions & 1 deletion packages/electrode-react-webapp/lib/react-webapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ const setupOptions = options => {
const setupPathOptions = (routeOptions, path) => {
const options = routeOptions.paths[path];
return _.defaults(
{ htmlFile: options.htmlFile, tokenHandler: options.tokenHandler },
{
htmlFile: options.htmlFile,
tokenHandler: options.tokenHandler,
tokenHandlers: options.tokenHandlers
},
routeOptions
);
};
Expand Down
6 changes: 3 additions & 3 deletions packages/electrode-react-webapp/lib/react/token-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const PREFETCH_MARKER = "PREFETCH_BUNDLES";
const META_TAGS_MARKER = "META_TAGS";
const CRITICAL_CSS_MARKER = "CRITICAL_CSS";

module.exports = function setup(handlerContext /* , asyncTemplate */) {
const routeOptions = handlerContext.routeOptions;
module.exports = function setup(handlerContext /*, asyncTemplate*/) {
const routeOptions = handlerContext.user.routeOptions;

const WEBPACK_DEV = routeOptions.webpackDev;
const RENDER_JS = routeOptions.renderJS;
Expand All @@ -49,7 +49,7 @@ module.exports = function setup(handlerContext /* , asyncTemplate */) {
criticalCSS
};

handlerContext.routeData = routeData;
handlerContext.user.routeData = routeData;

const bundleManifest = () => {
if (!assets.manifest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const Helmet = require("react-helmet").Helmet;
const emptyTitleRegex = /<title[^>]*><\/title>/;

module.exports = handlerContext => {
const routeOptions = handlerContext.routeOptions;
const iconStats = handlerContext.routeData.iconStats;
const routeOptions = handlerContext.user.routeOptions;
const iconStats = handlerContext.user.routeData.iconStats;

return {
HEAD_INITIALIZE: context => {
Expand All @@ -21,18 +21,11 @@ module.exports = handlerContext => {
return helmetTitleEmpty ? `<title>${routeOptions.pageTitle}</title>` : helmetTitleScript;
},

WEBAPP_HEADER_BUNDLES: (context, next) => {
context.handleTokenResult(
"WEBAPP_HEADER_BUNDLES",
context.getTokens("electrode-react-token-handlers").WEBAPP_HEADER_BUNDLES(context), // eslint-disable-line
() => {
const scriptsFromHelmet = ["link", "style", "script", "noscript"]
.map(tagName => context.user.helmet[tagName].toString())
.join("");
context.output.add(`<!--scripts from helmet-->${scriptsFromHelmet}`);
next();
}
);
REACT_HELMET_SCRIPTS: context => {
const scriptsFromHelmet = ["link", "style", "script", "noscript"]
.map(tagName => context.user.helmet[tagName].toString())
.join("");
return `<!--scripts from helmet-->${scriptsFromHelmet}`;
},

META_TAGS: context => {
Expand Down
Loading

0 comments on commit bc2272f

Please sign in to comment.