Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Rails engines #44

Open
richardvenneman opened this issue Oct 18, 2016 · 2 comments
Open

Support Rails engines #44

richardvenneman opened this issue Oct 18, 2016 · 2 comments

Comments

@richardvenneman
Copy link

richardvenneman commented Oct 18, 2016

I'm trying setup sprockets-commoner for use with a Rails engine. Haven't had much success so far, unfortunately as it seems the Babel transform are not getting applied.

In my engine, I have added the sprockets-commoner gem with the following .babelrc:

{
  "presets": ["es2015", "react"]
}

In my host app I also added the sprockets-commoner gem, with the samen .babelrc. I load the engine JavaScript in the host app with a javascript_include_tag. This is where I get a runtime error: SyntaxError: Unexpected keyword 'import'.

I tried loading the engine JS via 2 methods:

  1. Directly load the engine JS in the javascript_include_tag (<%= javascript_include_tag 'engine/application', 'data-turbolinks-track' => true %>). This results in the non-transpiled ES2015 code being loaded in the browser.
  2. Require the engine JS in a JavaScript file in the host app (//= require engine/application) and load that file in the javascript_include_tag instead. This results in some commoner processed code, but without Babel transforms applied (which results in the runtime syntax error on the import statement):
!function() {
var __commoner_initialize_module__ = function(f) {
  var module = {exports: {}};
  f.call(module.exports, module, module.exports);
  return module.exports;
};
var global = window;

import React from 'react';
import ReactDOM from 'react-dom';

import PhoneInput from './components/PhoneInput';

ReactDOM.render(
  <PhoneInput />,
  document.getElementById('phone-input')
);
var __commoner_module__app$assets$javascripts$engine_js = {};
}();

I was wondering if this kind of setup is actually supported.

I can setup a sample repo if you'd like.

@bouk
Copy link
Contributor

bouk commented Oct 21, 2016

It's not supported, commoner only works with things that are placed under the app root, not with gems.

@bouk bouk closed this as completed Oct 21, 2016
@bouk bouk reopened this Oct 21, 2016
@richardvenneman
Copy link
Author

All right, thanks for clearing that up! It seems I'll still have to use browserify-rails for the engine 😀

@richardvenneman richardvenneman changed the title Applying Babel transforms to Rails engine Support Rails engines Oct 25, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants