mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 22:43:33 +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:
@ -1,4 +1,4 @@
|
||||
/* global Babel:true */
|
||||
/* global Babel:true Terser:true */
|
||||
|
||||
// This is executed in mini_racer to provide the JS logic for lib/discourse_js_processor.rb
|
||||
|
||||
@ -110,3 +110,29 @@ exports.transpile = function (
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
// mini_racer doesn't have native support for getting the result of an async operation.
|
||||
// To work around that, we provide a getMinifyResult which can be used to fetch the result
|
||||
// in a followup method call.
|
||||
let lastMinifyError, lastMinifyResult;
|
||||
|
||||
exports.minify = async function (sources, options) {
|
||||
lastMinifyError = lastMinifyResult = null;
|
||||
try {
|
||||
lastMinifyResult = await Terser.minify(sources, options);
|
||||
} catch (e) {
|
||||
lastMinifyError = e;
|
||||
}
|
||||
};
|
||||
|
||||
exports.getMinifyResult = function () {
|
||||
const error = lastMinifyError;
|
||||
const result = lastMinifyResult;
|
||||
|
||||
lastMinifyError = lastMinifyResult = null;
|
||||
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
Reference in New Issue
Block a user