-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
umd build of preact/compat not working in standalone html file #2719
Comments
I was actually able to get this to work with <!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>Example</title>
<script src="https://unpkg.com/htm@latest/dist/htm.js"></script>
<script src="https://unpkg.com/preact@latest/dist/preact.umd.js"></script>
<script src="https://unpkg.com/preact@latest/hooks/dist/hooks.umd.js"></script>
<script src="https://unpkg.com/preact@latest/compat/dist/compat.umd.js"></script>
</html> It seems as if Curiously, <!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>Example</title>
<script src="https://npm.reversehttp.com/htm@latest/dist/htm.js"></script>
<script src="https://npm.reversehttp.com/preact@latest/dist/preact.umd.js"></script>
<script src="https://npm.reversehttp.com/preact@latest/hooks/dist/hooks.umd.js"></script>
<script src="https://npm.reversehttp.com/preact@latest/compat/dist/compat.umd.js"></script>
</html> does not work. |
Hi @JohnDDuncanIII - The two options are: 1. Use UMD from unpkg: (as you discovered) <script src="https://unpkg.com/htm@latest/dist/htm.js"></script>
<script src="https://unpkg.com/preact@latest/dist/preact.umd.js"></script>
<script src="https://unpkg.com/preact@latest/hooks/dist/hooks.umd.js"></script>
<script src="https://unpkg.com/preact@latest/compat/dist/compat.umd.js"></script>
<script>
const React = preactCompat,
ReactDOM = preactCompat;
const html = htm.bind(preact.h);
function App () {
const [c, add] = React.useReducer(x => x+1, 0);
return html`<button onClick=${add}>${c}</button>`;
}
ReactDOM.render(html`<${App} />`, document.body);
</script> 2. Use ESM from npm.reversehttp.com: This has the benefit of delivering all your dependencies in a single optimized bundle. <script type="module">
import { html, render, preactCompat as React } from 'https://npm.reversehttp.com/preact,preact/compat,htm/preact';
function App () {
const [c, add] = React.useReducer(x => x+1, 0);
return html`<button onClick=${add}>${c}</button>`;
}
render(html`<${App} />`, document.body);
</script> |
I'll close this issue out since it seems like everything's working. |
Thanks for the response, @developit! Your examples were helpful. All good on my end 👍 |
I am working on a minimal
index.html
preact
/preact-compat
/htm
example usingSuspense
andLazy
that should work in a standaloneindex.html
file.While it is easy to get this to work in an es6 module (see preactjs/preact-compat#544 (comment)), I cannot seem to get it to work in a standalone
index.html
file.Reproduction / Steps to reproduce
errors out with
compat.umd.js:1 Uncaught TypeError: Cannot set property 'preactCompat' of undefined
since
e.preactCompat
is undefined inhttps://npm.reversehttp.com/preact@latest/compat/dist/compat.umd.js
errors out with
Uncaught TypeError: Cannot read property 'useState' of undefined
since
useState
is undefined inhttps://unpkg.com/preact@latest/compat/src/index.js
I also tried
as well as
since I didn't see
useHooks
in thepreact
global var, but ran into similar errors for both.While I understand that the optimal way to use
preact
/preact-compat
/htm
is inside of an es6 module, given that this is a lightweight react alternative, I was hoping that I could just import it as a umd global without needing to use a bundler nor define an es6 module.Expected Behavior
react-compat
should load successfullyActual Behavior
react-compat
fails to load successfullyThanks for your time!
Similar issues:
preactjs/preact-compat#544
#2564
#2571
The text was updated successfully, but these errors were encountered: