packager
Packager
In Parcel, a Packager merges multiple Assets into a final output file package. This happens in the main process after all resources have been processed and a tree of package files is created. The packager is registered based on the output file type, and the resources that have generated that output type are sent to the packager to produce the final output file.
Packager interface
const {Packager} = require('parcel-bundler'); class MyPackager extends Packager { async start() { // 可选。写文件头部内容。 await this.dest.write(header); } async addAsset(asset) { // 必须。将资源写入生成文件。 await this.dest.write(asset.generated.foo); } async end() { // 可选。写文件尾内部内容。 await this.dest.end(trailer); } }
Register a packager
You can use addPackager method, uses the packaging tool to register the packager (packager). It accepts the file type to register and the path to the packager module.
const Bundler = require('parcel-bundler'); let bundler = new Bundler('input.js'); bundler.addPackager('foo', require.resolve('./MyPackager'));