-
-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds simplification code suggested by this awesome feedback from @jak…
- Loading branch information
Showing
1 changed file
with
32 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,22 +15,33 @@ class JavaScript extends TemplateEngine { | |
return result; | ||
} | ||
|
||
_getInstance(mod) { | ||
if (typeof mod === "string" || mod instanceof Buffer || mod.then) { | ||
return { render: () => mod }; | ||
} else if (typeof mod === "function") { | ||
if ( | ||
mod.prototype && | ||
This comment has been minimized.
Sorry, something went wrong. |
||
("data" in mod.prototype || "render" in mod.prototype) | ||
) { | ||
return new mod(); | ||
} else { | ||
return { render: mod }; | ||
} | ||
} else if ("data" in mod || "render" in mod) { | ||
return mod; | ||
} | ||
} | ||
|
||
getInstanceFromInputPath(inputPath) { | ||
if (this.instances[inputPath]) { | ||
return this.instances[inputPath]; | ||
} | ||
|
||
const cls = this._getRequire(inputPath); | ||
if (typeof cls === "function") { | ||
if ( | ||
cls.prototype && | ||
("data" in cls.prototype || "render" in cls.prototype) | ||
) { | ||
let inst = new cls(); | ||
this.instances[inputPath] = inst; | ||
return inst; | ||
} | ||
} | ||
const mod = this._getRequire(inputPath); | ||
let inst = this._getInstance(mod); | ||
|
||
this.instances[inputPath] = inst; | ||
return inst; | ||
} | ||
|
||
_getRequire(inputPath) { | ||
|
@@ -56,57 +67,28 @@ class JavaScript extends TemplateEngine { | |
|
||
async getExtraDataFromFile(inputPath) { | ||
let inst = this.getInstanceFromInputPath(inputPath); | ||
if (inst) { | ||
if (inst && "data" in inst) { | ||
// get extra data from `data` method, | ||
// either as a function or getter or object literal | ||
return typeof inst.data === "function" ? await inst.data() : inst.data; | ||
This comment has been minimized.
Sorry, something went wrong.
jakearchibald
|
||
} | ||
|
||
const cls = this._getRequire(inputPath); | ||
if (typeof cls === "object") { | ||
return typeof cls.data === "function" ? await cls.data() : cls.data; | ||
} | ||
} | ||
|
||
async compile(str, inputPath) { | ||
// for permalinks | ||
let inst; | ||
if (str) { | ||
// works with String, Buffer, Function! | ||
return function(data) { | ||
let target = str; | ||
if (typeof str === "function") { | ||
target = str.call(this.config.javascriptFunctions, data); | ||
} | ||
return this.normalize(target); | ||
}.bind(this); | ||
// When str has a value, it's being used for permalinks in data | ||
inst = this._getInstance(str); | ||
} else { | ||
// For normal templates, str will be falsy. | ||
inst = this.getInstanceFromInputPath(inputPath); | ||
} | ||
|
||
// for all other requires, str will be falsy | ||
const cls = this._getRequire(inputPath); | ||
if (typeof cls === "function") { | ||
// class with a `render` method | ||
if (cls.prototype && "render" in cls.prototype) { | ||
let inst = this.getInstanceFromInputPath(inputPath); | ||
Object.assign(inst, this.config.javascriptFunctions); | ||
return function(data) { | ||
return this.normalize(inst.render.call(inst, data)); | ||
}.bind(this); | ||
} | ||
if (inst && "render" in inst) { | ||
Object.assign(inst, this.config.javascriptFunctions); | ||
|
||
// raw function | ||
return function(data) { | ||
return this.normalize(cls.call(this.config.javascriptFunctions, data)); | ||
}.bind(this); | ||
} else if (typeof cls === "object" && "render" in cls) { | ||
return function(data) { | ||
return this.normalize( | ||
cls.render.call(this.config.javascriptFunctions, data) | ||
); | ||
}.bind(this); | ||
} else { | ||
// string type does not work with javascriptFunctions | ||
return function() { | ||
return this.normalize(cls); | ||
return this.normalize(inst.render.call(inst, data)); | ||
}.bind(this); | ||
} | ||
} | ||
|
Ohh good catch