Commit 7b99ed56 authored by Mike Greiling's avatar Mike Greiling Committed by Stan Hu

Clean up webpack integration settings

Previously, an integration with the webpack-rails gem required
juggling and consolidating two separate webpack configurations
the one used by the rails gem and the one defined in gitlab.yml

This update moves all webpack integration settings into the
gitlab.yml file and updates references such that they don't need
to be overridden in confusing ways.
parent 79de9f65
...@@ -57,10 +57,12 @@ module WebpackHelper ...@@ -57,10 +57,12 @@ module WebpackHelper
end end
def webpack_public_host def webpack_public_host
if Rails.env.test? && Rails.configuration.webpack.dev_server.enabled # We do not proxy the webpack output in the 'test' environment,
host = Rails.configuration.webpack.dev_server.host # so we must reference the webpack dev server directly.
port = Rails.configuration.webpack.dev_server.port if Rails.env.test? && Gitlab.config.webpack.dev_server.enabled
protocol = Rails.configuration.webpack.dev_server.https ? 'https' : 'http' host = Gitlab.config.webpack.dev_server.host
port = Gitlab.config.webpack.dev_server.port
protocol = Gitlab.config.webpack.dev_server.https ? 'https' : 'http'
"#{protocol}://#{host}:#{port}" "#{protocol}://#{host}:#{port}"
else else
ActionController::Base.asset_host.try(:chomp, '/') ActionController::Base.asset_host.try(:chomp, '/')
...@@ -68,8 +70,8 @@ module WebpackHelper ...@@ -68,8 +70,8 @@ module WebpackHelper
end end
def webpack_public_path def webpack_public_path
relative_path = Rails.application.config.relative_url_root relative_path = Gitlab.config.gitlab.relative_url_root
webpack_path = Rails.application.config.webpack.public_path webpack_path = Gitlab.config.webpack.public_path
File.join(webpack_public_host.to_s, relative_path.to_s, webpack_path.to_s, '') File.join(webpack_public_host.to_s, relative_path.to_s, webpack_path.to_s, '')
end end
end end
...@@ -151,20 +151,6 @@ module Gitlab ...@@ -151,20 +151,6 @@ module Gitlab
# like if you have constraints or database-specific column types # like if you have constraints or database-specific column types
config.active_record.schema_format = :sql config.active_record.schema_format = :sql
# Configure webpack
config.webpack = ActiveSupport::OrderedOptions.new
config.webpack.config_file = "config/webpack.config.js"
config.webpack.output_dir = "public/assets/webpack"
config.webpack.public_path = "assets/webpack"
config.webpack.manifest_filename = "manifest.json"
# Webpack dev server configuration is handled in initializers/static_files.rb
config.webpack.dev_server = ActiveSupport::OrderedOptions.new
config.webpack.dev_server.enabled = false
config.webpack.dev_server.host = 'localhost'
config.webpack.dev_server.port = 3808
config.webpack.dev_server.https = false
config.action_mailer.delivery_job = "ActionMailer::MailDeliveryJob" config.action_mailer.delivery_job = "ActionMailer::MailDeliveryJob"
# Enable the asset pipeline # Enable the asset pipeline
......
...@@ -785,10 +785,15 @@ Settings['gitaly'] ||= Settingslogic.new({}) ...@@ -785,10 +785,15 @@ Settings['gitaly'] ||= Settingslogic.new({})
# Webpack settings # Webpack settings
# #
Settings['webpack'] ||= Settingslogic.new({}) Settings['webpack'] ||= Settingslogic.new({})
Settings.webpack['config_file'] ||= 'config/webpack.config.js'
Settings.webpack['output_dir'] ||= 'public/assets/webpack'
Settings.webpack['public_path'] ||= 'assets/webpack'
Settings.webpack['manifest_filename'] ||= 'manifest.json'
Settings.webpack['dev_server'] ||= Settingslogic.new({}) Settings.webpack['dev_server'] ||= Settingslogic.new({})
Settings.webpack.dev_server['enabled'] ||= false Settings.webpack.dev_server['enabled'] ||= false
Settings.webpack.dev_server['host'] ||= 'localhost' Settings.webpack.dev_server['host'] ||= 'localhost'
Settings.webpack.dev_server['port'] ||= 3808 Settings.webpack.dev_server['port'] ||= 3808
Settings.webpack.dev_server['https'] ||= false
# #
# Monitoring settings # Monitoring settings
......
...@@ -15,32 +15,14 @@ if app.config.public_file_server.enabled ...@@ -15,32 +15,14 @@ if app.config.public_file_server.enabled
# If webpack-dev-server is configured, proxy webpack's public directory # If webpack-dev-server is configured, proxy webpack's public directory
# instead of looking for static assets # instead of looking for static assets
dev_server = Gitlab.config.webpack.dev_server if Gitlab.config.webpack.dev_server.enabled && Rails.env.development?
app.config.middleware.insert_before(
if dev_server.enabled Gitlab::Middleware::Static,
settings = { Gitlab::Webpack::DevServerMiddleware,
enabled: true, proxy_path: Gitlab.config.webpack.public_path,
host: dev_server.host, proxy_host: Gitlab.config.webpack.dev_server.host,
manifest_host: dev_server.host, proxy_port: Gitlab.config.webpack.dev_server.port,
manifest_port: dev_server.port, proxy_https: Gitlab.config.webpack.dev_server.https
port: dev_server.port )
}
if Rails.env.development?
settings.merge!(
host: Gitlab.config.gitlab.host,
port: Gitlab.config.gitlab.port,
https: Gitlab.config.gitlab.https
)
app.config.middleware.insert_before(
Gitlab::Middleware::Static,
Gitlab::Webpack::DevServerMiddleware,
proxy_path: app.config.webpack.public_path,
proxy_host: dev_server.host,
proxy_port: dev_server.port
)
end
app.config.webpack.dev_server.merge!(settings)
end end
end end
...@@ -18,6 +18,7 @@ const IS_DEV_SERVER = process.env.WEBPACK_DEV_SERVER === 'true'; ...@@ -18,6 +18,7 @@ const IS_DEV_SERVER = process.env.WEBPACK_DEV_SERVER === 'true';
const IS_EE = require('./helpers/is_ee_env'); const IS_EE = require('./helpers/is_ee_env');
const DEV_SERVER_HOST = process.env.DEV_SERVER_HOST || 'localhost'; const DEV_SERVER_HOST = process.env.DEV_SERVER_HOST || 'localhost';
const DEV_SERVER_PORT = parseInt(process.env.DEV_SERVER_PORT, 10) || 3808; const DEV_SERVER_PORT = parseInt(process.env.DEV_SERVER_PORT, 10) || 3808;
const DEV_SERVER_HTTPS = process.env.DEV_SERVER_HTTPS && process.env.DEV_SERVER_HTTPS !== 'false';
const DEV_SERVER_LIVERELOAD = IS_DEV_SERVER && process.env.DEV_SERVER_LIVERELOAD !== 'false'; const DEV_SERVER_LIVERELOAD = IS_DEV_SERVER && process.env.DEV_SERVER_LIVERELOAD !== 'false';
const WEBPACK_REPORT = process.env.WEBPACK_REPORT && process.env.WEBPACK_REPORT !== 'false'; const WEBPACK_REPORT = process.env.WEBPACK_REPORT && process.env.WEBPACK_REPORT !== 'false';
const WEBPACK_MEMORY_TEST = const WEBPACK_MEMORY_TEST =
...@@ -551,6 +552,7 @@ module.exports = { ...@@ -551,6 +552,7 @@ module.exports = {
devServer: { devServer: {
host: DEV_SERVER_HOST, host: DEV_SERVER_HOST,
port: DEV_SERVER_PORT, port: DEV_SERVER_PORT,
https: DEV_SERVER_HTTPS,
disableHostCheck: true, disableHostCheck: true,
headers: { headers: {
'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Origin': '*',
......
...@@ -11,8 +11,15 @@ module Gitlab ...@@ -11,8 +11,15 @@ module Gitlab
@proxy_host = opts.fetch(:proxy_host, 'localhost') @proxy_host = opts.fetch(:proxy_host, 'localhost')
@proxy_port = opts.fetch(:proxy_port, 3808) @proxy_port = opts.fetch(:proxy_port, 3808)
@proxy_path = opts[:proxy_path] if opts[:proxy_path] @proxy_path = opts[:proxy_path] if opts[:proxy_path]
@proxy_scheme = opts[:proxy_https] ? 'https' : 'http'
super(app, backend: "http://#{@proxy_host}:#{@proxy_port}", **opts) super(app, backend: "#{@proxy_scheme}://#{@proxy_host}:#{@proxy_port}", **opts)
end
# disable SSL check since any cert used here will likely be self-signed
def rewrite_env(env)
env["rack.ssl_verify_none"] = true
env
end end
def perform_request(env) def perform_request(env)
......
...@@ -9,7 +9,7 @@ module Gitlab ...@@ -9,7 +9,7 @@ module Gitlab
# Raised if we can't read our webpack manifest for whatever reason # Raised if we can't read our webpack manifest for whatever reason
class ManifestLoadError < StandardError class ManifestLoadError < StandardError
def initialize(message, orig) def initialize(message, orig)
super "#{message} (original error #{orig})" super "#{message}\n\n(original error #{orig.class.name}: #{orig})"
end end
end end
...@@ -35,7 +35,7 @@ module Gitlab ...@@ -35,7 +35,7 @@ module Gitlab
# Can be either a string or an array of strings. # Can be either a string or an array of strings.
# Do not include source maps as they are not javascript # Do not include source maps as they are not javascript
[dll_assets, entrypoint["assets"]].flatten.reject { |p| p =~ /.*\.map$/ }.map do |p| [dll_assets, entrypoint["assets"]].flatten.reject { |p| p =~ /.*\.map$/ }.map do |p|
"/#{::Rails.configuration.webpack.public_path}/#{p}" "/#{Gitlab.config.webpack.public_path}/#{p}"
end end
else else
raise AssetMissingError, "Can't find asset '#{source}' in webpack manifest" raise AssetMissingError, "Can't find asset '#{source}' in webpack manifest"
...@@ -50,7 +50,7 @@ module Gitlab ...@@ -50,7 +50,7 @@ module Gitlab
# Can be either a string or an array of strings. # Can be either a string or an array of strings.
# Do not include source maps as they are not javascript # Do not include source maps as they are not javascript
[paths].flatten.reject { |p| p =~ /.*\.map$/ }.map do |p| [paths].flatten.reject { |p| p =~ /.*\.map$/ }.map do |p|
"/#{::Rails.configuration.webpack.public_path}/#{p}" "/#{Gitlab.config.webpack.public_path}/#{p}"
end end
else else
raise AssetMissingError, "Can't find entry point '#{source}' in webpack manifest" raise AssetMissingError, "Can't find entry point '#{source}' in webpack manifest"
...@@ -68,7 +68,7 @@ module Gitlab ...@@ -68,7 +68,7 @@ module Gitlab
end end
def manifest def manifest
if ::Rails.configuration.webpack.dev_server.enabled if Gitlab.config.webpack.dev_server.enabled
# Don't cache if we're in dev server mode, manifest may change ... # Don't cache if we're in dev server mode, manifest may change ...
load_manifest load_manifest
else else
...@@ -78,7 +78,7 @@ module Gitlab ...@@ -78,7 +78,7 @@ module Gitlab
end end
def load_manifest def load_manifest
data = if ::Rails.configuration.webpack.dev_server.enabled data = if Gitlab.config.webpack.dev_server.enabled
load_dev_server_manifest load_dev_server_manifest
else else
load_static_manifest load_static_manifest
...@@ -88,9 +88,10 @@ module Gitlab ...@@ -88,9 +88,10 @@ module Gitlab
end end
def load_dev_server_manifest def load_dev_server_manifest
host = ::Rails.configuration.webpack.dev_server.manifest_host host = Gitlab.config.webpack.dev_server.host
port = ::Rails.configuration.webpack.dev_server.manifest_port port = Gitlab.config.webpack.dev_server.port
uri = Addressable::URI.new(scheme: 'http', host: host, port: port, path: dev_server_path) scheme = Gitlab.config.webpack.dev_server.https ? 'https' : 'http'
uri = Addressable::URI.new(scheme: scheme, host: host, port: port, path: dev_server_path)
# localhost could be blocked via Gitlab::HTTP # localhost could be blocked via Gitlab::HTTP
response = HTTParty.get(uri.to_s, verify: false) # rubocop:disable Gitlab/HTTParty response = HTTParty.get(uri.to_s, verify: false) # rubocop:disable Gitlab/HTTParty
...@@ -98,25 +99,28 @@ module Gitlab ...@@ -98,25 +99,28 @@ module Gitlab
return response.body if response.code == 200 return response.body if response.code == 200
raise "HTTP error #{response.code}" raise "HTTP error #{response.code}"
rescue OpenSSL::SSL::SSLError, EOFError => e
ssl_status = Gitlab.config.webpack.dev_server.https ? ' over SSL' : ''
raise ManifestLoadError.new("Could not connect to webpack-dev-server at #{uri}#{ssl_status}.\n\nIs SSL enabled? Check that settings in `gitlab.yml` and webpack-dev-server match.", e)
rescue => e rescue => e
raise ManifestLoadError.new("Could not load manifest from webpack-dev-server at #{uri} - is it running, and is stats-webpack-plugin loaded?", e) raise ManifestLoadError.new("Could not load manifest from webpack-dev-server at #{uri}.\n\nIs webpack-dev-server running? Try running `gdk status webpack` or `gdk tail webpack`.", e)
end end
def load_static_manifest def load_static_manifest
File.read(static_manifest_path) File.read(static_manifest_path)
rescue => e rescue => e
raise ManifestLoadError.new("Could not load compiled manifest from #{static_manifest_path} - have you run `rake webpack:compile`?", e) raise ManifestLoadError.new("Could not load compiled manifest from #{static_manifest_path}.\n\nHave you run `rake gitlab:assets:compile`?", e)
end end
def static_manifest_path def static_manifest_path
::Rails.root.join( ::Rails.root.join(
::Rails.configuration.webpack.output_dir, Gitlab.config.webpack.output_dir,
::Rails.configuration.webpack.manifest_filename Gitlab.config.webpack.manifest_filename
) )
end end
def dev_server_path def dev_server_path
"/#{::Rails.configuration.webpack.public_path}/#{::Rails.configuration.webpack.manifest_filename}" "/#{Gitlab.config.webpack.public_path}/#{Gitlab.config.webpack.manifest_filename}"
end end
end end
end end
......
...@@ -40,18 +40,15 @@ RSpec.describe Gitlab::Webpack::Manifest do ...@@ -40,18 +40,15 @@ RSpec.describe Gitlab::Webpack::Manifest do
before do before do
# Test that config variables work while we're here # Test that config variables work while we're here
::Rails.configuration.webpack.dev_server.host = 'hostname' allow(Gitlab.config.webpack.dev_server).to receive_messages(host: 'hostname', port: 2000, https: false)
::Rails.configuration.webpack.dev_server.port = 1999 allow(Gitlab.config.webpack).to receive(:manifest_filename).and_return('my_manifest.json')
::Rails.configuration.webpack.dev_server.manifest_host = 'hostname' allow(Gitlab.config.webpack).to receive(:public_path).and_return('public_path')
::Rails.configuration.webpack.dev_server.manifest_port = 2000 allow(Gitlab.config.webpack).to receive(:output_dir).and_return('manifest_output')
::Rails.configuration.webpack.manifest_filename = "my_manifest.json"
::Rails.configuration.webpack.public_path = "public_path"
::Rails.configuration.webpack.output_dir = "manifest_output"
end end
context "with dev server enabled" do context "with dev server enabled" do
before do before do
::Rails.configuration.webpack.dev_server.enabled = true allow(Gitlab.config.webpack.dev_server).to receive(:enabled).and_return(true)
stub_request(:get, "http://hostname:2000/public_path/my_manifest.json").to_return(body: manifest, status: 200) stub_request(:get, "http://hostname:2000/public_path/my_manifest.json").to_return(body: manifest, status: 200)
end end
...@@ -60,7 +57,7 @@ RSpec.describe Gitlab::Webpack::Manifest do ...@@ -60,7 +57,7 @@ RSpec.describe Gitlab::Webpack::Manifest do
it_behaves_like "a valid manifest" it_behaves_like "a valid manifest"
it "errors if we can't find the manifest" do it "errors if we can't find the manifest" do
::Rails.configuration.webpack.manifest_filename = "broken.json" allow(Gitlab.config.webpack).to receive(:manifest_filename).and_return('broken.json')
stub_request(:get, "http://hostname:2000/public_path/broken.json").to_raise(SocketError) stub_request(:get, "http://hostname:2000/public_path/broken.json").to_raise(SocketError)
expect { Gitlab::Webpack::Manifest.asset_paths("entry1") }.to raise_error(Gitlab::Webpack::Manifest::ManifestLoadError) expect { Gitlab::Webpack::Manifest.asset_paths("entry1") }.to raise_error(Gitlab::Webpack::Manifest::ManifestLoadError)
...@@ -99,7 +96,7 @@ RSpec.describe Gitlab::Webpack::Manifest do ...@@ -99,7 +96,7 @@ RSpec.describe Gitlab::Webpack::Manifest do
context "with dev server disabled" do context "with dev server disabled" do
before do before do
::Rails.configuration.webpack.dev_server.enabled = false allow(Gitlab.config.webpack.dev_server).to receive(:enabled).and_return(false)
allow(File).to receive(:read).with(::Rails.root.join("manifest_output/my_manifest.json")).and_return(manifest) allow(File).to receive(:read).with(::Rails.root.join("manifest_output/my_manifest.json")).and_return(manifest)
end end
...@@ -107,7 +104,7 @@ RSpec.describe Gitlab::Webpack::Manifest do ...@@ -107,7 +104,7 @@ RSpec.describe Gitlab::Webpack::Manifest do
it_behaves_like "a valid manifest" it_behaves_like "a valid manifest"
it "errors if we can't find the manifest" do it "errors if we can't find the manifest" do
::Rails.configuration.webpack.manifest_filename = "broken.json" allow(Gitlab.config.webpack).to receive(:manifest_filename).and_return('broken.json')
allow(File).to receive(:read).with(::Rails.root.join("manifest_output/broken.json")).and_raise(Errno::ENOENT) allow(File).to receive(:read).with(::Rails.root.join("manifest_output/broken.json")).and_raise(Errno::ENOENT)
expect { Gitlab::Webpack::Manifest.asset_paths("entry1") }.to raise_error(Gitlab::Webpack::Manifest::ManifestLoadError) expect { Gitlab::Webpack::Manifest.asset_paths("entry1") }.to raise_error(Gitlab::Webpack::Manifest::ManifestLoadError)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment