Skip to content
Snippets Groups Projects
Commit 86197a66 authored by Eric MORAND's avatar Eric MORAND
Browse files

Resolve issue #44

parent 6ff3cbe2
No related branches found
No related tags found
No related merge requests found
Pipeline #1216797084 failed
Showing with 132 additions and 77 deletions
......@@ -13,14 +13,10 @@ export type TwingLoaderOptions = {
export default function (this: LoaderContext<TwingLoaderOptions>, code: string) {
const callback = this.async();
const getTemplateHash = (name: string) => {
return relative('.', name);
};
const {environment} = this.getOptions();
const {resourcePath} = this;
const key = getTemplateHash(resourcePath);
const key = relative('.', resourcePath);
const source = createSource(key, code);
const tokenStream = environment.tokenize(source);
const ast = environment.parse(tokenStream);
......@@ -36,28 +32,24 @@ templates.set('${templateName}', template${index++});`
})
.join('');
const generatedCode = `
import {createTemplate} from "twing";
const generatedCode = `import {createTemplate} from "twing";
const templates = new Map();
${dependencies}
const template = createTemplate(${JSON.stringify(ast)});
const baseRender = template.render;
const baseLoadTemplate = template.loadTemplate;
template.render = (environment, context, options) => {
const baseLoadTemplate = environment.loadTemplate;
environment.loadTemplate = (name) => {
return templates.has(name) ? Promise.resolve(templates.get(name)) : baseLoadTemplate(name);
template.loadTemplate = (executionContext, identifier) => {
return templates.has(identifier) ? Promise.resolve(templates.get(identifier)) : baseLoadTemplate(executionContext, identifier);
};
return baseRender(environment, context, options);
};
for (const [, embeddedTemplate] of template.embeddedTemplates) {
embeddedTemplate.loadTemplate = template.loadTemplate;
}
export default template;
`;
export default template;`;
callback(null, generatedCode);
});
......
......@@ -65,7 +65,7 @@ export const visit = async (
}
if (node.type === "template") {
if (node.children.parent && node.children.parent.type === "constant") {
if (node.children.parent) {
await processExpressionNode(node.children.parent);
}
......
import {TestCase} from "../test-case";
import {TestSuite} from "../test-suite";
import {runTestSuite} from "../test-suite";
import {createEnvironment, createFilesystemLoader} from "twing";
import fs from "fs";
......@@ -15,6 +15,6 @@ class SelfTestCase extends TestCase {
const environment = createEnvironment(createFilesystemLoader(fs));
(new TestSuite('_self variable', [
runTestSuite('_self variable', [
new SelfTestCase(environment),
])).run();
]);
import template from './index.twig';
import {createEnvironment, createArrayLoader} from "twing";
export default template.render(
createEnvironment(createArrayLoader({})),
{
foo: 'BAR'
}
);
\ No newline at end of file
{% embed "./skeleton.twig" %}
{% block footer %}
Custom footer
{{ include("./skeleton.twig") }}
{% endblock %}
{% endembed %}
\ No newline at end of file
{% block foo %}
{{ foo }}
{% endblock %}
\ No newline at end of file
{% extends "../extends/parent.twig" %}
{% block foo %}
<div class="header">
{% block header %}
Skeleton header
{% endblock %}
</div>
<div class="content">
{{ parent() }}
</div>
<div class="footer">
{% block footer %}
Skeleton footer
{% endblock %}
</div>
{% endblock %}
\ No newline at end of file
import {TestCase} from "../test-case";
import {runTestSuite} from "../test-suite";
import {createEnvironment, createFilesystemLoader} from "twing";
import fs from "fs";
class ExtendsTestCase extends TestCase {
get entry() {
return __dirname + '/entry.js';
}
get expected() {
return ` <div class="header">
Skeleton header
</div>
<div class="content">
BAR
</div>
<div class="footer">
Custom footer
<div class="header">
Skeleton header
</div>
<div class="content">
BAR
</div>
<div class="footer">
Skeleton footer
</div>
</div>
`;
}
}
const environment = createEnvironment(createFilesystemLoader(fs));
runTestSuite('embed and extends tag altogether', [
new ExtendsTestCase(environment),
]);
import {TestCase} from "../test-case";
import {TestSuite} from "../test-suite";
import {runTestSuite} from "../test-suite";
import {createEnvironment, createFilesystemLoader} from "twing";
import fs from "fs";
......@@ -16,6 +16,6 @@ BAR`;
const environment = createEnvironment(createFilesystemLoader(fs));
(new TestSuite('embed tag', [
runTestSuite('embed tag', [
new EmbedTestCase(environment),
])).run();
]);
import {TestCase} from "../test-case";
import {TestSuite} from "../test-suite";
import {runTestSuite} from "../test-suite";
import {createEnvironment, createFilesystemLoader} from "twing";
import fs from "fs";
......@@ -16,6 +16,6 @@ class ExtendsTestCase extends TestCase {
const environment = createEnvironment(createFilesystemLoader(fs));
(new TestSuite('extends tag', [
runTestSuite('extends tag', [
new ExtendsTestCase(environment),
])).run();
]);
import {TestCase} from "../test-case";
import {TestSuite} from "../test-suite";
import {runTestSuite} from "../test-suite";
import {createEnvironment, createFilesystemLoader} from "twing";
import fs from "fs";
......@@ -16,6 +16,6 @@ class FromTestCase extends TestCase {
const environment = createEnvironment(createFilesystemLoader(fs));
(new TestSuite('from tag', [
runTestSuite('from tag', [
new FromTestCase(environment)
])).run();
]);
import {TestCase} from "../test-case";
import {TestSuite} from "../test-suite";
import {runTestSuite} from "../test-suite";
import {createEnvironment, createFilesystemLoader} from "twing";
import fs from "fs";
......@@ -17,6 +17,6 @@ class ImportTestCase extends TestCase {
const environment = createEnvironment(createFilesystemLoader(fs));
(new TestSuite('import tag', [
runTestSuite('import tag', [
new ImportTestCase(environment)
])).run();
]);
import {TestCase} from "../../test-case";
import {TestSuite} from "../../test-suite";
import {runTestSuite} from "../../test-suite";
import {createEnvironment, createFilesystemLoader} from "twing";
import fs from "fs";
......@@ -21,6 +21,6 @@ FOO`;
const environment = createEnvironment(createFilesystemLoader(fs));
(new TestSuite('include function', [
runTestSuite('include function', [
new IncludeFunctionTestCase(environment)
])).run();
]);
import {TestCase} from "../../test-case";
import {TestSuite} from "../../test-suite";
import {runTestSuite} from "../../test-suite";
import {createEnvironment, createFilesystemLoader} from "twing";
import fs from "fs";
......@@ -17,6 +17,6 @@ BAR`;
const environment = createEnvironment(createFilesystemLoader(fs));
(new TestSuite('include tag', [
runTestSuite('include tag', [
new IncludeTagTestCase(environment)
])).run();
]);
import {TestCase} from "../test-case";
import {TestSuite} from "../test-suite";
import {runTestSuite} from "../test-suite";
import {createEnvironment, createFilesystemLoader} from "twing";
import fs from "fs";
......@@ -21,6 +21,6 @@ BAR FROM THE ARRAY LOADER
const environment = createEnvironment(createFilesystemLoader(fs));
(new TestSuite('with an array loader', [
runTestSuite('with an array loader', [
new ArrayLoaderTestCase(environment)
])).run();
]);
......@@ -2,7 +2,7 @@ import {TestCase} from "../../test-case";
import {Test} from "tape";
import {resolve as resolvePath} from "path";
import MemoryFileSystem = require("memory-fs");
import {TestSuite} from "../../test-suite";
import {runTestSuite} from "../../test-suite";
import {createEnvironment, createFilesystemLoader} from "twing";
import fs from "fs";
......@@ -57,6 +57,6 @@ class HtmlWebpackPluginTestCase extends TestCase {
const environment = createEnvironment(createFilesystemLoader(fs));
(new TestSuite('coupled with HtmlWebpackPlugin', [
runTestSuite('coupled with HtmlWebpackPlugin', [
new HtmlWebpackPluginTestCase(environment),
])).run();
]);
import {TestCase} from "../test-case";
import {TestSuite} from "../test-suite";
import {runTestSuite} from "../test-suite";
import {createEnvironment, createFilesystemLoader} from "twing";
import fs from "fs";
......@@ -20,6 +20,6 @@ FOO 2`;
const environment = createEnvironment(createFilesystemLoader(fs));
(new TestSuite('source function', [
runTestSuite('source function', [
new SourceFunctionTestCase(environment)
])).run();
]);
import {TestCase} from "./test-case";
import {Test} from "tape";
import tape = require("tape");
import tape from "tape";
export class TestSuite {
private readonly _name: string;
private readonly _testCases: TestCase[];
export const runTestSuite = (
name: string,
testCases: Array<TestCase>
) => {
tape(name, (test) => {
const promises: Promise<void>[] = [];
constructor(name: string, testCases: TestCase[]) {
this._name = name;
this._testCases = testCases;
}
run() {
tape(this._name, (test: Test) => {
let promises: Promise<void>[] = [];
for (let testCase of this._testCases) {
for (const testCase of testCases) {
promises.push(testCase.run(test));
}
return Promise.all(promises)
.then(() => {
test.end();
})
.catch(() => {
test.end();
})
.finally(test.end)
.then();
});
}
}
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment