Giter VIP home page Giter VIP logo

esmodule-server's Introduction

esmodule-server

esmodule-server is a simple, zero-configuration ES module supported HTTP server. It is powerful enough for visiting ES module directly through modern browser. Fully support Typescript and React syntax. Not only that, but also a react container.

Features

  • Based on Deno, keep same code style with it.
  • Fully support Typescript.
  • Fully support React syntax, can visit jsx or tsx by modern browser directly.
  • A powerful react container, natively support CSR and SSR.
  • Production and Dev mode support by parameters(?dev, ?production).

DEMO

image

Running

Run with docker

DockerHub: hayond/esmodule-server

# ~/Downloads/static is local static directory, 8888 is local port

docker run -p 8888:8000 -v ~/Downloads/static:/workspaces --restart unless-stopped -d --name esmodule-server hayond/esmodule-server

Run with deno

DenoLand: esmoduleserver

deno run --allow-read --allow-net --allow-write https://deno.land/x/esmoduleserver/mod.js

Write code in currenty directory, and visit http://localhost:8000/ using modern browser.

Unfortunately, no Node implementation

The one of core feature is natively support react SSR, deno can run same format code with browser, but node can't. If have any good idea for this, merge request welcome or another variation.

Examples

simple server

create index.html and index.jsx

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, user-scalable=no, shrink-to-fit=no"
    />
    <title>index</title>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="./index.jsx"></script>
  </body>
</html>
import React from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";

const root = createRoot(document.querySelector("#root"));
root.render(<div>{content}</div>, { content: "Hello World!" });

react app

only need create index.jsx

import React from "https://esm.sh/react";

function App({ content }) {
  return <div>{content}</div>;
}

export const title = "esmodule-server";

export default function main({ preloadedState }) {
  return <App content={"Hello World!"} />;
}

react ssr app

implement export function getPreloadedState can open SSR, function getServerReady is useful server check whether data is ready, maximum try three times when return false.

import React from "https://esm.sh/react";

function App({ content }) {
  return <div>{content}</div>;
}

export const title = "esmodule-server";

const store = {
  content: "Hello World!",
  isLoaded: true,
};

export function getPreloadedState() {
  return { ...store };
}

export function getServerReady() {
  return store.isLoaded;
}

export default function main({ preloadedState = {} }) {
  Object.assign(store, preloadedState);
  return <App content={store.content} />;
}

production and dev mode

  • ?dev add dev parameter to all link dependences, check more details: esm.sh, for example http://localhost:8000/example/react-ssr-app/index.html?dev
  • ?production bundle all the local scripts and minify them, in the meantime, add bundle parameter to all link dependences, check more details: esm.sh.

upcoming features

  • set production mode as default by command line args.
  • custom server port by command line args.
  • CDN host support for production mode.
  • modulepreload support.
  • ROOT importmaps support.
  • html customization (scripts, stylesheets, modulepreloads)
  • setting.js support, dependencies suppport
  • localhost cdn producton build
  • es-module-shims support for old browser

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.