-
Notifications
You must be signed in to change notification settings - Fork 759
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
importing .jsx module from Engine, Webpacker 3, Rails 5.1.4 #786
Comments
When using Webpacker, Did you add any JSX converter to your webpack setup? |
@rmosolgo yes, I have a default |
This seems a pretty relevant PR on Webpacker. |
@BookOfGreg yes, although I follow the closing argument there and have solved the issue by making a a |
I suppose a part of the problem is Webpack won't have access to the 'invisible' gem files like Sprockets would, so it would be impossible to to compile them. The engine could hypothetically do something like have a rake task that generates a compiled JS in the parent's JS directory when it gets installed, similar to how the Devise engine dumps it's views in the parent if needed. I could document both solutions in the wiki once I get onto that task. |
@BookOfGreg , though the 'dumping' sounds awful ;-). Gem's own In the engine, my 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 The {
"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 |
That's a nice solution! Thank you for the work you put in here, this looks like it does solve the original issue. My apologies for not seeing what you meant originally, I'm much more a ruby dev than a JS dev so missed your meaning. Could you add something to the Readme as a starter under an "Engine" header? You deserve the contributor badge with a great solution like that. |
@BookOfGreg will do, thanks! BTW I have another one for binding the react components to dom elements via JS MutationObserver – this solves all the module lookup (each module registers itself to be bound to a DOM element) and Turbolinks, pjax etc. issues. Will post an example later. |
@BookOfGreg here it is: https://github.com/reactjs/react-rails/wiki/Using-JS-components-from-Rails-engines Please feel free to update as you see fit. |
Thanks @tomasc for your work on the wiki, I linked it from the Wiki's home. |
thanks @BookOfGreg ! |
Help us help you! Please choose one:
react-rails
, so I've included the stack trace and the exact steps which make it crash.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 am trying to import
.jsx
components from Rails engine.Engine
In the engine I have added simple
HelloWorld.jsx
underapp/javascript/components
:App
And in the main app (Webpacker 3, Rails 5.1.4, unreleased
react-rails
coming frommaster
branch):resolved_paths: ['engine/app/javascript']
inwebpacker.yml
Engine.jsx
underjavascript/component
that contains:Then in my
application.html.erb
, I added thejavascript_pack_tag 'application'
and tried to load the react component:It seems the component is being found, but the
.jsx
preprocessor is not being recognised/applied, as I am getting the following error:Am I doing something wrong, or is this a bug?
The text was updated successfully, but these errors were encountered: