A Rollup plugin for seamless integration between Rollup and Twig, fueled by Twing.
This plugin requires at least Twing 6.0.0.
The recommended way to install the package is via npm:
npm install @nightlycommit/rollup-plugin-twig --save-dev
Note that Twing is a peer dependency of this plugin that needs to be installed separately.
Create a Rollup configuration file, import the plugin factory and add an instance to the list of plugins
:
// rollup.config.mjs
import {createEnvironment, createFilesystemLoader} from 'twing';
import createTwigPlugin from '@nightlycommit/rollup-plugin-twig';
import * as fs from "fs";
const environment = createEnvironment(createFilesystemLoader(fs));
export default {
input: 'src/index.js',
output: {
dir: 'output'
},
plugins: [
createTwigPlugin(environment)
]
};
Then, in your JavaScript sources, import Twig files and execute the render
method of the exported module:
import {render} from "../templates/index.twig";
render({}).then(console.log);
Note that to import Twig files in TypeScript sources, you need to make the compiler aware of the existence of the *.twig
type by either referencing the type in the code...
/// <reference types="@nightlycommit/rollup-plugin-twig" />
import {render} from "../templates/index.twig";
render({}).then(console.log);
...or by adding <path to node_modules>/@nightlycommit/rollup-plugin-twig
to the typeRoots
entry of your TypeScript configuration, as explained there.
type PluginFactory = (environment: TwingEnvironment, options?: {
exclude?: string | Array<string>;
include?: string | Array<string>;
}) => Plugin;
An instance of TwingEnvironment
capable of loading and parsing the templates.
A picomatch pattern, or array of patterns, which specifies the files in the build the plugin should ignore. By default, no files are ignored.
A picomatch pattern, or array of patterns, which specifies the files in the build the plugin should operate on. By default, all .twig
files are targeted.
Twig files imported using this plugin are exposed to your code in the form of templates with the following signature:
import type {TwingTemplate} from "twing";
interface Template {
render: TwingTemplate["render"];
}
Please refer to Twing's documentation for more information.