Substance Bundler

0.27.4 · active · verified Tue Apr 21

Substance Bundler is a custom JavaScript bundling and build orchestration library, currently at version `0.27.4`. It offers a high-level, shell-script-like approach to defining build processes, distinguishing itself from traditional task runners like Gulp or Grunt by emphasizing simplicity and direct action execution. The library internally leverages `chokidar` for robust file watching, ensuring all defined operations automatically react to source file changes. While not a bundler itself, it seamlessly integrates with popular tools such as Webpack, Rollup, and PostCSS via dedicated extensions, acting as an orchestrator for these underlying technologies. This allows developers to define complex build pipelines, including copying files, removing directories, and executing custom scripts, all within a unified task-based system with inter-task dependencies. Its primary differentiator lies in its low-ceremony API for defining build steps and its integrated watch mode across all tasks, offering a streamlined development experience for projects that require custom but maintainable build logic.

Common errors

Warnings

Install

Imports

Quickstart

This `make.js` script demonstrates defining and orchestrating multiple build tasks, including cleaning, CSS compilation via PostCSS, JavaScript bundling with Rollup, and file copying, all managed by `substance-bundler`. It showcases task dependencies and a basic command-line runner for build operations.

const b = require('substance-bundler');
const postcss = require('substance-bundler/extensions/postcss');
const rollup = require('substance-bundler/extensions/rollup');

// Define a task to clean the build output directory
b.task('clean', () => {
  b.rm('dist');
  console.log('Removed dist directory.');
});

// Define a task to compile CSS using PostCSS extension
b.task('css', () => {
  console.log('Bundling CSS...');
  postcss(b, {
    from: 'styles/index.css', // Assumes 'styles/index.css' exists
    to: 'dist/app.css'
  });
});

// Define a task to bundle JavaScript using Rollup extension
// Requires a rollup.config.js and an entry point like index.js
b.task('js', () => {
  console.log('Bundling JavaScript with Rollup...');
  // For a runnable example, ensure rollup.config.js and index.js exist
  const rollupConfig = require('./rollup.config.js'); 
  rollup(b, rollupConfig);
});

// Define a task to copy static assets
b.task('copy-assets', () => {
  console.log('Copying assets...');
  b.copy('./assets', 'dist/assets/'); // Assumes an 'assets' directory exists
});

// Define the default task which runs other tasks sequentially
b.task('default', ['clean', 'css', 'js', 'copy-assets'], () => {
  console.log('All default tasks completed!');
});

// Basic CLI runner (for demonstration, not part of bundler API itself)
const tasksToRun = process.argv.slice(2).filter(arg => !arg.startsWith('-'));
const watchMode = process.argv.includes('-w');

if (watchMode) {
  console.log('Running in watch mode...');
  b.run(tasksToRun.length > 0 ? tasksToRun : ['default'], { watch: true });
} else {
  b.run(tasksToRun.length > 0 ? tasksToRun : ['default']);
}

// To run this script: 
// 1. Save as `make.js`.
// 2. Create dummy `dist`, `styles`, `assets` directories. 
// 3. Create dummy `styles/index.css`, `index.js`, `rollup.config.js`.
// 4. `npm install --save-dev rollup postcss @rollup/plugin-node-resolve`
// 5. Execute: `node make.js` or `node make.js -w`

view raw JSON →