Skip to content

Commit

Permalink
Feature: detect reactLib with pkg.alias.react
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinyaigeek committed Dec 11, 2021
1 parent b27515e commit 6081742
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion packages/transformers/js/src/JSTransformer.js
Expand Up @@ -126,6 +126,37 @@ const SCRIPT_ERRORS = {
},
};

function convertAliasReactIntoPragma(
alias: string | {|[string]: string|},
): 'react' | 'preact' | 'hyperapp' | 'nervjs' {
if (typeof alias === 'string') {
switch (alias) {
case 'react': {
return 'react';
}
case 'preact/compat':
case 'preact': {
return 'preact';
}
case 'hyperapp': {
return 'hyperapp';
}
case 'nervjs': {
return 'nervjs';
}
default: {
return 'react';
}
}
} else {
for (const key in alias) {
return convertAliasReactIntoPragma(alias[key]);
}
}

return 'react';
}

type TSConfig = {
compilerOptions?: {
// https://www.typescriptlang.org/tsconfig#jsx
Expand Down Expand Up @@ -157,7 +188,7 @@ export default (new Transformer({
let reactLib;
if (pkg?.alias && pkg.alias['react']) {
// e.g.: `{ alias: { "react": "preact/compat" } }`
reactLib = 'react';
reactLib = convertAliasReactIntoPragma(pkg.alias['react']);
} else {
// Find a dependency that we can map to a JSX pragma
reactLib = Object.keys(JSX_PRAGMA).find(
Expand Down

0 comments on commit 6081742

Please sign in to comment.