GithubHelp home page GithubHelp logo

isabella232 / module-loader Goto Github PK

View Code? Open in Web Editor NEW

This project forked from firstandthird/module-loader

0.0 0.0 0.0 146 KB

Front end module loader

License: MIT License

JavaScript 95.87% Nunjucks 4.13%

module-loader's Introduction

module-loader

NPM

Helper that automatically loads nunjucks modules (view, schema and example.js for using the template.

Installation

npm install @firstandthird/module-loader

or

yarn add @firstandthird/module-loader

Usage:

const moduleLoader = require('@firstandthird/module-loader');

const modules = await moduleLoader({
  path: `/modules`
});

const theModule = modules['my_module'];

const output = theModule.render({ val1: 'hello', val2: 'world' });

Each module in the returned set of modules will contain the following methods and properties:

  • renderRaw()

    A function that takes in the context to apply to the view, attempts to validate the context and then returns the rendered string, eg:

    theModule.renderRaw({ val1: 'hello', val2: 'world' });

    renderRaw() will log an error message if the validation check fails, but it has no error handling. If there is an error rendering the view then it will throw the error and you will need to handle it yourself in code.

  • render()

    is a wrapper for the renderRaw method that will attempt to safely render and handle any errors for you:

    theModule.render({ val1: 'hello', val2: 'world' });

  • getExamples()

    This will render and return an example of the template, along with the example data used to render it and any config data supplied to the module.

  • fullPath

    The full path to the module

  • name

    The name of the module

  • exampleData

    This will return an array containing the config and any example data used by the getExamples() function.

Module Format

A module is a sub-directory consisting of 3 parts:

  • view.njk

    A nunjucks .njk file containing the template to be rendered:

    <div>
      This is an example
      <h1>{{ val1 }}</h1>
      <p>{{ val2 }}</p>
    </div>
  • schema.js

    A Joi schema validator, this will be applied to the context before the view is rendered:

    const Joi = require('joi');
    module.exports = {
      val1: Joi.string().max(100),
      val2: Joi.string()
    };

_ example.js

A sample context and config in the form:

module.exports = [
  {
    _config: {
      wrapStart: '<div class="container">',
      wrapEnd: '</div>',
      name: 'main'
    },
    val1 'example 1',
    val2 'example 2'
  },
  {
    val1 'example 1',
    val2 'example 2'
  }
];

Additional Options

  • path (required)

    The path to search for modules

  • env

    A nunjucks compile environment that will be passed to the nunjucks compile() method.

  • debug

    When true this prints additional information when loading modules, default is false.

  • log

    A function that module-loader will use to print logging and debug information. Default is console.log().


A First + Third Project

module-loader's People

Contributors

dawnerd avatar ecwillis avatar jgallen23 avatar orthagonal 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.