Skip to content

Commit 9b6f4dd

Browse files
committed
Automatically add JSX pragma for Parcel template when JSX detected in TS
1 parent bdfd728 commit 9b6f4dd

File tree

1 file changed

+57
-18
lines changed
  • packages/common/templates/configuration/tsconfig

1 file changed

+57
-18
lines changed

packages/common/templates/configuration/tsconfig/index.js

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
// @flow
22
import type { ConfigurationFile } from '../types';
33

4+
const JSX_PRAGMA = {
5+
react: 'React.createElement',
6+
preact: 'h',
7+
};
8+
49
const config: ConfigurationFile = {
510
title: 'tsconfig.json',
611
type: 'typescript',
712
description: 'Configuration for how TypeScript transpiles.',
813
moreInfoUrl: 'http://www.typescriptlang.org/docs/handbook/tsconfig-json.html',
914

10-
getDefaultCode: (template: string) => {
15+
getDefaultCode: (
16+
template: string,
17+
resolveModule: (path: string) => ?{ code: string }
18+
) => {
1119
if (template === 'create-react-app-typescript') {
1220
return JSON.stringify(
1321
{
@@ -45,37 +53,68 @@ const config: ConfigurationFile = {
4553
}
4654

4755
if (template === 'parcel') {
48-
return JSON.stringify({
56+
const tsconfig = {
4957
compilerOptions: {
5058
module: 'commonjs',
5159
jsx: 'preserve',
60+
jsxFactory: undefined,
5261
esModuleInterop: true,
5362
sourceMap: true,
5463
allowJs: true,
5564
lib: ['es6', 'dom'],
5665
rootDir: 'src',
5766
moduleResolution: 'node',
5867
},
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);
6095
}
6196

6297
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+
},
77114
},
78-
});
115+
null,
116+
2
117+
);
79118
}
80119

81120
return JSON.stringify(

0 commit comments

Comments
 (0)