Giter VIP home page Giter VIP logo

sprockets-commonjs-twitter's Introduction

This library adds CommonJS support to Sprockets.

What is CommonJS?

The CommonJS module format is a way of encapsulating JavaScript libraries, ensuring they have to explicitly require and export properties they use. In a nutshell:

  1. You require in files using require():

    var Asset = require('models/asset');

  2. You export properties using module.exports:

    var Asset = function(){ /* ... */ }; module.exports = Asset;

This library

This library adds CommonJS support to Sprockets, so it can wrap up JavaScript files as modules, and serve them appropriately. This is done by giving any JS files you want as modules, the .module.js extension.

Sprockets will then wrap up the JS library when it's requested, with the following:

require.define({'library/name': function(exports, require, module){ /* Your library */ }});

require.define() is defined inside commonjs.js, which you'll need to include in the page before any modules are loaded.

One caveat to the approach this library takes, is that dependencies loaded through require() will not be added to the dependency graph. This library will not parse the AST tree for require calls. This decision has been made for a variety of reasons, but it does mean you need to require files through both CommonJS and Sprockets.

Usage

  1. Add gem 'sprockets-commonjs' to your Gemfile
  2. Add .module.js to any JavaScript files you want as modules, i.e. users.module.js
  3. Require all the modules, e.g.: //= require_tree ./models
  4. Or, require individual modules, e.g.: //= require users.module

sprockets-commonjs-twitter's People

Contributors

alex-stripe avatar andrewdeandrade avatar antifuchs avatar dominicpacquing avatar josh avatar ljharb avatar maccman avatar mjackson avatar

Stargazers

 avatar

Watchers

 avatar  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.