2023-08-24 20:25:44 +08:00
|
|
|
// This is executed in mini_racer to provide the JS logic for lib/discourse_js_processor.rb
|
2023-08-24 18:43:59 +08:00
|
|
|
|
2023-08-24 22:36:22 +08:00
|
|
|
/* global rails */
|
|
|
|
|
|
|
|
const CONSOLE_PREFIX = "[DiscourseJsProcessor] ";
|
|
|
|
globalThis.window = {};
|
|
|
|
globalThis.console = {
|
|
|
|
log(...args) {
|
|
|
|
rails.logger.info(CONSOLE_PREFIX + args.join(" "));
|
|
|
|
},
|
|
|
|
warn(...args) {
|
|
|
|
rails.logger.warn(CONSOLE_PREFIX + args.join(" "));
|
|
|
|
},
|
|
|
|
error(...args) {
|
|
|
|
rails.logger.error(CONSOLE_PREFIX + args.join(" "));
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-10-11 02:38:59 +08:00
|
|
|
import { transform as babelTransform } from "@babel/standalone";
|
2023-08-24 22:36:22 +08:00
|
|
|
import HTMLBarsInlinePrecompile from "babel-plugin-ember-template-compilation";
|
2023-10-23 18:08:35 +08:00
|
|
|
import { Preprocessor } from "content-tag";
|
2023-08-24 22:36:22 +08:00
|
|
|
import colocatedBabelPlugin from "ember-cli-htmlbars/lib/colocated-babel-plugin";
|
|
|
|
import { precompile } from "ember-source/dist/ember-template-compiler";
|
2023-09-05 17:16:12 +08:00
|
|
|
import EmberThisFallback from "ember-this-fallback";
|
2023-10-11 02:38:59 +08:00
|
|
|
import Handlebars from "handlebars";
|
2023-10-02 18:36:06 +08:00
|
|
|
// A sub-dependency of content-tag (getrandom) needs `getRandomValues`
|
|
|
|
// so we polyfill it
|
|
|
|
import getRandomValues from "polyfill-crypto.getrandomvalues";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { minify as terserMinify } from "terser";
|
|
|
|
import RawHandlebars from "discourse-common/addon/lib/raw-handlebars";
|
2023-10-18 18:07:09 +08:00
|
|
|
import { WidgetHbsCompiler } from "discourse-widget-hbs/lib/widget-hbs-compiler";
|
2023-10-02 18:36:06 +08:00
|
|
|
globalThis.crypto = { getRandomValues };
|
|
|
|
|
2023-09-05 17:16:12 +08:00
|
|
|
const thisFallbackPlugin = EmberThisFallback._buildPlugin({
|
|
|
|
enableLogging: false,
|
|
|
|
isTheme: true,
|
|
|
|
}).plugin;
|
2022-09-01 18:50:46 +08:00
|
|
|
|
|
|
|
function manipulateAstNodeForTheme(node, themeId) {
|
|
|
|
// Magically add theme id as the first param for each of these helpers)
|
|
|
|
if (
|
|
|
|
node.path.parts &&
|
|
|
|
["theme-i18n", "theme-prefix", "theme-setting"].includes(node.path.parts[0])
|
|
|
|
) {
|
|
|
|
if (node.params.length === 1) {
|
|
|
|
node.params.unshift({
|
|
|
|
type: "NumberLiteral",
|
|
|
|
value: themeId,
|
|
|
|
original: themeId,
|
|
|
|
loc: { start: {}, end: {} },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildEmberTemplateManipulatorPlugin(themeId) {
|
|
|
|
return function () {
|
|
|
|
return {
|
|
|
|
name: "theme-template-manipulator",
|
|
|
|
visitor: {
|
|
|
|
SubExpression: (node) => manipulateAstNodeForTheme(node, themeId),
|
|
|
|
MustacheStatement: (node) => manipulateAstNodeForTheme(node, themeId),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-10-02 18:36:06 +08:00
|
|
|
function buildTemplateCompilerBabelPlugins({ extension, themeId }) {
|
2023-02-28 01:43:28 +08:00
|
|
|
const compiler = { precompile };
|
|
|
|
|
2023-10-02 18:36:06 +08:00
|
|
|
if (themeId && extension !== "gjs") {
|
2023-02-28 01:43:28 +08:00
|
|
|
compiler.precompile = (src, opts) => {
|
2022-09-01 18:50:46 +08:00
|
|
|
return precompile(src, {
|
|
|
|
...opts,
|
|
|
|
plugins: {
|
2023-09-05 17:16:12 +08:00
|
|
|
ast: [
|
|
|
|
buildEmberTemplateManipulatorPlugin(themeId),
|
|
|
|
thisFallbackPlugin,
|
|
|
|
],
|
2022-09-01 18:50:46 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2022-10-17 21:47:16 +08:00
|
|
|
colocatedBabelPlugin,
|
2023-08-24 22:36:22 +08:00
|
|
|
WidgetHbsCompiler,
|
2022-09-01 18:50:46 +08:00
|
|
|
[
|
2023-02-28 01:43:28 +08:00
|
|
|
HTMLBarsInlinePrecompile,
|
|
|
|
{
|
|
|
|
compiler,
|
|
|
|
enableLegacyModules: ["ember-cli-htmlbars"],
|
|
|
|
},
|
2022-09-01 18:50:46 +08:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildThemeRawHbsTemplateManipulatorPlugin(themeId) {
|
|
|
|
return function (ast) {
|
|
|
|
["SubExpression", "MustacheStatement"].forEach((pass) => {
|
2023-08-24 22:36:22 +08:00
|
|
|
const visitor = new Handlebars.Visitor();
|
2022-09-01 18:50:46 +08:00
|
|
|
visitor.mutating = true;
|
|
|
|
visitor[pass] = (node) => manipulateAstNodeForTheme(node, themeId);
|
|
|
|
visitor.accept(ast);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-08-24 22:36:22 +08:00
|
|
|
globalThis.compileRawTemplate = function (source, themeId) {
|
2022-09-01 18:50:46 +08:00
|
|
|
try {
|
|
|
|
const plugins = [];
|
|
|
|
if (themeId) {
|
|
|
|
plugins.push(buildThemeRawHbsTemplateManipulatorPlugin(themeId));
|
|
|
|
}
|
|
|
|
return RawHandlebars.precompile(source, false, { plugins }).toString();
|
|
|
|
} catch (error) {
|
|
|
|
// Workaround for https://github.com/rubyjs/mini_racer/issues/262
|
|
|
|
error.message = JSON.stringify(error.message);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-08-24 22:36:22 +08:00
|
|
|
globalThis.transpile = function (source, options = {}) {
|
2023-10-02 18:36:06 +08:00
|
|
|
const { moduleId, filename, extension, skipModule, themeId, commonPlugins } =
|
|
|
|
options;
|
2023-08-24 22:36:22 +08:00
|
|
|
|
2023-10-02 18:36:06 +08:00
|
|
|
if (extension === "gjs") {
|
|
|
|
const preprocessor = new Preprocessor();
|
|
|
|
source = preprocessor.process(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
const plugins = [];
|
|
|
|
plugins.push(...buildTemplateCompilerBabelPlugins({ extension, themeId }));
|
2022-09-01 18:50:46 +08:00
|
|
|
if (moduleId && !skipModule) {
|
|
|
|
plugins.push(["transform-modules-amd", { noInterop: true }]);
|
|
|
|
}
|
|
|
|
plugins.push(...commonPlugins);
|
|
|
|
|
|
|
|
try {
|
2023-08-24 22:36:22 +08:00
|
|
|
return babelTransform(source, {
|
2022-09-01 18:50:46 +08:00
|
|
|
moduleId,
|
|
|
|
filename,
|
|
|
|
ast: false,
|
|
|
|
plugins,
|
|
|
|
}).code;
|
|
|
|
} catch (error) {
|
|
|
|
// Workaround for https://github.com/rubyjs/mini_racer/issues/262
|
|
|
|
error.message = JSON.stringify(error.message);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
2022-10-19 01:20:10 +08:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2023-08-24 22:36:22 +08:00
|
|
|
globalThis.minify = async function (sources, options) {
|
2022-10-19 01:20:10 +08:00
|
|
|
lastMinifyError = lastMinifyResult = null;
|
|
|
|
try {
|
2023-08-24 22:36:22 +08:00
|
|
|
lastMinifyResult = await terserMinify(sources, options);
|
2022-10-19 01:20:10 +08:00
|
|
|
} catch (e) {
|
|
|
|
lastMinifyError = e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-08-24 22:36:22 +08:00
|
|
|
globalThis.getMinifyResult = function () {
|
2022-10-19 01:20:10 +08:00
|
|
|
const error = lastMinifyError;
|
|
|
|
const result = lastMinifyResult;
|
|
|
|
|
|
|
|
lastMinifyError = lastMinifyResult = null;
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|