Skip to content

Commit

Permalink
Add webpack react and render default component
Browse files Browse the repository at this point in the history
  • Loading branch information
HacksForHugs committed Oct 8, 2017
1 parent 08f495d commit fcf3254
Show file tree
Hide file tree
Showing 18 changed files with 5,768 additions and 1 deletion.
26 changes: 26 additions & 0 deletions .babelrc
@@ -0,0 +1,26 @@
{
"presets": [
[
"env",
{
"modules": false,
"targets": {
"browsers": "> 1%",
"uglify": true
},
"useBuiltIns": true
}
],
"react"
],
"plugins": [
"syntax-dynamic-import",
"transform-object-rest-spread",
[
"transform-class-properties",
{
"spec": true
}
]
]
}
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -45,3 +45,6 @@ bower.json

# Ignore DS_Store
.DS_Store
/public/packs
/public/packs-test
/node_modules
3 changes: 3 additions & 0 deletions .postcssrc.yml
@@ -0,0 +1,3 @@
plugins:
postcss-smart-import: {}
postcss-cssnext: {}
5 changes: 5 additions & 0 deletions app/controllers/pages_controller.rb
@@ -0,0 +1,5 @@
# Static page controller
class PagesController < ApplicationController
def home
end
end
10 changes: 10 additions & 0 deletions app/javascript/packs/application.js
@@ -0,0 +1,10 @@
/* eslint no-console:0 */
// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
//
// To reference this file, add <%= javascript_pack_tag 'application' %> to the appropriate
// layout file, like app/views/layouts/application.html.erb

console.log('Hello World from Webpacker')
26 changes: 26 additions & 0 deletions app/javascript/packs/hello_react.jsx
@@ -0,0 +1,26 @@
// Run this example by adding <%= javascript_pack_tag 'hello_react' %> to the head of your layout file,
// like app/views/layouts/application.html.erb. All it does is render <div>Hello React</div> at the bottom
// of the page.

import React from 'react'
import ReactDOM from 'react-dom'
import PropTypes from 'prop-types'

const Hello = props => (
<div>Hello {props.name}!</div>
)

Hello.defaultProps = {
name: 'David'
}

Hello.propTypes = {
name: PropTypes.string
}

document.addEventListener('DOMContentLoaded', () => {
ReactDOM.render(
<Hello name="React" />,
document.body.appendChild(document.createElement('div')),
)
})
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Expand Up @@ -9,6 +9,7 @@
</head>

<body>
<%= javascript_pack_tag 'application' %>
<%= yield %>
</body>
</html>
1 change: 1 addition & 0 deletions app/views/pages/home.html.erb
@@ -0,0 +1 @@
<%= javascript_pack_tag 'hello_react' %>
27 changes: 27 additions & 0 deletions bin/webpack
@@ -0,0 +1,27 @@
#!/usr/bin/env ruby
$stdout.sync = true

require "shellwords"

ENV["RAILS_ENV"] ||= "development"
RAILS_ENV = ENV["RAILS_ENV"]

ENV["NODE_ENV"] ||= RAILS_ENV
NODE_ENV = ENV["NODE_ENV"]

APP_PATH = File.expand_path("../", __dir__)
NODE_MODULES_PATH = File.join(APP_PATH, "node_modules")
WEBPACK_CONFIG = File.join(APP_PATH, "config/webpack/#{NODE_ENV}.js")

unless File.exist?(WEBPACK_CONFIG)
puts "Webpack configuration not found."
puts "Please run bundle exec rails webpacker:install to install webpacker"
exit!
end

env = { "NODE_PATH" => NODE_MODULES_PATH.shellescape }
cmd = [ "#{NODE_MODULES_PATH}/.bin/webpack", "--config", WEBPACK_CONFIG ] + ARGV

Dir.chdir(APP_PATH) do
exec env, *cmd
end
68 changes: 68 additions & 0 deletions bin/webpack-dev-server
@@ -0,0 +1,68 @@
#!/usr/bin/env ruby
$stdout.sync = true

require "shellwords"
require "yaml"
require "socket"

ENV["RAILS_ENV"] ||= "development"
RAILS_ENV = ENV["RAILS_ENV"]

ENV["NODE_ENV"] ||= RAILS_ENV
NODE_ENV = ENV["NODE_ENV"]

APP_PATH = File.expand_path("../", __dir__)
CONFIG_FILE = File.join(APP_PATH, "config/webpacker.yml")
NODE_MODULES_PATH = File.join(APP_PATH, "node_modules")
WEBPACK_CONFIG = File.join(APP_PATH, "config/webpack/#{NODE_ENV}.js")

