Skip to content

Commit

Permalink
Add some example scripts to load with webpack
Browse files Browse the repository at this point in the history
The example JS files demonstrate
 * The use of CommonJS dependency management syntax[1]; 'require' and
'module.exports' statements
 * Using webpack to require dependencies that (for whatever reason) are
in the global scope
 * Using ES6 features in your JS (relying on the ES6-loader to polyfill
your code)
 * Using sprockets to load JS bundled by webpack as well as JS from gems
(in this case just the default Rails jquery gem
  • Loading branch information
flarnie committed Aug 31, 2014
1 parent 8c0ce12 commit 6e6e8db
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/assets/javascripts/_application.js
@@ -0,0 +1,2 @@
// files required here will run in the global scope
require('./hello_world');
2 changes: 1 addition & 1 deletion app/assets/javascripts/application.js
Expand Up @@ -13,4 +13,4 @@
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
//= require main.bundle
8 changes: 8 additions & 0 deletions app/assets/javascripts/hello_world.js
@@ -0,0 +1,8 @@
var $ = require('jquery'),
HelloWorldView = require('./views/hello_world_view');

// Any logic to initialize the app goes here
// For example, starting any routers or initializing the 'home' view
$(function() {
HelloWorldView.render();
});
22 changes: 22 additions & 0 deletions app/assets/javascripts/views/hello_world_view.js
@@ -0,0 +1,22 @@
var $ = require('jquery');

var firstPar = $('<p>'),
textStart = $('<span>').text("You're using "),
webpackURL = 'http://webpack.github.io/',
webpackLink = $('<a>').attr('href', webpackURL).text('webpack'),
textEnd = $('<span>').text(' with Rails.'),
es6text = 'ES6',
// The ES6 loader will compile this template string into the ES5 equivalent.
es6Par = $('<p>').text(
`You can also use ${es6text} features with the ES6 loader.`
);

firstPar.append(textStart).append(webpackLink).append(textEnd);

var HelloWorldView = {
render: function() {
$('.main').append(firstPar).append(es6Par);
}
};

module.exports = HelloWorldView;

0 comments on commit 6e6e8db

Please sign in to comment.