Commit 88eee144 authored by Eric MORAND's avatar Eric MORAND
Browse files

Resolve issue #44

parent 6ff3cbe2
Loading
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@
  ],
  "license": "ISC",
  "peerDependencies": {
    "twing": "^6.1.0-beta.2"
    "twing": "^7.0.0-beta.1"
  },
  "repository": {
    "type": "git",
@@ -58,7 +58,7 @@
    "stream-browserify": "^3.0.0",
    "tape": "^5.7.5",
    "ts-node": "^10.9.2",
    "twing": "^6.1.0-beta.2",
    "twing": "^7.0.0-beta.1",
    "typescript": "^5.4.2",
    "webpack": "^5.90.3"
  }
+9 −17
Original line number Diff line number Diff line
@@ -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);
    });
+1 −1
Original line number Diff line number Diff line
@@ -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);
            }

+3 −3
Original line number Diff line number Diff line
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();
]);
+9 −0
Original line number Diff line number Diff line
import template from './index.twig';
import {createEnvironment, createArrayLoader} from "twing";

export default template.render(
  createEnvironment(createArrayLoader({})),
  {
      foo: 'BAR'
  }
);
 No newline at end of file
Loading