Rucksack Lite Resource Management Core

6.0.0 · active · verified Wed Apr 22

rucksack-lite is a JavaScript library providing the foundational `RucksackLite` class for managing and preparing JavaScript and CSS resources. It serves as the core resource abstraction layer, differentiating it from actual bundlers, and is utilized by the more comprehensive `rucksack` bundler package. Currently at version 6.0.0, the library underwent a complete rewrite in this major release, focusing on robust handling of both local and remote resources. It defines a `RucksackResource` class internally for managing resource paths, types (JS/CSS), and generating the necessary HTML markup for inclusion. While its release cadence appears somewhat irregular, major versions signify active development with significant architectural shifts, such as licensing updates and full rewrites. Its primary differentiation lies in offering a clean, object-oriented API for resource management, abstracting away complexities of path resolution and markup generation prior to the actual bundling process.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate `RucksackLite`, add various types of resources (remote JS by string, remote CSS explicitly via `RucksackResource`), and generate the corresponding HTML markup. It highlights the resource management capabilities without performing actual file bundling, which is the domain of the `rucksack` package.

import RucksackLite, { RucksackResource } from 'rucksack-lite';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

async function setupRucksack() {
  const r = new RucksackLite({
    name: "my-app",
    bundle_dir: path.resolve(__dirname, "output"), // Ensure an absolute path for bundle_dir
    bundle_url: "/static",
    input: "main.js" // This input file isn't created in this example, but defines the primary bundle.
  });

  // Add a remote JavaScript resource by URL string
  r.add("https://example.com/index.js");

  // Add a remote CSS resource using the RucksackResource class for explicit typing
  r.add(new RucksackResource({
    path: "https://example.com/styles.css",
    type: "css"
  }));

  // Generate the HTML markup (note: this does not perform actual bundling)
  const htmlMarkup = r.html();
  console.log('Generated HTML Markup:\n', htmlMarkup);

  // The `bundle()` method primarily processes local resources for path resolution
  // It does NOT create actual bundled files without the full `rucksack` package and actual files.
  try {
    console.log('\nAttempting to call bundle() (no actual files created in this quickstart):');
    await r.bundle();
    console.log('Bundle operation completed (paths resolved, if any local resources were added).');
  } catch (error) {
    console.warn('Bundle operation warning (expected without actual files/bundler):', error.message);
  }

  console.log('\nRefreshed Markup after potential bundle operation:');
  console.log(r.html());
}

setupRucksack().catch(console.error);

view raw JSON →