UNPKG

1.47 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = getJSXOptions;
7
8var _path = _interopRequireDefault(require("path"));
9
10function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
12const JSX_EXTENSIONS = {
13 '.jsx': true,
14 '.tsx': true
15};
16const JSX_PRAGMA = {
17 react: {
18 pragma: 'React.createElement',
19 pragmaFrag: 'React.Fragment'
20 },
21 preact: {
22 pragma: 'h',
23 pragmaFrag: 'Fragment'
24 },
25 nervjs: {
26 pragma: 'Nerv.createElement',
27 pragmaFrag: undefined
28 },
29 hyperapp: {
30 pragma: 'h',
31 pragmaFrag: undefined
32 }
33};
34/**
35 * Generates a babel config for JSX. Attempts to detect react or react-like libraries
36 * and changes the pragma accordingly.
37 */
38
39async function getJSXOptions(config) {
40 if (!config.isSource) {
41 return null;
42 } // Find a dependency that we can map to a JSX pragma
43
44
45 const pkg = await config.getPackage();
46 const reactLib = Object.keys(JSX_PRAGMA).find(libName => pkg && (pkg.dependencies && pkg.dependencies[libName] || pkg.devDependencies && pkg.devDependencies[libName]));
47 const pragma = reactLib ? JSX_PRAGMA[reactLib].pragma : undefined;
48 const pragmaFrag = reactLib ? JSX_PRAGMA[reactLib].pragmaFrag : undefined;
49
50 if (pragma || JSX_EXTENSIONS[_path.default.extname(config.searchPath)]) {
51 return {
52 plugins: [['@babel/plugin-transform-react-jsx', {
53 pragma,
54 pragmaFrag
55 }]]
56 };
57 }
58}
\No newline at end of file