Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial impl with wontache #446

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -82,14 +82,13 @@
},
"dependencies": {
"diff": "5.1.0",
"hogan.js": "3.0.2"
"wontache": "0.1.0"
},
"optionalDependencies": {
"highlight.js": "11.6.0"
},
"devDependencies": {
"@types/diff": "5.0.2",
"@types/hogan.js": "3.0.1",
"@types/jest": "28.1.6",
"@types/mkdirp": "1.0.2",
"@types/node": "18.6.0",
Expand Down
31 changes: 15 additions & 16 deletions scripts/hulk.ts
@@ -1,3 +1,5 @@
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
/// <reference path="../typings/wontache/wontache.d.ts" />
/*
* Copyright 2011 Twitter, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -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';

Expand Down Expand Up @@ -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;
}
Expand All @@ -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:
Expand All @@ -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;

Expand Down
16 changes: 8 additions & 8 deletions 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';

Expand All @@ -7,7 +7,7 @@ export interface RawTemplates {
}

export interface CompiledTemplates {
[name: string]: Hogan.Template;
[name: string]: CompiledTemplate;
}

export interface HoganJsUtilsConfig {
Expand All @@ -21,7 +21,7 @@ export default class HoganJsUtils {
constructor({ compiledTemplates = {}, rawTemplates = {} }: HoganJsUtilsConfig) {
const compiledRawTemplates = Object.entries(rawTemplates).reduce<CompiledTemplates>(
(previousTemplates, [name, templateString]) => {
const compiledTemplate: Hogan.Template = Hogan.compile(templateString, { asString: false });
const compiledTemplate: CompiledTemplate = mustache(templateString);
return { ...previousTemplates, [name]: compiledTemplate };
},
{},
Expand All @@ -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)];
}

Expand Down
6 changes: 4 additions & 2 deletions src/line-by-line-renderer.ts
Expand Up @@ -63,18 +63,20 @@ 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),
},
{
partials: {
fileIcon: fileIconTemplate,
fileTag: fileTagTemplate,
},
},
),
});
}
Expand Down
6 changes: 4 additions & 2 deletions src/side-by-side-renderer.ts
Expand Up @@ -63,18 +63,20 @@ 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),
},
{
partials: {
fileIcon: fileIconTemplate,
fileTag: fileTagTemplate,
},
},
),
});
}
Expand Down
13 changes: 13 additions & 0 deletions 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;
}
}