Giter Site home page Giter Site logo

cybernetics / es6_module_transpiler-rails Goto Github PK

View Code? Open in Web Editor NEW

This project forked from davyjoneslocker/es6_module_transpiler-rails

0.0 2.0 0.0 373 KB

Transpile ES6 Modules in the Rails Asset Pipeline

Home Page: http://dockyard.com

License: MIT License

Ruby 3.79% JavaScript 96.21%

es6_module_transpiler-rails's Introduction

ES6ModuleTranspiler-Rails

Build Status Dependency Status Code Climate

Transpile ES6 Modules in the Rails Asset Pipeline

Uses Square's ES6 Module Transpiler

Installation

Node.js must be installed for the transpiling to happen

gem 'es6_module_transpiler-rails'

Usage

Your modules will transpile and are named based upon their directory nesting + filename, as long as the file has the .es6 extension. For example, app/assets/javascripts/controllers/fooController.js.es6

var fooController = function() {
  console.log('fooController is in the house!');
};

export default fooController;

will compile to /assets/controllers/fooController.js

define("controllers/fooController",
  ["exports"],
  function(__exports__) {
    "use strict";
    var fooController = function() {
      console.log('fooController is in the house!');
    };

    __exports__["default"] = fooController;
  });

Compiling

By default your module will compile to an AMD. You can also compile it to globals or CommonJS by making the following switch:

ES6ModuleTranspiler.compile_to = :globals
# or
ES6ModuleTranspiler.compile_to = :cjs

You may modify the options that are passed to the module compiler (i.e. compatFix) by modifying the compiler_options hash:

ES6ModuleTranspiler.compiler_options[:compatFix] = true;

The compiler_options hash is empty by default.

Loading Modules

When compiling to AMD or CommonJS, you'll need to be able to load the compiled result. In the below example, we're using a minimal AMD loader called loader.js. Without a loader, you'll get an error like:

Uncaught ReferenceError: define is not defined

For example, in application.js:

//= require loader
//= require_tree ./modules
//= require main

require('main');

Now main.js.es6 is our entry point - from this file we can use the es6 module syntax to load any es6 modules in the ./modules directory.

Custom Module Prefix

You can match module names based upon a pattern to apply a prefix to the name. You can add multiple patterns (which can each have separate prefixes):

ES6ModuleTranspiler.add_prefix_pattern Regexp.new(File.join(Rails.root, 'app')), 'app'
ES6ModuleTranspiler.add_prefix_pattern Regexp.new(File.join(Rails.root, 'config')), 'config'

This would match names that start with the pattern and prepend with app/ or config/. For example, /home/user/app/controllers/fooController would now be named app/controllers/fooController, and /home/user/config/router would now be named config/router.

Note the path is made up of the root path and the logical path for the asset. For example, if the path to your asset is /home/user/app/assets/javascripts/controllers/fooController.js.es6 the root path is /home/user/app/assets/javascripts/ and the logical path is controllers/fooController. It is entirely dependent upon what Sprockets considers to be the mount point for the asset.

Authors

Brian Cardarella

We are very thankful for the many contributors

Versioning

This gem follows Semantic Versioning

Want to help?

Please do! We are always looking to improve this gem.

Legal

DockYard, LLC © 2013

@dockyard

Licensed under the MIT license

es6_module_transpiler-rails's People

Contributors

bcardarella avatar caseywebdev avatar danmcclain avatar duggiefresh avatar hhff avatar pareeohnos avatar rwjblue avatar tricknotes 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.