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:
parent
87450f677b
commit
11369018b6
|
@ -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(() => {});
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue