Skip to content

Commit

Permalink
feat(core): Add support for functions and globalContext
Browse files Browse the repository at this point in the history
* Added support for Drupal functions

* Added support for Drupal functions - formatting issue fixed

* Add tests, use global context instead

---------

Co-authored-by: Lee Rowlands <lee.rowlands@previousnext.com.au>
  • Loading branch information
psebborn and larowlan committed Jan 8, 2024
1 parent 9e7de33 commit 1bb619b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 deletions.
21 changes: 18 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const defaultOptions = {
namespaces: {},
filters: {},
functions: {},
globalContext: {},
framework: FRAMEWORK_HTML,
pattern: /\.(twig)(\?.*)?$/,
}
Expand Down Expand Up @@ -113,9 +114,11 @@ const plugin = (options = {}) => {
}
let embed,
embeddedIncludes,
functions,
code,
includes,
seen = []

try {
const result = await compileTemplate(id, id, options).catch(
errorHandler(id)
Expand Down Expand Up @@ -160,6 +163,16 @@ const plugin = (options = {}) => {
)}';`
)
.join("\n")

functions = Object.entries(options.functions)
.map(([name, value]) => {
return `
const ${name} = ${value};
${name}(Twig);
`
})
.join("\n")

const includeResult = await Promise.all(includePromises).catch(
errorHandler(id)
)
Expand All @@ -176,10 +189,11 @@ const plugin = (options = {}) => {
import DrupalAttribute from 'drupal-attribute';
import { addDrupalExtensions } from 'drupal-twig-extensions/twig';
${frameworkInclude}
${embed}
addDrupalExtensions(Twig);
${functions}
// Disable caching.
Twig.cache(false);
Expand All @@ -191,7 +205,8 @@ const plugin = (options = {}) => {
${includes ? `component.options.allowInlineIncludes = true;` : ""}
try {
return frameworkTransform(component.render({
attributes: new DrupalAttribute(),
attributes: new DrupalAttribute(),
...${JSON.stringify(options.globalContext)},
...context
}));
}
Expand Down
36 changes: 34 additions & 2 deletions tests/__snapshots__/smoke.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Basic smoke test > Should support global context and functions 1`] = `
"<section>
<h1>Include</h1>
<article>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
</article>
</section>
<section>
<h1>Embed</h1>
<article>
Lorem ipsum dolor sit amet.
<button class=\\"button--primary\\">Nested include</button>
IT WORKS!
</article>
</section>
<section>
<h1></h1>
<article>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
</article>
</section>
<section>
<h1>Relative include</h1>
<article>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
</article>
</section>
"
`;
exports[`Basic smoke test > Should support includes 1`] = `
"<section>
<h1>Include</h1>
Expand All @@ -12,7 +42,8 @@ exports[`Basic smoke test > Should support includes 1`] = `
<article>
Lorem ipsum dolor sit amet.
<button class=\\"button--primary\\">Nested include</button>
</article>
IT WORKS!
</article>
</section>
<section>
<h1></h1>
Expand Down Expand Up @@ -58,7 +89,8 @@ exports[`Basic smoke test > Should support variables 1`] = `
<article>
Lorem ipsum dolor sit amet.
<button class=\\"button--primary\\">Nested include</button>
</article>
IT WORKS!
</article>
</section>
<section>
<h1>Pickle Fixie</h1>
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/mockup.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
{% block content %}
Lorem ipsum dolor sit amet.
{% include "@tests/button.twig" with {text: 'Nested include'} %}
{% if active_theme == 'poodles' %}
{{ testFunction() }}
{% endif %}
{% endblock %}
{% endembed %}
{% include "@tests/section.twig" %}
Expand Down
6 changes: 6 additions & 0 deletions tests/smoke.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@ describe("Basic smoke test", () => {
expect(markup).toContain("Contact")
expect(markup).toMatchSnapshot()
})
it("Should support global context and functions", () => {
const markup = Markup()
expect(markup).toMatchSnapshot()
expect(markup).toContain("Nested include")
expect(markup).toContain("IT WORKS!")
})
})
7 changes: 7 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ export default defineConfig({
},
plugins: [
twig({
globalContext: {
active_theme: "poodles",
},
functions: {
testFunction: (instance) =>
instance.extendFunction("testFunction", () => "IT WORKS!"),
},
namespaces: {
tests: join(__dirname, "/tests/fixtures"),
},
Expand Down

0 comments on commit 1bb619b

Please sign in to comment.