Skip to content
代码片段 群组 项目
提交 be71903c 编辑于 作者: Stanislav Lashmanov's avatar Stanislav Lashmanov 提交者: Lukas 'Eipi' Eipert
浏览文件

Configure vite_ruby via config file

This allows us to pass the port and whether vite_ruby is actually
enabled from the GDK to GitLab Rails and feature specs.
上级 41345010
No related branches found
No related tags found
无相关合并请求
......@@ -113,4 +113,4 @@ tags.temp
# Vite uses dotenv and suggests to ignore local-only env files. See
# https://vitejs.dev/guide/env-and-mode.html#env-files
*.local
/config/vite.gdk.json
......@@ -116,7 +116,7 @@ def self.endpoint_id_for_action(action_name)
content_security_policy do |p|
next if p.directives.blank?
if Rails.env.development? && Feature.enabled?(:vite)
if helpers.vite_enabled?
vite_host = ViteRuby.instance.config.host
vite_port = ViteRuby.instance.config.port
vite_origin = "#{vite_host}:#{vite_port}"
......
# frozen_string_literal: true
module ViteHelper
private
def vite_enabled?
# vite is not production ready yet
return false if Rails.env.production?
# Enable vite if explicitly turned on in the GDK
return ViteRuby.env['VITE_ENABLED'] if ViteRuby.env.key?('VITE_ENABLED')
def vite_enabled
Feature.enabled?(:vite) && !Rails.env.test? && vite_running
end
def vite_running
ViteRuby.instance.dev_server_running?
# Enable vite the legacy way (in case GDK hasn't been updated)
# This is going to be removed with https://gitlab.com/gitlab-org/gitlab/-/issues/431041
Rails.env.development? ? Feature.enabled?(:vite) : false
end
end
......@@ -16,7 +16,7 @@ def prefetch_link_tag(source)
end
def webpack_bundle_tag(bundle)
if vite_running
if vite_enabled?
vite_javascript_tag bundle
else
javascript_include_tag(*webpack_entrypoint_paths(bundle))
......@@ -24,6 +24,8 @@ def webpack_bundle_tag(bundle)
end
def webpack_preload_asset_tag(asset, options = {})
return if vite_enabled?
path = Gitlab::Webpack::Manifest.asset_paths(asset).first
if options.delete(:prefetch)
......@@ -38,7 +40,7 @@ def webpack_preload_asset_tag(asset, options = {})
end
def webpack_controller_bundle_tags
return if Feature.enabled?(:vite) && !Rails.env.test?
return if vite_enabled?
chunks = []
......
......@@ -50,7 +50,7 @@
= webpack_bundle_tag 'legacy_sentry'
= webpack_bundle_tag 'performance_bar' if performance_bar_enabled?
- if vite_enabled
- if vite_enabled?
%meta{ name: 'controller-path', content: controller_full_path }
- if Rails.env.development?
= vite_client_tag
......
......@@ -2,17 +2,8 @@
"all": {
"sourceCodeDir": "app/assets",
"entrypointsDir": "javascripts/entrypoints",
"devServerConnectTimeout": 3
},
"development": {
"autoBuild": true,
"port": 3038,
"publicOutputDir": "vite-dev",
"host": "localhost",
"port": 3038
},
"test": {
"autoBuild": true,
"publicOutputDir": "vite-test",
"port": 3037
"devServerConnectTimeout": 3
}
}
# frozen_string_literal: true
require 'yaml'
require_relative '../lib/vite_gdk'
ViteGdk.load_gdk_vite_config
# frozen_string_literal: true
module ViteGdk
def self.load_gdk_vite_config
# can't use Rails.env.production? here because this file is required outside of Gitlab app instance
return if ENV['RAILS_ENV'] == 'production'
return unless File.exist?(vite_gdk_config_path)
config = YAML.safe_load_file(vite_gdk_config_path)
enabled = config.fetch('enabled', false)
ViteRuby.env['VITE_ENABLED'] = enabled
return unless enabled
ViteRuby.configure(
port: Integer(config.fetch('port', 3038))
)
end
def self.vite_gdk_config_path
File.join(__dir__, '../config/vite.gdk.json')
end
end
......@@ -43,7 +43,7 @@
stub_feature_flags(vite: true)
allow(helper).to receive(:vite_javascript_tag).and_return('vite')
allow(helper).to receive(:vite_running).and_return(true)
allow(helper).to receive(:vite_enabled?).and_return(true)
end
describe '#webpack_bundle_tag' do
......
# frozen_string_literal: true
require 'spec_helper'
VITE_GDK_CONFIG_FILEPATH = "config/vite.gdk.json"
RSpec.describe ViteGdk, feature_category: :tooling do
before do
allow(ViteRuby).to receive(:configure)
allow(ViteRuby.env).to receive(:[]=)
allow(YAML).to receive(:safe_load_file)
end
describe '#load_gdk_vite_config' do
context 'when not in production environment' do
before do
stub_env('RAILS_ENV', nil)
end
context 'when it loads file successfully' do
it 'configures ViteRuby' do
expect(File).to receive(:exist?) do |file_path|
expect(file_path).to end_with(VITE_GDK_CONFIG_FILEPATH)
end.and_return(true)
expect(YAML).to receive(:safe_load_file) do |file_path|
expect(file_path).to end_with(VITE_GDK_CONFIG_FILEPATH)
end.and_return('enabled' => true, 'port' => 3038)
expect(ViteRuby).to receive(:configure).with(port: 3038)
expect(ViteRuby.env).to receive(:[]=).with('VITE_ENABLED', true)
described_class.load_gdk_vite_config
end
end
context 'when config file is missing' do
it 'does nothing' do
expect(File).to receive(:exist?) do |file_path|
expect(file_path).to end_with(VITE_GDK_CONFIG_FILEPATH)
end.and_return(false)
expect(ViteRuby).not_to receive(:configure)
expect(ViteRuby.env).not_to receive(:[]=).with('VITE_ENABLED', false)
expect(ViteRuby.env).not_to receive(:[]=).with('VITE_ENABLED', true)
described_class.load_gdk_vite_config
end
end
end
context 'when in production environment' do
before do
stub_env('RAILS_ENV', 'production')
end
it 'does not load and configure ViteRuby' do
expect(YAML).not_to receive(:safe_load_file)
expect(ViteRuby).not_to receive(:configure)
expect(ViteRuby.env).not_to receive(:[]=).with('VITE_ENABLED')
described_class.load_gdk_vite_config
end
end
end
end
......@@ -327,6 +327,7 @@
# Postgres is the primary data source, and ClickHouse only when enabled in certain cases.
stub_feature_flags(clickhouse_data_collection: false)
# This is going to be removed with https://gitlab.com/gitlab-org/gitlab/-/issues/431041
stub_feature_flags(vite: false)
else
unstub_all_feature_flags
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册