Skip to content

importing .jsx module from Engine, Webpacker 3, Rails 5.1.4 #786

@tomasc

Description

@tomasc

Help us help you! Please choose one:

  • My app crashes with react-rails, so I've included the stack trace and the exact steps which make it crash.
  • My app doesn't crash, but I'm getting unexpected behavior. So, I've described the unexpected behavior and suggested a new behavior.
  • I'm trying to use react-rails with another library, but I'm having trouble. I've described my JavaScript management setup (eg, Sprockets, Webpack...), how I'm trying to use this other library, and why it's not working.
  • I have another issue to discuss.

I am trying to import .jsx components from Rails engine.

Engine

In the engine I have added simple HelloWorld.jsx under app/javascript/components:

import React from 'react'

class HelloWorld extends React.Component {
  render() {
    return <div>HELLO WORLD</div>;
  }
}

export default HelloWorld;

App

And in the main app (Webpacker 3, Rails 5.1.4, unreleased react-rails coming from master branch):

  • updated resolved_paths: ['engine/app/javascript'] in webpacker.yml
  • added Engine.jsx under javascript/component that contains:
import HelloWorld from 'components/HelloWorld'

Then in my application.html.erb, I added the javascript_pack_tag 'application' and tried to load the react component:

<%= react_component 'Engine.HelloWorld' %>

It seems the component is being found, but the .jsx preprocessor is not being recognised/applied, as I am getting the following error:

fromRequireContextWithGlobalFallback.js?7c97:19 Error: Module build failed: SyntaxError: Unexpected token (5:11)

  3 | class HelloWorld extends React.Component {
  4 |   render() {
> 5 |     return <div>HELLO WORLD</div>;
    |            ^
  6 |   }
  7 | }
  8 | 


    at eval (107:1)
    at Object.<anonymous> (application-a71cca030c96f6cdcea4.js:1376)
    at __webpack_require__ (application-a71cca030c96f6cdcea4.js:20)
    at eval (Engine.jsx?76a9:1)
    at Object.<anonymous> (application-a71cca030c96f6cdcea4.js:783)
    at __webpack_require__ (application-a71cca030c96f6cdcea4.js:20)
    at webpackContext (application-a71cca030c96f6cdcea4.js:1173)
    at eval (fromRequireContext.js?f05c:13)
    at Object.eval [as getConstructor] (fromRequireContextWithGlobalFallback.js?7c97:13)
    at Object.mountComponents (index.js?0542:82)

Am I doing something wrong, or is this a bug?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions