Skip to content

Commit 43eab81

Browse files
committed
Update to Webpacker 3
1 parent 131969e commit 43eab81

15 files changed

+5447
-666
lines changed

.babelrc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"presets": [
3+
["env", {
4+
"modules": false,
5+
"targets": {
6+
"browsers": "> 1%",
7+
"uglify": true
8+
},
9+
"useBuiltIns": true
10+
}]
11+
],
12+
13+
"plugins": [
14+
"syntax-dynamic-import",
15+
"transform-object-rest-spread",
16+
["transform-class-properties", { "spec": true }]
17+
]
18+
}

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
!/log/.keep
1414
!/tmp/.keep
1515

16-
/node_modules
1716
/yarn-error.log
1817

1918
.byebug_history
@@ -23,3 +22,8 @@
2322

2423
# Figaro
2524
config/application.yml
25+
26+
# Webpacker
27+
/public/packs
28+
/public/packs-test
29+
/node_modules

.postcssrc.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
plugins:
2+
postcss-smart-import: {}
3+
postcss-cssnext: {}

Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ ruby File.read(version_file).strip
1414
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
1515
gem 'rails', '~> 5.1.3'
1616

17+
# Use newest version of webpacker explicitly until Rails updates
18+
gem 'webpacker', '~> 3.0'
19+
1720
# Use postgresql as the database for Active Record
1821
gem 'pg', '~> 0.18'
1922

Gemfile.lock

+7
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ GEM
169169
rack (>= 1.2.0)
170170
rack-protection (2.0.0)
171171
rack
172+
rack-proxy (0.6.2)
173+
rack
172174
rack-test (0.6.3)
173175
rack (>= 1.0)
174176
rack-timeout (0.4.2)
@@ -302,6 +304,10 @@ GEM
302304
addressable (>= 2.3.6)
303305
crack (>= 0.3.2)
304306
hashdiff
307+
webpacker (3.0.1)
308+
activesupport (>= 4.2)
309+
rack-proxy (>= 0.6.1)
310+
railties (>= 4.2)
305311
websocket-driver (0.6.5)
306312
websocket-extensions (>= 0.1.0)
307313
websocket-extensions (0.1.2)
@@ -356,6 +362,7 @@ DEPENDENCIES
356362
uglifier (>= 1.3.0)
357363
web-console (>= 3.3.0)
358364
webmock
365+
webpacker (~> 3.0)
359366

360367
RUBY VERSION
361368
ruby 2.4.1p111

app/javascript/packs/application.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* eslint no-console:0 */
2+
// This file is automatically compiled by Webpack, along with any other files
3+
// present in this directory. You're encouraged to place your actual application logic in
4+
// a relevant structure within app/javascript and only use these pack files to reference
5+
// that code so it'll be compiled.
6+
//
7+
// To reference this file, add <%= javascript_pack_tag 'application' %> to the appropriate
8+
// layout file, like app/views/layouts/application.html.erb
9+
10+
console.log('Hello World from Webpacker')

bin/webpack

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env ruby
2+
$stdout.sync = true
3+
4+
require "shellwords"
5+
6+
ENV["RAILS_ENV"] ||= "development"
7+
RAILS_ENV = ENV["RAILS_ENV"]
8+
9+
ENV["NODE_ENV"] ||= RAILS_ENV
10+
NODE_ENV = ENV["NODE_ENV"]
11+
12+
APP_PATH = File.expand_path("../", __dir__)
13+
NODE_MODULES_PATH = File.join(APP_PATH, "node_modules")
14+
WEBPACK_CONFIG = File.join(APP_PATH, "config/webpack/#{NODE_ENV}.js")
15+
16+
unless File.exist?(WEBPACK_CONFIG)
17+
puts "Webpack configuration not found."
18+
puts "Please run bundle exec rails webpacker:install to install webpacker"
19+
exit!
20+
end
21+
22+
env = { "NODE_PATH" => NODE_MODULES_PATH.shellescape }
23+
cmd = [ "#{NODE_MODULES_PATH}/.bin/webpack", "--config", WEBPACK_CONFIG ] + ARGV
24+
25+
Dir.chdir(APP_PATH) do
26+
exec env, *cmd
27+
end

