jwhitley / requirejs-rails

RequireJS support for your Rails 3 or 4 application
MIT License
592 stars 202 forks source link

Multiple Errors when requiring files #278

Open TrevorThomas4 opened 6 years ago

TrevorThomas4 commented 6 years ago

I am trying to use requirejs-rails in my rails project and I am running into problems with it. I have added the gem and ran bundle install, but I am unable to actually require a file. My relevant files are:

application.js

// = require require
require(['jquery'], function($) {
    'use strict';

    // Your awesome javascript goes here!

    // **********************************
    // This is just some guff to help you prove that everything works.
    // You can and should delete this once everything works.
    console.log('Successfully loaded jQuery version: ' + $('body').jquery);
    if (typeof $.rails === 'object') {
        console.log('The $ object seems to have been decorated by Rails UJS successfully.');
    }
    else {
        console.log('Uh-oh, it seems that $ has not been decorated by Rails UJS. You might want to check this.');
    }
    // **********************************

});

head of application.html.erb

<head>
    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
    <%= requirejs_include_tag "require" %>
    <%= csrf_meta_tags %>
  </head>

Then where I am trying to use it:

var antlr4 = require(['foo/antlr4/index']); 
var input = 'foobar';
var chars = new antlr4.InputStream(input);

My app/assets/javascripts/foo/antlr4/index.js looks like the following

exports.foo = require('./foo');
exports.bar = require('./bar')

and it gives me ReferenceError: Can't find variable: exports and TypeError: undefined is not a constructor (evaluating 'new antlr4.InputStream(input)')

If I change the head of application.html.erb, to the following:

<head>
    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
    <%= javascript_include_tag 'require.js' %>
    <%= requirejs_include_tag "application" %>
    <%= csrf_meta_tags %>
  </head>

It just gives me ReferenceError: Can't find variable: exports. Can anybody help with this issue? I might of included things incorrectly