Skip to content

Commit 224fc6f

Browse files
authored
Port back master changes to 3.4 (#1464)
1 parent fbb7dcc commit 224fc6f

27 files changed

+143
-39
lines changed

.travis.yml

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ language: ruby
33
before_install:
44
- gem update --system
55
rvm:
6-
- 2.2.9
7-
- 2.3.6
8-
- 2.4.3
9-
- 2.5.0
6+
- 2.3.7
7+
- 2.4.4
8+
- 2.5.1
109
- ruby-head
1110
gemfile:
1211
- gemfiles/Gemfile-rails.4.2.x

CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
**Please note that Webpacker 3.1.0 and 3.1.1 has some serious bugs so please consider using either 3.0.2 or 3.2.0**
1+
**Please note that Webpacker 3.1.0 and 3.1.1 have some serious bugs so please consider using either 3.0.2 or 3.2.0**
22

33
## [3.4.3] - 2018-04-3
44

@@ -37,7 +37,7 @@
3737

3838
### Breaking changes
3939

40-
- Fixes #1281 by installing binstubs only as local executables. To upgrade:
40+
- Fixes [#1281](https://github.com/rails/webpacker/issues/1281) by installing binstubs only as local executables. To upgrade:
4141

4242
```
4343
bundle exec rails webpacker:binstubs

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ GEM
5757
activesupport (>= 4.2.0)
5858
i18n (0.9.5)
5959
concurrent-ruby (~> 1.0)
60-
loofah (2.2.0)
60+
loofah (2.2.2)
6161
crass (~> 1.0.2)
6262
nokogiri (>= 1.5.9)
6363
mail (2.7.0)

MIT-LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2016-2017 David Heinemeier Hansson, Basecamp
1+
Copyright (c) 2016-2018 David Heinemeier Hansson, Basecamp
22

33
Permission is hereby granted, free of charge, to any person obtaining
44
a copy of this software and associated documentation files (the

README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Or add it to your `Gemfile`:
8181

8282
```ruby
8383
# Gemfile
84-
gem 'webpacker', '~> 3.3'
84+
gem 'webpacker', '~> 3.4'
8585

8686
# OR if you prefer to use master
8787
gem 'webpacker', git: 'https://github.com/rails/webpacker.git'
@@ -154,7 +154,7 @@ in the `app/javascript/packs/*.js` files and automatically reload the browser to
154154
./bin/webpack-dev-server
155155
156156
# watcher
157-
./bin/webpack --colors --progress
157+
./bin/webpack --watch --colors --progress
158158
159159
# standalone build
160160
./bin/webpack
@@ -186,7 +186,7 @@ WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server
186186
**Note:** You need to allow webpack-dev-server host as an allowed origin for `connect-src` if you are running your application in a restrict CSP environment (like Rails 5.2+). This can be done in Rails 5.2+ in the CSP initializer `config/initializers/content_security_policy.rb` with a snippet like this:
187187

188188
```ruby
189-
p.connect_src :self, :https, 'http://localhost:3035', 'ws://localhost:3035' if Rails.env.development?
189+
policy.connect_src :self, :https, 'http://localhost:3035', 'ws://localhost:3035' if Rails.env.development?
190190
```
191191

192192
**Note:** Don't forget to prefix `ruby` when running these binstubs on Windows
@@ -248,7 +248,7 @@ bundle exec rails webpacker:compile
248248

249249
### Upgrading
250250

251-
You can run following commands to upgrade Webpacker to the latest stable version. This process involves upgrading the gem and related npm modules:
251+
You can run following commands to upgrade Webpacker to the latest stable version. This process involves upgrading the gem and related JavaScript packages:
252252

253253
```bash
254254
bundle update webpacker
@@ -258,7 +258,7 @@ yarn add webpack-dev-server@^2.11.1
258258

259259
### Yarn Integrity
260260

261-
By default, in development, webpacker runs a yarn integrity check to ensure that all local npm packages are up-to-date. This is similar to what bundler does currently in Rails, but for JavaScript packages. If your system is out of date, then Rails will not initialize. You will be asked to upgrade your local npm packages by running `yarn install`.
261+
By default, in development, webpacker runs a yarn integrity check to ensure that all local JavaScript packages are up-to-date. This is similar to what bundler does currently in Rails, but for JavaScript packages. If your system is out of date, then Rails will not initialize. You will be asked to upgrade your local JavaScript packages by running `yarn install`.
262262

263263
To turn off this option, you will need to override the default by adding a new config option to your Rails development environment configuration file (`config/environment/development.rb`):
264264

@@ -324,9 +324,9 @@ with the following code:
324324

325325
```ruby
326326
if Rails.env.development?
327-
p.script_src :self, :https, :unsafe_eval
327+
policy.script_src :self, :https, :unsafe_eval
328328
else
329-
p.script_src :self, :https
329+
policy.script_src :self, :https
330330
end
331331
```
332332

@@ -352,9 +352,9 @@ configuration:
352352

353353
```ruby
354354
if Rails.env.development?
355-
p.script_src :self, :https, :unsafe_eval
355+
policy.script_src :self, :https, :unsafe_eval
356356
else
357-
p.script_src :self, :https
357+
policy.script_src :self, :https
358358
end
359359
```
360360
You can read more about this in the [Vue docs](https://vuejs.org/v2/guide/installation.html#CSP-environments).
@@ -486,7 +486,7 @@ Webpacker::Compiler.watched_paths << 'bower_components'
486486

487487
## Deployment
488488

489-
Webpacker hooks up a new `webpacker:compile` task to `assets:precompile`, which gets run whenever you run `assets:precompile`. If you are not using Sprockets, `webpacker:compile` is automatically aliased to `assets:precompile`. Remember to set NODE_ENV environment variable to production during deployment or when running this rake task.
489+
Webpacker hooks up a new `webpacker:compile` task to `assets:precompile`, which gets run whenever you run `assets:precompile`. If you are not using Sprockets, `webpacker:compile` is automatically aliased to `assets:precompile`. Similar to sprockets both rake tasks will compile packs in production mode but will use `RAILS_ENV` to load configuration from `config/webpacker.yml` (if available).
490490

491491
## Docs
492492

docs/cloud9.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ dev_server:
8383
use_local_ip: false
8484
```
8585
86-
into the these custom configuration:
86+
into these custom configuration:
8787
8888
```yaml
8989
dev_server:

docs/webpack.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = {
2929
}
3030

3131
// config/webpack/environment.js
32-
const environment = require('./environment')
32+
const { environment } = require('@rails/webpacker')
3333
const customConfig = require('./custom')
3434

3535
// Set nested object prop using path notation

lib/install/angular.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
say "This can be done in Rails 5.2+ for development environment in the CSP initializer", :yellow
1515
say "config/initializers/content_security_policy.rb with a snippet like this:", :yellow
1616
say "if Rails.env.development?", :yellow
17-
say " p.script_src :self, :https, :unsafe_eval", :yellow
17+
say " policy.script_src :self, :https, :unsafe_eval", :yellow
1818
say "else", :yellow
19-
say " p.script_src :self, :https", :yellow
19+
say " policy.script_src :self, :https", :yellow
2020
say "end", :yellow
2121
end
2222

lib/install/bin/webpack

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env ruby
22

33
ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development"
4-
ENV["NODE_ENV"] ||= ENV["NODE_ENV"] || "development"
4+
ENV["NODE_ENV"] ||= "development"
55

66
require "pathname"
77
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",

lib/install/bin/webpack-dev-server

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env ruby
22

33
ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development"
4-
ENV["NODE_ENV"] ||= ENV["NODE_ENV"] || "development"
4+
ENV["NODE_ENV"] ||= "development"
55

66
require "pathname"
77
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",

lib/install/coffee.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222
say "Installing all Coffeescript dependencies"
2323
run "yarn add coffeescript@1.12.7 coffee-loader"
2424

25-
say "Webpacker now supports Coffeeescript 🎉", :green
25+
say "Webpacker now supports Coffeescript 🎉", :green

lib/install/examples/erb/hello_erb.js.erb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Run this example by adding <%= javascript_pack_tag 'hello_erb' %> to the head of your layout file,
2-
// like app/views/layouts/application.html.erb.
1+
// Run this example by adding <%%= javascript_pack_tag 'hello_erb' %> to the head of your layout file,
2+
// like app/views/layouts/application.html.erb. Don't forget to uncomment erb tag after adding it to your layout file.
33

44
<% name = 'Erb' %>
55

lib/install/template.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
check_yarn_integrity_config = ->(value) { <<CONFIG }
2121
# Verifies that versions and hashed value of the package contents in the project's package.json
22-
config.webpacker.check_yarn_integrity = #{value}
22+
config.webpacker.check_yarn_integrity = #{value}
2323
CONFIG
2424

2525
if Rails::VERSION::MAJOR >= 5
@@ -50,7 +50,7 @@
5050
say "You need to allow webpack-dev-server host as allowed origin for connect-src.", :yellow
5151
say "This can be done in Rails 5.2+ for development environment in the CSP initializer", :yellow
5252
say "config/initializers/content_security_policy.rb with a snippet like this:", :yellow
53-
say "p.connect_src :self, :https, \"http://localhost:3035\", \"ws://localhost:3035\" if Rails.env.development?", :yellow
53+
say "policy.connect_src :self, :https, \"http://localhost:3035\", \"ws://localhost:3035\" if Rails.env.development?", :yellow
5454
end
5555

5656
say "Webpacker successfully installed 🎉 🍰", :green

lib/install/vue.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
say "This can be done in Rails 5.2+ for development environment in the CSP initializer", :yellow
3232
say "config/initializers/content_security_policy.rb with a snippet like this:", :yellow
3333
say "if Rails.env.development?", :yellow
34-
say " p.script_src :self, :https, :unsafe_eval", :yellow
34+
say " policy.script_src :self, :https, :unsafe_eval", :yellow
3535
say "else", :yellow
36-
say " p.script_src :self, :https", :yellow
36+
say " policy.script_src :self, :https", :yellow
3737
say "end", :yellow
3838
end
3939

lib/tasks/webpacker.rake

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
tasks = {
2+
"webpacker:info" => "Provides information on Webpacker's environment",
23
"webpacker:install" => "Installs and setup webpack with Yarn",
34
"webpacker:compile" => "Compiles webpack bundles based on environment",
45
"webpacker:clobber" => "Removes the webpack compiled output directory",

lib/tasks/webpacker/info.rake

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require "webpacker/version"
2+
3+
namespace :webpacker do
4+
desc "Provide information on Webpacker's environment"
5+
task :info do
6+
$stdout.puts "Ruby: #{`ruby --version`}"
7+
$stdout.puts "Rails: #{Rails.version}"
8+
$stdout.puts "Webpacker: #{Webpacker::VERSION}"
9+
$stdout.puts "Node: #{`node --version`}"
10+
$stdout.puts "Yarn: #{`yarn --version`}"
11+
12+
$stdout.puts "\n"
13+
$stdout.puts "@rails/webpacker: \n#{`npm list @rails/webpacker version`}"
14+
15+
$stdout.puts "Is bin/webpack present?: #{File.exist? 'bin/webpack'}"
16+
$stdout.puts "Is bin/webpack-dev-server present?: #{File.exist? 'bin/webpack-dev-server'}"
17+
$stdout.puts "Is bin/yarn present?: #{File.exist? 'bin/yarn'}"
18+
end
19+
end

lib/webpacker.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def instance
1515

1616
def with_node_env(env)
1717
original = ENV["NODE_ENV"]
18-
ENV["NODE_ENV"] = "production"
18+
ENV["NODE_ENV"] = env
1919
yield
2020
ensure
2121
ENV["NODE_ENV"] = original

lib/webpacker/compiler.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ def initialize(webpacker)
1919
def compile
2020
if stale?
2121
record_compilation_digest
22-
run_webpack
22+
run_webpack.tap do |success|
23+
remove_compilation_digest if !success
24+
end
2325
else
2426
true
2527
end
@@ -50,10 +52,14 @@ def record_compilation_digest
5052
compilation_digest_path.write(watched_files_digest)
5153
end
5254

55+
def remove_compilation_digest
56+
compilation_digest_path.delete if compilation_digest_path.exist?
57+
end
58+
5359
def run_webpack
5460
logger.info "Compiling…"
5561

56-
sterr, stdout, status = Open3.capture3(webpack_env, "#{RbConfig.ruby} ./bin/webpack")
62+
stdout, sterr , status = Open3.capture3(webpack_env, "#{RbConfig.ruby} ./bin/webpack")
5763

5864
if status.success?
5965
logger.info "Compiled all packs in #{config.public_output_path}"

lib/webpacker/dev_server.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ def fetch(key)
6060
end
6161

6262
def defaults
63-
config.send(:defaults)[:dev_server]
63+
config.send(:defaults)[:dev_server] || {}
6464
end
6565
end

lib/webpacker/dev_server_proxy.rb

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ def rewrite_response(response)
1111
def perform_request(env)
1212
if env["PATH_INFO"].start_with?("/#{public_output_uri_path}") && Webpacker.dev_server.running?
1313
env["HTTP_HOST"] = env["HTTP_X_FORWARDED_HOST"] = env["HTTP_X_FORWARDED_SERVER"] = Webpacker.dev_server.host_with_port
14+
env["HTTP_X_FORWARDED_PROTO"] = env["HTTP_X_FORWARDED_SCHEME"] = Webpacker.dev_server.protocol
15+
unless Webpacker.dev_server.https?
16+
env["HTTPS"] = env["HTTP_X_FORWARDED_SSL"] = "off"
17+
end
1418
env["SCRIPT_NAME"] = ""
1519

1620
super(env)

lib/webpacker/helper.rb

+10
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ def asset_pack_url(name, **options)
3333
end
3434
end
3535

36+
# Creates a image tag that references the named pack file.
37+
#
38+
# Example:
39+
#
40+
# <%= image_pack_tag 'application.png', size: '16x10', alt: 'Edit Entry' %>
41+
# <img alt='Edit Entry' src='/packs/application-k344a6d59eef8632c9d1.png' width='16' height='10' />
42+
def image_pack_tag(name, **options)
43+
image_tag(asset_path(Webpacker.manifest.lookup!(name)), **options)
44+
end
45+
3646
# Creates a script tag that references the named pack file, as compiled by webpack per the entries list
3747
# in config/webpack/shared.js. By default, this list is auto-generated to match everything in
3848
# app/javascript/packs/*.js. In production mode, the digested reference is automatically looked up.

lib/webpacker/railtie.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ class Webpacker::Engine < ::Rails::Engine
4646
end
4747

4848
initializer "webpacker.proxy" do |app|
49-
if Rails.env.development?
49+
insert_middleware = Webpacker.config.dev_server.present? rescue nil
50+
if insert_middleware
5051
app.middleware.insert_before 0,
5152
Rails::VERSION::MAJOR >= 5 ?
5253
Webpacker::DevServerProxy : "Webpacker::DevServerProxy", ssl_verify_none: true

package/__tests__/config.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
/* global test expect, describe */
22

3-
const { chdirTestApp, chdirCwd } = require('../utils/helpers')
3+
const { chdirCwd, chdirTestApp } = require('../utils/helpers')
44

55
chdirTestApp()
66

77
const config = require('../config')
88

99
describe('Config', () => {
10+
beforeEach(() => jest.resetModules())
1011
afterAll(chdirCwd)
1112

13+
test('public path', () => {
14+
process.env.RAILS_ENV = 'development'
15+
delete process.env.RAILS_RELATIVE_URL_ROOT
16+
const config = require('../config')
17+
expect(config.publicPath).toEqual('/packs/')
18+
})
19+
20+
// also tests removal of extra slashes
21+
test('public path with relative root', () => {
22+
process.env.RAILS_ENV = 'development'
23+
process.env.RAILS_RELATIVE_URL_ROOT = '/foo'
24+
const config = require('../config')
25+
expect(config.publicPath).toEqual('/foo/packs/')
26+
})
27+
1228
test('should return extensions as listed in app config', () => {
1329
expect(config.extensions).toEqual([
1430
'.js',

package/config.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ if (isArray(app.extensions) && app.extensions.length) delete defaults.extensions
2020

2121
const config = deepMerge(defaults, app)
2222
config.outputPath = resolve('public', config.public_output_path)
23-
config.publicPath = `/${config.public_output_path}/`.replace(/([^:]\/)\/+/g, '$1')
23+
24+
let publicPath = `/${config.public_output_path}/`
25+
// Add prefix to publicPath.
26+
if (process.env.RAILS_RELATIVE_URL_ROOT) {
27+
publicPath = `/${process.env.RAILS_RELATIVE_URL_ROOT}${publicPath}`
28+
}
29+
30+
// Remove extra slashes.
31+
config.publicPath = publicPath.replace(/(^\/|[^:]\/)\/+/g, '$1')
2432

2533
module.exports = config

0 commit comments

Comments
 (0)