From 09450e3baa90b7e4c942db1f55312aefb598686d Mon Sep 17 00:00:00 2001 From: Rodrigo Fernandes Date: Fri, 12 Aug 2022 23:01:54 +0100 Subject: [PATCH] initial impl with wontache --- package.json | 3 ++- scripts/hulk.ts | 31 +++++++++++++++---------------- src/hoganjs-utils.ts | 16 ++++++++-------- src/line-by-line-renderer.ts | 10 ++++++---- src/side-by-side-renderer.ts | 10 ++++++---- typings/wontache/wontache.d.ts | 13 +++++++++++++ yarn.lock | 12 ++++++++++++ 7 files changed, 62 insertions(+), 33 deletions(-) create mode 100644 typings/wontache/wontache.d.ts diff --git a/package.json b/package.json index 2f65647b..a5baf680 100644 --- a/package.json +++ b/package.json @@ -82,7 +82,8 @@ }, "dependencies": { "diff": "5.1.0", - "hogan.js": "3.0.2" + "hogan.js": "3.0.2", + "wontache": "^0.1.0" }, "optionalDependencies": { "highlight.js": "11.6.0" diff --git a/scripts/hulk.ts b/scripts/hulk.ts index 8239c765..8c4e8406 100755 --- a/scripts/hulk.ts +++ b/scripts/hulk.ts @@ -1,3 +1,5 @@ +// eslint-disable-next-line @typescript-eslint/triple-slash-reference +/// /* * Copyright 2011 Twitter, Inc. * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,7 +18,7 @@ import * as path from 'path'; import * as fs from 'fs'; -import * as hogan from 'hogan.js'; +import mustache from 'wontache'; import nopt from 'nopt'; import * as mkderp from 'mkdirp'; @@ -107,25 +109,20 @@ function removeByteOrderMark(text: string): string { } // Wrap templates -function wrap(file: string, name: string, openedFile: string): string { - const hoganTemplateString = `new Hogan.Template(${hogan.compile(openedFile, { asString: true })})`; +function wrap(name: string, openedFile: string): string { + const templateString = mustache(openedFile).source; const objectName = options.variable || 'templates'; const objectAccessor = `${objectName}["${name}"]`; - const objectStmt = `${objectAccessor} = ${hoganTemplateString};`; + const objectStmt = `${objectAccessor} = ${templateString};`; switch (options.wrapper) { - case 'amd': - return `define(${ - !options.outputdir ? `"${path.join(path.dirname(file), name)}", ` : '' - }["hogan.js"], function(Hogan) { return ${hoganTemplateString}; });`; - case 'node': // If we have a template per file the export will expose the template directly return options.outputdir ? `global.${objectStmt};\nmodule.exports = ${objectAccessor};` : `global.${objectStmt}`; case 'ts': - return `// @ts-ignore\n${objectStmt}`; + return `// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n// @ts-ignore\n${objectStmt}`; default: return objectStmt; } @@ -137,16 +134,18 @@ function prepareOutput(content: string): string { case 'amd': return content; case 'node': - return `(function() { + return `const mustache = require('wontache'); +(function() { if (!!!global.${variableName}) global.${variableName} = {}; -var Hogan = require("hogan.js"); ${content} ${!options.outputdir ? `module.exports = global.${variableName};\n` : ''})();`; case 'ts': - return `import * as Hogan from "hogan.js"; -type CompiledTemplates = { [name: string]: Hogan.Template }; -export const ${variableName}: CompiledTemplates = {}; + return `/* eslint-disable @typescript-eslint/no-unused-vars */ +// eslint-disable-next-line @typescript-eslint/ban-ts-comment +// @ts-nocheck +import mustache, { CompiledTemplate } from 'wontache'; +export const defaultTemplates: { [_: string]: CompiledTemplate } = {}; ${content}`; default: @@ -172,7 +171,7 @@ const templates = extractFiles(options.argv.remain) if (!timmedFileContents) return; const name = namespace(path.basename(file).replace(/\..*$/, '')); - const cleanFileContents = wrap(file, name, removeByteOrderMark(timmedFileContents)); + const cleanFileContents = wrap(name, removeByteOrderMark(timmedFileContents)); if (!options.outputdir) return cleanFileContents; diff --git a/src/hoganjs-utils.ts b/src/hoganjs-utils.ts index 88b54e54..def2dc98 100644 --- a/src/hoganjs-utils.ts +++ b/src/hoganjs-utils.ts @@ -1,4 +1,4 @@ -import * as Hogan from 'hogan.js'; +import mustache, { CompiledTemplate, Partials } from 'wontache'; import { defaultTemplates } from './diff2html-templates'; @@ -7,7 +7,7 @@ export interface RawTemplates { } export interface CompiledTemplates { - [name: string]: Hogan.Template; + [name: string]: CompiledTemplate; } export interface HoganJsUtilsConfig { @@ -21,7 +21,7 @@ export default class HoganJsUtils { constructor({ compiledTemplates = {}, rawTemplates = {} }: HoganJsUtilsConfig) { const compiledRawTemplates = Object.entries(rawTemplates).reduce( (previousTemplates, [name, templateString]) => { - const compiledTemplate: Hogan.Template = Hogan.compile(templateString, { asString: false }); + const compiledTemplate: CompiledTemplate = mustache(templateString); return { ...previousTemplates, [name]: compiledTemplate }; }, {}, @@ -30,21 +30,21 @@ export default class HoganJsUtils { this.preCompiledTemplates = { ...defaultTemplates, ...compiledTemplates, ...compiledRawTemplates }; } - static compile(templateString: string): Hogan.Template { - return Hogan.compile(templateString, { asString: false }); + static compile(templateString: string): CompiledTemplate { + return mustache(templateString); } - render(namespace: string, view: string, params: Hogan.Context, partials?: Hogan.Partials, indent?: string): string { + render(namespace: string, view: string, params: object, partials?: Partials): string { const templateKey = this.templateKey(namespace, view); try { const template = this.preCompiledTemplates[templateKey]; - return template.render(params, partials, indent); + return template(params, { partials }); } catch (e) { throw new Error(`Could not find template to render '${templateKey}'`); } } - template(namespace: string, view: string): Hogan.Template { + template(namespace: string, view: string): CompiledTemplate { return this.preCompiledTemplates[this.templateKey(namespace, view)]; } diff --git a/src/line-by-line-renderer.ts b/src/line-by-line-renderer.ts index 4258095d..9c750fd3 100644 --- a/src/line-by-line-renderer.ts +++ b/src/line-by-line-renderer.ts @@ -63,17 +63,19 @@ export default class LineByLineRenderer { const fileIconTemplate = this.hoganUtils.template(iconsBaseTemplatesPath, 'file'); const fileTagTemplate = this.hoganUtils.template(tagsBaseTemplatesPath, renderUtils.getFileIcon(file)); - return fileDiffTemplate.render({ + return fileDiffTemplate({ file: file, fileHtmlId: renderUtils.getHtmlId(file), diffs: diffs, - filePath: filePathTemplate.render( + filePath: filePathTemplate( { fileDiffName: renderUtils.filenameDiff(file), }, { - fileIcon: fileIconTemplate, - fileTag: fileTagTemplate, + partials: { + fileIcon: fileIconTemplate, + fileTag: fileTagTemplate, + }, }, ), }); diff --git a/src/side-by-side-renderer.ts b/src/side-by-side-renderer.ts index 9a0fb6b9..f8a52d9b 100644 --- a/src/side-by-side-renderer.ts +++ b/src/side-by-side-renderer.ts @@ -63,17 +63,19 @@ export default class SideBySideRenderer { const fileIconTemplate = this.hoganUtils.template(iconsBaseTemplatesPath, 'file'); const fileTagTemplate = this.hoganUtils.template(tagsBaseTemplatesPath, renderUtils.getFileIcon(file)); - return fileDiffTemplate.render({ + return fileDiffTemplate({ file: file, fileHtmlId: renderUtils.getHtmlId(file), diffs: diffs, - filePath: filePathTemplate.render( + filePath: filePathTemplate( { fileDiffName: renderUtils.filenameDiff(file), }, { - fileIcon: fileIconTemplate, - fileTag: fileTagTemplate, + partials: { + fileIcon: fileIconTemplate, + fileTag: fileTagTemplate, + }, }, ), }); diff --git a/typings/wontache/wontache.d.ts b/typings/wontache/wontache.d.ts new file mode 100644 index 00000000..728646ce --- /dev/null +++ b/typings/wontache/wontache.d.ts @@ -0,0 +1,13 @@ +declare module 'wontache' { + export default function compile(template: string | object): CompiledTemplate; + type Partials = { + [_: string]: string | object; + }; + type Options = { + partials?: Partials; + }; + interface CompiledTemplate { + (data: object, opt?: Options): string; + source: string; + } +} diff --git a/yarn.lock b/yarn.lock index 128e669d..4229b0ee 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6999,6 +6999,11 @@ unbzip2-stream@^1.0.9: buffer "^5.2.1" through "^2.3.8" +underscore@^1.13.0-2: + version "1.13.4" + resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.13.4.tgz#7886b46bbdf07f768e0052f1828e1dcab40c0dee" + integrity sha512-BQFnUDuAQ4Yf/cYY5LNrK9NCJFKriaRbD9uR1fTeXnBeoa97W0i41qkZfGO9pSo8I5KzjAcSY2XYtdf0oKd7KQ== + universalify@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/universalify/-/universalify-2.0.0.tgz#75a4984efedc4b08975c5aeb73f530d02df25717" @@ -7241,6 +7246,13 @@ wildcard@^2.0.0: resolved "https://registry.yarnpkg.com/wildcard/-/wildcard-2.0.0.tgz#a77d20e5200c6faaac979e4b3aadc7b3dd7f8fec" integrity sha512-JcKqAHLPxcdb9KM49dufGXn2x3ssnfjbcaQdLlfZsL9rH9wgDQjUtDxbo8NE0F6SFvydeu1VhZe7hZuHsB2/pw== +wontache@^0.1.0: + version "0.1.0" + resolved "https://registry.yarnpkg.com/wontache/-/wontache-0.1.0.tgz#125154b1c01e2bb15bae0924f4e96de11c282945" + integrity sha512-UH4ikvEVRtvqY3DoW9/NjctB11FDuHjkKPO1tjaUVIVnZevxNtvba7lhR7H5TfMBVCpF2jwxH1qlu0UQSQ/zCw== + dependencies: + underscore "^1.13.0-2" + word-wrap@^1.2.3: version "1.2.3" resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.3.tgz#610636f6b1f703891bd34771ccb17fb93b47079c"