PlasmoHQ / plasmo

🧩 The Browser Extension Framework
https://www.plasmo.com
MIT License
10.16k stars 351 forks source link

[BUG] Cannot find module 'react' inside a CSUI #799

Open TotomInc opened 10 months ago

TotomInc commented 10 months ago

What happened?

Unable to import hooks from react such as useState or useEffect, I got the following error: Uncaught Error: Cannot find module 'react'

This happens on a CSUI named contents/widget.tsx with TailwindCSS setup:

import cssText from "data-text:~style.css";

import type { PlasmoCSConfig, PlasmoGetStyle } from "plasmo";
import { useState } from "react";

export const config: PlasmoCSConfig = {
  matches: ["[REDACTED]"],
};

export const getStyle: PlasmoGetStyle = () => {
  const style = document.createElement("style");

  style.textContent = cssText;

  return style;
}

const Widget = () => {
  const [activeSearch, setActiveSearch] = useState(false);

  return (
    <div className="w-[320px] absolute lg:top-[60px] lg:left-[500px] bg-white p-4 shadow-md antialiased rounded-md">
      <button
        type="button"
        className="flex items-center justify-center w-full text-base text-white bg-zinc-900 hover:bg-zinc-800 focus:outline-none px-6 py-1.5 font-medium rounded-md"
      >
        [REDACTED]
      </button>
    </div>
  );
};

export default Widget;

Version

Latest

What OS are you seeing the problem on?

MacOSX

What browsers are you seeing the problem on?

Chrome

Relevant log output

❯ npm run dev             

> plasmo-project@0.0.1 dev
> plasmo dev

🟣 Plasmo v0.83.0
🔴 The Browser Extension Framework
🔵 INFO   | Starting the extension development server...
🔵 INFO   | Loaded environment variables from: []
🟢 DONE   | Extension re-packaged in 1346ms! 🚀

(OPTIONAL) Contribution

Code of Conduct

dougwithseismic commented 5 months ago

Seconded with a devtool panel. Can render a component fine but importing anything from react breaks it.

🎟 A workaround for this is to delete the contents of .build, rebuild (pnpm run dev), and remove/read the extension on chrome://extensions.

Docs do mention this, kinda, but I had to nuke it for things to work.

https://docs.plasmo.com/framework/ext-pages

Heads up! Make sure to refresh your extension manually when creating a new browser extension page as our runtime might not be listening and might not update in certain cases.