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

ejs-precompile

v1.0.0

Published

Precompile EJS templates to JS functions

Downloads

77

Readme

EJS Precompile

Build & Test codecov Known Vulnerabilities npm version

This package is a CLI tool to precompile EJS templates into JavaScript functions. Additionally, it provides a Node.js API to precompile EJS templates.

Installation

npm install -g ejs-precompile

or locally in your project

npm install --save-dev ejs-precompile

Samples

Please check samples directory for more details.

Template Variables

Template variables can be passed to the template function as an object. The object can be passed as a first argument to the template function.

Variables can be accessed in the template using locals object.

const renderedTemplate = template.templateFunction({
    name: 'John',
});

Default Values

Default values for template variables can be passed to the precompile function as an object.

const template = await precompileTemplate({
    templateString: 'Hello, <%= locals.name %>!',
    defaults: {
        name: 'John',
    },
});
const renderedTemplate = template.templateFunction();

CLI Usage

ejs-precompile [options]

Options

| Option | Description | Default | |---------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------| | -i, --input | Input file or directory. | current working directory | | -o, --output | Output file or directory. Is has to be directory when input is directory. | current working directory | | -f, --file | Output file name template, to be used together when input is directory.Available variables: [name, ext, lang-ext].- [name] - The name of the input file without extension.- [ext] - The extension of the input file.- [lang-ext] - The extension of the output file based on the language. | [name].template[lang-ext] | | -l, --language | Language of the output file. Default is "javascript".Available languages: [javascript, typescript]. | javascript | | --ejs.client | Returns standalone compiled function.Please check EJS documentation for more details. | true | | --ejs.strict | When set to true, generated function is in strict mode.Please check EJS documentation for more details. | true | | -h, --help | display help for command | |

API Usage

Precompile EJS template from file precompileFile

import { precompileFile } from 'ejs-precompile';

const template = await precompileFile({
    inputPath: inputPath,
    outputPath: outputPath,
    compileOptions: {
        strict: true,
        client: true,
    },
});
const renderedTemplate = template.templateFunction();

Precompile EJS templates from directory precompileDirectory

import { precompileDirectory } from 'ejs-precompile';

const templates = await precompileDirectory({
    inputPath: inputPath,
    outputPath: outputPath,
    compileOptions: {
        strict: true,
        client: true,
    },
});

for (const template of templates) {
    const renderedTemplate = template.templateFunction();
    // do something with renderedTemplate
}

Precompile EJS template from string precompileTemplate

import { precompileTemplate } from 'ejs-precompile';

const template = await precompileTemplate({
    templateString: 'Hello, world!',
    compileOptions: {
        strict: true,
        client: true,
    },
});

const renderedTemplate = template.templateFunction();

API

EjsCompileOptions

See "compile" function from EJS documentation

export type EjsCompileOptions = Parameters<typeof compile>[1];

precompileTemplate

Options:

export type PrecompileTemplateOptions = {
    /**
     * Path to the input file. Optional, works together with `outputPath`.
     */
    inputPath?: string | undefined;

    /**
     * Path to the output file. Optional, works together with `inputPath`.
     */
    outputPath?: string | undefined;

    /**
     * EJS template content.
     */
    inputContent: string;

    /**
     * EJS compile options.
     */
    compileOptions: EjsCompileOptions;

    /**
     * Default values for template variables.
     */
    defaults?: Record<string, unknown>;

    /**
     * Precompile options.
     */
    options?: PrecompileOptions;
};

Returns:

export type PrecompiledTemplate = {
    /**
     * EJS template content.
     */
    inputContent: string;

    /**
     * Generated JavaScript module content.
     */
    outputContent: string;

    /**
     * Compiled template function.
     */
    templateFunction: ReturnType<typeof compile>;
};

precompileFile

Options:

export type PrecompileFileOptions = {
    /**
     * Path to the input file. If relative, it will be resolved relative to the current working directory.
     */
    inputPath: string;

    /**
     * Path to the output file. If relative, it will be resolved relative to the current working directory.
     */
    outputPath: string;

    /**
     * EJS compile options.
     */
    compileOptions: EjsCompileOptions;

    /**
     * If `true`, the output file will be written to the file system.
     */
    write?: boolean;

    /**
     * Default values for template variables.
     */
    defaults?: Record<string, unknown>;

    /**
     * Precompile options.
     */
    options?: PrecompileOptions;
};

Returns:

export type PrecompiledFile = PrecompiledTemplate & {
    /**
     * Absolute path to the input file.
     */
    inputPath: string;

    /**
     * Absolute path to the output file.
     */
    outputPath: string;
};

precompileDirectory

Options:

export type PrecompileDirectoryOptions = {
    /**
     * Path to the input directory. If relative, it will be resolved relative to the current working directory.
     */
    inputPath: string;

    /**
     * Path to the output directory. If relative, it will be resolved relative to the current working directory.
     */
    outputPath: string;

    /**
     * Output file name template, to be used together when input is directory. Available variables: [name, ext, lang-ext].
     * @default '[name].template[lang-ext]'
     */
    fileNameTemplate?: string;

    /**
     * EJS compile options.
     */
    compileOptions: EjsCompileOptions;

    /**
     * If `true`, the output files will be written to the file system.
     */
    write?: boolean;

    /**
     * Default values for template variables.
     */
    defaults?: Record<string, unknown>;

    /**
     * Precompile options.
     */
    options?: PrecompileOptions;
};

Returns:

Array of PrecompiledFile