CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-dustjs-linkedin

Asynchronous templates for the browser and server (LinkedIn fork)

Overview
Eval results
Files

cli.mddocs/

Command Line Interface

Command-line tools for template compilation, file watching, and build integration through the dustc binary.

Capabilities

Basic Usage

The dustc command-line compiler for pre-compiling Dust templates to JavaScript.

# Basic command syntax
dustc [options] [path1 [path2 path3...]]

# Installation provides global binary
npm install dustjs-linkedin
# Binary available at: node_modules/.bin/dustc

Usage Examples:

# Compile single template
dustc templates/user.dust

# Compile multiple templates
dustc templates/header.dust templates/footer.dust templates/main.dust

# Compile all templates in directory
dustc templates/*.dust

# Use glob patterns
dustc templates/**/*.dust

Command Options

Comprehensive set of options for controlling compilation behavior and output format.

# Template naming
-n, --name <string>         # Template name for single file compilation

# Output control
-o, --output <path>         # Concatenate all output to this file
-s, --split                 # Create separate output files per input

# Path handling
--pwd <string>              # Base directory for template names

# Formatting
-w, --whitespace           # Preserve whitespace in templates

# Module formats
-a, --amd                  # Output AMD modules
--cjs                      # Output CommonJS modules (auto-enables --split)

# Development
--watch                    # Watch files for changes and recompile

Usage Examples:

# Compile with custom name
dustc -n "greeting" templates/hello.dust

# Concatenate multiple files
dustc --output=compiled.js templates/*.dust

# Create separate output files
dustc --split templates/*.dust

# Set base directory for template names
dustc --pwd=templates templates/user/*.dust

# Preserve whitespace
dustc -w templates/formatted.dust

# Generate AMD modules
dustc -a --split templates/*.dust

# Generate CommonJS modules (automatically splits)
dustc --cjs templates/*.dust

# Watch for changes during development
dustc --watch --split templates/**/*.dust

Standard Input Support

Support for piping template content through stdin for integration with build tools.

# Read from stdin
echo "Hello {name}!" | dustc -n "greeting"

# Pipe from other commands
cat template.dust | dustc -n "template"

# No template name (creates anonymous template)
echo "{message}" | dustc

Usage Examples:

# Compile template from stdin
echo "Welcome {user.name}!" | dustc -n "welcome" > welcome.js

# Process template through pipeline
curl https://templates.example.com/header.dust | dustc -n "header" -a > header-amd.js

# Batch processing with build tools
find templates -name "*.dust" -exec cat {} \; | dustc -n "combined"

Output Formats

Default JavaScript Output

Standard compiled JavaScript output format for direct loading.

# Default output format
dustc templates/example.dust

# Produces JavaScript like:
# (function(){dust.register("example",body_0);function body_0(chk,ctx){...}})();

Usage Examples:

# Compile to default format
dustc templates/user-profile.dust > user-profile.js

# Load in Node.js
dust.loadSource(fs.readFileSync('user-profile.js', 'utf8'));

# Load in browser
<script src="user-profile.js"></script>

AMD Module Output

Generate AMD (Asynchronous Module Definition) compatible modules.

# Generate AMD modules
dustc -a templates/*.dust

# Produces AMD modules like:
# define("dust-template-name", ["dust"], function(dust) {
#   dust.register("template-name", body_0);
#   function body_0(chk,ctx){...}
#   return "template-name";
# });

Usage Examples:

# Generate AMD modules with split output
dustc -a --split templates/*.dust

# Use with RequireJS
require(['dust-template-user'], function(templateName) {
  dust.render(templateName, userData, callback);
});

# Build system integration
dustc -a --output=dist/templates.js src/templates/**/*.dust

CommonJS Module Output

Generate CommonJS modules for Node.js and bundlers.

# Generate CommonJS modules (automatically enables --split)
dustc --cjs templates/*.dust

# Produces CommonJS modules like:
# var dust = require("dustjs-linkedin");
# dust.register("template-name", body_0);
# function body_0(chk,ctx){...}
# module.exports = "template-name";

Usage Examples:

# Generate CommonJS modules
dustc --cjs templates/*.dust

# Use in Node.js
const templateName = require('./templates/user.js');
dust.render(templateName, userData, callback);

# Use with bundlers (Webpack, Browserify)
const userTemplate = require('./templates/user.js');

File Watching

Development Mode

Watch templates for changes and automatically recompile during development.

# Enable file watching
dustc --watch [other-options] templates/**/*.dust

# Watch with specific output configuration
dustc --watch --split --output-dir=compiled templates/*.dust

Usage Examples:

# Watch and recompile on changes
dustc --watch --split templates/**/*.dust

# Watch with AMD output
dustc --watch -a --split templates/*.dust

# Development server integration
dustc --watch --cjs --output=dist/templates/ src/templates/**/*.dust &
node server.js

Template Naming

Automatic Naming

How dustc automatically generates template names from file paths.

# Automatic naming rules:
# templates/user/profile.dust -> "user/profile"
# src/views/header.dust -> "views/header"
# main.dust -> "main"

