question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
tomasccommented, Oct 25, 2017

@BookOfGreg , though the ‘dumping’ sounds awful 😉.

Gem’s own package.json works just fine. One just needs to add a bit of config for webpack and build the JS per release, eventually push to NPM.

In the engine, my webpack.config.js looks like this (I am using CoffeeScript 2, you might want to alter the config for .jsx etc.):

const path = require('path');
const webpack = require('webpack')

module.exports = {
  module: {
    rules: [
      {
        test: /\.coffee$/,
        use: ['babel-loader', 'coffee-loader']
      }
    ]
  },
  entry: './package/src/index.js',
  output: {
    library: '@tomasc/myengine',
    libraryTarget: 'umd',
    umdNamedDefine: true,
    filename: 'index.js',
    path: path.resolve(__dirname, 'package/dist')
  },
  resolve: {
    extensions: ['.coffee', '.js']
  }
};

So far I located all the JS under /package/src, but it might make more sense to pull the source directly from app/javascript

The package.json looks like this, basically:

{
  "name": "@tomasc/myengine",
  "version": "1.0.0",
  "description": "…",
  "main": "package/dist/index.js",
  "files": [
    "package"
  ],
  "repository": {
    "type": "git",
    "url": "git+https://github.com/tomasc/myengine.git"
  },
  "author": "…",
  "license": "…",
  "homepage": "…",
  "scripts": {
    "build": "webpack"
  },
  "dependencies": {
    "@rails/webpacker": "^3.0.1",
    "babel-preset-react": "^6.24.1",
    "coffee-loader": "^0.8.0",
    "coffeescript": "^2.0.1",
    "prop-types": "^15.5.10",
    "react": "^15.6.1",
    "react-dom": "^15.6.1",
    "webpacker-react": "^0.3.2"
  },
  "devDependencies": {
    "babel-preset-env": "^1.6.0",
    "babel-preset-es2015": "^6.24.1",
    "webpack-dev-server": "^2.8.2"
  }
}

You can then yarn link this package to your app, ev. run yarn build --watch in the engine to have the code re-compiled on change for development.

3reactions
rmosolgocommented, Sep 20, 2017

When using Webpacker, react-rails does not automatically convert JSX. Instead, it leaves this repsonsiblity to babel & your webpack config. So, you should convert JS as part of your webpack setup, for example, with webpackers React integration: https://github.com/rails/webpacker#react

Did you add any JSX converter to your webpack setup?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpacker - Ruby on Rails Guides
WebpackerThis guide will show you how to install and use Webpacker to package JavaScript, CSS, and other assets for the client-side of your...
Read more >
File: README — Documentation for webpacker (5.4.3)
You can either add Webpacker during setup of a new Rails 5.1+ application ... If you have styles imported in your pack file,...
Read more >
@rails/webpacker - npm
Use webpack to manage app-like JavaScript modules in Rails. Latest version: 5.4.3, last published: a year ago. Start using @rails/webpacker ...
Read more >
How to handle images in a Rails / Webpacker / React app?
import React from 'react' import MyImage from 'images/my_image.svg' const MyComponent = props => <img src={MyImage} /> export default ...
Read more >
Rails 5 meets Webpack and React - MagmaLabs Blog
New application. # Available Rails 5.1+ $ rails new myapp --webpack · Existing Rails application. Add webpacker gem to the Gemfile: · Install ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found