debian-mirror-gitlab/lib/tasks/gitlab/assets.rake

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

174 lines
6.3 KiB
Ruby
Raw Normal View History

2020-04-22 19:07:51 +05:30
# frozen_string_literal: true
module Tasks
module Gitlab
module Assets
2022-11-25 23:54:43 +05:30
FOSS_ASSET_FOLDERS = %w[app/assets fixtures/emojis vendor/assets].freeze
2021-09-30 23:02:18 +05:30
EE_ASSET_FOLDERS = %w[ee/app/assets].freeze
2021-11-18 22:05:49 +05:30
JH_ASSET_FOLDERS = %w[jh/app/assets].freeze
2022-11-25 23:54:43 +05:30
# In the new caching strategy, we check the assets hash sum *before* compiling
# the app/assets/javascripts/locale/**/app.js files. That means the hash sum
# must depend on locale/**/gitlab.po.
JS_ASSET_PATTERNS = %w[*.js config/**/*.js locale/**/gitlab.po].freeze
JS_ASSET_FILES = %w[
package.json
yarn.lock
babel.config.js
config/webpack.config.js
].freeze
2023-06-20 00:43:36 +05:30
# Ruby gems might emit assets which have an impact on compilation
# or have a direct impact on asset compilation (e.g. scss) and therefore
# we should compile when these change
RAILS_ASSET_FILES = %w[
Gemfile
Gemfile.lock
].freeze
2022-11-25 23:54:43 +05:30
EXCLUDE_PATTERNS = %w[
app/assets/javascripts/locale/**/app.js
].freeze
PUBLIC_ASSETS_DIR = 'public/assets'
HEAD_ASSETS_SHA256_HASH_ENV = 'GITLAB_ASSETS_HASH'
CACHED_ASSETS_SHA256_HASH_FILE = 'cached-assets-hash.txt'
def self.master_assets_sha256
@master_assets_sha256 ||=
if File.exist?(Tasks::Gitlab::Assets::CACHED_ASSETS_SHA256_HASH_FILE)
File.read(Tasks::Gitlab::Assets::CACHED_ASSETS_SHA256_HASH_FILE)
else
'missing!'
end
end
def self.head_assets_sha256
@head_assets_sha256 ||= ENV.fetch(Tasks::Gitlab::Assets::HEAD_ASSETS_SHA256_HASH_ENV) do
Tasks::Gitlab::Assets.sha256_of_assets_impacting_compilation(verbose: false)
end
end
2020-04-22 19:07:51 +05:30
2022-11-25 23:54:43 +05:30
def self.sha256_of_assets_impacting_compilation(verbose: true)
2020-04-22 19:07:51 +05:30
start_time = Time.now
2022-11-25 23:54:43 +05:30
asset_files = assets_impacting_compilation
puts "Generating the SHA256 hash for #{asset_files.size} Webpack-related assets..." if verbose
2020-04-22 19:07:51 +05:30
2022-11-25 23:54:43 +05:30
assets_sha256 = asset_files.map { |asset_file| Digest::SHA256.file(asset_file).hexdigest }.join
2020-04-22 19:07:51 +05:30
2022-11-25 23:54:43 +05:30
Digest::SHA256.hexdigest(assets_sha256).tap { |sha256| puts "=> SHA256 generated in #{Time.now - start_time}: #{sha256}" if verbose }
2020-04-22 19:07:51 +05:30
end
2023-06-20 00:43:36 +05:30
# Files listed here should match the list in:
# .assets-compilation-patterns in .gitlab/ci/rules.gitlab-ci.yml
# So we make sure that any impacting changes we do rebuild cache
2022-11-25 23:54:43 +05:30
def self.assets_impacting_compilation
2020-04-22 19:07:51 +05:30
assets_folders = FOSS_ASSET_FOLDERS
assets_folders += EE_ASSET_FOLDERS if ::Gitlab.ee?
2021-11-18 22:05:49 +05:30
assets_folders += JH_ASSET_FOLDERS if ::Gitlab.jh?
2020-04-22 19:07:51 +05:30
asset_files = Dir.glob(JS_ASSET_PATTERNS)
asset_files += JS_ASSET_FILES
2023-06-20 00:43:36 +05:30
asset_files += RAILS_ASSET_FILES
2020-04-22 19:07:51 +05:30
assets_folders.each do |folder|
asset_files.concat(Dir.glob(["#{folder}/**/*.*"]))
end
2022-11-25 23:54:43 +05:30
asset_files - Dir.glob(EXCLUDE_PATTERNS)
2020-04-22 19:07:51 +05:30
end
2022-11-25 23:54:43 +05:30
private_class_method :assets_impacting_compilation
2020-04-22 19:07:51 +05:30
end
end
end
2017-08-17 22:00:37 +05:30
namespace :gitlab do
namespace :assets do
2022-11-25 23:54:43 +05:30
desc 'GitLab | Assets | Return the hash sum of all frontend assets'
task :hash_sum do
print Tasks::Gitlab::Assets.sha256_of_assets_impacting_compilation(verbose: false)
end
2017-08-17 22:00:37 +05:30
desc 'GitLab | Assets | Compile all frontend assets'
2019-03-02 22:35:43 +05:30
task :compile do
2023-05-27 22:25:52 +05:30
require 'fileutils'
2019-03-02 22:35:43 +05:30
require_dependency 'gitlab/task_helpers'
2022-11-25 23:54:43 +05:30
puts "Assets SHA256 for `master`: #{Tasks::Gitlab::Assets.master_assets_sha256.inspect}"
puts "Assets SHA256 for `HEAD`: #{Tasks::Gitlab::Assets.head_assets_sha256.inspect}"
2020-04-22 19:07:51 +05:30
2022-11-25 23:54:43 +05:30
if Tasks::Gitlab::Assets.head_assets_sha256 != Tasks::Gitlab::Assets.master_assets_sha256
2023-01-13 00:05:48 +05:30
FileUtils.rm_rf([Tasks::Gitlab::Assets::PUBLIC_ASSETS_DIR] + Dir.glob('app/assets/javascripts/locale/**/app.js'))
2020-04-22 19:07:51 +05:30
2022-11-25 23:54:43 +05:30
# gettext:po_to_json needs to run before rake:assets:precompile because
# app/assets/javascripts/locale/**/app.js are pre-compiled by Sprockets
Gitlab::TaskHelpers.invoke_and_time_task('gettext:po_to_json')
Gitlab::TaskHelpers.invoke_and_time_task('rake:assets:precompile')
2021-02-22 17:27:13 +05:30
2022-10-11 01:57:18 +05:30
log_path = ENV['WEBPACK_COMPILE_LOG_PATH']
cmd = 'yarn webpack'
cmd += " > #{log_path}" if log_path
unless system(cmd)
2021-02-22 17:27:13 +05:30
abort 'Error: Unable to compile webpack production bundle.'.color(:red)
end
2022-10-11 01:57:18 +05:30
puts "Written webpack stdout log to #{log_path}" if log_path
puts "You can inspect the webpack log here: #{ENV['CI_JOB_URL']}/artifacts/file/#{log_path}" if log_path && ENV['CI_JOB_URL']
2022-11-25 23:54:43 +05:30
Gitlab::TaskHelpers.invoke_and_time_task('gitlab:assets:fix_urls')
Gitlab::TaskHelpers.invoke_and_time_task('gitlab:assets:check_page_bundle_mixins_css_for_sideeffects')
2020-04-22 19:07:51 +05:30
end
2019-03-02 22:35:43 +05:30
end
2017-08-17 22:00:37 +05:30
desc 'GitLab | Assets | Clean up old compiled frontend assets'
task clean: ['rake:assets:clean']
desc 'GitLab | Assets | Remove all compiled frontend assets'
task purge: ['rake:assets:clobber']
desc 'GitLab | Assets | Uninstall frontend dependencies'
task purge_modules: ['yarn:clobber']
desc 'GitLab | Assets | Fix all absolute url references in CSS'
task :fix_urls do
css_files = Dir['public/assets/*.css']
css_files.each do |file|
# replace url(/assets/*) with url(./*)
puts "Fixing #{file}"
system "sed", "-i", "-e", 's/url(\([\"\']\?\)\/assets\//url(\1.\//g', file
# rewrite the corresponding gzip file (if it exists)
gzip = "#{file}.gz"
2023-01-13 00:05:48 +05:30
next unless File.exist?(gzip)
2017-08-17 22:00:37 +05:30
2023-01-13 00:05:48 +05:30
puts "Fixing #{gzip}"
2017-08-17 22:00:37 +05:30
2023-01-13 00:05:48 +05:30
FileUtils.rm(gzip)
mtime = File.stat(file).mtime
2017-08-17 22:00:37 +05:30
2023-01-13 00:05:48 +05:30
File.open(gzip, 'wb+') do |f|
gz = Zlib::GzipWriter.new(f, Zlib::BEST_COMPRESSION)
gz.mtime = mtime
2023-03-04 22:38:38 +05:30
gz.write File.binread(file)
2023-01-13 00:05:48 +05:30
gz.close
File.utime(mtime, mtime, f.path)
2017-08-17 22:00:37 +05:30
end
end
end
2020-01-01 13:55:28 +05:30
desc 'GitLab | Assets | Compile vendor assets'
task :vendor do
unless system('yarn webpack-vendor')
abort 'Error: Unable to compile webpack DLL.'.color(:red)
end
end
2020-11-24 15:15:51 +05:30
desc 'GitLab | Assets | Check that scss mixins do not introduce any sideffects'
task :check_page_bundle_mixins_css_for_sideeffects do
2023-03-04 22:38:38 +05:30
unless system('./scripts/frontend/check_page_bundle_mixins_css_for_sideeffects.js')
abort 'Error: At least one CSS changes introduces an unwanted sideeffect'.color(:red)
end
2020-11-24 15:15:51 +05:30
end
2017-08-17 22:00:37 +05:30
end
end