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.

instructions on how to use preact as a module in ES6+ client side codebases?

See original GitHub issue

I’m writing a minimal, unbundled PoC in which the browser just gets <script src="index.js" type="module"></script> after which it discovers all the imports and does what it needs to do without a need for webpack or a large babel config.

Instead I only use babel to convert JSX to regular JS (no ES5 conversions, just straight up “only convert the JSX, thanks”) and then let the browser deal with the rest. However, that leaves me with code that looks like:

import { h, render, Component } from 'preact';
import { router } from "../router/router.js";
import { Pattern } from "./pattern.js";

class Arranger extends Component {
    constructor(top) {
        this.pattern = new Pattern(this, top);
        render(this, top);
    }

    render() {
        return React.createElement(
            "div",
            { className: "controls" },
            React.createElement(
                "button",
                { onClick: evt => this.stop() },
                "\u25FC"
            ),
            React.createElement(
                "button",
                { onClick: evt => this.play() },
                "\u25B6"
            ),
            React.createElement(
                "label",
                { className: "bpm" },
                React.createElement("input", { type: "number", min: "1", max: "400", value: "120", onChange: evt => this.setBPM(evt.target.value) }),
                "BPM"
            ),
            React.createElement(
                "button",
                { onClick: evt => this.demo() },
                "demo"
            )
        );
    }

    setBPM(bpm) {
        this.player.setBPM(bpm | 0);
    }

    demo() {
        this.pattern.demo();
        this.play();
    }

    play() {
        this.player.play();
    }

    stop() {
        this.pattern.stop();
        this.player.stop();
    }
}

export { Arranger };

but without a way to load preact as a normal javascript module. The CDN link mentioned on the website (<script src="https://cdn.jsdelivr.net/npm/preact/dist/preact.min.js"></script>) doesn’t export anything, so is there a module version of preact available off of CDN, and if so, can the link for that be added to the docs?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
pl12133commented, Sep 12, 2018

Good idea, the docs are over at preact-www so I’ve opened a PR there.

1reaction
pl12133commented, Sep 11, 2018

Can you try with the .mjs built copy? https://cdn.jsdelivr.net/npm/preact/dist/preact.mjs

Read more comments on GitHub >

github_iconTop Results From Across the Web

instructions on how to use preact as a module in ES6+ client ...
I'm writing a minimal, unbundled PoC in which the browser just gets <script src="index.js" type="module"></script> after which it discovers ...
Read more >
Switching to Preact (from React)
Switching to Preact can be as easy as installing and aliasing preact-compat in for react and react-dom . This lets you continue writing...
Read more >
Getting Started | Preact: Fast 3kb React alternative with the ...
New to Preact? New to Virtual DOM? Check out the tutorial. This guide helps you get up and running to start developing Preact...
Read more >
Tutorial | Preact: Fast 3kb React alternative with the same ES6 ...
Hello World. Out of the box, the two functions you'll always see in any Preact codebase are h() and render() . The h()...
Read more >
Getting Started | Preact: Fast 3kb React alternative with the ...
Import what you need. The preact module provides both named and default exports, so you can either import everything under a namespace of...
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