-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
PackagerRunner.js
56 lines (45 loc) 路 1.49 KB
/
PackagerRunner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// @flow
import type {Bundle, CLIOptions, Blob, FilePath} from '@parcel/types';
import type Config from './Config';
import {mkdirp, writeFile} from '@parcel/fs';
import nullthrows from 'nullthrows';
import path from 'path';
type Opts = {|
config: Config,
cliOpts: CLIOptions
|};
export default class PackagerRunner {
config: Config;
cliOpts: CLIOptions;
distDir: FilePath;
distExists: Set<FilePath>;
constructor({config, cliOpts}: Opts) {
this.config = config;
this.cliOpts = cliOpts;
this.distExists = new Set();
}
async writeBundle(bundle: Bundle) {
let contents = await this.package(bundle);
contents = await this.optimize(bundle, contents);
let filePath = nullthrows(bundle.filePath);
let dir = path.dirname(filePath);
if (!this.distExists.has(dir)) {
await mkdirp(dir);
this.distExists.add(dir);
}
await writeFile(filePath, contents);
}
async package(bundle: Bundle): Promise<Blob> {
// $FlowFixMe - filePath should already be filled in at this point
let packager = await this.config.getPackager(bundle.filePath);
return packager.package(bundle, this.cliOpts);
}
async optimize(bundle: Bundle, contents: Blob): Promise<Blob> {
// $FlowFixMe - filePath should already be filled in at this point
let optimizers = await this.config.getOptimizers(bundle.filePath);
for (let optimizer of optimizers) {
contents = await optimizer.optimize(bundle, contents, this.cliOpts);
}
return contents;
}
}