Code Monkey home page Code Monkey logo

sprockets-browserify's Introduction

Sprockets-Browserify

Serve npm (CommonJS) modules through Sprockets with the help of Browserify.

Requirements

Node.js, obviously. This gem calls node directly, no ExecJS involved.

How to use

Add this to your Gemfile:

gem 'sprockets-browserify'

Place a CommonJS Module in your assets directory and perform a npm install:

/app/assets/javascript/foo
  - index.js
  - package.json
  - node_modules/
    - ...

Require the module within your application.js or somewhere else:

//
//= require foo/index
//

Done.

Your node module should fulfill the following requirements:

  • The main file for the module must reside in the same directory as the package.json, in the root of the module

  • The main file should expose its API in some way to the browser environment, by setting propertes on window or sth. like that

If you want to use a module that doesn’t conform, consider creating a wrapper module in you assets directory that requires the module you actually want to use:

foo_wrapper/package.json:

...
"dependencies": {
  "foo": ...
},
...

foo_wrapper/index.js:

window.Foo = require('foo');

foo_wrapper/node_modules/foo/index.js:

module.exports = "Foo";

WARNING:

By default, the asset pipeline in Rails precompiles every file with a .js extension. This is probably not what you want if you have a bunch of modules in the node_modules directory. Disable this behavior by specifically listing only individual files you want to precompile:

config.assets.precompile = ['main.js', 'stylesheet.css']

instead of merely appending to

config.assets.precompile << 'main.js'

License

Released under the MIT License. See the MIT-LICENSE file for further details.

sprockets-browserify's People

Contributors

janv avatar pxlpnk 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

Watchers

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