question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React module detection doesn't work unless explicitly importing 'react'

See original GitHub issue

And not ‘react/index.js’ or whatever. That’s fine, but what I’m doing within our codebase is:

  1. Babel uses __react_createElement__ as its JSX pragma and these get injected during babel-loader.
  2. Webpack’s ProvidePlugin sees an unbound __react_createElement__ and imports node_modules/react/index.js. (I don’t know when this happens; maybe during the module templating? It’s after the loaders in any case.)

But the loader in this plugin is looking for 'react' or "react" in my source code. It looks in between step 1 and 2, so it sees __react_createElement__ but not the import of React from ProvidePlugin.

The reason I do this is to avoid the import react from 'react'; that we don’t explicitly use in our source code, and it lets me switch between React and Preact pretty easily. You could justifiably say “don’t do that” and just call it an unsupported use case, and I acknowledge that I’m going off the beaten path a bit, so I wouldn’t expect something like this to be supported out of the box. However, how would you feel about giving a little bit of control over how modules are detected? The current regex of /['"]react['"]/ could be the default, but for more bespoke situations I could pass /\b__react_createElement__\b/ or similar into the plugin options. What do you think? I’m willing to submit a pull request if you’re okay with this.

(My other option is to create a loader that looks for my JSX pragma and injects 'react'; as a directive or /*'react'*/ as a comment. I’ll do that just so I can proceed, but it feels a little weird.)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
xandriscommented, Apr 8, 2020

I can take a closer look at that. Does the refresh machinery need to transform code in node_modules to work properly?

0reactions
gaearoncommented, Apr 8, 2020

It doesn’t.

Read more comments on GitHub >

github_iconTop Results From Across the Web

why does "import { React } from 'react';" not work? [duplicate]
This says "find the export from the react module which is explicitly named React , and import that here as a constant that...
Read more >
Introducing the New JSX Transform
With the new transform, you can use JSX without importing React. Depending on your setup, its compiled output may slightly improve the ...
Read more >
React Testing Library Tutorial
Learn how to use React Testing Library in this tutorial. You will learn how to test your React components step by step with...
Read more >
Testing React Components with react-test-renderer, and ...
Learn how to test React components with react-test-renderer, and the Act API.
Read more >
Getting started with TypeScript and React
This happens when you import, for example, React, whose code is not written in ES2015 (the source code is, but React ships a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found