discourse/app/assets/javascripts/theme-transpiler/transpiler.js
David Taylor 2dff6d24af
FIX: Ensure ember version in cache key is coupled to compiler (#34694)
During in-container updates, the old version of the application
continues running while an update is applied. That means that it's
possible for the `node_modules/ember-source` version to be different to
the version currently loaded in the transpiler. That means it's
theoretically possible for theme assets to be built with the old
compiler, and then stored against the new version.

This commit removes that race condition by adding an `ember_version`
method to the JS transpiler. This is guaranteed to give us an accurate
version number for the template-compiler currently being used for
themes.

This commit also bumps the BASE_COMPILER_VERSION to force a recompile on
any sites affected by this race condition.
2025-09-02 22:29:02 +01:00

81 lines
2 KiB
JavaScript
Vendored

import "./shims";
import "./postcss";
import "./theme-rollup";
import { transform as babelTransform } from "@babel/standalone";
import DecoratorTransforms from "decorator-transforms";
import EMBER_PACKAGE from "ember-source/package.json";
import { minify as terserMinify } from "terser";
import { browsers } from "../discourse/config/targets";
globalThis.emberVersion = function () {
return EMBER_PACKAGE.version;
};
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;
};