npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mediamonks/twing-loader

v5.0.0-alpha.2

Published

Webpack loader for Twig templates, based on Twing.

Downloads

3

Readme

twing-loader

Webpack loader for Twig templates, based on Twing.

Modified for usage in Muban, to work with a more modern webpack and project setup (mostly ES Modules).

Prerequisites

  • Webpack 4
  • Twing 5.0.2

Installation

npm install -D twing-loader

Usage

twing-loader comes with two available behaviors. Depending on your need, you can use one or the other by setting the renderContext or environmentModulePathContent option accordingly.

Render at runtime

By default, twing-loader transforms a Twig template to a function that, when called with some optional data, renders the template:

webpack.config.js

module.exports = {
    entry: 'index.js',
    // ...
    module: {
        rules: [
            {
                test: /\.twig$/,
                use: [
                    {
                        loader: 'twing-loader',
                        options: {
                            // must be commonjs to allow the webpack require chain to work
                            environmentModulePathLoader: require.resolve('./environment.cjs'),
                            // should probably be a ES module when used in modern projects, is injected in the bundled code
                            // only needed when not providing a renderContext
                            environmentModulePathContent: require.resolve('./environment.js')
                        }
                    }
                ]
            }
        ]
    }
}

environment.js

const {TwingEnvironment, TwingLoaderRelativeFilesystem} = require("twing");

module.exports = new TwingEnvironment(
    new TwingLoaderRelativeFilesystem()
);

index.twig

{{ foo }}

index.js

let template = require('./index.twig');

template({
    foo: 'bar'
}).then((renderedTemplate) => {
    // "bar" 
});

This behavior, known as render at runtime, comes at the cost of having Twing as part of the bundle.

Render at compile time

When renderContext is defined, twing-loader transforms a Twig template to the result of the template rendering:

webpack.config.js

module.exports = {
    entry: 'index.js',
    // ...
    module: {
        rules: [
            {
                test: /\.twig$/,
                use: [
                    {
                        loader: 'twing-loader',
                        options: {
                            environmentModulePathLoader: require.resolve('./environment.js'),
                            // when passing a renderContext, it renders everything in node
                            // it doesn't generate any runtime template code, so `environmentModulePathContent` is not needed
                            renderContext: {
                                foo: 'bar'
                            }
                        }
                    }
                ]
            }
        ]
    }
}

environment.js

const {TwingEnvironment, TwingLoaderRelativeFilesystem} = require("twing");

module.exports = new TwingEnvironment(
    new TwingLoaderRelativeFilesystem()
);

index.twig

{{ foo }}

index.js

require('./index.twig').then((renderedTemplate) => {
    // "bar"
});

This second behavior, known as render at compile time, comes with the benefit of not having Twing as part of the bundle.

Options

|Name|Required|Type|Default|Description| |---|:---:|:---:|:---:|---| |environmentModulePathLoader|true|string|undefined| The absolute path or the identifier to the module that exports the TwingEnvironment instance that will be used by the loader to compile the templates at compile time.| |environmentModulePathContent|false|string|undefined| The absolute path or the identifier to the module that exports the TwingEnvironment instance that will be used by the loader to compile the templates in the bundle to render them at runtime. Only optional when passing a renderContext, otherwise it's required.| |renderContext|false|any|undefined|If different from undefined, enables the render at compile time behavior and serves as context for the rendering of the templates.|