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

Holderjs with Vite #236

Open
junseuk opened this issue Jan 4, 2023 · 0 comments
Open

Holderjs with Vite #236

junseuk opened this issue Jan 4, 2023 · 0 comments

Comments

@junseuk
Copy link

junseuk commented Jan 4, 2023

After migrating from create-react-app to vite, there is an issue with calling run() with this error: Uncaught TypeError: Cannot read properties of undefined (reading 'document'). To narrow it down, I created a test project using create vite command and install holderjs, yarn add holderjs. The details of the error is written below:

/* WEBPACK VAR INJECTION */(function(global) {/**
	 * Generic new DOM element function
	 *
	 * @param tag Tag to create
	 * @param namespace Optional namespace value
	 */
	exports.newEl = function(tag, namespace) {
	    if (!global.document) return; //ERROR

	    if (namespace == null) {
	        return global.document.createElement(tag);
	    } else {
	        return global.document.createElementNS(namespace, tag);
	    }
	};

I've tried to explicitly define global using <script>window.global = window</script> in index.html or put window undefined check in main.tsx:

if (typeof window !== 'undefined') {
  console.log(window);
  ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
    <React.StrictMode>
      <App />
    </React.StrictMode>,
  )
}

But none of these worked. This is an App.tsx:

import { useEffect, useState } from 'react';
import reactLogo from './assets/react.svg';
import './App.css';
import { run } from 'holderjs';

function App() {
  const [count, setCount] = useState(0)

  useEffect(() => {
    console.log(run)
  }, [])

  return (
    <div className="App">
      <div>
        <a href="https://vitejs.dev" target="_blank">
          <img src="/vite.svg" className="logo" alt="Vite logo" />
        </a>
        <a href="https://reactjs.org" target="_blank">
          <img src={reactLogo} className="logo react" alt="React logo" />
        </a>
      </div>
      <h1>Vite + React</h1>
      <div className="card">
        <button onClick={() => setCount((count) => count + 1)}>
          count is {count}
        </button>
        <p>
          Edit <code>src/App.tsx</code> and save to test HMR
        </p>
      </div>
      <p className="read-the-docs">
        Click on the Vite and React logos to learn more
      </p>
    </div>
  )
}

export default App

error

What is the cause of this error? I'll provide more information if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant