Skip to content

Rendering precompiled templates on the server, runtime export #1986

Description

@terrablue

Hi, I'm trying to precompile a template at build time and then render it at runtime (the data could be different every time) on the server.

I'm currently using this code:

import handlebars from "handlebars";
import runtime from "./node_modules/handlebars/dist/handlebars.runtime.js";

await write_to_path("./precompiled.js", `export default ${handlebars.precompile("{{foo}}")}`);
import precompiled from "./precompiled.js";
console.log(runtime.template(precompiled)({foo: "bar"}));
// -> bar

This works perfectly for me, I was wondering if it were possible to expose the runtime per a package.json export, to avoid me reaching directly into the node_modules directory? Perhaps something like,

import {default as handlebars, runtime} from "handlebars";

await write_to_path("./precompiled.js", `export default ${handlebars.precompile("{{foo}}")}`);
import precompiled from "./precompiled.js";
console.log(runtime.template(precompiled)({foo: "bar"}));
// -> bar

Referencing #955, which seems to suggest a similar way, but does not specify how to get the runtime on the server.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions