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
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.
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[]>;
}/**
* 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--plugindocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10