0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-06 13:08:40 +08:00
discourse/app/models/javascript_cache.rb
David Taylor e3054ef590
DEV: Improve cross-plugin/theme import handling (#40939)
1. Remove 'federated exports' system, which was named entrypoint exports
for every module inside the plugin. Replace it with a single import of
the target plugin's 'compatModules', and then update call sites to do a
'just in time' lookup of the module and export. This is implemented in a
new `babel-resolve-plugin-imports` plugin

2. Update theme & plugin build systems to produce a list of external
plugins which are imported. For plugins, it's stored in the manifest.
For themes, it's stored in a new column of the javascript_caches table.

3. Refactor theme extra_js loading to use a more structured data model,
and move the HTML generation to the erb template

4. Update core importmap to identify any missing plugin dependencies and
add a fake placeholder module for them. This allows optional imports to
exist without causing a boot error. The imported values will resolve to
'null'.

5. Update core plugins to remove use of `optionalRequire`, and replace
it with regular imports, and a `with { discourseImport: "optional" }`
suffix. This is functionally equivalent to optionalRequire, but without
leaning on the legacy `loader.js` system of core

6. Add support for `with { discourseImport: "optional" }` for
plugins/themes importing core modules. This is useful when a
theme/plugin needs to target multiple versions of Discourse core.

Co-authored-by: Jarek Radosz <jarek@cvx.dev>
2026-06-30 16:11:38 +01:00

61 lines
1.7 KiB
Ruby
Vendored

# frozen_string_literal: true
class JavascriptCache < ActiveRecord::Base
belongs_to :theme_field
belongs_to :theme
validate :content_cannot_be_nil
before_save :update_digest
def url
"#{GlobalSetting.cdn_url}#{Discourse.base_path}#{path}"
end
def local_url
"#{Discourse.base_path}#{path}"
end
private
def path
"/theme-javascripts/#{digest}.js?__ws=#{Discourse.current_hostname}"
end
def update_digest
self.digest =
Digest::SHA1.hexdigest(
"#{content}|#{source_map}|#{GlobalSetting.asset_url_salt}",
) if content_changed? || source_map_changed?
end
def content_cannot_be_nil
errors.add(:content, :empty) if content.nil?
end
end
# == Schema Information
#
# Table name: javascript_caches
#
# id :bigint not null, primary key
# content :text not null
# digest :string
# external_plugin_imports :string default([]), not null, is an Array
# name :string
# source_map :text
# created_at :datetime not null
# updated_at :datetime not null
# theme_field_id :bigint
# theme_id :bigint
#
# Indexes
#
# index_javascript_caches_on_digest (digest)
# index_javascript_caches_on_theme_field_id_and_name (theme_field_id,name) UNIQUE NULLS NOT DISTINCT WHERE (theme_field_id IS NOT NULL)
# index_javascript_caches_on_theme_id (theme_id) UNIQUE
#
# Foreign Keys
#
# fk_rails_... (theme_field_id => theme_fields.id) ON DELETE => cascade
# fk_rails_... (theme_id => themes.id) ON DELETE => cascade
#