|
1 | 1 | // @flow |
2 | 2 | import type { ConfigurationFile } from '../types'; |
3 | 3 |
|
| 4 | +const JSX_PRAGMA = { |
| 5 | + react: 'React.createElement', |
| 6 | + preact: 'h', |
| 7 | +}; |
| 8 | + |
4 | 9 | const config: ConfigurationFile = { |
5 | 10 | title: 'tsconfig.json', |
6 | 11 | type: 'typescript', |
7 | 12 | description: 'Configuration for how TypeScript transpiles.', |
8 | 13 | moreInfoUrl: 'http://www.typescriptlang.org/docs/handbook/tsconfig-json.html', |
9 | 14 |
|
10 | | - getDefaultCode: (template: string) => { |
| 15 | + getDefaultCode: ( |
| 16 | + template: string, |
| 17 | + resolveModule: (path: string) => ?{ code: string } |
| 18 | + ) => { |
11 | 19 | if (template === 'create-react-app-typescript') { |
12 | 20 | return JSON.stringify( |
13 | 21 | { |
@@ -45,37 +53,68 @@ const config: ConfigurationFile = { |
45 | 53 | } |
46 | 54 |
|
47 | 55 | if (template === 'parcel') { |
48 | | - return JSON.stringify({ |
| 56 | + const tsconfig = { |
49 | 57 | compilerOptions: { |
50 | 58 | module: 'commonjs', |
51 | 59 | jsx: 'preserve', |
| 60 | + jsxFactory: undefined, |
52 | 61 | esModuleInterop: true, |
53 | 62 | sourceMap: true, |
54 | 63 | allowJs: true, |
55 | 64 | lib: ['es6', 'dom'], |
56 | 65 | rootDir: 'src', |
57 | 66 | moduleResolution: 'node', |
58 | 67 | }, |
59 | | - }); |
| 68 | + }; |
| 69 | + |
| 70 | + const packageJSONModule = resolveModule('/package.json'); |
| 71 | + |
| 72 | + if (packageJSONModule) { |
| 73 | + try { |
| 74 | + const parsed = JSON.parse(packageJSONModule.code); |
| 75 | + |
| 76 | + let pragma = null; |
| 77 | + Object.keys(JSX_PRAGMA).forEach(dep => { |
| 78 | + if ( |
| 79 | + (parsed.dependencies && parsed.dependencies[dep]) || |
| 80 | + (parsed.devDependencies && parsed.devDependencies[dep]) |
| 81 | + ) { |
| 82 | + pragma = JSX_PRAGMA[dep]; |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + if (pragma !== null) { |
| 87 | + tsconfig.compilerOptions.jsx = 'react'; |
| 88 | + tsconfig.compilerOptions.jsxFactory = pragma; |
| 89 | + } |
| 90 | + } catch (e) { |
| 91 | + /* do nothing */ |
| 92 | + } |
| 93 | + } |
| 94 | + return JSON.stringify(tsconfig, null, 2); |
60 | 95 | } |
61 | 96 |
|
62 | 97 | if (template === 'nest') { |
63 | | - return JSON.stringify({ |
64 | | - compilerOptions: { |
65 | | - module: 'commonjs', |
66 | | - declaration: true, |
67 | | - noImplicitAny: false, |
68 | | - removeComments: true, |
69 | | - noLib: false, |
70 | | - allowSyntheticDefaultImports: true, |
71 | | - emitDecoratorMetadata: true, |
72 | | - experimentalDecorators: true, |
73 | | - target: 'es6', |
74 | | - sourceMap: true, |
75 | | - outDir: './dist', |
76 | | - baseUrl: './src', |
| 98 | + return JSON.stringify( |
| 99 | + { |
| 100 | + compilerOptions: { |
| 101 | + module: 'commonjs', |
| 102 | + declaration: true, |
| 103 | + noImplicitAny: false, |
| 104 | + removeComments: true, |
| 105 | + noLib: false, |
| 106 | + allowSyntheticDefaultImports: true, |
| 107 | + emitDecoratorMetadata: true, |
| 108 | + experimentalDecorators: true, |
| 109 | + target: 'es6', |
| 110 | + sourceMap: true, |
| 111 | + outDir: './dist', |
| 112 | + baseUrl: './src', |
| 113 | + }, |
77 | 114 | }, |
78 | | - }); |
| 115 | + null, |
| 116 | + 2 |
| 117 | + ); |
79 | 118 | } |
80 | 119 |
|
81 | 120 | return JSON.stringify( |
|
0 commit comments