DEFAULT_LISTEN_HOST_ADDR = NODE_ENV == 'development' ? 'localhost' : '0.0.0.0'

def args(key)
index = ARGV.index(key)
index ? ARGV[index + 1] : nil
end

begin
dev_server = YAML.load_file(CONFIG_FILE)[RAILS_ENV]["dev_server"]

HOSTNAME = args('--host') || dev_server["host"]
PORT = args('--port') || dev_server["port"]
HTTPS = ARGV.include?('--https') || dev_server["https"]
DEV_SERVER_ADDR = "http#{"s" if HTTPS}://#{HOSTNAME}:#{PORT}"
LISTEN_HOST_ADDR = args('--listen-host') || DEFAULT_LISTEN_HOST_ADDR

rescue Errno::ENOENT, NoMethodError
$stdout.puts "Webpack dev_server configuration not found in #{CONFIG_FILE}."
$stdout.puts "Please run bundle exec rails webpacker:install to install webpacker"
exit!
end

begin
server = TCPServer.new(LISTEN_HOST_ADDR, PORT)
server.close

rescue Errno::EADDRINUSE
$stdout.puts "Another program is running on port #{PORT}. Set a new port in #{CONFIG_FILE} for dev_server"
exit!
end

# Delete supplied host, port and listen-host CLI arguments
["--host", "--port", "--listen-host"].each do |arg|
ARGV.delete(args(arg))
ARGV.delete(arg)
end

env = { "NODE_PATH" => NODE_MODULES_PATH.shellescape }

cmd = [
"#{NODE_MODULES_PATH}/.bin/webpack-dev-server", "--progress", "--color",
"--config", WEBPACK_CONFIG,
"--host", LISTEN_HOST_ADDR,
"--public", "#{HOSTNAME}:#{PORT}",
"--port", PORT.to_s
] + ARGV

Dir.chdir(APP_PATH) do
exec env, *cmd
end
3 changes: 3 additions & 0 deletions config/routes.rb
@@ -1,3 +1,6 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'pages#home'
namespace :api, defaults: { format: :json } do
end
end
3 changes: 3 additions & 0 deletions config/webpack/development.js
@@ -0,0 +1,3 @@
const environment = require('./environment')

module.exports = environment.toWebpackConfig()
3 changes: 3 additions & 0 deletions config/webpack/environment.js
@@ -0,0 +1,3 @@
const { environment } = require('@rails/webpacker')

module.exports = environment
3 changes: 3 additions & 0 deletions config/webpack/production.js
@@ -0,0 +1,3 @@
const environment = require('./environment')

module.exports = environment.toWebpackConfig()
3 changes: 3 additions & 0 deletions config/webpack/test.js
@@ -0,0 +1,3 @@
const environment = require('./environment')

module.exports = environment.toWebpackConfig()
56 changes: 56 additions & 0 deletions config/webpacker.yml
@@ -0,0 +1,56 @@
# Note: You must restart bin/webpack-dev-server for changes to take effect

default: &default
source_path: app/javascript
source_entry_path: packs
public_output_path: packs
cache_path: tmp/cache/webpacker

# Additional paths webpack should lookup modules
# ['app/assets', 'engine/foo/app/assets']
resolved_paths: []

# Reload manifest.json on all requests so we reload latest compiled packs
cache_manifest: false

extensions:
- .coffee
- .erb
- .js
- .jsx
- .ts
- .vue
- .sass
- .scss
- .css
- .png
- .svg
- .gif
- .jpeg
- .jpg

development:
<<: *default
compile: true

dev_server:
host: localhost
port: 8080
hmr: false
https: false

test:
<<: *default
compile: true

# Compile test packs to a separate directory
public_output_path: packs-test

production:
<<: *default

# Production depends on precompilation of packs prior to booting for performance.
compile: false

# Cache manifest.json for performance
cache_manifest: true
11 changes: 10 additions & 1 deletion package.json
@@ -1,5 +1,14 @@
{
"name": "rmi",
"private": true,
"dependencies": {}
"dependencies": {
"@rails/webpacker": "^3.0.1",
"babel-preset-react": "^6.24.1",
"prop-types": "^15.6.0",
"react": "^16.0.0",
"react-dom": "^16.0.0"
},
"devDependencies": {
"webpack-dev-server": "^2.8.2"
}
}

0 comments on commit fcf3254

Please sign in to comment.