UNPKG

1.04 kBJavaScriptView Raw
1const fs = require('fs');
2const {promisify} = require('@parcel/utils');
3const path = require('path');
4const {mkdirp} = require('@parcel/fs');
5
6class Packager {
7 constructor(bundle, bundler) {
8 this.bundle = bundle;
9 this.bundler = bundler;
10 this.options = bundler.options;
11 }
12
13 static shouldAddAsset() {
14 return true;
15 }
16
17 async setup() {
18 // Create sub-directories if needed
19 if (this.bundle.name.includes(path.sep)) {
20 await mkdirp(path.dirname(this.bundle.name));
21 }
22
23 this.dest = fs.createWriteStream(this.bundle.name);
24 this.dest.write = promisify(this.dest.write.bind(this.dest));
25 this.dest.end = promisify(this.dest.end.bind(this.dest));
26 }
27
28 async write(string) {
29 await this.dest.write(string);
30 }
31
32 async start() {}
33
34 // eslint-disable-next-line no-unused-vars
35 async addAsset(asset) {
36 throw new Error('Must be implemented by subclasses');
37 }
38
39 getSize() {
40 return this.dest.bytesWritten;
41 }
42
43 async end() {
44 await this.dest.end();
45 }
46}
47
48module.exports = Packager;