importing .jsx module from Engine, Webpacker 3, Rails 5.1.4
See original GitHub issueHelp 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']
inwebpacker.yml
- added
Engine.jsx
underjavascript/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:
- Created 6 years ago
- Comments:11 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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.):So far I located all the JS under
/package/src
, but it might make more sense to pull the source directly fromapp/javascript
…The
package.json
looks like this, basically:You can then
yarn link
this package to your app, ev. runyarn build --watch
in the engine to have the code re-compiled on change for development.When using Webpacker,
react-rails
does not automatically convert JSX. Instead, it leaves this repsonsiblity tobabel
& 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#reactDid you add any JSX converter to your webpack setup?