mirror of
https://github.com/discourse/discourse.git
synced 2025-05-21 18:12:32 +08:00
DEV: Introduce minification and source maps for Theme JS (#18646)
Theme javascript is now minified using Terser, just like our core/plugin JS bundles. This reduces the amount of data sent over the network. This commit also introduces sourcemaps for theme JS. Browser developer tools will now be able show each source file separately when browsing, and also in backtraces. For theme test JS, the sourcemap is inlined for simplicity. Network load is not a concern for tests.
This commit is contained in:
@ -152,6 +152,10 @@ class DiscourseJsProcessor
|
||||
}
|
||||
JS
|
||||
|
||||
# Terser
|
||||
load_file_in_context(ctx, "node_modules/source-map/dist/source-map.js")
|
||||
load_file_in_context(ctx, "node_modules/terser/dist/bundle.min.js")
|
||||
|
||||
# Template Compiler
|
||||
load_file_in_context(ctx, "node_modules/ember-source/dist/ember-template-compiler.js")
|
||||
load_file_in_context(ctx, "node_modules/babel-plugin-ember-template-compilation/src/plugin.js", wrap_in_module: "babel-plugin-ember-template-compilation/index")
|
||||
@ -197,6 +201,8 @@ class DiscourseJsProcessor
|
||||
ctx.eval <<~JS
|
||||
globalThis.compileRawTemplate = require('discourse-js-processor').compileRawTemplate;
|
||||
globalThis.transpile = require('discourse-js-processor').transpile;
|
||||
globalThis.minify = require('discourse-js-processor').minify;
|
||||
globalThis.getMinifyResult = require('discourse-js-processor').getMinifyResult;
|
||||
JS
|
||||
|
||||
ctx
|
||||
@ -219,9 +225,16 @@ class DiscourseJsProcessor
|
||||
@ctx
|
||||
end
|
||||
|
||||
# Call a method in the global scope of the v8 context.
|
||||
# The `fetch_result_call` kwarg provides a workaround for the lack of mini_racer async
|
||||
# result support. The first call can perform some async operation, and then `fetch_result_call`
|
||||
# will be called to fetch the result.
|
||||
def self.v8_call(*args, **kwargs)
|
||||
fetch_result_call = kwargs.delete(:fetch_result_call)
|
||||
mutex.synchronize do
|
||||
v8.call(*args, **kwargs)
|
||||
result = v8.call(*args, **kwargs)
|
||||
result = v8.call(fetch_result_call) if fetch_result_call
|
||||
result
|
||||
end
|
||||
rescue MiniRacer::RuntimeError => e
|
||||
message = e.message
|
||||
@ -276,5 +289,8 @@ class DiscourseJsProcessor
|
||||
self.class.v8_call("compileRawTemplate", source, theme_id)
|
||||
end
|
||||
|
||||
def terser(tree, opts)
|
||||
self.class.v8_call("minify", tree, opts, fetch_result_call: "getMinifyResult")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user