2022-10-18 13:20:10 -04:00
|
|
|
/* global Babel:true Terser:true */
|
2022-09-01 06:50:46 -04:00
|
|
|
|
|
|
|
// This is executed in mini_racer to provide the JS logic for lib/discourse_js_processor.rb
|
|
|
|
|
2023-02-27 12:43:28 -05:00
|
|
|
const HTMLBarsInlinePrecompile =
|
2022-09-01 06:50:46 -04:00
|
|
|
require("babel-plugin-ember-template-compilation").default;
|
2022-10-17 09:47:16 -04:00
|
|
|
const colocatedBabelPlugin = require("colocated-babel-plugin").default;
|
2022-09-01 06:50:46 -04:00
|
|
|
const precompile = require("ember-template-compiler").precompile;
|
|
|
|
const Handlebars = require("handlebars").default;
|
|
|
|
|
|
|
|
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),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildTemplateCompilerBabelPlugins({ themeId }) {
|
2023-02-27 12:43:28 -05:00
|
|
|
const compiler = { precompile };
|
|
|
|
|
2022-09-01 06:50:46 -04:00
|
|
|
if (themeId) {
|
2023-02-27 12:43:28 -05:00
|
|
|
compiler.precompile = (src, opts) => {
|
2022-09-01 06:50:46 -04:00
|
|
|
return precompile(src, {
|
|
|
|
...opts,
|
|
|
|
plugins: {
|
|
|
|
ast: [buildEmberTemplateManipulatorPlugin(themeId)],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2022-10-17 09:47:16 -04:00
|
|
|
colocatedBabelPlugin,
|
2022-09-01 06:50:46 -04:00
|
|
|
require("widget-hbs-compiler").WidgetHbsCompiler,
|
|
|
|
[
|
2023-02-27 12:43:28 -05:00
|
|
|
HTMLBarsInlinePrecompile,
|
|
|
|
{
|
|
|
|
compiler,
|
|
|
|
enableLegacyModules: ["ember-cli-htmlbars"],
|
|
|
|
},
|
2022-09-01 06:50:46 -04:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildThemeRawHbsTemplateManipulatorPlugin(themeId) {
|
|
|
|
return function (ast) {
|
|
|
|
["SubExpression", "MustacheStatement"].forEach((pass) => {
|
|
|
|
let visitor = new Handlebars.Visitor();
|
|
|
|
visitor.mutating = true;
|
|
|
|
visitor[pass] = (node) => manipulateAstNodeForTheme(node, themeId);
|
|
|
|
visitor.accept(ast);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.compileRawTemplate = function (source, themeId) {
|
|
|
|
try {
|
|
|
|
const RawHandlebars = require("raw-handlebars").default;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.transpile = function (
|
|
|
|
source,
|
|
|
|
{ moduleId, filename, skipModule, themeId, commonPlugins } = {}
|
|
|
|
) {
|
|
|
|
const plugins = [];
|
|
|
|
plugins.push(...buildTemplateCompilerBabelPlugins({ themeId }));
|
|
|
|
if (moduleId && !skipModule) {
|
|
|
|
plugins.push(["transform-modules-amd", { noInterop: true }]);
|
|
|
|
}
|
|
|
|
plugins.push(...commonPlugins);
|
|
|
|
|
|
|
|
try {
|
|
|
|
return Babel.transform(source, {
|
|
|
|
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-18 13:20:10 -04: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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|