Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: set pragmafrag for jsx in v2 #3617

Merged
merged 3 commits into from
Oct 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions packages/transformers/babel/src/jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@ const JSX_EXTENSIONS = {
};

const JSX_PRAGMA = {
react: 'React.createElement',
preact: 'h',
nervjs: 'Nerv.createElement',
hyperapp: 'h'
react: {
pragma: 'React.createElement',
pragmaFrag: 'React.Fragment'
},
preact: {
pragma: 'h',
pragmaFrag: 'Fragment'
},
nervjs: {
pragma: 'Nerv.createElement',
pragmaFrag: undefined
},
hyperapp: {
pragma: 'h',
pragmaFrag: undefined
}
};

/**
Expand All @@ -24,22 +36,20 @@ export default async function getJSXOptions(config: Config) {
}

// Find a dependency that we can map to a JSX pragma
let pkg = await config.getPackage();
let pragma = null;
for (let dep in JSX_PRAGMA) {
if (
const pkg = await config.getPackage();
const reactLib = Object.keys(JSX_PRAGMA).find(
libName =>
pkg &&
((pkg.dependencies && pkg.dependencies[dep]) ||
(pkg.devDependencies && pkg.devDependencies[dep]))
) {
pragma = JSX_PRAGMA[dep];
break;
}
}
((pkg.dependencies && pkg.dependencies[libName]) ||
(pkg.devDependencies && pkg.devDependencies[libName]))
);

const pragma = reactLib ? JSX_PRAGMA[reactLib].pragma : undefined;
const pragmaFrag = reactLib ? JSX_PRAGMA[reactLib].pragmaFrag : undefined;

if (pragma || JSX_EXTENSIONS[path.extname(config.searchPath)]) {
return {
plugins: [['@babel/plugin-transform-react-jsx', {pragma}]]
plugins: [['@babel/plugin-transform-react-jsx', {pragma, pragmaFrag}]]
};
}
}