CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-parcel--plugin

Plugin API for Parcel bundler - provides base classes for creating Parcel plugins including transformers, resolvers, bundlers, namers, runtimes, packagers, optimizers, compressors, reporters, and validators

67

1.06x
Overview
Eval results
Files

packaging.mddocs/

Asset Packaging

The Packager plugin packages bundles into final output formats for different target environments and module systems. Packagers handle the final step of converting internal bundle representations into files that can be executed or loaded.

Capabilities

Packager Class

Base class for creating asset packaging plugins.

/**
 * Base class for asset packaging plugins
 * @template C - Configuration type
 * @template B - Bundle configuration type
 */
export declare class Packager<C, B> {
  constructor(opts: PackagerOpts<C, B>);
}

/**
 * Packager plugin configuration interface
 */
interface PackagerOpts<ConfigType, BundleConfigType> {
  /** Load global configuration */
  loadConfig?: (args: {
    config: Config;
    options: PluginOptions;
    logger: PluginLogger;
    tracer: PluginTracer;
  }) => Promise<ConfigType>;

  /** Load bundle-specific configuration */
  loadBundleConfig?: (args: {
    bundle: NamedBundle;
    bundleGraph: BundleGraph<NamedBundle>;
    config: Config;
    options: PluginOptions;
    logger: PluginLogger;
    tracer: PluginTracer;
  }) => Promise<BundleConfigType>;

  /** Package a bundle into final output (required) */
  package(args: {
    bundle: NamedBundle;
    bundleGraph: BundleGraph<NamedBundle>;
    options: PluginOptions;
    logger: PluginLogger;
    tracer: PluginTracer;
    config: ConfigType;
    bundleConfig: BundleConfigType;
    getInlineBundleContents: (
      bundle: Bundle,
      bundleGraph: BundleGraph<NamedBundle>
    ) => Promise<{contents: Blob}>;
    getSourceMapReference: (map?: SourceMap) => Promise<string | null>;
  }): Promise<BundleResult | BundleResult[]>;
}

Bundle Results

/**
 * Result from packaging a bundle
 */
interface BundleResult {
  /** Output file contents */
  contents: Blob;
  
  /** Source map for the bundle */
  map?: SourceMap;
  
  /** Bundle type */
  type?: string;
}

/**
 * File contents as binary data
 */
type Blob = Buffer | Uint8Array | string;

Usage Example:

import { Packager } from "@parcel/plugin";

export default new Packager({
  // Package bundle into final output (required)
  async package({
    bundle, 
    bundleGraph, 
    getInlineBundleContents,
    getSourceMapReference
  }) {
    const assets = bundleGraph.getBundleAssets(bundle);
    let contents = '';
    let sourceMaps = [];

    // Package JavaScript bundle
    if (bundle.type === 'js') {
      // Add module wrapper
      contents += '(function() {\n';

      // Add each asset
      for (const asset of assets) {
        const assetCode = await asset.getCode();
        const assetMap = await asset.getMap();
        
        contents += `// Asset: ${asset.filePath}\n`;
        contents += assetCode + '\n\n';
        
        if (assetMap) {
          sourceMaps.push(assetMap);
        }
      }

      // Close module wrapper
      contents += '})();';

      // Generate combined source map
      const combinedMap = this.combineMaps(sourceMaps);
      const mapReference = await getSourceMapReference(combinedMap);
      
      if (mapReference) {
        contents += `\n//# sourceMappingURL=${mapReference}`;
      }

      return {
        contents: contents,
        map: combinedMap
      };
    }

    // Handle other bundle types
    return this.packageGeneric(assets);
  },

  combineMaps(maps) {
    // Combine multiple source maps into one
    // Implementation depends on source map library
    return maps.length > 0 ? maps[0] : null;
  },

  packageGeneric(assets) {
    // Default packaging for other file types
    if (assets.length === 1) {
      return {
        contents: assets[0].getCode()
      };
    }
    throw new Error('Cannot package multiple assets of this type');
  }
});

Install with Tessl CLI

npx tessl i tessl/npm-parcel--plugin

docs

bundling.md

compression.md

index.md

naming.md

optimization.md

packaging.md

reporting.md

resolution.md

runtime.md

transformation.md

validation.md

tile.json