Giter Site home page Giter Site logo

mediamonks / twing-loader Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nightlycommit/twing-loader

0.0 2.0 0.0 128 KB

Fork of the official twing-loader. Making it work properly, and better integrate with our Muban 2 Skeleton.

JavaScript 6.16% TypeScript 89.74% Twig 4.11%

twing-loader's Introduction

twing-loader

NPM version Build Status Coverage percentage

Webpack loader for Twig templates, based on Twing.

Prerequisites

  • Webpack 4
  • Twing 5.0.2

Installation

npm install 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 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: {
                            environmentModulePath: 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: {
                            environmentModulePath: require.resolve('./environment.js'),
                            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
environmentModulePath 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 (and render) the templates at compile time and in the bundle to render them at runtime.
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.

Contributing

  • Fork this repository
  • Code
  • Implement tests using tape
  • Issue a pull request keeping in mind that all pull requests must reference an issue in the issue queue

twing-loader's People

Contributors

ericmorand avatar nicolasrdr avatar derekroth avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.