Skip to content

Commit

Permalink
Adds simplification code suggested by this awesome feedback from @jak…
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Jul 25, 2019
1 parent febbea4 commit 752b39a
Showing 1 changed file with 32 additions and 50 deletions.
82 changes: 32 additions & 50 deletions src/Engines/JavaScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
@jakearchibald

jakearchibald Jul 25, 2019

Ohh good catch

("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) {
Expand All @@ -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.

Copy link
@jakearchibald

jakearchibald Jul 25, 2019

Could await inst.data here to support:

export const data = new Promise();

This comment has been minimized.

Copy link
@zachleat

zachleat Jul 25, 2019

Author Member

Oh nice, yeah!

This comment has been minimized.

Copy link
@zachleat

zachleat Jul 25, 2019

Author Member

I checked that change in

}

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);
}
}
Expand Down

0 comments on commit 752b39a

Please sign in to comment.