Skip to content

Commit

Permalink
Fix mini_racer build problems linux (rubyjs/mini_racer#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
spenserw committed Apr 21, 2022
1 parent 59690de commit df57449
Show file tree
Hide file tree
Showing 26 changed files with 1,557 additions and 32 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ end
gem "react_on_rails", "13.0.2"

gem "shakapacker", "6.1.1"

gem "mini_racer", "~> 0.6.2"
10 changes: 9 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ GEM
jbuilder (2.11.5)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
libv8-node (16.10.0.0)
libv8-node (16.10.0.0-x86_64-linux)
loofah (2.14.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
Expand All @@ -112,6 +114,8 @@ GEM
method_source (1.0.0)
mini_mime (1.1.2)
mini_portile2 (2.8.0)
mini_racer (0.6.2)
libv8-node (~> 16.10.0.0)
minitest (5.15.0)
msgpack (1.4.5)
net-imap (0.2.3)
Expand All @@ -133,6 +137,8 @@ GEM
nokogiri (1.13.3)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
nokogiri (1.13.3-x86_64-linux)
racc (~> 1.4)
public_suffix (4.0.6)
puma (5.6.2)
nio4r (~> 2.0)
Expand Down Expand Up @@ -222,12 +228,14 @@ GEM

PLATFORMS
ruby
x86_64-linux

DEPENDENCIES
bootsnap
capybara
debug
jbuilder
mini_racer (~> 0.6.2)
puma (~> 5.0)
rails (~> 7.0.2, >= 7.0.2.3)
react_on_rails (= 13.0.2)
Expand All @@ -243,4 +251,4 @@ RUBY VERSION
ruby 2.7.0p0

BUNDLED WITH
2.1.2
2.3.9
5 changes: 5 additions & 0 deletions Procfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Procfile for development using HMR
# You can run these commands in separate shells
rails: bundle exec rails s -p 3000
wp-client: HMR=true bin/webpacker-dev-server
wp-server: HMR=true SERVER_BUNDLE_ONLY=yes bin/webpacker --watch
9 changes: 9 additions & 0 deletions Procfile.dev-static
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# You can run these commands in separate shells
web: rails s -p 3000

# Next line runs a watch process with webpack to compile the changed files.
# When making frequent changes to client side assets, you will prefer building webpack assets
# upon saving rather than when you refresh your browser page.
# Note, if using React on Rails localization you will need to run
# `bundle exec rake react_on_rails:locale` before you run bin/webpacker
webpack: sh -c 'rm -rf public/packs/* || true && bin/webpacker -w'
9 changes: 9 additions & 0 deletions app/controllers/hello_world_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class HelloWorldController < ApplicationController
layout "hello_world"

def index
@hello_world_props = { name: "Stranger" }
end
end
26 changes: 26 additions & 0 deletions app/javascript/bundles/HelloWorld/components/HelloWorld.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import PropTypes from 'prop-types';
import React, { useState } from 'react';
import style from './HelloWorld.module.css';

const HelloWorld = (props) => {
const [name, setName] = useState(props.name);

return (
<div>
<h3>Hello, {name}!</h3>
<hr />
<form>
<label className={style.bright} htmlFor="name">
Say hello to:
<input id="name" type="text" value={name} onChange={(e) => setName(e.target.value)} />
</label>
</form>
</div>
);
};

HelloWorld.propTypes = {
name: PropTypes.string.isRequired, // this is passed from the Rails view
};

export default HelloWorld;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.bright {
color: green;
font-weight: bold;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import HelloWorld from './HelloWorld';
// This could be specialized for server rendering
// For example, if using React-Router, we'd have the SSR setup here.

export default HelloWorld;
8 changes: 8 additions & 0 deletions app/javascript/packs/hello-world-bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ReactOnRails from 'react-on-rails';

import HelloWorld from '../bundles/HelloWorld/components/HelloWorld';

// This is how react_on_rails can see the HelloWorld in the browser.
ReactOnRails.register({
HelloWorld,
});
8 changes: 8 additions & 0 deletions app/javascript/packs/server-bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ReactOnRails from 'react-on-rails';

import HelloWorld from '../bundles/HelloWorld/components/HelloWorldServer';

// This is how react_on_rails can see the HelloWorld in the browser.
ReactOnRails.register({
HelloWorld,
});
2 changes: 2 additions & 0 deletions app/views/hello_world/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Hello World</h1>
<%= react_component("HelloWorld", props: @hello_world_props, prerender: false) %>
13 changes: 13 additions & 0 deletions app/views/layouts/hello_world.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>ReactOnRailsWithWebpacker</title>
<%= csrf_meta_tags %>
<%= javascript_pack_tag 'hello-world-bundle' %>
<%= stylesheet_pack_tag 'hello-world-bundle' %>
</head>

<body>
<%= yield %>
</body>
</html>
33 changes: 33 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// The source code including full typescript support is available at:
// https://github.com/shakacode/react_on_rails_demo_ssr_hmr/blob/master/babel.config.js

module.exports = function (api) {
const defaultConfigFunc = require('shakapacker/package/babel/preset.js')
const resultConfig = defaultConfigFunc(api)
const isProductionEnv = api.env('production')

const changesOnDefault = {
presets: [
[
'@babel/preset-react',
{
development: !isProductionEnv,
useBuiltIns: true
}
]
].filter(Boolean),
plugins: [
process.env.WEBPACK_SERVE && 'react-refresh/babel',
isProductionEnv && ['babel-plugin-transform-react-remove-prop-types',
{
removeImport: true
}
]
].filter(Boolean),
}

resultConfig.presets = [...resultConfig.presets, ...changesOnDefault.presets]
resultConfig.plugins = [...resultConfig.plugins, ...changesOnDefault.plugins ]

return resultConfig
}
45 changes: 45 additions & 0 deletions config/initializers/react_on_rails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

# See https://github.com/shakacode/react_on_rails/blob/master/docs/basics/configuration.md
# for many more options.

ReactOnRails.configure do |config|
# This configures the script to run to build the production assets by webpack. Set this to nil
# if you don't want react_on_rails building this file for you.
# If nil, then the standard shakacode/shakapacker assets:precompile will run
# config.build_production_command = nil

################################################################################
################################################################################
# TEST CONFIGURATION OPTIONS
# Below options are used with the use of this test helper:
# ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
################################################################################

# If you are using this in your spec_helper.rb (or rails_helper.rb):
#
# ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
#
# with rspec then this controls what yarn command is run
# to automatically refresh your webpack assets on every test run.
#
# Alternately, you can remove the `ReactOnRails::TestHelper.configure_rspec_to_compile_assets`
# and set the config/webpacker.yml option for test to true.
config.build_test_command = "RAILS_ENV=test bin/webpacker"

################################################################################
################################################################################
# SERVER RENDERING OPTIONS
################################################################################
# This is the file used for server rendering of React when using `(prerender: true)`
# If you are never using server rendering, you should set this to "".
# Note, there is only one server bundle, unlike JavaScript where you want to minimize the size
# of the JS sent to the client. For the server rendering, React on Rails creates a pool of
# JavaScript execution instances which should handle any component requested.
#
# While you may configure this to be the same as your client bundle file, this file is typically
# different. You should have ONE server bundle which can create all of your server rendered
# React components.
#
config.server_bundle_js_file = "server-bundle.js"
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
get 'hello_world', to: 'hello_world#index'
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Defines the root path route ("/")
Expand Down
18 changes: 18 additions & 0 deletions config/webpack/clientWebpackConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// The source code including full typescript support is available at:
// https://github.com/shakacode/react_on_rails_demo_ssr_hmr/blob/master/config/webpack/clientWebpackConfig.js

const commonWebpackConfig = require('./commonWebpackConfig');

const configureClient = () => {
const clientConfig = commonWebpackConfig();

// server-bundle is special and should ONLY be built by the serverConfig
// In case this entry is not deleted, a very strange "window" not found
// error shows referring to window["webpackJsonp"]. That is because the
// client config is going to try to load chunks.
delete clientConfig.entry['server-bundle'];

return clientConfig;
};

module.exports = configureClient;
16 changes: 16 additions & 0 deletions config/webpack/commonWebpackConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// The source code including full typescript support is available at:
// https://github.com/shakacode/react_on_rails_demo_ssr_hmr/blob/master/config/webpack/commonWebpackConfig.js

// Common configuration applying to client and server configuration
const { webpackConfig: baseClientWebpackConfig, merge } = require('shakapacker');

const commonOptions = {
resolve: {
extensions: ['.css', '.ts', '.tsx'],
},
};

// Copy the object using merge b/c the baseClientWebpackConfig and commonOptions are mutable globals
const commonWebpackConfig = () => merge({}, baseClientWebpackConfig, commonOptions);

module.exports = commonWebpackConfig;
26 changes: 26 additions & 0 deletions config/webpack/development.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// The source code including full typescript support is available at:
// https://github.com/shakacode/react_on_rails_demo_ssr_hmr/blob/master/config/webpack/development.js

const { devServer, inliningCss } = require('shakapacker');

const webpackConfig = require('./webpackConfig');

const developmentEnvOnly = (clientWebpackConfig, _serverWebpackConfig) => {
// plugins
if (inliningCss) {
// Note, when this is run, we're building the server and client bundles in separate processes.
// Thus, this plugin is not applied to the server bundle.

// eslint-disable-next-line global-require
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
clientWebpackConfig.plugins.push(
new ReactRefreshWebpackPlugin({
overlay: {
sockPort: devServer.port,
},
}),
);
}
};

module.exports = webpackConfig(developmentEnvOnly);
10 changes: 10 additions & 0 deletions config/webpack/production.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// The source code including full typescript support is available at:
// https://github.com/shakacode/react_on_rails_demo_ssr_hmr/blob/master/config/webpack/production.js

const webpackConfig = require('./webpackConfig');

const productionEnvOnly = (_clientWebpackConfig, _serverWebpackConfig) => {
// place any code here that is for production only
};

module.exports = webpackConfig(productionEnvOnly);
Loading

0 comments on commit df57449

Please sign in to comment.