bin/webpack-dev-server

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env ruby
2+
$stdout.sync = true
3+
4+
require "shellwords"
5+
require "yaml"
6+
require "socket"
7+
8+
ENV["RAILS_ENV"] ||= "development"
9+
RAILS_ENV = ENV["RAILS_ENV"]
10+
11+
ENV["NODE_ENV"] ||= RAILS_ENV
12+
NODE_ENV = ENV["NODE_ENV"]
13+
14+
APP_PATH = File.expand_path("../", __dir__)
15+
CONFIG_FILE = File.join(APP_PATH, "config/webpacker.yml")
16+
NODE_MODULES_PATH = File.join(APP_PATH, "node_modules")
17+
WEBPACK_CONFIG = File.join(APP_PATH, "config/webpack/#{NODE_ENV}.js")
18+
19+
DEFAULT_LISTEN_HOST_ADDR = NODE_ENV == 'development' ? 'localhost' : '0.0.0.0'
20+
21+
def args(key)
22+
index = ARGV.index(key)
23+
index ? ARGV[index + 1] : nil
24+
end
25+
26+
begin
27+
dev_server = YAML.load_file(CONFIG_FILE)[RAILS_ENV]["dev_server"]
28+
29+
HOSTNAME = args('--host') || dev_server["host"]
30+
PORT = args('--port') || dev_server["port"]
31+
HTTPS = ARGV.include?('--https') || dev_server["https"]
32+
DEV_SERVER_ADDR = "http#{"s" if HTTPS}://#{HOSTNAME}:#{PORT}"
33+
LISTEN_HOST_ADDR = args('--listen-host') || DEFAULT_LISTEN_HOST_ADDR
34+
35+
rescue Errno::ENOENT, NoMethodError
36+
$stdout.puts "Webpack dev_server configuration not found in #{CONFIG_FILE}."
37+
$stdout.puts "Please run bundle exec rails webpacker:install to install webpacker"
38+
exit!
39+
end
40+
41+
begin
42+
server = TCPServer.new(LISTEN_HOST_ADDR, PORT)
43+
server.close
44+
45+
rescue Errno::EADDRINUSE
46+
$stdout.puts "Another program is running on port #{PORT}. Set a new port in #{CONFIG_FILE} for dev_server"
47+
exit!
48+
end
49+
50+
# Delete supplied host, port and listen-host CLI arguments
51+
["--host", "--port", "--listen-host"].each do |arg|
52+
ARGV.delete(args(arg))
53+
ARGV.delete(arg)
54+
end
55+
56+
env = { "NODE_PATH" => NODE_MODULES_PATH.shellescape }
57+
58+
cmd = [
59+
"#{NODE_MODULES_PATH}/.bin/webpack-dev-server", "--progress", "--color",
60+
"--config", WEBPACK_CONFIG,
61+
"--host", LISTEN_HOST_ADDR,
62+
"--public", "#{HOSTNAME}:#{PORT}",
63+
"--port", PORT.to_s
64+
] + ARGV
65+
66+
Dir.chdir(APP_PATH) do
67+
exec env, *cmd
68+
end

config/webpack/development.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const environment = require('./environment')
2+
3+
module.exports = environment.toWebpackConfig()

config/webpack/environment.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const { environment } = require('@rails/webpacker')
2+
3+
module.exports = environment

config/webpack/production.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const environment = require('./environment')
2+
3+
module.exports = environment.toWebpackConfig()

config/webpack/test.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const environment = require('./environment')
2+
3+
module.exports = environment.toWebpackConfig()

config/webpacker.yml

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Note: You must restart bin/webpack-dev-server for changes to take effect
2+
3+
default: &default
4+
source_path: app/javascript
5+
source_entry_path: packs
6+
public_output_path: packs
7+
cache_path: tmp/cache/webpacker
8+
9+
# Additional paths webpack should lookup modules
10+
# ['app/assets', 'engine/foo/app/assets']
11+
resolved_paths: []
12+
13+
# Reload manifest.json on all requests so we reload latest compiled packs
14+
cache_manifest: false
15+
16+
extensions:
17+
- .coffee
18+
- .erb
19+
- .js
20+
- .jsx
21+
- .ts
22+
- .vue
23+
- .sass
24+
- .scss
25+
- .css
26+
- .png
27+
- .svg
28+
- .gif
29+
- .jpeg
30+
- .jpg
31+
32+
development:
33+
<<: *default
34+
compile: true
35+
36+
dev_server:
37+
host: localhost
38+
port: 3035
39+
hmr: false
40+
https: false
41+
42+
test:
43+
<<: *default
44+
compile: true
45+
46+
# Compile test packs to a separate directory
47+
public_output_path: packs-test
48+
49+
production:
50+
<<: *default
51+
52+
# Production depends on precompilation of packs prior to booting for performance.
53+
compile: false
54+
55+
# Cache manifest.json for performance
56+
cache_manifest: true

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "rails_new",
33
"private": true,
44
"dependencies": {
5+
"@rails/webpacker": "^3.0.1",
56
"bootstrap": "4.0.0-beta",
67
"jquery": "^3.2.1",
78
"jquery-ujs": "^1.2.2",
@@ -12,6 +13,7 @@
1213
"eslint-config-airbnb": "^15.0.1",
1314
"eslint-plugin-import": "^2.2.0",
1415
"eslint-plugin-jsx-a11y": "^5.0.3",
15-
"eslint-plugin-react": "^7.0.1"
16+
"eslint-plugin-react": "^7.0.1",
17+
"webpack-dev-server": "^2.7.1"
1618
}
1719
}

0 commit comments

Comments
 (0)