Inline Source CLI: HTML Resource Inliner

2.0.0 · active · verified Wed Apr 22

The `inline-source-cli` package offers a dedicated command-line interface (CLI) for the `inline-source` library, a utility designed to embed external resources such as CSS stylesheets, JavaScript code, and image data directly into an HTML file. This functionality is crucial for scenarios requiring self-contained HTML documents, such as preparing email templates, optimizing static site assets for single-file deployment, or creating portable web components. The package is currently at stable version 2.0.0, with a release cadence that appears driven by dependency updates or minor feature enhancements like the addition of stdin support. Its primary advantage lies in providing a straightforward, opinionated command-line tool that abstracts away the programmatic complexity of resource inlining, making it easy to integrate into existing build scripts and CI/CD pipelines without writing custom JavaScript code. Developers can quickly achieve asset optimization and delivery benefits without deep diving into the underlying library's API, using standard shell commands.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to programmatically execute `inline-source-cli` from a Node.js script, creating dummy files, running the inliner, and capturing its output.

import { spawn } from 'child_process';
import path from 'path';
import fs from 'fs';

// Create a dummy HTML file and resources for demonstration
const buildDir = path.join(process.cwd(), 'build-cli-demo');
if (!fs.existsSync(buildDir)) fs.mkdirSync(buildDir, { recursive: true });

fs.writeFileSync(path.join(buildDir, 'style.css'), 'h1 { color: blue; }');
fs.writeFileSync(path.join(buildDir, 'script.js'), 'console.log(\'Script inlined!\');');
fs.writeFileSync(path.join(buildDir, 'index.html'), `<!DOCTYPE html>\n<html>\n<head>\n    <link rel=\"stylesheet\" href=\"./style.css\">\n</head>\n<body>\n    <h1>Hello, Inliner!</h1>\n    <script src=\"./script.js\"></script>\n</body>\n</html>`);

console.log('Created dummy files in:', buildDir);

// Resolve the path to the inline-source CLI executable
const inlineSourceCliPath = path.resolve(process.cwd(), 'node_modules', '.bin', 'inline-source');

// Execute the inline-source CLI tool
const child = spawn(inlineSourceCliPath, [
  '--compress', 'false',
  '--root', buildDir,
  path.join(buildDir, 'index.html')
], { stdio: 'pipe' });

let output = '';
child.stdout.on('data', (data) => {
  output += data.toString();
});

child.stderr.on('data', (data) => {
  console.error(`stderr: ${data}`);
});

child.on('close', (code) => {
  if (code === 0) {
    console.log('\n--- Inlined HTML Output ---');
    console.log(output);
    fs.writeFileSync(path.join(buildDir, 'bundle.html'), output);
    console.log('\nOutput written to', path.join(buildDir, 'bundle.html'));
  } else {
    console.error(`CLI process exited with code ${code}`);
  }
  // Clean up dummy files
  fs.unlinkSync(path.join(buildDir, 'index.html'));
  fs.unlinkSync(path.join(buildDir, 'style.css'));
  fs.unlinkSync(path.join(buildDir, 'script.js'));
  fs.unlinkSync(path.join(buildDir, 'bundle.html'));
  fs.rmdirSync(buildDir);
  console.log('Cleaned up dummy files.');
});

view raw JSON →