mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-02 09:31:40 +08:00
Plugin code is trusted. If it takes a long time to build on low-resource machines, so be it.
45 lines
1.1 KiB
Ruby
45 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class Plugin::JsCompiler
|
|
def initialize(
|
|
plugin_name,
|
|
minify: true,
|
|
tree: {},
|
|
entrypoints: {},
|
|
filename_prefix: nil,
|
|
filename_suffix: nil
|
|
)
|
|
@plugin_name = plugin_name
|
|
@tree = tree
|
|
@entrypoints = entrypoints
|
|
@minify = minify
|
|
@filename_prefix = filename_prefix
|
|
@filename_suffix = filename_suffix
|
|
end
|
|
|
|
def compile!
|
|
AssetProcessor.new.rollup(
|
|
@tree,
|
|
{
|
|
pluginName: @plugin_name,
|
|
minify: @minify,
|
|
entrypoints: @entrypoints,
|
|
filenamePrefix: @filename_prefix,
|
|
filenameSuffix: @filename_suffix,
|
|
},
|
|
)
|
|
rescue AssetProcessor::TimeoutError => e
|
|
raise AssetProcessor::TimeoutError, "[PLUGIN #{@plugin_name}] #{e.message}"
|
|
rescue AssetProcessor::TranspileError => e
|
|
message = "[PLUGIN #{@plugin_name}] Compile error: #{e.message}"
|
|
{
|
|
"#{@filename_prefix}main#{@filename_suffix}.js" => {
|
|
"name" => "main",
|
|
"imports" => [],
|
|
"isEntry" => true,
|
|
"code" => "throw new Error(#{message.to_json});\n",
|
|
"map" => nil,
|
|
},
|
|
}
|
|
end
|
|
end
|