Code Monkey home page Code Monkey logo

hercules's Introduction

Hercules

By mimicking node.js' require, Herluces lets you use your CommonJS packages in the browser. It even supports packages.json dependencies.

This enables you to develop and test your code server side, and bundle it for the browser later.

The code examples below are written in Coffeescript but plain Javascript will work too.

How does it work?

Hercules = require 'hercules'
bundle = Hercules.bundle '/path/to/my/package'
source = bundle.toString() # Returns a chunk of Javascript that defines this.require

Send source to the browser (for example via express). All code run after source, will have access to a require method.

If you don't want to attach require to window you can do like this:

Hercules = require 'hercules'
bundle = Hercules.bundle '/path/to/my/package'
source = "
  (function() {
    #{bundle.toString()}
    window.MyAwesomeApp = require('.');
  })(window);
"

Installation

Add hercules to your package.json and run npm install. Then you'll be able to do require 'hercules'.

Dependencies

Hercules automatically add dependencies defined in your package.json (read more about package.json).

If you try to bundle a package that depends on a package that require node (defined in package.json's engines field), Hercules will throw an error. If you instead want Hercules to just ignore these packages, you can use ignoreNodePackages like this:

Hercules = require 'hercules'
bundle = Hercules.bundle '/path/to/my/package', ignoreNodePackages: true
source = bundle.toString()

Example

If you want to play with Hercules, I recommend checking out this little example app.

Alternatives

Here's a list of other libraries that somehow enables you to use require in the browser.

Very elegant and simple! Unfortunately, it is based on load paths which makes its require behave different from node's. Written by one of my heroes, Sam Stephenson.

Mature solution with a lot of options and features. It doesn't handle dependencies however. It tries to do too many different things for my taste - but you might see this as an advantage.

Other alternatives I haven't tried:

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.