|
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +const loader_utils_1 = require("loader-utils"); |
| 4 | +const twing_1 = require("twing"); |
| 5 | +const visitor_1 = require("./visitor"); |
| 6 | +const sha256 = require('crypto-js/sha256'); |
| 7 | +const hex = require('crypto-js/enc-hex'); |
| 8 | +const slash = require('slash'); |
| 9 | +const validateOptions = require('schema-utils'); |
| 10 | +const optionsSchema = { |
| 11 | + type: 'object', |
| 12 | + properties: { |
| 13 | + environmentModulePath: { |
| 14 | + type: 'string' |
| 15 | + }, |
| 16 | + renderContext: { |
| 17 | + type: 'object' |
| 18 | + } |
| 19 | + }, |
| 20 | + required: [ |
| 21 | + 'environmentModulePath' |
| 22 | + ], |
| 23 | + additionalProperties: false |
| 24 | +}; |
| 25 | +class PathSupportingArrayLoader extends twing_1.TwingLoaderArray { |
| 26 | + getSourceContext(name, from) { |
| 27 | + return super.getSourceContext(name, from).then((source) => { |
| 28 | + return new twing_1.TwingSource(source.getCode(), source.getName(), name); |
| 29 | + }); |
| 30 | + } |
| 31 | +} |
| 32 | +async function default_1(source) { |
| 33 | + const callback = this.async(); |
| 34 | + const getTemplateHash = (name) => { |
| 35 | + return this.mode !== 'production' ? name : hex.stringify(sha256(name)); |
| 36 | + }; |
| 37 | + const options = loader_utils_1.getOptions(this); |
| 38 | + validateOptions(optionsSchema, options, 'Twing loader'); |
| 39 | + let resourcePath = slash(this.resourcePath); |
| 40 | + let environmentModulePath = options.environmentModulePath; |
| 41 | + let renderContext = options.renderContext; |
| 42 | + this.addDependency(slash(environmentModulePath)); |
| 43 | + // require takes module name separated with forward slashes |
| 44 | + let environment = require(slash(environmentModulePath)); |
| 45 | + let loader = environment.getLoader(); |
| 46 | + if (renderContext === undefined) { |
| 47 | + let parts = [ |
| 48 | + `const env = require('${slash(environmentModulePath)}');` |
| 49 | + ]; |
| 50 | + let key = getTemplateHash(resourcePath); |
| 51 | + let sourceContext = new twing_1.TwingSource(source, `${key}`); |
| 52 | + let tokenStream; |
| 53 | + let nodeModule; |
| 54 | + try { |
| 55 | + tokenStream = environment.tokenize(sourceContext); |
| 56 | + nodeModule = environment.parse(tokenStream); |
| 57 | + } |
| 58 | + catch (err) { |
| 59 | + callback(err); |
| 60 | + return null; |
| 61 | + } |
| 62 | + let visitor = new visitor_1.Visitor(loader, resourcePath, getTemplateHash); |
| 63 | + await visitor.visit(nodeModule); |
| 64 | + let precompiledTemplate = environment.compile(nodeModule); |
| 65 | + parts.push(`let templatesModule = (() => { |
| 66 | +let module = { |
| 67 | + exports: undefined |
| 68 | +}; |
| 69 | +
|
| 70 | +${precompiledTemplate} |
| 71 | +
|
| 72 | + return module.exports; |
| 73 | +})(); |
| 74 | +`); |
| 75 | + for (let foundTemplateName of visitor.foundTemplateNames) { |
| 76 | + // require takes module name separated with forward slashes |
| 77 | + parts.push(`require('${slash(foundTemplateName)}');`); |
| 78 | + } |
| 79 | + parts.push(`env.registerTemplatesModule(templatesModule, '${key}');`); |
| 80 | + parts.push(` |
| 81 | +let loadTemplate = () => env.loadTemplate('${key}'); |
| 82 | +
|
| 83 | +module.exports = (context = {}) => { |
| 84 | + return loadTemplate().then((template) => template.render(context)); |
| 85 | +};`); |
| 86 | + callback(null, parts.join('\n')); |
| 87 | + } |
| 88 | + else { |
| 89 | + environment.setLoader(new twing_1.TwingLoaderChain([ |
| 90 | + new PathSupportingArrayLoader(new Map([ |
| 91 | + [resourcePath, source] |
| 92 | + ])), |
| 93 | + loader |
| 94 | + ])); |
| 95 | + environment.on('template', (name, from) => { |
| 96 | + environment.getLoader().resolve(name, from) |
| 97 | + .then((path) => this.addDependency(path)) |
| 98 | + .catch((e) => { }); |
| 99 | + }); |
| 100 | + callback(null, `module.exports = ${JSON.stringify(await environment.render(resourcePath, renderContext))};`); |
| 101 | + } |
| 102 | +} |
| 103 | +exports.default = default_1; |
| 104 | +; |
0 commit comments