A platform for community discussion. Free, open, simple.
DEV: Only rename JS chunks whose content changed (#44761)
David Taylor686490158002
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rbindex eb1cca3e605..cb5c399c19b 100644--- a/app/helpers/application_helper.rb+++ b/app/helpers/application_helper.rb@@ -120,6 +120,11 @@ module ApplicationHelperendend+ EmberAssets.import_map.each do |stable, real|+ real_url = script_asset_path(real)+ imports[real_url.sub(File.basename(real), File.basename(stable))] = real_url+ end+JSON.pretty_generate({ imports: }).html_safeenddiff --git a/frontend/discourse/lib/chunk-import-map-plugin.mjs b/frontend/discourse/lib/chunk-import-map-plugin.mjsnew file mode 100644index 00000000000..2010c1d522c--- /dev/null+++ b/frontend/discourse/lib/chunk-import-map-plugin.mjs@@ -0,0 +1,84 @@+import { createHash } from "crypto";+import { relative } from "path";++const PLACEHOLDER = /!~\{[\w$]{1,17}\}~/g;++/*+ * A JS port of rolldown's builtin `chunkImportMap`, which cannot exclude chunks.+ *+ * Before hashing, chunk filename placeholders in code are swapped for names+ * derived from module identity, so one changed chunk does not rename every chunk+ * that imports it. `onImportMap` receives stable-to-real filename pairs.+ * Chunks matching `noImportMap`, and everything they reach, keep real filenames.+ */+export default function chunkImportMapPlugin({+ root,+ noImportMap,+ onImportMap,+}) {+ let state;+ const stabilize = (str) =>+ str.replace(PLACEHOLDER, (p) => state.stableNames.get(p) ?? p);++ return {+ name: "discourse-chunk-import-map",++ renderChunk: {+ order: "post",+ handler(code, chunk, _options, meta) {+ state ??= {+ stableNames: computeStableNames(meta.chunks, root),+ withoutImportMap: computeWithoutImportMap(meta.chunks, noImportMap),+ };+ if (state.withoutImportMap.has(chunk.fileName)) {+ return null;+ }+ return { code: stabilize(code), map: null };+ },+ },++ generateBundle(_options, bundle) {+ const importMap = {};+ for (const chunk of Object.values(bundle)) {+ if (chunk.type === "chunk") {+ const stable = stabilize(chunk.preliminaryFileName);+ if (stable !== chunk.preliminaryFileName) {+ importMap[stable] = chunk.fileName;+ }+ }+ }+ onImportMap(importMap);+ state = null;+ },+ };+}++function computeStableNames(chunks, root) {+ const names = new Map();+ for (const chunk of Object.values(chunks)) {+ const identity = chunk.facadeModuleId ?? [...chunk.moduleIds].sort()[0];+ const hex = createHash("sha256")+ .update(relative(root, identity))+ .digest("hex");+ const hash = BigInt(`0x${hex}`).toString(36);+ for (const placeholder of chunk.fileName.match(PLACEHOLDER) ?? []) {+ names.set(placeholder, hash.slice(0, placeholder.length));+ }+ }+ return names;+}++function computeWithoutImportMap(chunks, noImportMap) {+ const withoutImportMap = new Set();+ const visit = (fileName) => {+ const chunk = chunks[fileName];+ if (chunk && !withoutImportMap.has(fileName)) {+ withoutImportMap.add(fileName);+ [...chunk.imports, ...chunk.dynamicImports].forEach(visit);+ }+ };+ Object.values(chunks)+ .filter(noImportMap)+ .forEach((c) => visit(c.fileName));+ return withoutImportMap;+}diff --git a/frontend/discourse/lib/dynamic-chunk-url-plugin.mjs b/frontend/discourse/lib/dynamic-chunk-url-plugin.mjsindex a4cf63aabd6..7c8a1e16909 100644--- a/frontend/discourse/lib/dynamic-chunk-url-plugin.mjs+++ b/frontend/discourse/lib/dynamic-chunk-url-plugin.mjs@@ -40,7 +40,7 @@ export default function dynamicChunkUrlPlugin() {preserveSignature: "strict",});return {- code: `export default import.meta.ROLLUP_FILE_URL_${refId};`,+ code: `export default import.meta.resolve(import.meta.ROLLUP_FILE_URL_${refId});`,moduleType: "js",};},diff --git a/frontend/discourse/rolldown.config.mjs b/frontend/discourse/rolldown.config.mjsindex 05db5baf799..e733af719b1 100644--- a/frontend/discourse/rolldown.config.mjs+++ b/frontend/discourse/rolldown.config.mjs@@ -1,6 +1,7 @@import * as fs from "fs";import { basename, relative } from "path";import { viteAliasPlugin, viteImportGlobPlugin } from "rolldown/experimental";+import chunkImportMapPlugin from "./lib/chunk-import-map-plugin.mjs";import discourseChunkNamesPlugin from "./lib/discourse-chunk-names.mjs";import discourseSourceImports from "./lib/discourse-source-imports.mjs";import dynamicChunkUrlPlugin from "./lib/dynamic-chunk-url-plugin.mjs";@@ -56,6 +57,8 @@ export function buildConfig({ devMode } = {}) {process.env.NODE_ENV = "development";}+ let importMap = {};+return {tsconfig: false,resolve: {@@ -110,6 +113,11 @@ export function buildConfig({ devMode } = {}) {}),wrapTestModulesPlugin(),discourseChunkNamesPlugin(),+ chunkImportMapPlugin({+ root: import.meta.dirname,+ noImportMap: (chunk) => chunk.facadeModuleId?.includes("/app/workers/"),+ onImportMap: (map) => (importMap = map),+ }),{name: "require-relative-import-extensions",resolveId: {@@ -206,6 +214,7 @@ export function buildConfig({ devMode } = {}) {entrypoints: {},dynamicEntrypoints: {},chunks: {},+ importMap,};for (const [fileName, chunk] of Object.entries(bundle)) {diff --git a/lib/ember_assets.rb b/lib/ember_assets.rbindex 45638d2c7a9..5070def6964 100644--- a/lib/ember_assets.rb+++ b/lib/ember_assets.rb@@ -38,12 +38,24 @@ class EmberAssets < ActiveSupport::CurrentAttributesentrypoints[entry_name.delete_suffix(".js")] = deep_imports_for(chunk_filename: entry_filename,chunks: manifest["chunks"],- ).map { it.delete_prefix("assets/").delete_suffix(".js") }+ ).map { logical_name(it) }endcache[:script_chunks] = entrypointsend+ # Stable chunk names, as chunks import each other, to the content-hashed files on disk.+ def self.import_map+ raise_if_build_error!+ cache[:import_map] ||= (+ read_manifest!(exception: true)&.dig("importMap") || {}+ ).to_h { |stable, real| [logical_name(stable), logical_name(real)] }+ end++ def self.logical_name(filename)+ filename.delete_prefix("assets/").delete_suffix(".js")+ end+def self.deep_imports_for(chunk_filename:, chunks:, seen: Set.new)return [] unless seen.add?(chunk_filename)[@@ -124,7 +136,7 @@ class EmberAssets < ActiveSupport::CurrentAttributes# caller has opted out of raising. When `exception:` is true and the manifest# is missing, raises BuildError so the dev error page is shown.def self.read_manifest!(exception:)- JSON.parse(File.read("#{dist_dir}/manifest/manifest.json"))+ cache[:manifest] ||= JSON.parse(File.read("#{dist_dir}/manifest/manifest.json"))rescue Errno::ENOENTif exception && Rails.env.local?raise BuildError.new("status" => "missing", "error" => plain_error(<<~MSG))diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rbindex 0c0ae7c78bc..11129724453 100644--- a/spec/helpers/application_helper_spec.rb+++ b/spec/helpers/application_helper_spec.rb@@ -21,6 +21,72 @@ RSpec.describe ApplicationHelper doendend+ describe "generate_import_map" do+ before do+ EmberAssets.stubs(:import_map).returns(+ { "js/chunk-stable11.digested" => "js/chunk-real1111.digested" },+ )+ end++ def imports(plugin_assets = [])+ JSON.parse(helper.generate_import_map(plugin_assets))["imports"]+ end++ it "maps stable chunk names to their hashed files" do+ expect(imports).to eq(+ { "/assets/js/chunk-stable11.digested.js" => "/assets/js/chunk-real1111.digested.js" },+ )+ end++ it "applies the same subfolder and CDN prefix to both sides" do+ set_cdn_url "https://awesome.com"+ set_subfolder "/community"++ expect(imports).to eq(+ {+ "https://awesome.com/community/assets/js/chunk-stable11.digested.js" =>+ "https://awesome.com/community/assets/js/chunk-real1111.digested.js",+ },+ )+ end++ context "with s3 CDN" do+ before do+ global_setting :s3_bucket, "test_bucket"+ global_setting :s3_region, "ap-australia"+ global_setting :s3_access_key_id, "123"+ global_setting :s3_secret_access_key, "123"+ global_setting :s3_cdn_url, "https://s3cdn.com"+ end++ it "applies the brotli path mangling to both sides" do+ helper.request.env["HTTP_ACCEPT_ENCODING"] = "br"++ expect(imports).to eq(+ {+ "https://s3cdn.com/assets/br/chunk-stable11.digested.js" =>+ "https://s3cdn.com/assets/br/chunk-real1111.digested.js",+ },+ )+ end+ end++ it "keeps plugin entries alongside chunk entries" do+ plugin = stub(directory_name: "my-plugin", name: "my-plugin")+ plugin_assets = [+ {+ name: "js/plugins/my-plugin-abcdefgh.digested",+ plugin: plugin,+ importmap_name: "discourse/plugins/my-plugin",+ },+ ]+ expect(imports(plugin_assets).keys).to contain_exactly(+ "discourse/plugins/my-plugin",+ "/assets/js/chunk-stable11.digested.js",+ )+ end+ end+describe "preload_script" dodef script_tag(url, entrypoint, nonce)<<~HTMLdiff --git a/spec/lib/ember_assets_spec.rb b/spec/lib/ember_assets_spec.rbindex 64ac714743a..dfe969bcb2b 100644--- a/spec/lib/ember_assets_spec.rb+++ b/spec/lib/ember_assets_spec.rb@@ -35,4 +35,28 @@ describe EmberAssets doendendend++ describe ".import_map" do+ before { EmberAssets.unstub(:import_map) }+ after { EmberAssets.clear_cache! }++ it "returns the stable-to-hashed chunk mapping as logical paths" do+ EmberAssets.stubs(:read_manifest!).returns(+ {+ "importMap" => {+ "assets/js/chunk-stable11.digested.js" => "assets/js/chunk-real1111.digested.js",+ },+ },+ )++ expect(EmberAssets.import_map).to eq(+ { "js/chunk-stable11.digested" => "js/chunk-real1111.digested" },+ )+ end++ it "is empty when the manifest has no import map" do+ EmberAssets.stubs(:read_manifest!).returns({ "entrypoints" => {} })+ expect(EmberAssets.import_map).to eq({})+ end+ endenddiff --git a/spec/rails_helper.rb b/spec/rails_helper.rbindex 33e1da9d455..783168a64c7 100644--- a/spec/rails_helper.rb+++ b/spec/rails_helper.rb@@ -255,6 +255,7 @@ RSpec.configure do |config|if example.metadata[:type] != :systemEmberAssets.stubs(:read_manifest!).returns(nil)EmberAssets.stubs(:script_chunks).returns({})+ EmberAssets.stubs(:import_map).returns({})endend