discourse/app/assets/javascripts/theme-transpiler/transpiler.js
Jarek Radosz 5a42bdc6b4
DEV: Remove template compilation from theme transpiler (#33862)
gjs,js,hbs, etc compilation goes through the rollup pipeline now
2025-07-27 12:30:10 +02:00

76 lines
1.9 KiB
JavaScript
Vendored

import "./shims";
import "./postcss";
import "./theme-rollup";
import { transform as babelTransform } from "@babel/standalone";
import DecoratorTransforms from "decorator-transforms";
import { minify as terserMinify } from "terser";
import { browsers } from "../discourse/config/targets";
globalThis.transpile = function (source, options = {}) {
const { moduleId, filename, skipModule, generateMap } = options;
const plugins = [];
if (moduleId && !skipModule) {
plugins.push(["transform-modules-amd", { noInterop: true }]);
}
plugins.push([DecoratorTransforms, { runEarly: true }]);
try {
const result = babelTransform(source, {
moduleId,
filename,
ast: false,
plugins,
presets: [
[
"env",
{
modules: false,
targets: {
browsers,
},
},
],
],
sourceMaps: generateMap,
});
if (generateMap) {
return {
code: result.code,
map: JSON.stringify(result.map),
};
} else {
return result.code;
}
} catch (error) {
// Workaround for https://github.com/rubyjs/mini_racer/issues/262
error.message = JSON.stringify(error.message);
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;
globalThis.minify = async function (sources, options) {
lastMinifyError = lastMinifyResult = null;
try {
lastMinifyResult = await terserMinify(sources, options);
} catch (e) {
lastMinifyError = e;
}
};
globalThis.getMinifyResult = function () {
const error = lastMinifyError;
const result = lastMinifyResult;
lastMinifyError = lastMinifyResult = null;
if (error) {
throw error.toString();
}
return result;
};