Last active
July 23, 2019 14:42
-
-
Save ericmorand/7beb7efdd487d6f81e3eb5686a02f190 to your computer and use it in GitHub Desktop.
twing-loader proof of concept
This file contains hidden or 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
const {TwingEnvironment, TwingLoaderArray} = require('twing'); | |
const {readFileSync} = require('fs'); | |
class CustomCache { | |
constructor() { | |
/** | |
* @type {Map<*, string>} | |
*/ | |
this.templates = new Map(); | |
this.TwingCacheInterfaceImpl = this; | |
} | |
/** | |
* Generates a cache key for the given template class name. | |
* | |
* @param {string} name The template name | |
* @param {string} className The template class name | |
* | |
* @return string | |
*/ | |
generateKey(name, className) { | |
return className; | |
} | |
/** | |
* Writes the compiled template to cache. | |
* | |
* @param {string} key The cache key | |
* @param {string} content The template representation as a PHP class | |
*/ | |
write(key, content) { | |
this.templates.set(key, content); | |
}; | |
/** | |
* Loads a template from the cache. | |
* | |
* @param {string} key The cache key | |
*/ | |
load(key) { | |
console.warn('LOAD', key); | |
let content = this.templates.get(key); | |
let f = new Function(`let module = { | |
exports: undefined | |
}; | |
${content} | |
return module.exports; | |
`); | |
console.warn(f()); | |
return f(); | |
} | |
/** | |
* Returns the modification timestamp of a key. | |
* | |
* @param {string} key The cache key | |
* | |
* @returns {number} | |
*/ | |
getTimestamp(key) { | |
return 0; | |
} | |
} | |
class CustomLoader extends TwingLoaderArray { | |
getCacheKey(name) { | |
console.warn(this.templates, name); | |
return this.templates.get(name); | |
} | |
} | |
class CustomEnvironment extends TwingEnvironment { | |
getTemplateClass(name, index = null, from = null) { | |
let key = this.getLoader().getCacheKey(name, from); | |
return key + (index === null ? '' : '_' + index); | |
} | |
} | |
let cache = new CustomCache(); | |
let indexAsString = readFileSync('cache/98/98bf74c5a7c4771ad5a280d1021876b1f55094365c7c7c06e7d15155c20fbd07.js', 'UTF-8'); | |
let parentAsString = readFileSync('cache/a3/a3ca1b89ea58826d6b345e1051822bd8b0b0928d713b07203ee4e2a4657b92f7.js', 'UTF-8'); | |
let partialAsString = readFileSync('cache/ea/eaa15d3686b0dc5230420bb03a37cf927e8b5b5e62fad80eff85410c5a1ce829.js', 'UTF-8'); | |
let skeletonAsString = readFileSync('cache/6c/6c67a2435e97a9b2758d66c565bcdbfe93218ef564d1b074e11052259c22e89b.js', 'UTF-8'); | |
let indexClassName = '__TwingTemplate_42c2ebb8182f0d7aaf434822b1db3df611d7eb6bce3bddab9678e19e9e3c53cb'; | |
let parentClassName = '__TwingTemplate_e686c9163e21c21622917f76462c9454b507077586c734948e918d0865ee94f7'; | |
let partialClassName = '__TwingTemplate_145a017a1f3709f0ae6396f73b4ede3b8bb0eb23580ab102bb111ee654c0a017'; | |
let skeletonClassName = '__TwingTemplate_bff4a4771cf09e4bd107900999dca27a4dceeb40a8c5290c7a6d27aaf2af23f8'; | |
cache.write(indexClassName, indexAsString); | |
cache.write(parentClassName, parentAsString); | |
cache.write(partialClassName, partialAsString); | |
cache.write(skeletonClassName, skeletonAsString); | |
let env = new CustomEnvironment(new CustomLoader({ | |
index: indexClassName, | |
parent: parentClassName, | |
partial: partialClassName, | |
skeleton: skeletonClassName | |
}), { | |
cache: cache | |
}); | |
console.warn(env.render('index', { | |
data: { | |
title: 'Hello Twing!' | |
} | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment