Coder Social home page Coder Social logo

moduleserve's Introduction

Moduleserve

This is a shim HTTP server for directly running your CommonJS (or ES6) modules for development and testing (without a bundling step, and with the compilation/transformation integrated in the server shim).

Warnings:

  • The server basically exposes your whole filesystem over HTTP. It binds to "localhost" by default, but if you bind it to something else or proxy in some uncautious way, you are putting yourself at risk.

  • This does everything syncronously, both on client and server.

  • This is a hack that I am using for my own development, not generally-useful, supported software. You might be able to make use of it, but if you run into a problem, you should try to debug it yourself, not ask me for help.

  • The code is likely to break if you do something that I'm not doing. Pull requests welcome!

What it does

You run the server for a given directory...

moduleserve demo/ --port 8080

It will start up an HTTP server on the given port, serving the content of the demo directory statically. In addition, it exposes a URL /moduleserve/load.js, which you use to load your main module:

<script src="/moduleserve/load.js" data-module="./mymodule"></script>

That will pull in the client-side scaffolding and look for ./mymodule, resolved relative to the directory that the server is running on. You can add a data-require attribute to the tag to have it set a global require variable, which you can use to load modules from the console.

That module is loaded as a CommonJS module, and may use require with the regular node.js conventions (implicit .js or /index.js, searching node_modules, etc). The intention is that, contrary to systems like JSPM, you don't have to set up your project to please this tool, but you can just directly use it on an npm-style codebase.

You can pass moduleserve a --transform option, which should point at a node module that defines a transformer. Such a transformer is called on a file before it is served. It should export a transform function that takes (filename, text) parameter and returns the transformed text. If it also exports an init function, that is called once with the target directory of the server.

If you pass --transform babel, you get a built-in transformer that loads babel in the context of the target directory (i.e. you have to have it installed locally there, or globally) that you're running the tool on, and uses it to transform any files whose path does not contain node_modules/.

If you pass --spa, every file path where no file can be found automatically will fall back to serving the index.html. This option allows you to implement routing for Single Page Applications.

The client is going to make a request for every single module file, so for a bigger project the initial load is bound to be slow, especially if you're using an expensive transform. The server then caches these and sends 302 responses whenever possible, so assuming localhost-level latency, subsequent loads should be faster.

Source

This code is open-source under an MIT license. If you want to contribute, create pull requests on GitHub.

moduleserve's People

Contributors

ericandrewlewis avatar marijnh avatar nikgraf avatar sprzybylski avatar thorn0 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

moduleserve's Issues

Techniques for accessing modules directly

I wanted to have access to modules directly, so I can access them in the browser JS console and play around.

I exposed var loaded onto window.LoadedModules, so I can access modules directly via their relative path.

e.g.

LoadedModules["/../dist/format/index"].parseFrom( LoadedModules["/../dist/model/defaultschema"].defaultSchema, "* test*", "markdown" )

I also ended up exposing the root module, so I can load other modules on the fly via mainModule.require( "/../dist/markdown" )

Not really suggesting any changes, just sharing what I'm doing.

Resolving fails for a moudule with two folders up dependency

I've modified the ProseMirror demo to require a module for an experiment.

The module has a dependency on a module in ProseMirror, so I import it accordingly. I run this through babel via ./node_modules/.bin/babel -d demo/dist demo/src, which gets required by the demo's main script.

The name ../../dist/model ends up being resolved to /dist/dist/model, where I might expect it to resolve to /../dist/model.

I tried debugging why this is happening inside the resolving code, but I'm not sure what would need to change to support this.

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.