DEV: Stop storing theme-transpiler on filesystem in development (#28198)

We were writing theme-transpiler JS files to the filesystem on a per-process basis, and then immediately reading them back in. Plus, there was no cleanup mechanism, so the tmp directory would grow indefinitely.

This commit refactors things so that the `build.js` script outputs the theme-transpiler source to stdout. That way, we can read it directly into the process, and then into mini-racer, without needing to go via the filesystem. No cleanup required!

In production, the theme-transpiler is still cached in a file during `assets:precompile`
This commit is contained in:
David Taylor 2024-08-01 17:59:34 +01:00 committed by GitHub
parent 87450f677b
commit 11369018b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 16 additions and 20 deletions

View File

@ -3,7 +3,6 @@
const esbuild = require("esbuild");
const path = require("node:path");
const fs = require("node:fs");
const { argv } = require("node:process");
let wasmPlugin = {
name: "wasm",
@ -57,7 +56,6 @@ esbuild
},
external: ["fs", "path"],
entryPoints: ["./app/assets/javascripts/theme-transpiler/transpiler.js"],
outfile: argv[2],
plugins: [wasmPlugin],
})
.then(() => {});

View File

@ -63,14 +63,7 @@ class DiscourseJsProcessor
end
class Transpiler
TRANSPILER_PATH =
(
if Rails.env.production?
"tmp/theme-transpiler.js"
else
"tmp/theme-transpiler/#{Process.pid}.js"
end
)
TRANSPILER_PATH = "tmp/theme-transpiler.js"
@mutex = Mutex.new
@ctx_init = Mutex.new
@ -81,11 +74,12 @@ class DiscourseJsProcessor
end
def self.build_theme_transpiler
Discourse::Utils.execute_command(
"node",
"app/assets/javascripts/theme-transpiler/build.js",
TRANSPILER_PATH,
)
FileUtils.rm_rf("tmp/theme-transpiler") # cleanup old files - remove after Jan 2025
Discourse::Utils.execute_command("node", "app/assets/javascripts/theme-transpiler/build.js")
end
def self.build_production_theme_transpiler
File.write(TRANSPILER_PATH, build_theme_transpiler)
TRANSPILER_PATH
end
@ -98,10 +92,14 @@ class DiscourseJsProcessor
ctx.attach("rails.logger.warn", proc { |err| Rails.logger.warn(err.to_s) })
ctx.attach("rails.logger.error", proc { |err| Rails.logger.error(err.to_s) })
# Theme template AST transformation plugins
@processor_mutex.synchronize { build_theme_transpiler } if !Rails.env.production?
source =
if Rails.env.production?
File.read(TRANSPILER_PATH)
else
@processor_mutex.synchronize { build_theme_transpiler }
end
ctx.eval(File.read(TRANSPILER_PATH), filename: "theme-transpiler.js")
ctx.eval(source, filename: "theme-transpiler.js")
ctx
end

View File

@ -321,7 +321,7 @@ task "assets:precompile:compress_js": "environment" do
end
task "assets:precompile:theme_transpiler": "environment" do
DiscourseJsProcessor::Transpiler.build_theme_transpiler
DiscourseJsProcessor::Transpiler.build_production_theme_transpiler
end
# Run these tasks **before** Rails' "assets:precompile" task

View File

@ -10,7 +10,7 @@ RSpec.describe "assets:precompile" do
it "compiles the js processor" do
path = Rake::Task["assets:precompile:theme_transpiler"].actions.first.call
expect(path).to match(%r{tmp/theme-transpiler})
expect(path).to end_with("tmp/theme-transpiler.js")
expect(File.exist?(path)).to eq(true)
end
end