Skip to content

Commit b17cbc5

Browse files
committed
initial impl with wontache
1 parent 0ed9e76 commit b17cbc5

File tree

7 files changed

+62
-54
lines changed

7 files changed

+62
-54
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
},
8383
"dependencies": {
8484
"diff": "5.1.0",
85-
"hogan.js": "3.0.2"
85+
"wontache": "^0.1.0"
8686
},
8787
"optionalDependencies": {
8888
"highlight.js": "11.6.0"

scripts/hulk.ts

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
2+
/// <reference path="../typings/wontache/wontache.d.ts" />
13
/*
24
* Copyright 2011 Twitter, Inc.
35
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,7 +18,7 @@
1618
import * as path from 'path';
1719
import * as fs from 'fs';
1820

19-
import * as hogan from 'hogan.js';
21+
import mustache from 'wontache';
2022
import nopt from 'nopt';
2123
import * as mkderp from 'mkdirp';
2224

@@ -107,25 +109,20 @@ function removeByteOrderMark(text: string): string {
107109
}
108110

109111
// Wrap templates
110-
function wrap(file: string, name: string, openedFile: string): string {
111-
const hoganTemplateString = `new Hogan.Template(${hogan.compile(openedFile, { asString: true })})`;
112+
function wrap(name: string, openedFile: string): string {
113+
const templateString = mustache(openedFile).source;
112114

113115
const objectName = options.variable || 'templates';
114116
const objectAccessor = `${objectName}["${name}"]`;
115-
const objectStmt = `${objectAccessor} = ${hoganTemplateString};`;
117+
const objectStmt = `${objectAccessor} = ${templateString};`;
116118

117119
switch (options.wrapper) {
118-
case 'amd':
119-
return `define(${
120-
!options.outputdir ? `"${path.join(path.dirname(file), name)}", ` : ''
121-
}["hogan.js"], function(Hogan) { return ${hoganTemplateString}; });`;
122-
123120
case 'node':
124121
// If we have a template per file the export will expose the template directly
125122
return options.outputdir ? `global.${objectStmt};\nmodule.exports = ${objectAccessor};` : `global.${objectStmt}`;
126123

127124
case 'ts':
128-
return `// @ts-ignore\n${objectStmt}`;
125+
return `// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n// @ts-ignore\n${objectStmt}`;
129126
default:
130127
return objectStmt;
131128
}
@@ -137,16 +134,18 @@ function prepareOutput(content: string): string {
137134
case 'amd':
138135
return content;
139136
case 'node':
140-
return `(function() {
137+
return `const mustache = require('wontache');
138+
(function() {
141139
if (!!!global.${variableName}) global.${variableName} = {};
142-
var Hogan = require("hogan.js");
143140
${content}
144141
${!options.outputdir ? `module.exports = global.${variableName};\n` : ''})();`;
145142

146143
case 'ts':
147-
return `import * as Hogan from "hogan.js";
148-
type CompiledTemplates = { [name: string]: Hogan.Template };
149-
export const ${variableName}: CompiledTemplates = {};
144+
return `/* eslint-disable @typescript-eslint/no-unused-vars */
145+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
146+
// @ts-nocheck
147+
import mustache, { CompiledTemplate } from 'wontache';
148+
export const defaultTemplates: { [_: string]: CompiledTemplate } = {};
150149
${content}`;
151150

152151
default:
@@ -172,7 +171,7 @@ const templates = extractFiles(options.argv.remain)
172171
if (!timmedFileContents) return;
173172

174173
const name = namespace(path.basename(file).replace(/\..*$/, ''));
175-
const cleanFileContents = wrap(file, name, removeByteOrderMark(timmedFileContents));
174+
const cleanFileContents = wrap(name, removeByteOrderMark(timmedFileContents));
176175

177176
if (!options.outputdir) return cleanFileContents;
178177

src/hoganjs-utils.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as Hogan from 'hogan.js';
1+
import mustache, { CompiledTemplate, Partials } from 'wontache';
22

33
import { defaultTemplates } from './diff2html-templates';
44

@@ -7,7 +7,7 @@ export interface RawTemplates {
77
}
88

99
export interface CompiledTemplates {
10-
[name: string]: Hogan.Template;
10+
[name: string]: CompiledTemplate;
1111
}
1212

1313
export interface HoganJsUtilsConfig {
@@ -21,7 +21,7 @@ export default class HoganJsUtils {
2121
constructor({ compiledTemplates = {}, rawTemplates = {} }: HoganJsUtilsConfig) {
2222
const compiledRawTemplates = Object.entries(rawTemplates).reduce<CompiledTemplates>(
2323
(previousTemplates, [name, templateString]) => {
24-
const compiledTemplate: Hogan.Template = Hogan.compile(templateString, { asString: false });
24+
const compiledTemplate: CompiledTemplate = mustache(templateString);
2525
return { ...previousTemplates, [name]: compiledTemplate };
2626
},
2727
{},
@@ -30,21 +30,21 @@ export default class HoganJsUtils {
3030
this.preCompiledTemplates = { ...defaultTemplates, ...compiledTemplates, ...compiledRawTemplates };
3131
}
3232

33-
static compile(templateString: string): Hogan.Template {
34-
return Hogan.compile(templateString, { asString: false });
33+
static compile(templateString: string): CompiledTemplate {
34+
return mustache(templateString);
3535
}
3636

37-
render(namespace: string, view: string, params: Hogan.Context, partials?: Hogan.Partials, indent?: string): string {
37+
render(namespace: string, view: string, params: object, partials?: Partials): string {
3838
const templateKey = this.templateKey(namespace, view);
3939
try {
4040
const template = this.preCompiledTemplates[templateKey];
41-
return template.render(params, partials, indent);
41+
return template(params, { partials });
4242
} catch (e) {
4343
throw new Error(`Could not find template to render '${templateKey}'`);
4444
}
4545
}
4646

47-
template(namespace: string, view: string): Hogan.Template {
47+
template(namespace: string, view: string): CompiledTemplate {
4848
return this.preCompiledTemplates[this.templateKey(namespace, view)];
4949
}
5050

src/line-by-line-renderer.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,19 @@ export default class LineByLineRenderer {
6363
const fileIconTemplate = this.hoganUtils.template(iconsBaseTemplatesPath, 'file');
6464
const fileTagTemplate = this.hoganUtils.template(tagsBaseTemplatesPath, renderUtils.getFileIcon(file));
6565

66-
return fileDiffTemplate.render({
66+
return fileDiffTemplate({
6767
file: file,
6868
fileHtmlId: renderUtils.getHtmlId(file),
6969
diffs: diffs,
70-
filePath: filePathTemplate.render(
70+
filePath: filePathTemplate(
7171
{
7272
fileDiffName: renderUtils.filenameDiff(file),
7373
},
7474
{
75-
fileIcon: fileIconTemplate,
76-
fileTag: fileTagTemplate,
75+
partials: {
76+
fileIcon: fileIconTemplate,
77+
fileTag: fileTagTemplate,
78+
},
7779
},
7880
),
7981
});

src/side-by-side-renderer.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,19 @@ export default class SideBySideRenderer {
6363
const fileIconTemplate = this.hoganUtils.template(iconsBaseTemplatesPath, 'file');
6464
const fileTagTemplate = this.hoganUtils.template(tagsBaseTemplatesPath, renderUtils.getFileIcon(file));
6565

66-
return fileDiffTemplate.render({
66+
return fileDiffTemplate({
6767
file: file,
6868
fileHtmlId: renderUtils.getHtmlId(file),
6969
diffs: diffs,
70-
filePath: filePathTemplate.render(
70+
filePath: filePathTemplate(
7171
{
7272
fileDiffName: renderUtils.filenameDiff(file),
7373
},
7474
{
75-
fileIcon: fileIconTemplate,
76-
fileTag: fileTagTemplate,
75+
partials: {
76+
fileIcon: fileIconTemplate,
77+
fileTag: fileTagTemplate,
78+
},
7779
},
7880
),
7981
});

typings/wontache/wontache.d.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
declare module 'wontache' {
2+
export default function compile(template: string | object): CompiledTemplate;
3+
type Partials = {
4+
[_: string]: string | object;
5+
};
6+
type Options = {
7+
partials?: Partials;
8+
};
9+
interface CompiledTemplate {
10+
(data: object, opt?: Options): string;
11+
source: string;
12+
}
13+
}

yarn.lock

+13-21
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@
11621162
resolved "https://registry.yarnpkg.com/@xtuc/long/-/long-4.2.2.tgz#d291c6a4e97989b5c61d9acf396ae4fe133a718d"
11631163
integrity sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ==
11641164

1165-
abbrev@1, abbrev@^1.0.0:
1165+
abbrev@^1.0.0:
11661166
version "1.1.1"
11671167
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8"
11681168
integrity sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==
@@ -3485,14 +3485,6 @@ [email protected]:
34853485
resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-11.6.0.tgz#a50e9da05763f1bb0c1322c8f4f755242cff3f5a"
34863486
integrity sha512-ig1eqDzJaB0pqEvlPVIpSSyMaO92bH1N2rJpLMN/nX396wTpDA4Eq0uK+7I/2XG17pFaaKE0kjV/XPeGt7Evjw==
34873487

3488-
3489-
version "3.0.2"
3490-
resolved "https://registry.yarnpkg.com/hogan.js/-/hogan.js-3.0.2.tgz#4cd9e1abd4294146e7679e41d7898732b02c7bfd"
3491-
integrity sha512-RqGs4wavGYJWE07t35JQccByczmNUXQT0E12ZYV1VKYu5UiAU9lsos/yBAcf840+zrUQQxgVduCR5/B8nNtibg==
3492-
dependencies:
3493-
mkdirp "0.3.0"
3494-
nopt "1.0.10"
3495-
34963488
html-escaper@^2.0.0:
34973489
version "2.0.2"
34983490
resolved "https://registry.yarnpkg.com/html-escaper/-/html-escaper-2.0.2.tgz#dfd60027da36a36dfcbe236262c00a5822681453"
@@ -4869,11 +4861,6 @@ mixin-deep@^1.1.3:
48694861
for-in "^1.0.2"
48704862
is-extendable "^1.0.1"
48714863

4872-
4873-
version "0.3.0"
4874-
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.3.0.tgz#1bbf5ab1ba827af23575143490426455f481fe1e"
4875-
integrity sha512-OHsdUcVAQ6pOtg5JYWpCBo9W/GySVuwvP9hueRMW7UqshC0tbfzLv8wjySTPm3tfUZ/21CE9E1pJagOA91Pxew==
4876-
48774864
48784865
version "1.0.4"
48794866
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
@@ -4957,13 +4944,6 @@ node-releases@^2.0.6:
49574944
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-2.0.6.tgz#8a7088c63a55e493845683ebf3c828d8c51c5503"
49584945
integrity sha512-PiVXnNuFm5+iYkLBNeq5211hvO38y63T0i2KKh2KnUs3RpzJ+JtODFjkD8yjLwnDkTYF1eKXheUwdssR+NRZdg==
49594946

4960-
4961-
version "1.0.10"
4962-
resolved "https://registry.yarnpkg.com/nopt/-/nopt-1.0.10.tgz#6ddd21bd2a31417b92727dd585f8a6f37608ebee"
4963-
integrity sha512-NWmpvLSqUrgrAC9HCuxEvb+PSloHpqVu+FqcO4eeF2h5qYRhA7ev6KvelyQAKtegUbC6RypJnlEOhd8vloNKYg==
4964-
dependencies:
4965-
abbrev "1"
4966-
49674947
49684948
version "6.0.0"
49694949
resolved "https://registry.yarnpkg.com/nopt/-/nopt-6.0.0.tgz#245801d8ebf409c6df22ab9d95b65e1309cdb16d"
@@ -6999,6 +6979,11 @@ unbzip2-stream@^1.0.9:
69996979
buffer "^5.2.1"
70006980
through "^2.3.8"
70016981

6982+
underscore@^1.13.0-2:
6983+
version "1.13.4"
6984+
resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.13.4.tgz#7886b46bbdf07f768e0052f1828e1dcab40c0dee"
6985+
integrity sha512-BQFnUDuAQ4Yf/cYY5LNrK9NCJFKriaRbD9uR1fTeXnBeoa97W0i41qkZfGO9pSo8I5KzjAcSY2XYtdf0oKd7KQ==
6986+
70026987
universalify@^2.0.0:
70036988
version "2.0.0"
70046989
resolved "https://registry.yarnpkg.com/universalify/-/universalify-2.0.0.tgz#75a4984efedc4b08975c5aeb73f530d02df25717"
@@ -7241,6 +7226,13 @@ wildcard@^2.0.0:
72417226
resolved "https://registry.yarnpkg.com/wildcard/-/wildcard-2.0.0.tgz#a77d20e5200c6faaac979e4b3aadc7b3dd7f8fec"
72427227
integrity sha512-JcKqAHLPxcdb9KM49dufGXn2x3ssnfjbcaQdLlfZsL9rH9wgDQjUtDxbo8NE0F6SFvydeu1VhZe7hZuHsB2/pw==
72437228

7229+
wontache@^0.1.0:
7230+
version "0.1.0"
7231+
resolved "https://registry.yarnpkg.com/wontache/-/wontache-0.1.0.tgz#125154b1c01e2bb15bae0924f4e96de11c282945"
7232+
integrity sha512-UH4ikvEVRtvqY3DoW9/NjctB11FDuHjkKPO1tjaUVIVnZevxNtvba7lhR7H5TfMBVCpF2jwxH1qlu0UQSQ/zCw==
7233+
dependencies:
7234+
underscore "^1.13.0-2"
7235+
72447236
word-wrap@^1.2.3:
72457237
version "1.2.3"
72467238
resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.3.tgz#610636f6b1f703891bd34771ccb17fb93b47079c"

0 commit comments

Comments
 (0)