Express Simple Bundler

0.2.0 · maintenance · verified Tue Apr 21

express-simple-bundler is a specialized utility designed for Express.js applications, focusing on the streamlined creation of minified and cache-busted JavaScript and CSS bundles. The package, currently at version 0.2.0, indicates an early stage of development with an likely infrequent release cadence. Its core functionality involves processing specified source files, applying compression via configurable engines like UglifyJS for JavaScript and clean-css for CSS, and then outputting these bundles to a designated public directory. A key differentiator is its automatic appending of MD5 hashes to the generated filenames, which is crucial for effective cache invalidation and ensuring browsers fetch the latest asset versions upon deployment. The library integrates directly into the server-side logic of an Express application, providing bundled file paths through a global object for use in view templates. This direct integration aims to simplify asset management without requiring a separate, complex build pipeline, making it suitable for projects prioritizing simplicity within the Express ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to configure and execute the bundler, creating minified and hashed asset files for an Express application. Includes setup for dummy files and directories for a runnable example.

const bundler = require('express-simple-bundler');

const bundleSettings = {
  compress : true, // Set to true to compress and minify files
  bundles: [
     {name : 'main.js', type:"js",output:"public/js/dist/",files:['public/js/functions.js', 'public/js/another.js']},
     {name : 'main.css', type:"css",output:"public/css/dist/", files:['public/css/style.css', 'public/css/theme.css']}
  ],
  jsEngine: 'uglifyjs', // Specify a JavaScript compression engine
  cssEngine: 'clean-css' // Specify a CSS compression engine
};

// Create dummy files for demonstration purposes
const fs = require('fs');
if (!fs.existsSync('public/js')) fs.mkdirSync('public/js', { recursive: true });
if (!fs.existsSync('public/css')) fs.mkdirSync('public/css', { recursive: true });
fs.writeFileSync('public/js/functions.js', 'console.log("hello");');
fs.writeFileSync('public/js/another.js', 'function greet() { return "world"; }');
fs.writeFileSync('public/css/style.css', 'body { margin: 0; }');
fs.writeFileSync('public/css/theme.css', '.container { padding: 10px; }');

// Ensure output directories exist before bundling
if (!fs.existsSync(bundleSettings.bundles[0].output)) fs.mkdirSync(bundleSettings.bundles[0].output, { recursive: true });
if (!fs.existsSync(bundleSettings.bundles[1].output)) fs.mkdirSync(bundleSettings.bundles[1].output, { recursive: true });

// Execute the bundler
bundler.bundle(bundleSettings);

console.log('Bundling complete. Check public/js/dist/ and public/css/dist/ for output.');
// In a real Express app, 'bundles.js' and 'bundles.css' would now be available in your views.

view raw JSON →