UNPKG

852 BJavaScriptView Raw
1// @flow
2
3import type {Config, PluginOptions} from '@parcel/types';
4import type {BabelConfig} from './types';
5
6/**
7 * Generates a babel config for stripping away Flow types.
8 */
9export default async function getFlowOptions(
10 config: Config,
11 options: PluginOptions,
12): Promise<?BabelConfig> {
13 if (!config.isSource) {
14 return null;
15 }
16
17 // Only add flow plugin if `flow-bin` is listed as a dependency in the root package.json
18 let conf = await config.getConfigFrom(options.projectRoot + '/index', [
19 'package.json',
20 ]);
21 let pkg = conf?.contents;
22 if (
23 !pkg ||
24 (!(pkg.dependencies && pkg.dependencies['flow-bin']) &&
25 !(pkg.devDependencies && pkg.devDependencies['flow-bin']))
26 ) {
27 return null;
28 }
29
30 return {
31 plugins: [
32 ['@babel/plugin-transform-flow-strip-types', {requireDirective: true}],
33 ],
34 };
35}