打包器
打包器
在 Parcel 中,一個 Packager (打包器)將多個 Asset (資源)合併到一起成為最終的輸出檔案包。這發生在處理所有資源之後的主進程中,並創建了一個套件檔案樹。打包器根據輸出檔案類型進行註冊,並且已經產生該輸出類型的資源被傳送到該打包器以產生最終的輸出檔案。
打包器介面
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); } }
#註冊一個打包器
你可以使用addPackager 方法,使用打包工具註冊打包器(packager)。它接受要註冊的檔案類型以及打包器模組的路徑。
const Bundler = require('parcel-bundler'); let bundler = new Bundler('input.js'); bundler.addPackager('foo', require.resolve('./MyPackager'));