Skip to content

Commit 60e7a2e

Browse files
committed
initial impl with wontache
1 parent 26adbdb commit 60e7a2e

File tree

7 files changed

+367
-327
lines changed

7 files changed

+367
-327
lines changed

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,13 @@
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"
8989
},
9090
"devDependencies": {
9191
"@types/diff": "5.0.2",
92-
"@types/hogan.js": "3.0.1",
9392
"@types/jest": "28.1.6",
9493
"@types/mkdirp": "1.0.2",
9594
"@types/node": "18.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+
}

0 commit comments

Comments
 (0)