Giter VIP home page Giter VIP logo

srvs's Introduction

srvs serves modern webapps for dev, with none of the fat

Build Status codecov npm

srvs is a zero dependency dev server with support for static content in addition to JavaScript modules hosted from local files and esm.sh.

Installation

Global

npm i -g srvs

Then you may run with:

srvs

npx

No install required, just run:

npx srvs

(npx comes with npm 5.2+ and higher.)

Local

Install with npm / Yarn:

npm i -D srvs

Then add srvs to the scripts in your package.json:

"scripts": {
  "start": "srvs"
}

Now you may run with:

npm start

Usage

Command Line

Here are the available command line arguments:

Argument Usage Default
port The port on which the dev server will listen. 8080
docRoot The relative path from which static assets such as index.html will be served. public
scriptRoot The relative path from which local JavaScript modules will be served. src

Each argument is passed in the form --argument=value. Here is an example using all available arguments:

npx srvs --port=3000 --docRoot=static --scriptRoot=js

API

srvs offers a programmatic way to integrate running with existing JavaScript code.

You may bring in the srvs API function using import if you have support for ES6 syntax:

import srvs from "srvs";

srvs(options).then(config => {
  // dev server is now open for business
});

Or using require:

const srvs = require("srvs");

srvs(options).then(config => {
  // dev server is now open for business
});

The options object has the same properties and values as the arguments supported by the command line version. The config parameter provided to the resolved Promise has the same properties as options.

Notes

  • This is only for use as a development tool, please do not use in production.
  • The docRoot and scriptRoot options will fall back to the current directory if they don't exist.
  • The BROWSER environment variable can be used to control which application to open your page in, or set to none to disable browser opening entirely. This feature is inspired by the popular create-react-app.

License

srvs is MIT licensed. See LICENSE.

srvs's People

Contributors

okwolf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

srvs's Issues

--hot on by default

Another enhancement, but I think it would really be magical if --hot was on by default, because I think most users would want to turn it on.

If all the enhancements go through, all you have to do to set up a quick webserver is cd to your project and:
srvs
to get hot reloading, current directory as root, and auto-open browser. That would literally be magical

project priorities

must have

  • serve static files relative to a local folder
  • ES modules with Node resolution for relative imports and installed node_modules (works offline)
  • decent performance (loads in under 1s for medium-sized app)
  • fallback to index.html for routers using the history.pushState API
  • launch a browser and navigate to the entry point
  • watch files for changes and refresh the page for open browsers
  • control the port listening for HTTP requests
  • option to control which browser to open or disable
  • hot module replacement without reloading the page
  • automated defaults that work for common projects (src, public, etc)

nice to have

  • zero runtime dependencies
  • no compile time delays
  • robust error handling with useful output
  • informative output with pretty colors
  • not installed modules are imported from esm.sh
  • inject env variables such as process.env.NODE_ENV

non-goals

  • production
  • bundling
  • security / HTTPS
  • compression
  • older browsers (IE, polyfills, transpiling)
  • proxy for APIs hosted under the same context
  • URL rewriting/routing
  • JSX
  • Express-style middleware
  • non-HTTP protocols

docRoot should be . when the folder doesn't exist

This is up to you, but I think docRoot should be current directory by default instead of public.
This would reduce my calls from
srvs docRoot=.
to
srvs ๐Ÿฅ‡

I don't know if hosting all files in public is best practice, but I usually host with Github pages, which I think requires the index.html to be either in the root of the project, or in docs folder.

What do you think Wolfgang?

inject globals from environment variables

Common use cases for this include logic that's conditionally run depending on environment and API access keys that you don't want to hardcode. We should always support NODE_ENV and possibly any variables that start with a configurable prefix like create-react-app has REACT_APP_.

To implement this, I'm thinking add a <script> that sets window.process.env = { ... } instead of trying to parse and replace the usages of the variables in scripts. It might actually be more helpful during development to see the original code and look at the values with the devtools.

Watch service for hot reload

  • Provide Server-Sent Events endpoint for receiving change events related to the files being served.
  • Add flag to enable this feature.
  • Provide a client JavaScript API wrapping this for use in hot reload situations.
  • Inject script automatically when watch/hot is enabled.

This should probably work similar to module.hot from webpack.

Make opening browser optional

Should the default behavior be open like it currently is or should users have to provide an argument like --open?

Error thrown at openBrowser.js:35

Occurs after npm i -g srvs, and then srvs, or npx srvs, or with npm start/yarn start with script added to package.json.

Environment: WSL Windows 10

Not really sure what is the cause

root@Iaye:/mnt/c/Users/Kimeiga/Github/muzik# npx srvs --docRoot=.
srvs v0.3.1
Available on:
  http://192.168.1.10:8080
  http://127.0.0.1:8080
Hit CTRL-C to stop the server
Error: spawn xdg-open ENOENT
^C
root@Iaye:/mnt/c/Users/Kimeiga/Github/muzik# srvs
srvs v0.3.1
Available on:
  http://192.168.1.10:8080
  http://127.0.0.1:8080
Hit CTRL-C to stop the server
/usr/local/lib/node_modules/srvs/src/client/openBrowser.js:35
    throw new Error(error);
    ^

Error: Error: spawn xdg-open ENOENT
    at ChildProcess.browser.once.error (/usr/local/lib/node_modules/srvs/src/client/openBrowser.js:35:11)
    at Object.onceWrapper (events.js:273:13)
    at ChildProcess.emit (events.js:182:13)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:233:12)
    at onErrorNT (internal/child_process.js:404:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:721:11)
    at startup (internal/bootstrap/node.js:228:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:576:3)
root@Iaye:/mnt/c/Users/Kimeiga/Github/muzik#

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.