# Control base path with --pwd
dustc --pwd=templates templates/user/profile.dust
# Results in template name: "user/profile"

dustc --pwd=src src/views/header.dust
# Results in template name: "views/header"

Usage Examples:

# Default naming (includes directory structure)
dustc templates/components/header.dust
# Template name: "templates/components/header"

# Set base directory
dustc --pwd=templates templates/components/header.dust
# Template name: "components/header"

# Single file with explicit name
dustc -n "app-header" templates/components/header.dust
# Template name: "app-header"

Custom Naming

Override automatic naming with explicit template names.

# Single template with custom name
dustc -n "custom-name" template.dust

# Note: --name only works with single input file
# For multiple files, use --pwd to control automatic naming

Integration Examples

Build System Integration

Examples of integrating dustc with various build systems.

# Makefile integration
templates: $(DUST_FILES)
	dustc --split --output-dir=dist/templates/ src/templates/**/*.dust

# NPM scripts
{
  "scripts": {
    "build:templates": "dustc --cjs --split src/templates/**/*.dust",
    "dev:templates": "dustc --watch --cjs --split src/templates/**/*.dust",
    "build": "npm run build:templates && webpack"
  }
}

# Gulp integration
gulp.task('templates', () => {
  return exec('dustc --amd --split src/templates/**/*.dust');
});

# Webpack integration (as npm script)
"prebuild": "dustc --cjs --split src/templates/**/*.dust"

CI/CD Pipeline Integration

Using dustc in continuous integration and deployment pipelines.

# GitHub Actions
- name: Compile Dust templates
  run: |
    dustc --cjs --split src/templates/**/*.dust
    ls -la templates/

# Docker build
RUN npm install dustjs-linkedin && \
    dustc --cjs --split templates/**/*.dust && \
    rm -rf node_modules

# Production build script
#!/bin/bash
set -e
echo "Compiling Dust templates..."
dustc --amd --output=dist/templates.js src/templates/**/*.dust
echo "Templates compiled successfully"

Error Handling and Debugging

Compilation Errors

How dustc reports template compilation errors.

# Syntax errors include file and line information
dustc templates/broken.dust
# Error: Template syntax error in templates/broken.dust:5:12
# Expected closing tag but found EOF

# Missing file errors
dustc non-existent.dust
# Error: ENOENT: no such file or directory, open 'non-existent.dust'

Verbose Output

Getting detailed information during compilation.

# Use Node.js DEBUG environment variable
DEBUG=dust dustc templates/*.dust

# Enable verbose logging
NODE_ENV=development dustc templates/*.dust

# Check compiled output
dustc templates/test.dust && cat test.js

Common Issues and Solutions

Troubleshooting common dustc usage problems.

# Issue: Templates not found at runtime
# Solution: Ensure template names match registration
dustc --pwd=templates templates/user.dust  # Creates "user" template
dust.render("user", data, callback);       # Correct reference

# Issue: Module format compatibility
# Solution: Match output format to usage environment
dustc --cjs templates/*.dust              # For Node.js/bundlers
dustc --amd templates/*.dust              # For RequireJS
dustc templates/*.dust                    # For direct browser loading

# Issue: File watching not working
# Solution: Use glob patterns and check file permissions
dustc --watch "templates/**/*.dust"       # Quote glob patterns

Advanced Usage

Custom Build Scripts

Creating custom build scripts that use dustc.

#!/usr/bin/env node

const { exec } = require('child_process');
const fs = require('fs');
const path = require('path');

// Custom build script using dustc
function buildTemplates() {
  const templateDir = 'src/templates';
  const outputDir = 'dist/templates';

  // Ensure output directory exists
  if (!fs.existsSync(outputDir)) {
    fs.mkdirSync(outputDir, { recursive: true });
  }

  // Compile templates
  const command = `dustc --cjs --split ${templateDir}/**/*.dust`;

  exec(command, (error, stdout, stderr) => {
    if (error) {
      console.error('Template compilation failed:', error);
      process.exit(1);
    }

    console.log('Templates compiled successfully');
    console.log(stdout);
  });
}

buildTemplates();

Template Preprocessing

Using dustc with template preprocessing pipelines.

# Preprocess templates before compilation
for file in templates/*.dust; do
  # Apply preprocessing (example: variable substitution)
  sed 's/{{VERSION}}/'$VERSION'/g' "$file" > "processed/$(basename $file)"
done

# Compile processed templates
dustc --split processed/*.dust

# Clean up
rm -rf processed/

Performance Optimization

Optimizing dustc usage for large projects.

# Parallel compilation for large projects
find templates -name "*.dust" -print0 | xargs -0 -n 10 -P 4 dustc --split

# Incremental compilation (only changed files)
find templates -name "*.dust" -newer dist/templates.json -exec dustc --split {} +

# Memory optimization for large template sets
NODE_OPTIONS="--max-old-space-size=4096" dustc templates/**/*.dust

Install with Tessl CLI

npx tessl i tessl/npm-dustjs-linkedin

docs

cli.md

compilation.md

context.md

filters.md

helpers.md

index.md

parsing.md

rendering.md

tile.json