UNPKG

1.98 kBJavaScriptView Raw
1// @flow strict-local
2import invariant from 'assert';
3import type {Bundle, ParcelOptions} from './types';
4import BundleGraph from './BundleGraph';
5import type {WorkerApi} from '@parcel/workers';
6
7import Transformation, {type TransformationOpts} from './Transformation';
8import {reportWorker} from './ReporterRunner';
9import PackagerRunner from './PackagerRunner';
10import Validation, {type ValidationOpts} from './Validation';
11import ParcelConfig from './ParcelConfig';
12import {registerCoreWithSerializer} from './utils';
13
14import '@parcel/cache'; // register with serializer
15import '@parcel/package-manager';
16import '@parcel/fs';
17
18registerCoreWithSerializer();
19
20// Remove the workerApi type from the TransformationOpts and ValidationOpts types:
21// https://github.com/facebook/flow/issues/2835
22type TransformationOptsWithoutWorkerApi = $Diff<
23 TransformationOpts,
24 {|workerApi: mixed|},
25>;
26type ValidationOptsWithoutWorkerApi = $Diff<
27 ValidationOpts,
28 {|workerApi: mixed|},
29>;
30
31export function runTransform(
32 workerApi: WorkerApi,
33 opts: TransformationOptsWithoutWorkerApi,
34) {
35 return new Transformation({
36 workerApi,
37 report: reportWorker.bind(null, workerApi),
38 ...opts,
39 }).run();
40}
41
42export function runValidate(
43 workerApi: WorkerApi,
44 opts: ValidationOptsWithoutWorkerApi,
45) {
46 return new Validation({
47 workerApi,
48 report: reportWorker.bind(null, workerApi),
49 ...opts,
50 }).run();
51}
52
53export function runPackage(
54 workerApi: WorkerApi,
55 {
56 bundle,
57 bundleGraphReference,
58 config,
59 cacheKey,
60 options,
61 }: {|
62 bundle: Bundle,
63 bundleGraphReference: number,
64 config: ParcelConfig,
65 cacheKey: string,
66 options: ParcelOptions,
67 |},
68) {
69 let bundleGraph = workerApi.getSharedReference(bundleGraphReference);
70 invariant(bundleGraph instanceof BundleGraph);
71 return new PackagerRunner({
72 config,
73 options,
74 report: reportWorker.bind(null, workerApi),
75 }).packageAndWriteBundle(bundle, bundleGraph, cacheKey);
76}