Coder Social home page Coder Social logo

Comments (3)

smalluban avatar smalluban commented on June 22, 2024 2

Of course, you can :) Your example shows how to use hybrids in mode, which supports older browsers. However, it might not be the best option to create standalone web components with own entry.

If you target ES modules ready browsers, you can serve and use single file simple-counter.js like this:

import { define, html } from 'https://unpkg.com/[email protected]/src/index.js';

function increaseCount(host) {
  host.count += 1;
}

const SimpleCounter = {
  count: 0,
  render: ({ count }) => html`
    <button onclick="${increaseCount}">
      Count: ${count}
    </button>
  `,
};

define('simple-counter', SimpleCounter);

And simply use it in html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Simple Counter Example</title>
  <!-- IMPORT SIMPLE COUNTER -->
  <script type="module" src="https://your-domain.com/simple-counter.js"></script>
  <!-- IMPORT SIMPLE COUNTER -->
</head>
<body>
  <simple-counter count="10"></simple-counter>
</body>
</html>

Some highlights:

  • We are pointing src - yes, we can, because hybrids uses only implemented features, which all ES module ready browsers support already
  • Be aware of multiple files fetching (19 source files). If your server supports http/2 it should not be a problem, but it might be better to bundle that in larger applications
  • There is no entry, which is ES module ready, but served in one file - it has no sense eighter (how then modules would be separated - this is something that webpack does for us)

from hybrids.

tovare avatar tovare commented on June 22, 2024

Thanks :-)

I like being able to refine and optimize at a later stage if needed.

from hybrids.

smalluban avatar smalluban commented on June 22, 2024

Feel free to reopen if you have any additional questions.

from hybrids.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.