/
jsx.js
83 lines (71 loc) 路 1.87 KB
/
jsx.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const path = require('path');
const JSX_EXTENSIONS = {
'.jsx': true,
'.tsx': true
};
const JSX_PRAGMA = {
react: 'React.createElement',
preact: 'h',
nervjs: 'Nerv.createElement',
hyperapp: 'h'
};
function createJSXRegexFor(dependency) {
// result looks like /from\s+[`"']react[`"']|require\([`"']react[`"']\)/
return new RegExp(
`from\\s+[\`"']${dependency}[\`"']|require\\([\`"']${dependency}[\`"']\\)`
);
}
/**
* Solves a use case when JSX is used in .js files, but
* package.json is empty or missing yet and therefore pragma cannot
* be determined based on pkg.dependencies / pkg.devDependencies
*/
const cacheJsxRegexFor = {};
function maybeCreateFallbackPragma(asset) {
for (const dep in JSX_PRAGMA) {
let regex = cacheJsxRegexFor[dep];
if (!regex) {
regex = createJSXRegexFor(dep);
cacheJsxRegexFor[dep] = regex;
}
if (asset.contents.match(regex)) {
return JSX_PRAGMA[dep];
}
}
}
/**
* Generates a babel config for JSX. Attempts to detect react or react-like libraries
* and changes the pragma accordingly.
*/
async function getJSXConfig(asset, isSourceModule) {
// Don't enable JSX in node_modules
if (!isSourceModule) {
return null;
}
let pkg = await asset.getPackage();
// Find a dependency that we can map to a JSX pragma
let pragma = null;
for (let dep in JSX_PRAGMA) {
if (
pkg &&
((pkg.dependencies && pkg.dependencies[dep]) ||
(pkg.devDependencies && pkg.devDependencies[dep]))
) {
pragma = JSX_PRAGMA[dep];
break;
}
}
if (!pragma) {
pragma = maybeCreateFallbackPragma(asset);
}
if (pragma || JSX_EXTENSIONS[path.extname(asset.name)]) {
return {
internal: true,
babelVersion: 7,
config: {
plugins: [[require('@babel/plugin-transform-react-jsx'), {pragma}]]
}
};
}
}
module.exports = getJSXConfig;