Giter Club home page Giter Club logo

commonjs-demos's Introduction

CommonJS Require

browser-side CommonJS require() function.

Usage

Add commonjs-require.js as script src to your site.

Example

<script src="commonjs-require.js"></script>
<script>
require.register("module", function(exports, require, module){
  /* Some Module content */
  module.exports = "content"
})
alert(require('module'))
</script>

This will alert content

Automated wrapping into require.register

Why not browserify?

Browserify requires (scnr) that you use some magic AST/require.resolve mechanism to built your CommonJS package/files for the browser. This is really complex - especially if you have to bundle different files for different sections of your website, use dynamic requires or have circular dependencies.

API

  • require(name) — loads registered module and returns its exports.
  • require.register(name, fn) — registers new module. fn should have signature exports, require, module.
  • require.list() — lists all registered modules.

Node Module support

commonjs-require emulates the actual loading mechanism from Node.js in that it delegates all require() calls to Module._load() (Module is exposed via the "module" module). This improves support for modules which rely on the Node module system implementation (for example Mockery).

Module API

  • Module._load(request, parent) - does the actual module loading and instantiation
  • Module._cache - is the cache of loaded modules, can be cleared by assigning an empty object
  • Module._resolveFilename - determines the actual module id (e.g. adds "index" if its missing)

License

MIT

commonjs-demos's People

Watchers

 avatar

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.