Skip to content

A platform for community discussion. Free, open, simple.

PERF: Minify core JavaScript for CI system tests (#44526)

Alan Guo Xiang Tanef39767ea7f0
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index 3fef8e2e4fe..f012d64cf43 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -68,6 +68,7 @@ jobs:
CHEAP_SOURCE_MAPS: "1"
DBUS_SESSION_BUS_ADDRESS: "unix:path=/dev/null"
EMBER_ENV: development
+ DISCOURSE_CI_MINIFY: ${{ matrix.build_type == 'system' && '1' || '' }}
LOAD_PLUGINS: ${{ (matrix.target == 'core-plugins' || matrix.target == 'official-plugins' || matrix.target == 'plugins' || matrix.target == 'chat') && '1' || '0' }}
QUNIT_REUSE_BUILD: 1
PLAYWRIGHT_BROWSERS_PATH: /home/discourse/.cache/ms-playwright
diff --git a/frontend/discourse/lib/browser-error-stack.js b/frontend/discourse/lib/browser-error-stack.js
new file mode 100644
index 00000000000..b8df3824961
--- /dev/null
+++ b/frontend/discourse/lib/browser-error-stack.js
@@ -0,0 +1,36 @@
+"use strict";
+
+const fs = require("node:fs");
+const {
+ DeprecationStackResolver,
+ parseStack,
+} = require("./deprecation-report");
+
+function resolveMessages(messages) {
+ const resolver = new DeprecationStackResolver();
+ return messages.map((message) =>
+ message
+ .split("\n")
+ .flatMap((line) => {
+ const [frame] = parseStack(line);
+ if (!frame) {
+ return [line];
+ }
+
+ const original = resolver.resolveFrame(frame);
+ if (!original.resolved || original.approximate) {
+ return [line];
+ }
+
+ const location = `${original.file}:${original.line}:${original.column}`;
+ return [
+ ` at ${frame.fn ? `${frame.fn} (${location})` : location}`,
+ ...(original.code ? [` ${original.code}`] : []),
+ ];
+ })
+ .join("\n")
+ );
+}
+
+const messages = JSON.parse(fs.readFileSync(0, "utf8"));
+process.stdout.write(JSON.stringify(resolveMessages(messages)));
diff --git a/frontend/discourse/rolldown.config.mjs b/frontend/discourse/rolldown.config.mjs
index 9f5fe1bf4b9..a44bd7c900b 100644
--- a/frontend/discourse/rolldown.config.mjs
+++ b/frontend/discourse/rolldown.config.mjs
@@ -82,7 +82,7 @@ export function buildConfig({ devMode } = {}) {
: undefined),
},
output: {
- minify: isProduction,
+ minify: isProduction || process.env.DISCOURSE_CI_MINIFY === "1",
dir: "dist",
sourcemap: true,
cleanDir: !devMode,
diff --git a/frontend/playwright-runner/package.json b/frontend/playwright-runner/package.json
index 8310376ff9a..5f62e1208ab 100644
--- a/frontend/playwright-runner/package.json
+++ b/frontend/playwright-runner/package.json
@@ -3,6 +3,7 @@
"version": "1.0.0",
"private": true,
"dependencies": {
- "playwright": "1.62.1"
+ "playwright": "1.62.1",
+ "source-map-js": "^1.2.1"
}
}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 900ee48c71f..83190467f60 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -701,6 +701,9 @@ importers:
playwright:
specifier: 1.62.1
version: 1.62.1
+ source-map-js:
+ specifier: ^1.2.1
+ version: 1.2.1
frontend/pretty-text:
dependencies:
diff --git a/script/assemble_ember_build.rb b/script/assemble_ember_build.rb
index 672d3e1022b..3eae6912d7b 100755
--- a/script/assemble_ember_build.rb
+++ b/script/assemble_ember_build.rb
@@ -67,8 +67,14 @@ def resolved_ember_env
ENV["EMBER_ENV"] || "development"
end
+def ci_minify?
+ ENV["DISCOURSE_CI_MINIFY"] == "1"
+end
+
def build_info
- { "ember_env" => resolved_ember_env, "core_tree_hash" => core_tree_hash }
+ info = { "ember_env" => resolved_ember_env, "core_tree_hash" => core_tree_hash }
+ info["ci_minify"] = true if ci_minify?
+ info
end
def existing_core_build_usable?
@@ -152,7 +158,8 @@ if low_memory_environment?
end
core_build_reusable =
- existing_core_build_usable? || (download_prebuild_assets! && existing_core_build_usable?)
+ existing_core_build_usable? ||
+ (!ci_minify? && download_prebuild_assets! && existing_core_build_usable?)
if core_build_reusable
log "Reusing existing core ember build. All done."
diff --git a/spec/support/system/browser_error_stack.rb b/spec/support/system/browser_error_stack.rb
new file mode 100644
index 00000000000..5193b7ef6ab
--- /dev/null
+++ b/spec/support/system/browser_error_stack.rb
@@ -0,0 +1,40 @@
+# frozen_string_literal: true
+require "open3"
+
+module BrowserErrorStack
+ def self.resolve(messages)
+ return messages if ENV["DISCOURSE_CI_MINIFY"] != "1" || messages.empty?
+
+ env = {}
+ if (cli_path = ENV["PLAYWRIGHT_CLI_EXECUTABLE_PATH"])
+ env["NODE_PATH"] = [
+ File.expand_path("..", File.dirname(cli_path)),
+ ENV["NODE_PATH"],
+ ].compact.join(File::PATH_SEPARATOR)
+ end
+ output, _stderr, status =
+ Open3.capture3(
+ env,
+ "node",
+ Rails.root.join("frontend/discourse/lib/browser-error-stack.js").to_s,
+ stdin_data: JSON.generate(messages),
+ )
+ if status.success?
+ resolved = JSON.parse(output)
+ if resolved.is_a?(Array) && resolved.size == messages.size &&
+ resolved.all? { |message| message.is_a?(String) }
+ return resolved
+ end
+ end
+
+ failed_mapping(messages)
+ rescue SystemCallError, JSON::ParserError
+ failed_mapping(messages)
+ end
+
+ def self.failed_mapping(messages)
+ warn "Browser stack mapping failed. Showing raw stacks."
+ messages
+ end
+ private_class_method :failed_mapping
+end
diff --git a/spec/support/system/ember_deprecations.rb b/spec/support/system/ember_deprecations.rb
index 70ed2fddc26..45c981aa596 100644
--- a/spec/support/system/ember_deprecations.rb
+++ b/spec/support/system/ember_deprecations.rb
@@ -36,14 +36,12 @@ module EmberDeprecations
# Formatted error for the first fatal JS deprecation in the logs, or nil.
def self.fatal_error(logs)
- logs
- &.filter_map do |log|
- if log[:level] == "trace"
- error = JSON.parse(log[:message][/^fatal_deprecation:(.+)$/, 1])
- "~~~~~~~ JS ERROR ~~~~~~~\n#{error}\n~~~~~ END JS ERROR ~~~~~"
- end
- end
- &.first
+ log = logs&.find { |entry| entry[:level] == "trace" }
+ return if log.nil?
+
+ error = JSON.parse(log[:message][/^fatal_deprecation:(.+)$/, 1])
+ error = BrowserErrorStack.resolve([error]).first
+ "~~~~~~~ JS ERROR ~~~~~~~\n#{error}\n~~~~~ END JS ERROR ~~~~~"
end
# Count non-fatal deprecation_id counts into the example metadata, excluding
diff --git a/spec/support/system/playwright_logger.rb b/spec/support/system/playwright_logger.rb
index b85dfb078d5..4915e22901a 100644
--- a/spec/support/system/playwright_logger.rb
+++ b/spec/support/system/playwright_logger.rb
@@ -26,6 +26,7 @@ class PlaywrightLogger
@logs << {
level: "error",
message: error.message,
+ stack: error.stack,
timestamp: Time.now.to_i * 1000,
source: "pageerror-api",
}
@@ -41,18 +42,21 @@ class PlaywrightLogger
if logs.empty?
lines << "(no logs)"
else
- logs.each do |log|
- if (
- log[:message].include?("Failed to load resource: net::ERR_CONNECTION_REFUSED") &&
- (log[:message].include?("uploads") || log[:message].include?("images"))
- ) || log[:message].include?("favicon.ico") ||
- # Machine-readable stacks for the deprecation report, not for humans.
- log[:message].start_with?("deprecation_detail:")
- next
+ messages =
+ logs.filter_map do |log|
+ if (
+ log[:message].include?("Failed to load resource: net::ERR_CONNECTION_REFUSED") &&
+ (log[:message].include?("uploads") || log[:message].include?("images"))
+ ) || log[:message].include?("favicon.ico") ||
+ # Machine-readable stacks for the deprecation report, not for humans.
+ log[:message].start_with?("deprecation_detail:")
+ next
+ end
+
+ log[:stack].presence || log[:message]
end
- lines << log[:message]
- end
+ BrowserErrorStack.resolve(messages).each { |message| lines << message }
end
lines << "~~~~~ END JS LOGS ~~~~~"