UNPKG

2.68 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const extname_js_1 = require("./libs/extname.js");
4function generateConfig(filePath, code, _options) {
5 const plugins = [
6 'asyncGenerators',
7 'bigInt',
8 'classProperties',
9 'classPrivateProperties',
10 'classPrivateMethods',
11 'decorators-legacy',
12 'doExpressions',
13 'dynamicImport',
14 'exportDefaultFrom',
15 'exportNamespaceFrom',
16 'functionBind',
17 'functionSent',
18 'importMeta',
19 'logicalAssignment',
20 'nullishCoalescingOperator',
21 'numericSeparator',
22 'objectRestSpread',
23 'optionalCatchBinding',
24 'optionalChaining',
25 ['pipelineOperator', { proposal: 'smart' }],
26 'throwExpressions',
27 ];
28 // { all: true } option is bullshit thus I do it manually, just in case
29 // I still specify it
30 const flowPlugins = [['flow', { all: true }], 'flowComments'];
31 // If line has @flow header, include flow plug-ins
32 const dynamicFlowPlugins = code.includes('@flow') ? flowPlugins : [];
33 const fileExt = (0, extname_js_1.getExtNameFromFilePath)(filePath);
34 switch (fileExt) {
35 case '.ts':
36 plugins.push('typescript');
37 break;
38 case '.tsx':
39 plugins.push('typescript', 'jsx');
40 break;
41 // Adding .jsx extension by default because it doesn't affect other syntax features
42 // (unlike .tsx) and because people are seem to use it with regular file extensions
43 // (e.g. .js) see https://github.com/dotansimha/graphql-code-generator/issues/1967
44 case '.js':
45 plugins.push('jsx', ...dynamicFlowPlugins);
46 break;
47 case '.jsx':
48 plugins.push('jsx', ...dynamicFlowPlugins);
49 break;
50 case '.flow.js':
51 plugins.push('jsx', ...flowPlugins);
52 break;
53 case '.flow.jsx':
54 plugins.push('jsx', ...flowPlugins);
55 break;
56 case '.flow':
57 plugins.push('jsx', ...flowPlugins);
58 break;
59 case '.vue':
60 plugins.push('typescript', 'vue');
61 break;
62 case '.svelte':
63 plugins.push('typescript', 'svelte');
64 break;
65 default:
66 plugins.push('jsx', ...dynamicFlowPlugins);
67 break;
68 }
69 // The _options filed will be used to retrieve the original options.
70 // Useful when we wanna get not config related options later on
71 return {
72 sourceType: 'module',
73 plugins,
74 allowUndeclaredExports: true,
75 };
76}
77exports.default = generateConfig;