initial impl with wontache
This commit is contained in:
parent
26adbdb220
commit
60e7a2e68c
7 changed files with 367 additions and 327 deletions
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -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<CompiledTemplates>(
|
||||
(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)];
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
},
|
||||
},
|
||||
),
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
},
|
||||
},
|
||||
),
|
||||
});
|
||||
}
|
||||
|
|
|
|||
13
typings/wontache/wontache.d.ts
vendored
Normal file
13
typings/wontache/wontache.d.ts
vendored
Normal file
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue