Skip to content

Commit 09450e3

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

File tree

7 files changed

+62
-33
lines changed

7 files changed

+62
-33
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@
8282
},
8383
"dependencies": {
8484
"diff": "5.1.0",
85-
"hogan.js": "3.0.2"
85+
"hogan.js": "3.0.2",
86+
"wontache": "^0.1.0"
8687
},
8788
"optionalDependencies": {
8889
"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

+12
Original file line numberDiff line numberDiff line change
@@ -6999,6 +6999,11 @@ unbzip2-stream@^1.0.9:
69996999
buffer "^5.2.1"
70007000
through "^2.3.8"
70017001

7002+
underscore@^1.13.0-2:
7003+
version "1.13.4"
7004+
resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.13.4.tgz#7886b46bbdf07f768e0052f1828e1dcab40c0dee"
7005+
integrity sha512-BQFnUDuAQ4Yf/cYY5LNrK9NCJFKriaRbD9uR1fTeXnBeoa97W0i41qkZfGO9pSo8I5KzjAcSY2XYtdf0oKd7KQ==
7006+
70027007
universalify@^2.0.0:
70037008
version "2.0.0"
70047009
resolved "https://registry.yarnpkg.com/universalify/-/universalify-2.0.0.tgz#75a4984efedc4b08975c5aeb73f530d02df25717"
@@ -7241,6 +7246,13 @@ wildcard@^2.0.0:
72417246
resolved "https://registry.yarnpkg.com/wildcard/-/wildcard-2.0.0.tgz#a77d20e5200c6faaac979e4b3aadc7b3dd7f8fec"
72427247
integrity sha512-JcKqAHLPxcdb9KM49dufGXn2x3ssnfjbcaQdLlfZsL9rH9wgDQjUtDxbo8NE0F6SFvydeu1VhZe7hZuHsB2/pw==
72437248

7249+
wontache@^0.1.0:
7250+
version "0.1.0"
7251+
resolved "https://registry.yarnpkg.com/wontache/-/wontache-0.1.0.tgz#125154b1c01e2bb15bae0924f4e96de11c282945"
7252+
integrity sha512-UH4ikvEVRtvqY3DoW9/NjctB11FDuHjkKPO1tjaUVIVnZevxNtvba7lhR7H5TfMBVCpF2jwxH1qlu0UQSQ/zCw==
7253+
dependencies:
7254+
underscore "^1.13.0-2"
7255+
72447256
word-wrap@^1.2.3:
72457257
version "1.2.3"
72467258
resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.3.tgz#610636f6b1f703891bd34771ccb17fb93b47079c"

0 commit comments

Comments
 (0)