From 8680d9c95ec4e5d9bfc5f8467070d7b90d39eaab Mon Sep 17 00:00:00 2001 From: Clint Priest Date: Mon, 15 May 2017 18:54:06 -0500 Subject: [PATCH 1/4] I believe this (along with the original PR#252) fully corrects the original issue #227 as well as the regression #382. Also addresses the comments from PR #502, now closed. --- src/lib/output/renderer.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/lib/output/renderer.ts b/src/lib/output/renderer.ts index ea8521a15..323f0189a 100644 --- a/src/lib/output/renderer.ts +++ b/src/lib/output/renderer.ts @@ -201,7 +201,24 @@ export class Renderer extends ChildableComponent } } - this.theme = this.addComponent('theme', new DefaultTheme(this, path)); + let filename = Path.join(path, 'theme.js'); + if (!FS.existsSync(filename)) { + this.theme = this.addComponent('theme', new DefaultTheme(this, path)); + } else { + try { + const plugin = require(filename); + if(typeof plugin === 'function') { + plugin(this.application); + } else if (plugin.default) { + this.theme = this.addComponent('theme', new (plugin.default)(this, path)); + } else { + this.application.logger.error('Failed to load theme.js from theme directory, theme.js ' + + 'must either export a default class or a function accepting an application reference.'); + } + } catch (e) { + throw new Error(`Exception while evaluating ${filename}:\n${e}`); + } + } } this.theme.resources.activate(); From 3fb7d7cf3f6ca718925fd2f4f3f7ef105ce9a951 Mon Sep 17 00:00:00 2001 From: Clint Priest Date: Wed, 17 May 2017 08:04:32 -0500 Subject: [PATCH 2/4] Updates to use plugin method for theme.js (breaks backward compatibility). --- src/lib/output/renderer.ts | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/lib/output/renderer.ts b/src/lib/output/renderer.ts index 323f0189a..d13e270e3 100644 --- a/src/lib/output/renderer.ts +++ b/src/lib/output/renderer.ts @@ -201,22 +201,24 @@ export class Renderer extends ChildableComponent } } - let filename = Path.join(path, 'theme.js'); + const filename = Path.join(path, 'theme.js'); if (!FS.existsSync(filename)) { this.theme = this.addComponent('theme', new DefaultTheme(this, path)); } else { try { const plugin = require(filename); - if(typeof plugin === 'function') { - plugin(this.application); - } else if (plugin.default) { - this.theme = this.addComponent('theme', new (plugin.default)(this, path)); - } else { - this.application.logger.error('Failed to load theme.js from theme directory, theme.js ' + - 'must either export a default class or a function accepting an application reference.'); - } + const themeClass = plugin(this.application); + + this.theme = this.addComponent('theme', new (themeClass)(this, path)); } catch (e) { - throw new Error(`Exception while evaluating ${filename}:\n${e}`); + throw new Error( + `Exception while loading ${filename}.\n` + + '\n' + + 'Your theme.js must export a function accepting an application reference.\n' + + 'Your function should return your Theme based class.\n' + + '\n' + + e + ); } } } From b1caa708c91072c2dccf6867bcfdbbf21d6f5d61 Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Thu, 18 May 2017 16:00:49 -0400 Subject: [PATCH 3/4] Update the `require` and error thrown --- src/lib/output/renderer.ts | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/lib/output/renderer.ts b/src/lib/output/renderer.ts index d13e270e3..ae5ec73f0 100644 --- a/src/lib/output/renderer.ts +++ b/src/lib/output/renderer.ts @@ -206,18 +206,13 @@ export class Renderer extends ChildableComponent this.theme = this.addComponent('theme', new DefaultTheme(this, path)); } else { try { - const plugin = require(filename); - const themeClass = plugin(this.application); + const themeClass = typeof require(filename) === 'function' ? require(filename) : require(filename).default; this.theme = this.addComponent('theme', new (themeClass)(this, path)); - } catch (e) { + } catch (err) { throw new Error( - `Exception while loading ${filename}.\n` + - '\n' + - 'Your theme.js must export a function accepting an application reference.\n' + - 'Your function should return your Theme based class.\n' + - '\n' + - e + `Exception while loading ${filename}. You must export a \`new Theme(renderer, basePath)\` compatible class.\n' + + err ); } } From b285cbbaac2777e3ff5591e48d0c163920557c44 Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Thu, 18 May 2017 16:06:52 -0400 Subject: [PATCH 4/4] Shit happens when editing in GitHub UI --- src/lib/output/renderer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/output/renderer.ts b/src/lib/output/renderer.ts index ae5ec73f0..5b21525a6 100644 --- a/src/lib/output/renderer.ts +++ b/src/lib/output/renderer.ts @@ -211,7 +211,7 @@ export class Renderer extends ChildableComponent this.theme = this.addComponent('theme', new (themeClass)(this, path)); } catch (err) { throw new Error( - `Exception while loading ${filename}. You must export a \`new Theme(renderer, basePath)\` compatible class.\n' + + `Exception while loading "${filename}". You must export a \`new Theme(renderer, basePath)\` compatible class.\n` + err ); }