Registry / web-framework / twigjs-loader

twigjs-loader

JSON →
library1.0.3jsnpmunverified

Webpack loader for compiling Twig.js templates. Current stable version is 1.0.3, released with support for Node.js >=8. It integrates Twig templates into webpack builds, allowing template imports in JavaScript and TypeScript. Key differentiators: provides an Express view engine helper, supports Hot Module Replacement, and offers a customizable renderTemplate option for advanced compilation control. Ships TypeScript type definitions. Peer dependency on twig 1.x.

npm install twigjs-loader
INSTALL
IMPORT
SIG · TWIGJS-LOADER
T
twigjs-loader
web-frameworkjavascriptv1.0.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

twigjs-loader (default)
import indexView from './index.twig'
const indexView = require('./index.twig')
ESM import works with webpack module rules; CommonJS require also works but not recommended for consistency.
ExpressView
import { ExpressView } from 'twigjs-loader'
const ExpressView = require('twigjs-loader').ExpressView
ESM named export; CommonJS require is also valid but less idiomatic.
renderTemplate option
use: { loader: 'twigjs-loader', options: { renderTemplate(twigData, dependencies, isHot) { ... } } }
use: { loader: 'twigjs-loader', options: { renderTemplate: (twigData, dependencies, isHot) => { ... } } }
Both arrow and regular functions work; ensure the function returns a string of JavaScript code.

Basic setup: webpack config with twigjs-loader, importing a Twig template in ESM, and rendering it with data.

// webpack.config.js module.exports = { module: { rules: [ { test: /\.twig$/, use: { loader: 'twigjs-loader', options: { renderTemplate(twigData, dependencies, isHot) { return ` ${dependencies} var twig = require("twig").twig; var tpl = twig(${JSON.stringify(twigData)}); module.exports = function(context) { return tpl.render(context); }; `; }, }, }, }, ], }, }; // index.js import indexView from './index.twig'; document.body.innerHTML = indexView({ name: 'World' }); // index.twig <h1>Hello {{ name }}!</h1>
Debug
Known issues
gotchaRequires peer dependency 'twig' version 1.x - must be installed separately.
fix
Run 'npm install twig@1' alongside twigjs-loader.
affects: >=1.0.0
gotchaTemplate path resolution is delegated to webpack; custom namespaces require additional webpack resolve configuration.
fix
Configure webpack resolve.alias or resolve.modules for custom template directories.
affects: >=1.0.0
gotchaExpressView helper requires twigjs-loader to be in the same project; not a separate export.
fix
Import { ExpressView } from 'twigjs-loader' and set app.set('view', ExpressView).
affects: >=1.0.0
Errors
Common errors & fixes
Module not found: Error: Can't resolve 'twig' in '...'
Missing peer dependency 'twig'.
fix
npm install twig@1
Error: Template not found: ... .twig
Webpack cannot resolve the template path relative to the importing file.
fix
Use absolute paths or configure webpack resolve.modules/resolve.alias.
TypeError: Cannot read property 'render' of undefined
The twig object is not initialized correctly in custom renderTemplate.
fix
Ensure 'require("twig").twig' returns a function and that twigData is valid.
Upgrade
Version history
1.0.3latest on npm
Audit
Dependencies
twigrequiredRuntime dependency for template rendering; must be installed separately (peer dependency).
Agent activity
10 hits · last 30 days
node
10
Resources
twigjs-loader — npm install twigjs-loader · libregistry