-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Enables jsx plugin in case jsx syntax is used in js files #2530
Changes from 4 commits
33f945c
acb4ced
caa2ebc
213383a
22bc1d4
3d62037
64e1cfa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import * as Hyperapp from 'hyperapp' | ||
|
||
module.exports = <div />; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"private": true | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import * as Nerv from 'nervjs'; | ||
|
||
module.exports = <div />; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"private": true | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const Preact = require('preact'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not using import here for the sake of testing require syntax as well. |
||
|
||
module.exports = <div />; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"private": true | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import * as React from 'react'; | ||
|
||
module.exports = <div />; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"private": true | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,26 @@ const JSX_PRAGMA = { | |
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 | ||
*/ | ||
lustoykov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
function maybeCreateFallbackPragma(asset) { | ||
for (const dep in JSX_PRAGMA) { | ||
if (asset.contents.match(createJSXRegexFor(dep))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should create these regexes ahead of time rather than recreating them for every file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 thanks for the hint, will fix tomorrow morning |
||
return JSX_PRAGMA[dep]; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Generates a babel config for JSX. Attempts to detect react or react-like libraries | ||
* and changes the pragma accordingly. | ||
|
@@ -37,6 +57,10 @@ async function getJSXConfig(asset, isSourceModule) { | |
} | ||
} | ||
|
||
if (!pragma) { | ||
pragma = maybeCreateFallbackPragma(asset); | ||
} | ||
|
||
if (pragma || JSX_EXTENSIONS[path.extname(asset.name)]) { | ||
return { | ||
internal: true, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The underlying problem this PR is trying to solve is that JSX in JS support fails when no dependencies are specified.
No dependencies are two-folded
Unfortunately, I couldn't capture the second case with those tests as omitting package.json would modify package.json in other directories (the top-level package.json in integration-tests I think).
However, creation of package.json is tested on many other occasions, which would lead us back to case 1. So case 2. should work by implication (tested locally, is fine).